[tip: x86/mtrr] x86/mtrr: Restrict MTRR ranges dumping and ioctl()

From: tip-bot2 for Zhang Xiaoxu
Date: Fri Nov 08 2019 - 15:07:15 EST


The following commit has been merged into the x86/mtrr branch of tip:

Commit-ID: d5a8b06841082ead88493eb918dd646a12c19d8e
Gitweb: https://git.kernel.org/tip/d5a8b06841082ead88493eb918dd646a12c19d8e
Author: Zhang Xiaoxu <zhangxiaoxu5@xxxxxxxxxx>
AuthorDate: Tue, 05 Nov 2019 15:17:14 +08:00
Committer: Borislav Petkov <bp@xxxxxxx>
CommitterDate: Fri, 08 Nov 2019 20:59:40 +01:00

x86/mtrr: Restrict MTRR ranges dumping and ioctl()

/proc/mtrr dumps the physical memory ranges of the variable range MTRRs
along with their respective sizes and caching attributes. Since that
file is world-readable, it presents a small information leak about the
physical address ranges of a system which should be blocked.

Make that file root-only.

Make the ioctl root-only as well because the $NAME read ioctl also
allows access. Replace the checks in the write ioctls with a single one
on entry.

[ bp: rewrite commit message. ]

Signed-off-by: Zhang Xiaoxu <zhangxiaoxu5@xxxxxxxxxx>
Signed-off-by: Borislav Petkov <bp@xxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Cc: Colin Ian King <colin.king@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Tyler Hicks <tyhicks@xxxxxxxxxxxxx>
Cc: x86-ml <x86@xxxxxxxxxx>
Cc: zhangxiaoxu@xxxxxxxxxx
Link: https://lkml.kernel.org/r/20191105071714.27376-1-zhangxiaoxu5@xxxxxxxxxx
---
arch/x86/kernel/cpu/mtrr/if.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/arch/x86/kernel/cpu/mtrr/if.c b/arch/x86/kernel/cpu/mtrr/if.c
index 4d36dcc..8e0cee8 100644
--- a/arch/x86/kernel/cpu/mtrr/if.c
+++ b/arch/x86/kernel/cpu/mtrr/if.c
@@ -84,6 +84,15 @@ mtrr_file_del(unsigned long base, unsigned long size,
return reg;
}

+static ssize_t
+mtrr_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
+{
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ return seq_read(file, buf, size, ppos);
+}
+
/*
* seq_file can seek but we ignore it.
*
@@ -165,6 +174,9 @@ mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
struct mtrr_gentry gentry;
void __user *arg = (void __user *) __arg;

+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
memset(&gentry, 0, sizeof(gentry));

switch (cmd) {
@@ -226,8 +238,6 @@ mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
#ifdef CONFIG_COMPAT
case MTRRIOC32_ADD_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err =
mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
file, 0);
@@ -236,24 +246,18 @@ mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
#ifdef CONFIG_COMPAT
case MTRRIOC32_SET_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
break;
case MTRRIOC_DEL_ENTRY:
#ifdef CONFIG_COMPAT
case MTRRIOC32_DEL_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err = mtrr_file_del(sentry.base, sentry.size, file, 0);
break;
case MTRRIOC_KILL_ENTRY:
#ifdef CONFIG_COMPAT
case MTRRIOC32_KILL_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err = mtrr_del(-1, sentry.base, sentry.size);
break;
case MTRRIOC_GET_ENTRY:
@@ -279,8 +283,6 @@ mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
#ifdef CONFIG_COMPAT
case MTRRIOC32_ADD_PAGE_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err =
mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
file, 1);
@@ -289,8 +291,6 @@ mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
#ifdef CONFIG_COMPAT
case MTRRIOC32_SET_PAGE_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err =
mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
break;
@@ -298,16 +298,12 @@ mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
#ifdef CONFIG_COMPAT
case MTRRIOC32_DEL_PAGE_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err = mtrr_file_del(sentry.base, sentry.size, file, 1);
break;
case MTRRIOC_KILL_PAGE_ENTRY:
#ifdef CONFIG_COMPAT
case MTRRIOC32_KILL_PAGE_ENTRY:
#endif
- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
err = mtrr_del_page(-1, sentry.base, sentry.size);
break;
case MTRRIOC_GET_PAGE_ENTRY:
@@ -387,7 +383,7 @@ static int mtrr_open(struct inode *inode, struct file *file)
static const struct file_operations mtrr_fops = {
.owner = THIS_MODULE,
.open = mtrr_open,
- .read = seq_read,
+ .read = mtrr_read,
.llseek = seq_lseek,
.write = mtrr_write,
.unlocked_ioctl = mtrr_ioctl,
@@ -436,7 +432,7 @@ static int __init mtrr_if_init(void)
(!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
return -ENODEV;

- proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
+ proc_create("mtrr", 0600, NULL, &mtrr_fops);
return 0;
}
arch_initcall(mtrr_if_init);