]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
sysfs: fix use after free in case of concurrent read/write and readdir
authorMing Lei <ming.lei@canonical.com>
Tue, 2 Apr 2013 02:12:26 +0000 (10:12 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 3 Apr 2013 18:09:02 +0000 (11:09 -0700)
The inode->i_mutex isn't hold when updating filp->f_pos
in read()/write(), so the filp->f_pos might be read as
0 or 1 in readdir() when there is concurrent read()/write()
on this same file, then may cause use after free in readdir().

The bug can be reproduced with Li Zefan's test code on the
link:

https://patchwork.kernel.org/patch/2160771/

This patch fixes the use after free under this situation.

Cc: stable <stable@vger.kernel.org>
Reported-by: Li Zefan <lizefan@huawei.com>
Signed-off-by: Ming Lei <ming.lei@canonical.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/sysfs/dir.c

index c6f54abe98529258c24b228f9c2e90f705c6222d..1bf016b5e88fa12bc7d9627f1a47376cff7b1d9f 100644 (file)
@@ -999,6 +999,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
        enum kobj_ns_type type;
        const void *ns;
        ino_t ino;
+       loff_t off;
 
        type = sysfs_ns_type(parent_sd);
        ns = sysfs_info(dentry->d_sb)->ns[type];
@@ -1021,6 +1022,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                        return 0;
        }
        mutex_lock(&sysfs_mutex);
+       off = filp->f_pos;
        for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos);
             pos;
             pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) {
@@ -1032,19 +1034,24 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                len = strlen(name);
                ino = pos->s_ino;
                type = dt_type(pos);
-               filp->f_pos = pos->s_hash;
+               off = filp->f_pos = pos->s_hash;
                filp->private_data = sysfs_get(pos);
 
                mutex_unlock(&sysfs_mutex);
-               ret = filldir(dirent, name, len, filp->f_pos, ino, type);
+               ret = filldir(dirent, name, len, off, ino, type);
                mutex_lock(&sysfs_mutex);
                if (ret < 0)
                        break;
        }
        mutex_unlock(&sysfs_mutex);
-       if ((filp->f_pos > 1) && !pos) { /* EOF */
-               filp->f_pos = INT_MAX;
+
+       /* don't reference last entry if its refcount is dropped */
+       if (!pos) {
                filp->private_data = NULL;
+
+               /* EOF and not changed as 0 or 1 in read/write path */
+               if (off == filp->f_pos && off > 1)
+                       filp->f_pos = INT_MAX;
        }
        return 0;
 }