[PATCH] mtd: rawnand: Annotate struct mtk_nfc_nand_chip with __counted_by

Kees Cook keescook at chromium.org
Fri Oct 6 13:17:28 PDT 2023


Prepare for the coming implementation by GCC and Clang of the __counted_by
attribute. Flexible array members annotated with __counted_by can have
their accesses bounds-checked at run-time via CONFIG_UBSAN_BOUNDS (for
array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
functions).

As found with Coccinelle[1], add __counted_by for struct
mtk_nfc_nand_chip.

Cc: Miquel Raynal <miquel.raynal at bootlin.com>
Cc: Richard Weinberger <richard at nod.at>
Cc: Vignesh Raghavendra <vigneshr at ti.com>
Cc: Matthias Brugger <matthias.bgg at gmail.com>
Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno at collabora.com>
Cc: "Gustavo A. R. Silva" <gustavoars at kernel.org>
Cc: Heiko Stuebner <heiko at sntech.de>
Cc: Martin Blumenstingl <martin.blumenstingl at googlemail.com>
Cc: Thierry Reding <treding at nvidia.com>
Cc: Roger Quadros <rogerq at kernel.org>
Cc: "Uwe Kleine-König" <u.kleine-koenig at pengutronix.de>
Cc: Cai Huoqing <cai.huoqing at linux.dev>
Cc: Chuanhong Guo <gch981213 at gmail.com>
Cc: Rob Herring <robh at kernel.org>
Cc: Li Zetao <lizetao1 at huawei.com>
Cc: linux-mtd at lists.infradead.org
Cc: linux-arm-kernel at lists.infradead.org
Cc: linux-mediatek at lists.infradead.org
Cc: linux-hardening at vger.kernel.org
Link: https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci [1]
Signed-off-by: Kees Cook <keescook at chromium.org>
---
 drivers/mtd/nand/raw/mtk_nand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/nand/raw/mtk_nand.c b/drivers/mtd/nand/raw/mtk_nand.c
index 29c8bddde67f..60198e33d2d5 100644
--- a/drivers/mtd/nand/raw/mtk_nand.c
+++ b/drivers/mtd/nand/raw/mtk_nand.c
@@ -130,7 +130,7 @@ struct mtk_nfc_nand_chip {
 	u32 spare_per_sector;
 
 	int nsels;
-	u8 sels[];
+	u8 sels[] __counted_by(nsels);
 	/* nothing after this field */
 };
 
-- 
2.34.1




More information about the Linux-mediatek mailing list