aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/mem.c')
-rw-r--r--drivers/char/mem.c28
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
112static 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
129static 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;