All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maoyi Xie <maoyixie.tju@gmail.com>
To: Maxim Levitsky <maximlevitsky@gmail.com>
Cc: Alex Dubov <oakad@yahoo.com>, Ulf Hansson <ulfh@kernel.org>,
	linux-mmc@vger.kernel.org
Subject: [PATCH] memstick: ms_block: reject an invalid bad block table size
Date: Fri, 27 Nov 2026 14:35:53 +0800	[thread overview]
Message-ID: <20261127063553.36734-1-maoyixie.tju@gmail.com> (raw)

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")
Cc: stable@vger.kernel.org
Signed-off-by: Maoyi Xie <maoyixie.tju@gmail.com>
---
I used an LLM to help write the harness and this patch.

 drivers/memstick/core/ms_block.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
index d4143a871d69..37b810760642 100644
--- a/drivers/memstick/core/ms_block.c
+++ b/drivers/memstick/core/ms_block.c
@@ -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 =
-- 
2.34.1


                 reply	other threads:[~2026-11-27  6:35 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127063553.36734-1-maoyixie.tju@gmail.com \
    --to=maoyixie.tju@gmail.com \
    --cc=linux-mmc@vger.kernel.org \
    --cc=maximlevitsky@gmail.com \
    --cc=oakad@yahoo.com \
    --cc=ulfh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.