* [PATCH] mtd: jedec_probe: fix shift-out-of-bounds UB in JEDEC ID masking
@ 2026-01-22 1:32 Chenxi Hou
2026-01-22 14:08 ` Miquel Raynal
0 siblings, 1 reply; 2+ messages in thread
From: Chenxi Hou @ 2026-01-22 1:32 UTC (permalink / raw)
To: linux-kernel
Cc: colin.i.king, linux-mtd, miquel.raynal, richard, rk0006818,
vigneshr, Chenxi Hou, Hui Peng, Zhihao (Zephyr) Yao
UBSAN reports shift-out-of-bounds in jedec_read_mfr() and jedec_read_id():
UBSAN: shift-out-of-bounds in drivers/mtd/chips/jedec_probe.c:1924:13
shift exponent 32 is too large for 32-bit type 'int'
UBSAN: shift-out-of-bounds in drivers/mtd/chips/jedec_probe.c:1940:12
shift exponent 32 is too large for 32-bit type 'int'
The JEDEC manufacturer/device ID masking uses:
(1 << (cfi->device_type * 8)) - 1
When cfi->device_type is 4, this evaluates to 1 << 32. Since the
literal '1' has type int, this is a 32-bit shift and is undefined behavior.
Fix it by using a 64-bit literal (1ULL) so the shift is performed in a 64-bit type.
Co-developed-by: Hui Peng <benquike@gmail.com>
Signed-off-by: Hui Peng <benquike@gmail.com>
Co-developed-by: Zhihao (Zephyr) Yao <zhihao.yao@njit.edu>
Signed-off-by: Zhihao (Zephyr) Yao <zhihao.yao@njit.edu>
Signed-off-by: Chenxi Hou <ch395@njit.edu>
---
drivers/mtd/chips/jedec_probe.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/chips/jedec_probe.c b/drivers/mtd/chips/jedec_probe.c
index b285962eee2a..3c631608f6d6 100644
--- a/drivers/mtd/chips/jedec_probe.c
+++ b/drivers/mtd/chips/jedec_probe.c
@@ -1921,7 +1921,7 @@ static inline u32 jedec_read_mfr(struct map_info *map, uint32_t base,
*/
do {
uint32_t ofs = cfi_build_cmd_addr(0 + (bank << 8), map, cfi);
- mask = (1 << (cfi->device_type * 8)) - 1;
+ mask = (1ULL << (cfi->device_type * 8)) - 1;
if (ofs >= map->size)
return 0;
result = map_read(map, base + ofs);
@@ -1937,7 +1937,7 @@ static inline u32 jedec_read_id(struct map_info *map, uint32_t base,
map_word result;
unsigned long mask;
u32 ofs = cfi_build_cmd_addr(1, map, cfi);
- mask = (1 << (cfi->device_type * 8)) -1;
+ mask = (1ULL << (cfi->device_type * 8)) - 1;
result = map_read(map, base + ofs);
return result.x[0] & mask;
}
--
2.52.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] mtd: jedec_probe: fix shift-out-of-bounds UB in JEDEC ID masking
2026-01-22 1:32 [PATCH] mtd: jedec_probe: fix shift-out-of-bounds UB in JEDEC ID masking Chenxi Hou
@ 2026-01-22 14:08 ` Miquel Raynal
0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2026-01-22 14:08 UTC (permalink / raw)
To: linux-kernel, Chenxi Hou
Cc: colin.i.king, linux-mtd, richard, rk0006818, vigneshr, Hui Peng,
Zhihao (Zephyr) Yao
On Wed, 21 Jan 2026 20:32:50 -0500, Chenxi Hou wrote:
> UBSAN reports shift-out-of-bounds in jedec_read_mfr() and jedec_read_id():
>
> UBSAN: shift-out-of-bounds in drivers/mtd/chips/jedec_probe.c:1924:13
> shift exponent 32 is too large for 32-bit type 'int'
> UBSAN: shift-out-of-bounds in drivers/mtd/chips/jedec_probe.c:1940:12
> shift exponent 32 is too large for 32-bit type 'int'
>
> [...]
Applied to mtd/next, thanks!
[1/1] mtd: jedec_probe: fix shift-out-of-bounds UB in JEDEC ID masking
commit: 2b14660c1caf0b67adf99428a225f71ed653437d
Patche(s) should be available on mtd/linux.git and will be
part of the next PR (provided that no robot complains by then).
Kind regards,
Miquèl
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-01-22 14:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-22 1:32 [PATCH] mtd: jedec_probe: fix shift-out-of-bounds UB in JEDEC ID masking Chenxi Hou
2026-01-22 14:08 ` Miquel Raynal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox