[PATCH -v1.1] x86/msr: Block writes to certain MSRs unconditionally

From: Borislav Petkov
Date: Sat Apr 10 2021 - 11:33:49 EST


On Sat, Apr 10, 2021 at 07:51:58AM -0700, Andy Lutomirski wrote:
> Can you add STAR, CSTAR, LSTAR, SYSENTER*, SYSCALL*, and EFER please?

Sure.

---

From: Borislav Petkov <bp@xxxxxxx>
Date: Sat, 10 Apr 2021 14:08:13 +0200

There are a bunch of MSRs which luserspace has no business poking
at, whatsoever. Add a ban list and put the TSC-related MSRs and the
ring0-entry and features control MSRs in there. Issue a big juicy splat
to catch offenders.

Signed-off-by: Borislav Petkov <bp@xxxxxxx>
---
arch/x86/kernel/msr.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/arch/x86/kernel/msr.c b/arch/x86/kernel/msr.c
index ed8ac6bcbafb..2435a619cd9f 100644
--- a/arch/x86/kernel/msr.c
+++ b/arch/x86/kernel/msr.c
@@ -78,6 +78,21 @@ static ssize_t msr_read(struct file *file, char __user *buf,
return bytes ? bytes : err;
}

+static const u32 msr_ban_list[] = {
+ MSR_IA32_TSC,
+ MSR_TSC_AUX,
+ MSR_IA32_TSC_ADJUST,
+ MSR_IA32_TSC_DEADLINE,
+ MSR_EFER,
+ MSR_STAR,
+ MSR_CSTAR,
+ MSR_LSTAR,
+ MSR_SYSCALL_MASK,
+ MSR_IA32_SYSENTER_CS,
+ MSR_IA32_SYSENTER_ESP,
+ MSR_IA32_SYSENTER_EIP,
+};
+
static int filter_write(u32 reg)
{
/*
@@ -89,6 +104,16 @@ static int filter_write(u32 reg)
* avoid saturating the ring buffer.
*/
static DEFINE_RATELIMIT_STATE(fw_rs, 30 * HZ, 1);
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(msr_ban_list); i++) {
+ if (msr_ban_list[i] != reg)
+ continue;
+
+ WARN_ONCE(1, "Blocked write to MSR 0x%x.\n", reg);
+
+ return -EINVAL;
+ }

switch (allow_writes) {
case MSR_WRITES_ON: return 0;
--
2.29.2

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette