Skip to contentSkip to conversation

memstick: ms_block: reject an invalid bad block table size

PATCH New Maoyi Xie 1 participant 1 message Nov 27

Subsystems (2)
lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

PATCH 1/1 memstick: ms_block: reject an invalid bad block table size

msb_read_bad_block_table() stores the u32 table size from the card boot page in an int and checks it only for zero. A size of 0xffffffff becomes -1. size_to_read is then 0 or one page, but the loop bound data_size / sizeof(u16) becomes huge. The loop faults on ZERO_SIZE_PTR or reads past the buffer.

The table is stored in the boot block. Reject a size that is negative or larger than the block.

Found with a CodeQL-based static analysis tool that we are developing. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 0ab30494bc4f ("memstick: add support for legacy memorysticks")

---
I used an LLM to help write the harness and this patch.

 drivers/memstick/core/ms_block.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/memstick/core/ms_block.c +5 -0
index d4143a871d69..37b810760642 100644
@@ -1274,6 +1274,11 @@ static int msb_read_bad_block_table(struct msb_data *msb, int block_nr)
 	if (!data_size)
 		return 0;
 
+	if (data_size < 0 || data_size > msb->block_size) {
+		pr_err("Invalid bad block table size: %d\n", data_size);
+		return -EINVAL;
+	}
+
 	page = data_offset / msb->page_size;
 	page_offset = data_offset % msb->page_size;
 	size_to_read =
Signature · 1 line
2.34.1