diff mbox series

[v2,2/2] bcche: fix overflow in offset_to_stripe()

Message ID 20200713111501.19061-2-colyli@suse.de
State New, archived
Headers show
Series [v2,1/2] bcache: avoid nr_stripes overflow in bcache_device_init() | expand

Commit Message

Coly Li July 13, 2020, 11:15 a.m. UTC
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-by: Ken Raeburn <raeburn@redhat.com>
Signed-off-by: Coly Li <colyli@suse.de>
Link: https://bugzilla.redhat.com/show_bug.cgi?id=1783075
Cc: stable@vger.kernel.org
---
Changelog:
v2: Improve overflow fix on 32bit machine, suggested by Jens and Ken.
v1: initial version.

 drivers/md/bcache/bcache.h    |  2 +-
 drivers/md/bcache/writeback.c | 14 +++++++++-----
 drivers/md/bcache/writeback.h | 19 +++++++++++++++++--
 3 files changed, 27 insertions(+), 8 deletions(-)

Comments

Sasha Levin July 17, 2020, 5:08 p.m. UTC | #1
Hi

[This is an automated email]

This commit has been processed because it contains a -stable tag.
The stable tag indicates that it's relevant for the following trees: all

The bot has tested the following trees: v5.7.8, v5.4.51, v4.19.132, v4.14.188, v4.9.230, v4.4.230.

v5.7.8: Build OK!
v5.4.51: Build OK!
v4.19.132: Build OK!
v4.14.188: Failed to apply! Possible dependencies:
    1d316e658374f ("bcache: implement PI controller for writeback rate")
    25d8be77e1922 ("block: move bio_alloc_pages() to bcache")
    27a40ab9269e7 ("bcache: add backing_request_endio() for bi_end_io")
    2831231d4c3f9 ("bcache: reduce cache_set devices iteration by devices_max_used")
    3b304d24a718a ("bcache: convert cached_dev.count from atomic_t to refcount_t")
    3fd47bfe55b00 ("bcache: stop dc->writeback_rate_update properly")
    5138ac6748e38 ("bcache: fix misleading error message in bch_count_io_errors()")
    539d39eb27083 ("bcache: fix wrong return value in bch_debug_init()")
    5fa89fb9a86bc ("bcache: don't write back data if reading it failed")
    6f10f7d1b02b1 ("bcache: style fix to replace 'unsigned' by 'unsigned int'")
    771f393e8ffc9 ("bcache: add CACHE_SET_IO_DISABLE to struct cache_set flags")
    7ba0d830dc0e4 ("bcache: set error_limit correctly")
    7e027ca4b534b ("bcache: add stop_when_cache_set_failed option to backing device")
    804f3c6981f5e ("bcache: fix cached_dev->count usage for bch_cache_set_error()")
    a8500fc816b19 ("bcache: rearrange writeback main thread ratelimit")
    b1092c9af9ed8 ("bcache: allow quick writeback when backing idle")
    bc082a55d25c8 ("bcache: fix inaccurate io state for detached bcache devices")
    c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev")

v4.9.230: Failed to apply! Possible dependencies:
    1d316e658374f ("bcache: implement PI controller for writeback rate")
    2831231d4c3f9 ("bcache: reduce cache_set devices iteration by devices_max_used")
    297e3d8547848 ("blk-throttle: make throtl_slice tunable")
    3fd47bfe55b00 ("bcache: stop dc->writeback_rate_update properly")
    4e4cbee93d561 ("block: switch bios to blk_status_t")
    5138ac6748e38 ("bcache: fix misleading error message in bch_count_io_errors()")
    6f10f7d1b02b1 ("bcache: style fix to replace 'unsigned' by 'unsigned int'")
    7e027ca4b534b ("bcache: add stop_when_cache_set_failed option to backing device")
    87760e5eef359 ("block: hook up writeback throttling")
    9e234eeafbe17 ("blk-throttle: add a simple idle detection")
    c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev")
    cf43e6be865a5 ("block: add scalable completion tracking of requests")
    e806402130c9c ("block: split out request-only flags into a new namespace")
    fbbaf700e7b16 ("block: trace completion of all bios.")

v4.4.230: Failed to apply! Possible dependencies:
    005411ea7ee77 ("doc: update block/queue-sysfs.txt entries")
    1d316e658374f ("bcache: implement PI controller for writeback rate")
    2831231d4c3f9 ("bcache: reduce cache_set devices iteration by devices_max_used")
    297e3d8547848 ("blk-throttle: make throtl_slice tunable")
    38f8baae89056 ("block: factor out chained bio completion")
    3fd47bfe55b00 ("bcache: stop dc->writeback_rate_update properly")
    4e4cbee93d561 ("block: switch bios to blk_status_t")
    511cbce2ff8b9 ("irq_poll: make blk-iopoll available outside the block layer")
    5138ac6748e38 ("bcache: fix misleading error message in bch_count_io_errors()")
    6f10f7d1b02b1 ("bcache: style fix to replace 'unsigned' by 'unsigned int'")
    7e027ca4b534b ("bcache: add stop_when_cache_set_failed option to backing device")
    87760e5eef359 ("block: hook up writeback throttling")
    9467f85960a31 ("blk-mq/cpu-notif: Convert to new hotplug state machine")
    9e234eeafbe17 ("blk-throttle: add a simple idle detection")
    af3e3a5259e35 ("block: don't unecessarily clobber bi_error for chained bios")
    ba8c6967b7391 ("block: cleanup bio_endio")
    c7b7bd07404c5 ("bcache: add io_disable to struct cached_dev")
    cf43e6be865a5 ("block: add scalable completion tracking of requests")
    e57690fe009b2 ("blk-mq: don't overwrite rq->mq_ctx")
    fbbaf700e7b16 ("block: trace completion of all bios.")


NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?
diff mbox series

Patch

diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h
index 0bfbf4729285..0ebfda284866 100644
--- a/drivers/md/bcache/bcache.h
+++ b/drivers/md/bcache/bcache.h
@@ -264,7 +264,7 @@  struct bcache_device {
 #define BCACHE_DEV_UNLINK_DONE		2
 #define BCACHE_DEV_WB_RUNNING		3
 #define BCACHE_DEV_RATE_DW_RUNNING	4
-	unsigned int		nr_stripes;
+	int			nr_stripes;
 	unsigned int		stripe_size;
 	atomic_t		*stripe_sectors_dirty;
 	unsigned long		*full_dirty_stripes;
diff --git a/drivers/md/bcache/writeback.c b/drivers/md/bcache/writeback.c
index 5397a2c5d6cc..4f4ad6b3d43a 100644
--- a/drivers/md/bcache/writeback.c
+++ b/drivers/md/bcache/writeback.c
@@ -521,15 +521,19 @@  void bcache_dev_sectors_dirty_add(struct cache_set *c, unsigned int inode,
 				  uint64_t offset, int nr_sectors)
 {
 	struct bcache_device *d = c->devices[inode];
-	unsigned int stripe_offset, stripe, sectors_dirty;
+	unsigned int stripe_offset, sectors_dirty;
+	int stripe;
 
 	if (!d)
 		return;
 
+	stripe = offset_to_stripe(d, offset);
+	if (stripe < 0)
+		return;
+
 	if (UUID_FLASH_ONLY(&c->uuids[inode]))
 		atomic_long_add(nr_sectors, &c->flash_dev_dirty_sectors);
 
-	stripe = offset_to_stripe(d, offset);
 	stripe_offset = offset & (d->stripe_size - 1);
 
 	while (nr_sectors) {
@@ -569,12 +573,12 @@  static bool dirty_pred(struct keybuf *buf, struct bkey *k)
 static void refill_full_stripes(struct cached_dev *dc)
 {
 	struct keybuf *buf = &dc->writeback_keys;
-	unsigned int start_stripe, stripe, next_stripe;
+	unsigned int start_stripe, next_stripe;
+	int stripe;
 	bool wrapped = false;
 
 	stripe = offset_to_stripe(&dc->disk, KEY_OFFSET(&buf->last_scanned));
-
-	if (stripe >= dc->disk.nr_stripes)
+	if (stripe < 0)
 		stripe = 0;
 
 	start_stripe = stripe;
diff --git a/drivers/md/bcache/writeback.h b/drivers/md/bcache/writeback.h
index b029843ce5b6..3f1230e22de0 100644
--- a/drivers/md/bcache/writeback.h
+++ b/drivers/md/bcache/writeback.h
@@ -52,10 +52,22 @@  static inline uint64_t bcache_dev_sectors_dirty(struct bcache_device *d)
 	return ret;
 }
 
-static inline unsigned int offset_to_stripe(struct bcache_device *d,
+static inline int offset_to_stripe(struct bcache_device *d,
 					uint64_t offset)
 {
 	do_div(offset, d->stripe_size);
+
+	/* d->nr_stripes is in range [1, INT_MAX] */
+	if (unlikely(offset >= d->nr_stripes)) {
+		pr_err("Invalid stripe %llu (>= nr_stripes %d).\n",
+			offset, d->nr_stripes);
+		return -EINVAL;
+	}
+
+	/*
+	 * Here offset is definitly smaller than INT_MAX,
+	 * return it as int will never overflow.
+	 */
 	return offset;
 }
 
@@ -63,7 +75,10 @@  static inline bool bcache_dev_stripe_dirty(struct cached_dev *dc,
 					   uint64_t offset,
 					   unsigned int nr_sectors)
 {
-	unsigned int stripe = offset_to_stripe(&dc->disk, offset);
+	int stripe = offset_to_stripe(&dc->disk, offset);
+
+	if (stripe < 0)
+		return false;
 
 	while (1) {
 		if (atomic_read(dc->disk.stripe_sectors_dirty + stripe))