mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 2/3] commands: md: fix OOB read when mmapping
Date: Mon, 12 Jan 2026 09:56:05 +0100	[thread overview]
Message-ID: <20260112085617.2952747-2-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260112085617.2952747-1-a.fatoum@barebox.org>

md -s /dev/mmappable.device will read out-of-bounds if the byte count to
read exceeds the device size.

Limit the size read from the memmap to fix this.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 commands/md.c    | 4 ++++
 common/ratp/md.c | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/commands/md.c b/commands/md.c
index f3758f571fb2..401538d4d8be 100644
--- a/commands/md.c
+++ b/commands/md.c
@@ -25,6 +25,7 @@ static int do_mem_md(int argc, char *argv[])
 	loff_t	start = 0, size = 0x100;
 	int	r, now;
 	int	ret = 0;
+	struct stat st;
 	int fd;
 	char *filename = "/dev/mem";
 	int mode = O_RWSIZE_4;
@@ -54,6 +55,9 @@ static int do_mem_md(int argc, char *argv[])
 		return 1;
 	}
 
+	if (!fstat(fd, &st) && st.st_size != FILE_SIZE_STREAM)
+		size = min(size, st.st_size);
+
 	map = memmap(fd, PROT_READ);
 	if (map != MAP_FAILED) {
 		ret = memory_display(map + start, start, size,
diff --git a/common/ratp/md.c b/common/ratp/md.c
index 8221afaebc22..f3302dedafb7 100644
--- a/common/ratp/md.c
+++ b/common/ratp/md.c
@@ -61,6 +61,7 @@ static int do_ratp_mem_md(const char *filename,
 			  loff_t size,
 			  uint8_t *output)
 {
+	struct stat st;
 	int r, now, t;
 	int ret = 0;
 	int fd;
@@ -73,6 +74,9 @@ static int do_ratp_mem_md(const char *filename,
 		return -errno;
 	}
 
+	if (!fstat(fd, &st) && st.st_size != FILE_SIZE_STREAM)
+		size = min(size, st.st_size);
+
 	map = memmap(fd, PROT_READ);
 	if (map != MAP_FAILED) {
 		memcpy(output, (uint8_t *)(map + start), size);
-- 
2.47.3




  reply	other threads:[~2026-01-12  8:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-12  8:56 [PATCH 1/3] regulator: bcm2835: register only on Raspberry Pi Ahmad Fatoum
2026-01-12  8:56 ` Ahmad Fatoum [this message]
2026-01-12  8:56 ` [PATCH 3/3] pbl: decomp: report errors on PBL console as well Ahmad Fatoum

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=20260112085617.2952747-2-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox