* [PATCH 6.1.y 2/3] dm cache: parse invalidate_cblocks with kstrtouint()
2026-11-26 5:09 ` [PATCH 6.1.y 1/3] dm-cache: remove pointless error check Sasha Levin
@ 2026-11-26 5:09 ` Sasha Levin
2026-11-26 5:09 ` [PATCH 6.1.y 3/3] dm cache: validate the discard block size before narrowing Sasha Levin
1 sibling, 0 replies; 4+ messages in thread
From: Sasha Levin @ 2026-11-26 5:09 UTC (permalink / raw)
To: stable; +Cc: Samuel Moelius, Mikulas Patocka, Ming-Hung Tsai, Sasha Levin
From: Samuel Moelius <sam.moelius@trailofbits.com>
[ Upstream commit 00211ede62facec2915a7e6cfc17f8ce987a0078 ]
invalidate_cblocks parses cache block numbers with sscanf() and then
stores them in the narrower dm_cblock_t type. Values larger than the
cblock representation are truncated before invalidation, so a request
for one cache block can invalidate a different block.
Checking the parsed value after sscanf() is not sufficient because
sscanf() does not reliably reject values beyond U64_MAX before storing
into the destination. Such inputs can still be converted to a wrapped
u64 value and then pass a later range check.
Split ranges in place and parse each single value or range endpoint
directly with kstrtouint() instead. This rejects malformed values and
values that do not fit in dm_cblock_t before they can be converted to
cblock values. The existing range validation continues to reject empty
or out-of-cache ranges, including the single-value U32_MAX case whose
exclusive end wraps to zero.
Assisted-by: Codex:gpt-5.5-cyber-preview
Signed-off-by: Samuel Moelius <sam.moelius@trailofbits.com>
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Reviewed-by: Ming-Hung Tsai <mtsai@redhat.com>
Stable-dep-of: df8daa4d916c ("dm cache: validate the discard block size before narrowing")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/md/dm-cache-target.c | 56 ++++++++++++++++++------------------
1 file changed, 28 insertions(+), 28 deletions(-)
diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c
index 69ab5f9a33483..d304e62fe2cc2 100644
--- a/drivers/md/dm-cache-target.c
+++ b/drivers/md/dm-cache-target.c
@@ -14,6 +14,7 @@
#include <linux/dm-kcopyd.h>
#include <linux/jiffies.h>
#include <linux/init.h>
+#include <linux/kstrtox.h>
#include <linux/mempool.h>
#include <linux/module.h>
#include <linux/rwsem.h>
@@ -3306,42 +3307,46 @@ struct cblock_range {
dm_cblock_t end;
};
+static inline dm_cblock_t cblock_succ(dm_cblock_t b)
+{
+ return to_cblock(from_cblock(b) + 1);
+}
+
/*
* A cache block range can take two forms:
*
* i) A single cblock, eg. '3456'
* ii) A begin and end cblock with a dash between, eg. 123-234
*/
-static int parse_cblock_range(struct cache *cache, const char *str,
+static int parse_cblock_range(struct cache *cache, char *str,
struct cblock_range *result)
{
- char dummy;
- uint64_t b, e;
+ char *blocknr = strsep(&str, "-");
+ unsigned int b, e;
int r;
- /*
- * Try and parse form (ii) first.
- */
- r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
+ r = kstrtouint(blocknr, 10, &b);
+ if (r)
+ goto bad;
- if (r == 2) {
- result->begin = to_cblock(b);
- result->end = to_cblock(e);
- return 0;
- }
+ result->begin = to_cblock(b);
- /*
- * That didn't work, try form (i).
- */
- r = sscanf(str, "%llu%c", &b, &dummy);
+ if (str) {
+ blocknr = str;
- if (r == 1) {
- result->begin = to_cblock(b);
- result->end = to_cblock(from_cblock(result->begin) + 1u);
- return 0;
+ r = kstrtouint(blocknr, 10, &e);
+ if (r)
+ goto bad;
+
+ result->end = to_cblock(e);
+ } else {
+ result->end = cblock_succ(result->begin);
}
- DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
+ return 0;
+
+bad:
+ DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), blocknr);
return -EINVAL;
}
@@ -3372,11 +3377,6 @@ static int validate_cblock_range(struct cache *cache, struct cblock_range *range
return 0;
}
-static inline dm_cblock_t cblock_succ(dm_cblock_t b)
-{
- return to_cblock(from_cblock(b) + 1);
-}
-
static int request_invalidation(struct cache *cache, struct cblock_range *range)
{
int r = 0;
@@ -3400,7 +3400,7 @@ static int request_invalidation(struct cache *cache, struct cblock_range *range)
}
static int process_invalidate_cblocks_message(struct cache *cache, unsigned int count,
- const char **cblock_ranges)
+ char **cblock_ranges)
{
int r = 0;
unsigned int i;
@@ -3455,7 +3455,7 @@ static int cache_message(struct dm_target *ti, unsigned int argc, char **argv,
}
if (!strcasecmp(argv[0], "invalidate_cblocks"))
- return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
+ return process_invalidate_cblocks_message(cache, argc - 1, argv + 1);
if (argc != 2)
return -EINVAL;
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 6.1.y 3/3] dm cache: validate the discard block size before narrowing
2026-11-26 5:09 ` [PATCH 6.1.y 1/3] dm-cache: remove pointless error check Sasha Levin
2026-11-26 5:09 ` [PATCH 6.1.y 2/3] dm cache: parse invalidate_cblocks with kstrtouint() Sasha Levin
@ 2026-11-26 5:09 ` Sasha Levin
1 sibling, 0 replies; 4+ messages in thread
From: Sasha Levin @ 2026-11-26 5:09 UTC (permalink / raw)
To: stable
Cc: Kirtikumar Anandrao Ramchandani, syzbot+c8564fc0a96e3ecbe4d4,
Mikulas Patocka, Sasha Levin
From: Kirtikumar Anandrao Ramchandani <kirtiar15502@gmail.com>
[ 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 <kirtiar15502@gmail.com>
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
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 d304e62fe2cc2..7d7bd25e27517 100644
--- a/drivers/md/dm-cache-target.c
+++ b/drivers/md/dm-cache-target.c
@@ -15,6 +15,7 @@
#include <linux/jiffies.h>
#include <linux/init.h>
#include <linux/kstrtox.h>
+#include <linux/math64.h>
#include <linux/mempool.h>
#include <linux/module.h>
#include <linux/rwsem.h>
@@ -2392,9 +2393,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,
@@ -2431,6 +2430,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;
@@ -2530,9 +2530,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
^ permalink raw reply related [flat|nested] 4+ messages in thread