The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] mtd: mtdpart: fix overflow check for partition offset and size
@ 2026-12-03  8:05 Fang Xieyan
  2026-12-03 10:00 ` Miquel Raynal
  0 siblings, 1 reply; 4+ messages in thread
From: Fang Xieyan @ 2026-12-03  8:05 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr; +Cc: linux-mtd, linux-kernel, stable

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)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: mtdpart: fix overflow check for partition offset and size
  2026-12-03  8:05 [PATCH] mtd: mtdpart: fix overflow check for partition offset and size Fang Xieyan
@ 2026-12-03 10:00 ` Miquel Raynal
  2026-12-03 23:53   ` Fang Xieyan
  0 siblings, 1 reply; 4+ messages in thread
From: Miquel Raynal @ 2026-12-03 10:00 UTC (permalink / raw)
  To: Fang Xieyan; +Cc: richard, vigneshr, linux-mtd, linux-kernel, stable

Hi,

On 03/12/2026 at 16:05:47 +08, Fang Xieyan <fangxy@xiaopeng.com> wrote:

> 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.

The day you see an 8 Zetta bytes flash, you're gonna have many more
issues than this one.

MTD maintainers, shall we take this kind of over denfensive fix? The
diff is minimal, so I'm not bothered by the change besides the fact that
I don't think it is in any way useful.

Thanks,
Miquèl

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: mtdpart: fix overflow check for partition offset and size
  2026-12-03 10:00 ` Miquel Raynal
@ 2026-12-03 23:53   ` Fang Xieyan
  2026-12-04 10:38     ` Miquel Raynal
  0 siblings, 1 reply; 4+ messages in thread
From: Fang Xieyan @ 2026-12-03 23:53 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel

Hi Miquel,

Thanks for the review.

The more convincing problem in this check is functional and
independent of the overflow, so I should have led with it.

Commit 9e3307a16953 ("mtd: Add support for emulated SLC mode on MLC
NANDs") converted the offset check, the MTDPART_SIZ_FULL case, the
MTDPART_OFS_RETAIN case and the truncation itself to parent_size, but
left this condition comparing against parent->size.  For a parent with
MTD_SLC_ON_MLC_EMULATION set, add_mtd_device() scales parent->size
down while parent->part.size keeps the unscaled size (the distinction
is documented in struct mtd_part).  The check then compares the
child's unscaled size against the parent's scaled size, so it can
fire for a sub-partition that fits; and since the truncation assigns
parent_size - offset, a size smaller than the remaining space is
increased to it.

I reproduced this on v7.4-rc5 with a debug-only harness: a fake MLC
master with a 256 KiB eraseblock and two pairing groups.  All sizes
below are eraseblock-aligned.

  parent: part.size = 0x100000, scaled mtd->size = 0x80000
  child:  offset = 0, size = 0xc0000 (fits the unscaled extent; after
          scaling it is also within the parent's visible size)

  baseline:  mtd: partition "emu-child" extends beyond the end of
             device "emu-parent" -- size truncated to 0x100000
             (part.size 0xc0000 -> 0x100000, increased, not truncated)
  patched:   no truncation, 0xc0000 kept

So the miscomparison is a consequence of mixing the scaled and
unscaled size domains, reachable with ordinary slc-mode partition
tables, not with extreme values.

The original overflow case is reproducible too, admittedly from a
corrupted description rather than a typical one: on a 12 MiB parent,
a child at offset 0x100 with size 0xffffffffffffff80 wraps the sum
to 0x80, bypasses the clamp, and is registered with that size
(/proc/mtd shows ffffffffffffff80); the patched check truncates it to
the remaining 0xbfff00.

Comparing against parent_size - offset keeps the check in the domain
the rest of allocate_partition() adopted in 9e3307a16953, and bounds
the overflow on top.  If you agree the SLC case is worth fixing, I
can respin with the commit message focused on it and the Fixes tag
pointing at 9e3307a16953.

Thanks,

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: mtdpart: fix overflow check for partition offset and size
  2026-12-03 23:53   ` Fang Xieyan
@ 2026-12-04 10:38     ` Miquel Raynal
  0 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2026-12-04 10:38 UTC (permalink / raw)
  To: Fang Xieyan
  Cc: Richard Weinberger, Vignesh Raghavendra, linux-mtd, linux-kernel

On 04/12/2026 at 07:53:09 +08, Fang Xieyan <fangxy@xiaopeng.com> wrote:

> Hi Miquel,
>
> Thanks for the review.
>
> The more convincing problem in this check is functional and
> independent of the overflow, so I should have led with it.
>
> Commit 9e3307a16953 ("mtd: Add support for emulated SLC mode on MLC
> NANDs") converted the offset check, the MTDPART_SIZ_FULL case, the
> MTDPART_OFS_RETAIN case and the truncation itself to parent_size, but
> left this condition comparing against parent->size.  For a parent with
> MTD_SLC_ON_MLC_EMULATION set, add_mtd_device() scales parent->size
> down while parent->part.size keeps the unscaled size (the distinction
> is documented in struct mtd_part).  The check then compares the
> child's unscaled size against the parent's scaled size, so it can
> fire for a sub-partition that fits; and since the truncation assigns
> parent_size - offset, a size smaller than the remaining space is
> increased to it.
>
> I reproduced this on v7.4-rc5 with a debug-only harness: a fake MLC
> master with a 256 KiB eraseblock and two pairing groups.  All sizes
> below are eraseblock-aligned.
>
>   parent: part.size = 0x100000, scaled mtd->size = 0x80000
>   child:  offset = 0, size = 0xc0000 (fits the unscaled extent; after
>           scaling it is also within the parent's visible size)
>
>   baseline:  mtd: partition "emu-child" extends beyond the end of
>              device "emu-parent" -- size truncated to 0x100000
>              (part.size 0xc0000 -> 0x100000, increased, not truncated)
>   patched:   no truncation, 0xc0000 kept
>
> So the miscomparison is a consequence of mixing the scaled and
> unscaled size domains, reachable with ordinary slc-mode partition
> tables, not with extreme values.
>
> The original overflow case is reproducible too, admittedly from a
> corrupted description rather than a typical one: on a 12 MiB parent,
> a child at offset 0x100 with size 0xffffffffffffff80 wraps the sum
> to 0x80, bypasses the clamp, and is registered with that size
> (/proc/mtd shows ffffffffffffff80); the patched check truncates it to
> the remaining 0xbfff00.

That is not a case we need to protect against. DT and cmdline is reputed
correct. Garbage in, garbage out.

> Comparing against parent_size - offset keeps the check in the domain
> the rest of allocate_partition() adopted in 9e3307a16953, and bounds
> the overflow on top.  If you agree the SLC case is worth fixing, I
> can respin with the commit message focused on it and the Fixes tag
> pointing at 9e3307a16953.

You can update the commit log, as I said, the fix is not invasive so
that's fine, but I'm still not convinced it can be useful in
reality. Please lower the dramatical tone of your commit log to mention
that this is pure in-depth protection.

Thanks,
Miquèl

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04 10:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  8:05 [PATCH] mtd: mtdpart: fix overflow check for partition offset and size Fang Xieyan
2026-12-03 10:00 ` Miquel Raynal
2026-12-03 23:53   ` Fang Xieyan
2026-12-04 10:38     ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox