diff options
author | Arjan van de Ven <arjan@linux.intel.com> | 2008-04-24 17:40:47 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2008-04-24 17:40:47 -0400 |
commit | ae531c26c5c2a28ca1b35a75b39b3b256850f2c8 (patch) | |
tree | e4c2f3ec25bdb0e2e5f7f15f79a60c3175f03718 /drivers/char | |
parent | 94bc891b00e40cbec375feb4568780af183fd7f4 (diff) |
x86: introduce /dev/mem restrictions with a config option
This patch introduces a restriction on /dev/mem: Only non-memory can be
read or written unless the newly introduced config option is set.
The X server needs access to /dev/mem for the PCI space, but it doesn't need
access to memory; both the file permissions and SELinux permissions of /dev/mem
just make X effectively super-super powerful. With the exception of the
BIOS area, there's just no valid app that uses /dev/mem on actual memory.
Other popular users of /dev/mem are rootkits and the like.
(note: mmap access of memory via /dev/mem was already not allowed since
a really long time)
People who want to use /dev/mem for kernel debugging can enable the config
option.
The restrictions of this patch have been in the Fedora and RHEL kernels for
at least 4 years without any problems.
Signed-off-by: Arjan van de Ven <arjan@linux.intel.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'drivers/char')
-rw-r--r-- | drivers/char/mem.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/drivers/char/mem.c b/drivers/char/mem.c index 20070b7c573d..dcf6e31970a1 100644 --- a/drivers/char/mem.c +++ b/drivers/char/mem.c | |||
@@ -108,6 +108,30 @@ static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) | |||
108 | } | 108 | } |
109 | #endif | 109 | #endif |
110 | 110 | ||
111 | #ifdef CONFIG_NONPROMISC_DEVMEM | ||
112 | static inline int range_is_allowed(unsigned long from, unsigned long to) | ||
113 | { | ||
114 | unsigned long cursor; | ||
115 | |||
116 | cursor = from >> PAGE_SHIFT; | ||
117 | while ((cursor << PAGE_SHIFT) < to) { | ||
118 | if (!devmem_is_allowed(cursor)) { | ||
119 | printk(KERN_INFO "Program %s tried to read /dev/mem " | ||
120 | "between %lx->%lx.\n", | ||
121 | current->comm, from, to); | ||
122 | return 0; | ||
123 | } | ||
124 | cursor++; | ||
125 | } | ||
126 | return 1; | ||
127 | } | ||
128 | #else | ||
129 | static inline int range_is_allowed(unsigned long from, unsigned long to) | ||
130 | { | ||
131 | return 1; | ||
132 | } | ||
133 | #endif | ||
134 | |||
111 | /* | 135 | /* |
112 | * This funcion reads the *physical* memory. The f_pos points directly to the | 136 | * This funcion reads the *physical* memory. The f_pos points directly to the |
113 | * memory location. | 137 | * memory location. |
@@ -157,6 +181,8 @@ static ssize_t read_mem(struct file * file, char __user * buf, | |||
157 | */ | 181 | */ |
158 | ptr = xlate_dev_mem_ptr(p); | 182 | ptr = xlate_dev_mem_ptr(p); |
159 | 183 | ||
184 | if (!range_is_allowed(p, p+count)) | ||
185 | return -EPERM; | ||
160 | if (copy_to_user(buf, ptr, sz)) | 186 | if (copy_to_user(buf, ptr, sz)) |
161 | return -EFAULT; | 187 | return -EFAULT; |
162 | buf += sz; | 188 | buf += sz; |
@@ -214,6 +240,8 @@ static ssize_t write_mem(struct file * file, const char __user * buf, | |||
214 | */ | 240 | */ |
215 | ptr = xlate_dev_mem_ptr(p); | 241 | ptr = xlate_dev_mem_ptr(p); |
216 | 242 | ||
243 | if (!range_is_allowed(p, p+sz)) | ||
244 | return -EPERM; | ||
217 | copied = copy_from_user(ptr, buf, sz); | 245 | copied = copy_from_user(ptr, buf, sz); |
218 | if (copied) { | 246 | if (copied) { |
219 | written += sz - copied; | 247 | written += sz - copied; |