From: Fang Xieyan <fangxy@xiaopeng.com>
To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH] mtd: mtdpart: fix overflow check for partition offset and size
Date: Thu, 3 Dec 2026 16:05:47 +0800 [thread overview]
Message-ID: <20261203080547.15619-1-fangxy@xiaopeng.com> (raw)
allocate_partition() checks whether a partition extends beyond its
parent by adding the partition offset and size. Both values are u64,
so the addition can overflow and wrap to a value smaller than the
parent size. This allows an oversized partition to bypass the sanity
check and be registered without truncation, potentially causing
subsequent partitions to be placed at incorrect offsets.
Partition definitions supplied by MTD partition parsers, such as the
mtdparts= command line parser and device tree fixed-partitions, can
provide values that trigger this overflow.
Compare the partition size against the remaining space after its
offset instead. The preceding check guarantees that the offset is
less than parent_size, so the subtraction cannot underflow.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
Base: v7.4-rc5 (fe6ff8d5d675); the original check is unchanged since
1da177e4c3f4.
Boundary analysis: with parent_size = 0x1000, offset = 0x100, and
size = 0xffffffffffffff80, the original addition wraps to 0x80. The
original check therefore misses the oversized partition. The new
check compares size against the remaining space (0xf00) and
correctly truncates the partition.
Testing: static analysis only; no runtime test performed. The patch
applies cleanly in both directions against v7.4-rc5.
drivers/mtd/mtdpart.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index ddded0dbe..1b396f3bd 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -143,7 +143,7 @@ static struct mtd_info *allocate_partition(struct mtd_info *parent,
part->name);
goto out_register;
}
- if (child->part.offset + child->part.size > parent->size) {
+ if (child->part.size > parent_size - child->part.offset) {
child->part.size = parent_size - child->part.offset;
printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
part->name, parent->name, child->part.size);
--
2.50.1 (Apple Git-155)
next reply other threads:[~2026-12-03 8:06 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 8:05 Fang Xieyan [this message]
2026-12-03 10:00 ` Miquel Raynal
2026-12-03 23:53 ` Fang Xieyan
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=20261203080547.15619-1-fangxy@xiaopeng.com \
--to=fangxy@xiaopeng.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=stable@vger.kernel.org \
--cc=vigneshr@ti.com \
/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
all inboxes | Powered by JetHome®