Staging: zram: Fix access of NULL pointer
authorRashika Kheria <rashika.kheria@gmail.com>
Wed, 30 Oct 2013 13:06:32 +0000 (18:36 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 30 Oct 2013 16:36:53 +0000 (09:36 -0700)
This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory
pressure is severe and hence can return NULL because allocation of
inode in bdget could fail.

Hence, this patch introduces a check for bdev to prevent reference to a
NULL pointer in the later part of the code. It also removes unnecessary
check of bdev for fsync_bdev().

Cc: stable <stable@vger.kernel.org>
Acked-by: Jerome Marchand <jmarchan@redhat.com>
Signed-off-by: Rashika Kheria <rashika.kheria@gmail.com>
Acked-by: Minchan Kim <minchan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/zram/zram_drv.c

index b704d06943f5ca3ed3db70c2ed1200d617cdf415..79ce363b2ea9d1dd2cb0b79362b607659ca37345 100644 (file)
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
        zram = dev_to_zram(dev);
        bdev = bdget_disk(zram->disk, 0);
 
+       if (!bdev)
+               return -ENOMEM;
+
        /* Do not reset an active device! */
        if (bdev->bd_holders)
                return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
                return -EINVAL;
 
        /* Make sure all pending I/O is finished */
-       if (bdev)
-               fsync_bdev(bdev);
+       fsync_bdev(bdev);
 
        zram_reset_device(zram, true);
        return len;