block: fix BLKSECTGET ioctl when max_sectors is greater than USHRT_MAX
authorAkinobu Mita <akinobu.mita@gmail.com>
Sun, 25 May 2014 12:43:33 +0000 (21:43 +0900)
committerJens Axboe <axboe@fb.com>
Tue, 1 Jul 2014 16:43:07 +0000 (10:43 -0600)
BLKSECTGET ioctl loads the request queue's max_sectors as unsigned
short value to the argument pointer.  So if the max_sector is greater
than USHRT_MAX, the upper 16 bits of that is just discarded.

In such case, USHRT_MAX is more preferable than the lower 16 bits of
max_sectors.

Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: "James E.J. Bottomley" <JBottomley@parallels.com>
Cc: Douglas Gilbert <dgilbert@interlog.com>
Cc: linux-scsi@vger.kernel.org
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@fb.com>
block/compat_ioctl.c
block/ioctl.c

index fbd5a67cb773886104cac49698ee589b8fbc9933..e0393cd2ea7f31ad0207a6423dba1213a4edeea9 100644 (file)
@@ -663,6 +663,7 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
        fmode_t mode = file->f_mode;
        struct backing_dev_info *bdi;
        loff_t size;
+       unsigned int max_sectors;
 
        /*
         * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
@@ -718,8 +719,9 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
        case BLKSSZGET: /* get block device hardware sector size */
                return compat_put_int(arg, bdev_logical_block_size(bdev));
        case BLKSECTGET:
-               return compat_put_ushort(arg,
-                                        queue_max_sectors(bdev_get_queue(bdev)));
+               max_sectors = min_t(unsigned int, USHRT_MAX,
+                                   queue_max_sectors(bdev_get_queue(bdev)));
+               return compat_put_ushort(arg, max_sectors);
        case BLKROTATIONAL:
                return compat_put_ushort(arg,
                                         !blk_queue_nonrot(bdev_get_queue(bdev)));
index 7d5c3b20af451a111834efbabbdd83269085f123..d6cda8147c91ea828ea0dcd45f21d4ec6773abd6 100644 (file)
@@ -278,6 +278,7 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
        struct backing_dev_info *bdi;
        loff_t size;
        int ret, n;
+       unsigned int max_sectors;
 
        switch(cmd) {
        case BLKFLSBUF:
@@ -375,7 +376,9 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
        case BLKDISCARDZEROES:
                return put_uint(arg, bdev_discard_zeroes_data(bdev));
        case BLKSECTGET:
-               return put_ushort(arg, queue_max_sectors(bdev_get_queue(bdev)));
+               max_sectors = min_t(unsigned int, USHRT_MAX,
+                                   queue_max_sectors(bdev_get_queue(bdev)));
+               return put_ushort(arg, max_sectors);
        case BLKROTATIONAL:
                return put_ushort(arg, !blk_queue_nonrot(bdev_get_queue(bdev)));
        case BLKRASET: