From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383149; b=zHSkzIsIJzdyo92aoRjRUuHVRufWE6tU5JohlUbKKOM+Gnw5Yy3VkLrvTzINyaHScpqq x9AmTXtsBvVn+e/p5Me7I+qvHD695DugumIVT6f7ZEZ1kF8xQ7CIZBABGa0IKPZ8rqWr6 EggLHE35fu9PGzllVvMYCgF82G6fYtjn787ZFeuq316OGsXqJbZWkvMDmA+Og9De7m2ZC UoyM7guhq2TV/2/umDtiOhsUrGFAuD8oC1fssbuhWbTceanxYROJPCQmJb7fEm40kaef2 vIld/safdmJh6bCx8+5SWDEZNEGmSJFDWVssUbxn1IdgNDumkfv9ohiqJ+J3GqZbThQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383149; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=mE65SQlVn5+HeaI++Q0MKDUyuua+ElmV4RTJbS8s9BI=; b=QA8DGImEM7L1Ci1wvm8HoIDH/dcmYTV04tNWzFmQT0vpuEeXkvxeD2L6hDyHxYLZbpHB 7eYhSNFD2bTuYBITcuT60j9HnQneqxd7Kk1pIc/I6SHgghuK6BA+UUvYTYLEzvcO2NsVB 3tOnxoZA9ogAfvIOc5OoAhb+iNvamyuUzPZ4fFK2wsVd3IBElewK6+WTAFl9Nji7AzDk0 B+gvdQwhLzWm1AW9WYtH4hQNpF+Y5CoMsXx/GwXR1WzLgLaG95XIF5kaEm9YOKOjftakp 0Q8IPvGB9eBk4sainZzV9cLzg+qPRjmN9PUvvFbsBLYJsdHpr8OfP049rz+arDK6DRw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=GUD4sFd8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2624D606A322; Fri, 4 Dec 2026 11:19:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=GUD4sFd8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A0921F00897; Fri, 4 Dec 2026 11:19:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383148; bh=mE65SQlVn5+HeaI++Q0MKDUyuua+ElmV4RTJbS8s9BI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=GUD4sFd87jfjmbuDZrk/cx5o5VT8ucKuxVP1XaffChPwVTs15Sn5SL+v43JyJsdNd jt1AtX74kHi6AkPo63qb/yWrEM/cNzfQ9YZ2q2ypLXXk2EqS9AuhfFFt1wxNcNmXG0 Bp0OaOrml4xACO1Yvz42KrsG0bx4AcAhHZAaAVKw= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, syzbot+c8564fc0a96e3ecbe4d4@syzkaller.appspotmail.com, Kirtikumar Anandrao Ramchandani , Mikulas Patocka , Sasha Levin Subject: [PATCH 6.12 104/555] dm cache: validate the discard block size before narrowing Date: Fri, 4 Dec 2026 19:02:27 +0900 Message-ID: <20261204100611.403458364@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kirtikumar Anandrao Ramchandani [ Upstream commit df8daa4d916c88ef7888c738e0eb3b6f11714a6f ] The discard block size doubles until the origin fits within the discard bitset limit. too_many_discard_blocks() passes this sector_t size to sector_div(), whose divisor is only 32 bits. Once the size exceeds that range, the division uses a truncated divisor, which can become zero. Use a 64-bit divisor when checking the block count. The selected size is stored in a u32, so reject sizes that cannot be represented before the assignment and before calculating the bitset size. This prevents a truncated zero size from reaching dm_sector_div_up(). An earlier proposal checks whether the mapped length exceeds the origin device. This fix also covers an origin device whose valid size requires a discard block size beyond the field's range. Reported-by: syzbot+c8564fc0a96e3ecbe4d4@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=c8564fc0a96e3ecbe4d4 Cc: stable@vger.kernel.org Fixes: 08b184514f65 ("dm cache: revert 'prevent corruption caused by discard_block_size > cache_block_size'") Signed-off-by: Kirtikumar Anandrao Ramchandani Signed-off-by: Mikulas Patocka [ sashal: Adjusted the include context: added only #include between init.h and mempool.h, since kstrtox.h isn't present in this tree. ] Signed-off-by: Sasha Levin --- drivers/md/dm-cache-target.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c index a9e54bdd378bd..a3706ebdd2ac2 100644 --- a/drivers/md/dm-cache-target.c +++ b/drivers/md/dm-cache-target.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -2389,9 +2390,7 @@ static int create_cache_policy(struct cache *cache, struct cache_args *ca, static bool too_many_discard_blocks(sector_t discard_block_size, sector_t origin_size) { - (void) sector_div(origin_size, discard_block_size); - - return origin_size > MAX_DISCARD_BLOCKS; + return div64_u64(origin_size, discard_block_size) > MAX_DISCARD_BLOCKS; } static sector_t calculate_discard_block_size(sector_t cache_block_size, @@ -2428,6 +2427,7 @@ static int cache_create(struct cache_args *ca, struct cache **result) struct cache *cache; struct dm_target *ti = ca->ti; dm_block_t origin_blocks; + sector_t discard_block_size; struct dm_cache_metadata *cmd; bool may_format = ca->features.mode == CM_WRITE; @@ -2527,9 +2527,15 @@ static int cache_create(struct cache_args *ca, struct cache **result) } clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size)); - cache->discard_block_size = + discard_block_size = calculate_discard_block_size(cache->sectors_per_block, cache->origin_sectors); + if (discard_block_size > U32_MAX) { + *error = "Discard block size exceeds 32 bits"; + r = -EOVERFLOW; + goto bad; + } + cache->discard_block_size = discard_block_size; cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors, cache->discard_block_size)); cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks)); -- 2.53.0