Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1103042pxa; Thu, 20 Aug 2020 02:32:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCv/rCRNmuG+c/J/1S1d1BNoHs2/jvl8AWNB1zHHb87WRtilEMndqBEC7lU+2HGq46JWdt X-Received: by 2002:a17:906:27db:: with SMTP id k27mr2320862ejc.433.1597915927354; Thu, 20 Aug 2020 02:32:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597915927; cv=none; d=google.com; s=arc-20160816; b=mdAYe7uhtNg1C5Fp8dvQGhgfGBzDSVzz2PxSHsFSqXQcklBh7ruko/N1dTYTx8d9yh NR4rrO3N9xYd61PjTqczRKNL/0agGpyJU7+UfJMqKV4v25p7b+cIoT+TAsWA2xZyyT8h eI63SWwl1YbgSeFS4dYci59jTmx1wIKGSsleqzKcg1ba/jt9FR6kgLQivLlxm/TlTtcR aDLHvBVn9tO6rd5z8l1EKp6fyiNUybrhm6pjvVz5i6YyleuX0gDNUmaUcI8izIJL19yi ABBhEZn9szq+mOZ/PO9XTpJbKv+ZXsy0xKDoQz/cksjhkPjsWrbe4sVXEICiGGw0uAsT ikJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=If6pYjHf3nQjM+0omi3FZSr23cJ2OLZcrxP8XA6Rdv8=; b=prBPDf6hJOe05fKcmqyZluRNo3q+pP+mTySMiJZIjf8BWE3z2PZT/NhBNG/EGWPTD8 o6JZEtj616m3abisyfkhRPn0rjyRyBQxgE+oavXjcRDeXPnhHYAa2gDLGqzvNqUYdNN4 4IcBgqwF9tNRUcVxCQADy+ASS9t/zFj+RQ3rHSzNtCjXywOU5VYN45vF2uX2ulhT5c0g SIMcPnyByb5G8eIlo6ATBgVJdp8OdBLX3RzXY9RAB8th/1tB34/CrCYYa3JCCRPQtdRj Ex8+4KpJ9f5MK+fdkonUY6PaHRzVHE0XskXXzc2yJVezMaWK3ReVOZDE+wpQ+TNz8WLz RYeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mbBXS6Mp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c67si1044940edf.421.2020.08.20.02.31.43; Thu, 20 Aug 2020 02:32:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mbBXS6Mp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728029AbgHTJ2v (ORCPT + 99 others); Thu, 20 Aug 2020 05:28:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:37712 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728013AbgHTJ1z (ORCPT ); Thu, 20 Aug 2020 05:27:55 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DC86622CF6; Thu, 20 Aug 2020 09:27:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597915674; bh=leFGFO6VIFJLxyhjNyZWZnKHXhV2UKbC1qbZhu5S6/w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mbBXS6MpEOKCYt/JyRtoLl1z69xR/iCh2T+gIhqW6omRgl9GEgOBzuTjO7h/BiqXs z5tbsOyxplGkKIyUYcuWSTOaHNCExhCBDLNlknB9d4A0ifwKu8eiETaJmxEkunFebW EB+KiawgpgJvNdmGUEP9U9AjMsS1yr5VKVKf7uPk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Coly Li , Jens Axboe , Ken Raeburn Subject: [PATCH 5.8 066/232] bcache: fix overflow in offset_to_stripe() Date: Thu, 20 Aug 2020 11:18:37 +0200 Message-Id: <20200820091615.995276361@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200820091612.692383444@linuxfoundation.org> References: <20200820091612.692383444@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Coly Li commit 7a1481267999c02abf4a624515c1b5c7c1fccbd6 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 Signed-off-by: Coly Li Cc: stable@vger.kernel.org Link: https://bugzilla.redhat.com/show_bug.cgi?id=1783075 Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- 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(-) --- 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; --- a/drivers/md/bcache/writeback.c +++ b/drivers/md/bcache/writeback.c @@ -523,15 +523,19 @@ void bcache_dev_sectors_dirty_add(struct 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) { @@ -571,12 +575,12 @@ static bool dirty_pred(struct keybuf *bu 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; --- a/drivers/md/bcache/writeback.h +++ b/drivers/md/bcache/writeback.h @@ -52,10 +52,22 @@ static inline uint64_t bcache_dev_sector 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_dir 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))