x86: Restrict MSR access when securelevel is set
authorMatthew Garrett <mjg59@srcf.ucam.org>
Fri, 8 Feb 2013 19:12:13 +0000 (11:12 -0800)
committerYves-Alexis Perez <corsac@debian.org>
Wed, 21 Feb 2018 15:29:03 +0000 (15:29 +0000)
Permitting write access to MSRs allows userspace to modify the running
kernel. Prevent this if securelevel has been set. Based on a patch by Kees
Cook.

Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Matthew Garrett <mjg59@srcf.ucam.org>
Gbp-Pq: Topic features/all/securelevel
Gbp-Pq: Name x86-restrict-msr-access-when-securelevel-is-set.patch

arch/x86/kernel/msr.c

index 7f3550acde1b8ce604f4b5cc8b53c232450c524f..531d8dbc955ddcc85b1e68090ffb62ab703270c9 100644 (file)
@@ -39,6 +39,7 @@
 #include <linux/notifier.h>
 #include <linux/uaccess.h>
 #include <linux/gfp.h>
+#include <linux/security.h>
 
 #include <asm/cpufeature.h>
 #include <asm/msr.h>
@@ -83,6 +84,9 @@ static ssize_t msr_write(struct file *file, const char __user *buf,
        int err = 0;
        ssize_t bytes = 0;
 
+       if (get_securelevel() > 0)
+               return -EPERM;
+
        if (count % 8)
                return -EINVAL; /* Invalid chunk size */
 
@@ -130,6 +134,10 @@ static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
                        err = -EBADF;
                        break;
                }
+               if (get_securelevel() > 0) {
+                       err = -EPERM;
+                       break;
+               }
                if (copy_from_user(&regs, uregs, sizeof regs)) {
                        err = -EFAULT;
                        break;