From nobody Sat Jul 25 15:52:31 2026 Received: from canpmsgout03.his.huawei.com (canpmsgout03.his.huawei.com [113.46.200.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 109A642376D for ; Thu, 16 Jul 2026 13:23:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=113.46.200.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784208190; cv=none; b=qyU4M0edPlNimGCNgEUT2fl2a9uMjha4/33o1NXNP2jitt4bolOQ8y1xGQkMBh2pWdSmpkUiXmV04p0Avs7Yy6QUNJ2pMScrDboUpg/MgvkQ4qzBwyMy40hLlxW4+yiLeoW3KqIiGLBgngJKmF2SOvjZpDJTDZZJ4gzNeDuWxEk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784208190; c=relaxed/simple; bh=xHeHCvrVAL/Tcmu5Z0N0//+UjldhWTGW/5jy5lXbRzg=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=ETBBKt99b+Oj+WGPJQEpNi0hNhKNKyQgICiNwT6aK9HU8YB3wqHTWqWFbwhFUmboE/+jl24M9ZlNeblgV8d7F5UTJyJHbDx4ZeBEMNEwYM3effhidhYqUur+BeK+Eq9/Xm9aTHBPUPFWc2jYHMijUls3T8Kp+3ZgOlvuDV45lh4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b=KdHy22eQ; arc=none smtp.client-ip=113.46.200.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=huawei.com header.i=@huawei.com header.b="KdHy22eQ" dkim-signature: v=1; a=rsa-sha256; d=huawei.com; s=dkim; c=relaxed/relaxed; q=dns/txt; h=From; bh=4UzHivmPv4sGDOdOPE7VyO5rahWexPUdRRxBAZUmcC0=; b=KdHy22eQ10zKrthqbPhl6DL1SHyYt5WEbE92vv8W0rEGaQw1Ojicnb/0UWk6NSDfIwjmfbnzA bHd4wPq01D78rLCT8gf38ZZlC9cqwhCXcQIflDIa7K03F77o4VdpQGu4QHCvNR9t1zrK8BlEg91 xeM4NjJnBZjgRAkAdOzwRpE= Received: from mail.maildlp.com (unknown [172.19.163.104]) by canpmsgout03.his.huawei.com (SkyGuard) with ESMTPS id 4h1D5S66GSzpT0h; Thu, 16 Jul 2026 21:14:04 +0800 (CST) Received: from kwepemg100005.china.huawei.com (unknown [7.202.181.23]) by mail.maildlp.com (Postfix) with ESMTPS id 7A91C4058C; Thu, 16 Jul 2026 21:22:59 +0800 (CST) Received: from huawei.com (10.50.85.155) by kwepemg100005.china.huawei.com (7.202.181.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.36; Thu, 16 Jul 2026 21:22:58 +0800 From: zhouminqiang To: , , , , CC: , , , , Subject: [PATCH] mtd: part: reject MTDPART_OFS_RETAIN in mtd_add_partition() Date: Thu, 16 Jul 2026 21:14:32 +0800 Message-ID: <20260716131432.2355777-1-zhouminqiang2@huawei.com> X-Mailer: git-send-email 2.52.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems200001.china.huawei.com (7.221.188.67) To kwepemg100005.china.huawei.com (7.202.181.23) Content-Type: text/plain; charset="utf-8" mtd_add_partition() does not reject the special offset value MTDPART_OFS_RETAIN (-3), which leads to a WARN_ON in add_mtd_device() when called through the BLKPG ioctl on NAND devices. The RETAIN value depends on cur_offset being the end of the previous partition, but in the dynamic partition path cur_offset equals the offset argument itself, causing undefined behavior. Commit 5daa7b21496a ("mtd: prepare partition add and del functions for ioctl requests") introduced mtd_add_partition() and correctly rejected MTDPART_OFS_APPEND (-1) and MTDPART_OFS_NXTBLK (-2), since those special offsets rely on cur_offset tracking the previous partition's end. However, commit 1a31368bf92e ("mtd: add a flags for partitions which should just leave smth. after them") later added MTDPART_OFS_RETAIN (-3) for the static partition table path without updating mtd_add_partition() to also reject this value. With offset=3D-3 passed via BLKPG, the RETAIN size calculation in allocate_partition() underflows (parent_size - 0xFFFFFFFFFFFFFFFD =3D parent_size + 3). If the underflow result does not appear to leave enough space, allocate_partition() jumps to out_register via goto, skipping erasesize initialization. This results in erasesize=3D0, which triggers: WARN_ON((!mtd->erasesize || !master->_erase) && !(mtd->flags & MTD_NO_ERASE)) in add_mtd_device(). If the underflow result appears to leave enough space, a bogus partition size is calculated, but the "out of reach" sanity check catches the invalid offset and creates a disabled empty partition (offset=3D0, size=3D0) instead of returning an error. Fix this by adding MTDPART_OFS_RETAIN to the rejection list in mtd_add_partition(), consistent with the existing handling of APPEND and NXTBLK. Fixes: 1a31368bf92e ("mtd: add a flags for partitions which should just lea= ve smth. after them") Signed-off-by: zhouminqiang Reviewed-by: Zhihao Cheng --- drivers/mtd/mtdpart.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c index 795a94e6b482..11cab777a1ac 100644 --- a/drivers/mtd/mtdpart.c +++ b/drivers/mtd/mtdpart.c @@ -255,7 +255,8 @@ int mtd_add_partition(struct mtd_info *parent, const ch= ar *name, =20 /* the direct offset is expected */ if (offset =3D=3D MTDPART_OFS_APPEND || - offset =3D=3D MTDPART_OFS_NXTBLK) + offset =3D=3D MTDPART_OFS_NXTBLK || + offset =3D=3D MTDPART_OFS_RETAIN) return -EINVAL; =20 if (length =3D=3D MTDPART_SIZ_FULL) --=20 2.52.0