zram: protect sysfs handler from invalid memory access
authorJiang Liu <liuj97@gmail.com>
Thu, 6 Jun 2013 16:07:27 +0000 (00:07 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 16 Jun 2014 20:45:45 +0000 (13:45 -0700)
commit 5863e10b441e7ea4b492f930f1be180a97d026f3 upstream.

Use zram->init_lock to protect access to zram->meta, otherwise it
may cause invalid memory access if zram->meta has been freed by
zram_reset_device().

This issue may be triggered by:
Thread 1:
while true; do cat mem_used_total; done
Thread 2:
while true; do echo 8M > disksize; echo 1 > reset; done

Signed-off-by: Jiang Liu <jiang.liu@huawei.com>
Acked-by: Minchan Kim <minchan@kernel.org>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
[wyj: Backported to 3.4: adjust context]
Signed-off-by: Yijing Wang <wangyijing@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/zram/zram_sysfs.c

index 826653fff70ec70b393e4a8ca04afe0ac28ffd9b..01f9a79aad4318d27ae3c38dc871e037f418eae1 100644 (file)
@@ -188,10 +188,12 @@ static ssize_t mem_used_total_show(struct device *dev,
        u64 val = 0;
        struct zram *zram = dev_to_zram(dev);
 
+       down_read(&zram->init_lock);
        if (zram->init_done) {
                val = zs_get_total_size_bytes(zram->mem_pool) +
                        ((u64)(zram->stats.pages_expand) << PAGE_SHIFT);
        }
+       up_read(&zram->init_lock);
 
        return sprintf(buf, "%llu\n", val);
 }