linux/drivers/md/bcache
Coly Li 2a72c28331 bcache: fix overflow in offset_to_stripe()
commit 7a14812679 upstream.

offset_to_stripe() returns the stripe number (in type unsigned int) from
an offset (in type uint64_t) by the following calculation,
	do_div(offset, d->stripe_size);
For large capacity backing device (e.g. 18TB) with small stripe size
(e.g. 4KB), the result is 4831838208 and exceeds UINT_MAX. The actual
returned value which caller receives is 536870912, due to the overflow.

Indeed in bcache_device_init(), bcache_device->nr_stripes is limited in
range [1, INT_MAX]. Therefore all valid stripe numbers in bcache are
in range [0, bcache_dev->nr_stripes - 1].

This patch adds a upper limition check in offset_to_stripe(): the max
valid stripe number should be less than bcache_device->nr_stripes. If
the calculated stripe number from do_div() is equal to or larger than
bcache_device->nr_stripe, -EINVAL will be returned. (Normally nr_stripes
is less than INT_MAX, exceeding upper limitation doesn't mean overflow,
therefore -EOVERFLOW is not used as error code.)

This patch also changes nr_stripes' type of struct bcache_device from
'unsigned int' to 'int', and return value type of offset_to_stripe()
from 'unsigned int' to 'int', to match their exact data ranges.

All locations where bcache_device->nr_stripes and offset_to_stripe() are
referenced also get updated for the above type change.

Reported-and-tested-by: Ken Raeburn <raeburn@redhat.com>
Signed-off-by: Coly Li <colyli@suse.de>
Cc: stable@vger.kernel.org
Link: https://bugzilla.redhat.com/show_bug.cgi?id=1783075
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-08-21 11:05:32 +02:00
..
alloc.c bcache: fix deadlock in bcache_allocator 2019-12-31 16:36:02 +01:00
bcache.h bcache: fix overflow in offset_to_stripe() 2020-08-21 11:05:32 +02:00
bset.c bcache: allocate meta data pages as compound pages 2020-08-21 11:05:32 +02:00
bset.h bcache: explicity type cast in bset_bkey_last() 2020-02-24 08:34:53 +01:00
btree.c bcache: allocate meta data pages as compound pages 2020-08-21 11:05:32 +02:00
btree.h bcache: fix race in btree_flush_write() 2019-09-16 08:22:23 +02:00
closure.c closures: fix a race on wakeup from closure_sync 2019-10-05 13:09:54 +02:00
closure.h
debug.c bcache: Fix an error code in bch_dump_read() 2020-01-27 14:51:09 +01:00
debug.h
extents.c
extents.h
io.c
journal.c bcache: allocate meta data pages as compound pages 2020-08-21 11:05:32 +02:00
journal.h
Kconfig
Makefile
movinggc.c
request.c bcache: add readahead cache policy options via sysfs interface 2020-02-11 04:34:08 -08:00
request.h
stats.c
stats.h
super.c bcache: allocate meta data pages as compound pages 2020-08-21 11:05:32 +02:00
sysfs.c bcache: add readahead cache policy options via sysfs interface 2020-02-11 04:34:08 -08:00
sysfs.h
trace.c
util.c
util.h
writeback.c bcache: fix overflow in offset_to_stripe() 2020-08-21 11:05:32 +02:00
writeback.h bcache: fix overflow in offset_to_stripe() 2020-08-21 11:05:32 +02:00