lkml.org 
[lkml]   [2010]   [Jan]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    Date
    From
    Subject[PATCH 1/4] devmem: check vmalloc address on kmem read/write
    From: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>

    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.

    CC: Greg Kroah-Hartman <gregkh@suse.de>
    CC: Hugh Dickins <hugh.dickins@tiscali.co.uk>
    CC: <stable@kernel.org>
    Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
    Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
    ---
    drivers/char/mem.c | 28 ++++++++++++++++++----------
    1 file changed, 18 insertions(+), 10 deletions(-)

    --- linux-mm.orig/drivers/char/mem.c 2010-01-11 10:22:35.000000000 +0800
    +++ linux-mm/drivers/char/mem.c 2010-01-11 10:32:32.000000000 +0800
    @@ -395,6 +395,7 @@ static ssize_t read_kmem(struct file *fi
    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) {
    @@ -441,12 +442,16 @@ static ssize_t read_kmem(struct file *fi
    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;
    @@ -455,8 +460,8 @@ static ssize_t read_kmem(struct file *fi
    }
    free_page((unsigned long)kbuf);
    }
    - *ppos = p;
    - return read;
    + *ppos = p;
    + return read ? read : err;
    }


    @@ -520,6 +525,7 @@ static ssize_t write_kmem(struct file *
    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,
    @@ -540,12 +546,14 @@ static ssize_t write_kmem(struct file *
    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;
    @@ -556,8 +564,8 @@ static ssize_t write_kmem(struct file *
    free_page((unsigned long)kbuf);
    }

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




    \
     
     \ /
      Last update: 2010-01-22 06:23    [W:0.031 / U:30.152 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site