| From e9290031f736e99ad17c25c00311c92c266843b7 Mon Sep 17 00:00:00 2001 |
| From: Pengpeng Hou <pengpeng@iscas.ac.cn> |
| Date: Wed, 8 Jul 2026 09:49:06 +0800 |
| Subject: mtd: afs: validate v2 image info bounds |
| |
| From: Pengpeng Hou <pengpeng@iscas.ac.cn> |
| |
| commit e9290031f736e99ad17c25c00311c92c266843b7 upstream. |
| |
| 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 are 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. |
| |
| Fixes: b7cf5e2830bb ("mtd: afs: add v2 partition parsing") |
| Cc: stable@vger.kernel.org |
| Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn> |
| Acked-by: Linus Walleij <linusw@kernel.org> |
| Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> |
| Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
| --- |
| drivers/mtd/parsers/afs.c | 7 +++++++ |
| 1 file changed, 7 insertions(+) |
| |
| --- a/drivers/mtd/parsers/afs.c |
| +++ b/drivers/mtd/parsers/afs.c |
| @@ -235,6 +235,9 @@ static int afs_parse_v2_partition(struct |
| 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 |
| } |
| 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 |
| 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]; |
| |