Skip to content

Commit

Permalink
devmem: check vmalloc address on kmem read/write
Browse files Browse the repository at this point in the history
Otherwise vmalloc_to_page() will BUG().

This also makes the kmem read/write implementation aligned with mem(4):
"References to nonexistent locations cause errors to be returned." Here we
return -ENXIO (inspired by Hugh) if no bytes have been transfered to/from
user space, otherwise return partial read/write results.

Signed-off-by: KAMEZAWA Hiroyuki <[email protected]>
Signed-off-by: Wu Fengguang <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
hkamezawa authored and torvalds committed Feb 3, 2010
1 parent 931e80e commit 325fda7
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions drivers/char/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
unsigned long p = *ppos;
ssize_t low_count, read, sz;
char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
int err = 0;

read = 0;
if (p < (unsigned long) high_memory) {
Expand Down Expand Up @@ -441,12 +442,16 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
return -ENOMEM;
while (count > 0) {
sz = size_inside_page(p, count);
if (!is_vmalloc_or_module_addr((void *)p)) {
err = -ENXIO;
break;
}
sz = vread(kbuf, (char *)p, sz);
if (!sz)
break;
if (copy_to_user(buf, kbuf, sz)) {
free_page((unsigned long)kbuf);
return -EFAULT;
err = -EFAULT;
break;
}
count -= sz;
buf += sz;
Expand All @@ -455,8 +460,8 @@ static ssize_t read_kmem(struct file *file, char __user *buf,
}
free_page((unsigned long)kbuf);
}
*ppos = p;
return read;
*ppos = p;
return read ? read : err;
}


Expand Down Expand Up @@ -520,6 +525,7 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
ssize_t wrote = 0;
ssize_t virtr = 0;
char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
int err = 0;

if (p < (unsigned long) high_memory) {
unsigned long to_write = min_t(unsigned long, count,
Expand All @@ -540,12 +546,14 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
unsigned long sz = size_inside_page(p, count);
unsigned long n;

if (!is_vmalloc_or_module_addr((void *)p)) {
err = -ENXIO;
break;
}
n = copy_from_user(kbuf, buf, sz);
if (n) {
if (wrote + virtr)
break;
free_page((unsigned long)kbuf);
return -EFAULT;
err = -EFAULT;
break;
}
sz = vwrite(kbuf, (char *)p, sz);
count -= sz;
Expand All @@ -556,8 +564,8 @@ static ssize_t write_kmem(struct file * file, const char __user * buf,
free_page((unsigned long)kbuf);
}

*ppos = p;
return virtr + wrote;
*ppos = p;
return virtr + wrote ? : err;
}
#endif

Expand Down

0 comments on commit 325fda7

Please sign in to comment.