From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.246.85.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380739; b=l8ZALARB+ixtHXQtXqA4fZsccXTMhShyqGrMwzIWt1Zh5XsR85rQm2XwiyzhbEfpuM/H 4U413JRXttWhLsJOlhmzWn2rpnPOIjSBR8vEU2ltYxJDKC/NdVcAIcXypd9/Oa4MEKQnT rJVg4oP5p6Nja3iAit2LoNQBc38BZvD+oevz9L4VbnT/To25RWWV7Y8V9n3gZ3AMIfjuM 9m1/3p0BTMBRnbPmm3bekABgB3VT5zZ6kneRDyvnBieXDpgo4OFAlIbQqTSW0P796D8qK oDLbKWW23vNa6ytAQTdhhHAm9KmK9PSEC0SDcfkgVZm/abY9fzZFPk7DEH70zhSfmEg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380739; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:From:To:Cc: Subject:In-Reply-To:References:User-Agent:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding: X-Last-TLS-Session-Version; bh=fXBV59PnQmqRx9df/GpSeN6f+XQ63upta15GI7KHzyo=; b=IwlkGOcoYoYLElPpfZ2MFeWh6GwK57emxLO5PyyZ2qwMSYRL2x059ItUPA+H4f1NOMH6 mLGhjPlHTVTLKELEvD7AXZoDSnO7jhdmAmhnKyn7YiLUGfJ3ToXpiOvgXCOJaGtZ2uv2Q Spd/LxCIsRnWcD6lwdf64xtQiLjk7hoFf+UpN3xSh8rUVSkPkit0ankBtUjGRYdr5aV95 RZyOq86vpPMUqDHohXR5AcyKMKlYuHqM9FN/okewjXNOKm9+OoBk2gFDE7g6A3HtNkqQ8 6TGBQ+/SDfBqjuXBmJhLEOglii+2J+hfRHLxH++/lqSoKbUWMGbs+41VWqvRJrqWh7g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=U++Em+gZ; arc=none smtp.remote-ip=185.246.85.4 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 012C5606A336 for ; Fri, 4 Dec 2026 10:38:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 012C5606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=U++Em+gZ Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 5EBB04E41885; Fri, 4 Dec 2026 10:38:57 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 338D1603C4; Fri, 4 Dec 2026 10:38:57 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id CF87C11EA1821; Fri, 04 Dec 2026 11:38:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1796380735; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=fXBV59PnQmqRx9df/GpSeN6f+XQ63upta15GI7KHzyo=; b=U++Em+gZf96/WoUusMnWaW+tQkl3cmiqcsXe5wnOqDfU6XawVBheL7ps58ZmHLteTfli/l 5hKf4gQOybBAPJLtg20KYopK7J2vg5xfviI7xDWItFMuSzRmT9dOMXeWUCnu1vegStlxGB a+gDOy/CXii3HWiiQFxG15Ibs80IH0szC8fFPaKfaL6OdA5oJTIguzD4uqS/M9AqVl3Slq MU1mjOhBjl20eKtyhGtf+0r9BEBZ/XqFb/7gjIHhKe/ulW9AaXfFNbcrowhalVLpRt8L7w GOB+sVgLanhG4P9bNj0Zgos1FPvROjz8J3I24gfPVa1rpHGIX9zN+tEou1lJ1g== From: Miquel Raynal To: Fang Xieyan Cc: Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mtd: mtdpart: fix overflow check for partition offset and size In-Reply-To: <20261203235309.56663-1-fangxy@xiaopeng.com> (Fang Xieyan's message of "Fri, 4 Dec 2026 07:53:09 +0800") References: <87se0ek7jo.fsf@bootlin.com> <20261203235309.56663-1-fangxy@xiaopeng.com> User-Agent: mu4e 1.12.12; emacs 30.2 Date: Fri, 04 Dec 2026 11:38:50 +0100 Message-ID: <87v759gwjp.fsf@bootlin.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 On 04/12/2026 at 07:53:09 +08, Fang Xieyan 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 =3D 0x100000, scaled mtd->size =3D 0x80000 > child: offset =3D 0, size =3D 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=C3=A8l