* [PATCH] mtd: afs: validate v2 image info bounds
@ 2026-07-06 9:40 Pengpeng Hou
2026-07-06 14:47 ` Miquel Raynal
0 siblings, 1 reply; 2+ messages in thread
From: Pengpeng Hou @ 2026-07-06 9:40 UTC (permalink / raw)
To: Miquel Raynal
Cc: Pengpeng Hou, Richard Weinberger, Vignesh Raghavendra, linux-mtd,
linux-kernel
The AFS v2 parser uses footer[8] to locate the image information block
inside the current erase block, then uses the image information
region_count to walk entries from a fixed local array. The footer offset
and region count come from flash contents and were not checked against the
erase block or the local image-info array before use.
Reject v2 entries whose image information offset would underflow the erase
block calculation, and reject region counts that cannot fit in the local
image-info array before walking region entries.
Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
---
drivers/mtd/parsers/afs.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c
index 26116694c821..7ab3d50f565e 100644
--- a/drivers/mtd/parsers/afs.c
+++ b/drivers/mtd/parsers/afs.c
@@ -235,6 +235,9 @@ static int afs_parse_v2_partition(struct mtd_info *mtd,
pr_debug("Parsing v2 partition @%08x-%08x\n",
off, off + mtd->erasesize);
+ if (mtd->erasesize < sizeof(footer))
+ return -EINVAL;
+
/* First read the footer */
ptr = off + mtd->erasesize - sizeof(footer);
ret = mtd_read(mtd, ptr, sizeof(footer), &sz, (u_char *)footer);
@@ -245,6 +248,8 @@ static int afs_parse_v2_partition(struct mtd_info *mtd,
}
name = (char *) &footer[0];
version = footer[9];
+ if (footer[8] > mtd->erasesize - sizeof(footer))
+ return -EINVAL;
ptr = off + mtd->erasesize - sizeof(footer) - footer[8];
pr_debug("found image \"%s\", version %08x, info @%08x\n",
@@ -278,6 +283,8 @@ static int afs_parse_v2_partition(struct mtd_info *mtd,
entrypoint = imginfo[pad];
attributes = imginfo[pad+1];
region_count = imginfo[pad+2];
+ if (region_count > (ARRAY_SIZE(imginfo) - pad - 3) / 4)
+ return -EINVAL;
block_start = imginfo[20];
block_end = imginfo[21];
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] mtd: afs: validate v2 image info bounds
2026-07-06 9:40 [PATCH] mtd: afs: validate v2 image info bounds Pengpeng Hou
@ 2026-07-06 14:47 ` Miquel Raynal
0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2026-07-06 14:47 UTC (permalink / raw)
To: Pengpeng Hou
Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel
On 06/07/2026 at 17:40:59 +08, Pengpeng Hou <pengpeng@iscas.ac.cn> wrote:
> The AFS v2 parser uses footer[8] to locate the image information block
> inside the current erase block, then uses the image information
> region_count to walk entries from a fixed local array. The footer offset
> and region count come from flash contents and were not checked against the
> erase block or the local image-info array before use.
>
> Reject v2 entries whose image information offset would underflow the erase
> block calculation, and reject region counts that cannot fit in the local
> image-info array before walking region entries.
>
> Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
I'll let you guess what is missing in this contribution?
Thanks,
Miquèl
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-06 14:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-06 9:40 [PATCH] mtd: afs: validate v2 image info bounds Pengpeng Hou
2026-07-06 14:47 ` Miquel Raynal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox