[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <91f29d89-8c9b-46c4-b059-687ea3c57682@embeddedor.com>
Date: Sat, 10 Feb 2024 10:52:36 -0600
From: "Gustavo A. R. Silva" <gustavo@...eddedor.com>
To: Erick Archer <erick.archer@....com>,
Miquel Raynal <miquel.raynal@...tlin.com>,
Richard Weinberger <richard@....at>, Vignesh Raghavendra <vigneshr@...com>,
Matthias Brugger <matthias.bgg@...il.com>,
AngeloGioacchino Del Regno <angelogioacchino.delregno@...labora.com>,
Martin Blumenstingl <martin.blumenstingl@...glemail.com>,
Heiko Stuebner <heiko@...ech.de>, Philippe Mathieu-Daudé
<philmd@...aro.org>, Rob Herring <robh@...nel.org>,
Li Zetao <lizetao1@...wei.com>,
Uwe Kleine-König <u.kleine-koenig@...gutronix.de>,
"Gustavo A. R. Silva" <gustavoars@...nel.org>,
Kees Cook <keescook@...omium.org>
Cc: linux-mtd@...ts.infradead.org, linux-kernel@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org, linux-mediatek@...ts.infradead.org,
linux-hardening@...r.kernel.org
Subject: Re: [PATCH] mtd: rawnand: Prefer struct_size over open coded
arithmetic
On 2/10/24 10:16, Erick Archer wrote:
> This is an effort to get rid of all multiplications from allocation
> functions in order to prevent integer overflows [1].
>
> As the "chip" variable is a pointer to "struct mtk_nfc_nand_chip" and
> this structure ends in a flexible array:
>
> struct mtk_nfc_nand_chip {
> [...]
> u8 sels[] __counted_by(nsels);
> };
>
> the preferred way in the kernel is to use the struct_size() helper to
> do the arithmetic instead of the argument "size + count * size" in the
> devm_kzalloc() function.
>
> This way, the code is more readable and more safer.
>
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/160 [2]
> Signed-off-by: Erick Archer <erick.archer@....com>
LGTM:
Reviewed-by: Gustavo A. R. Silva <gustavoars@...nel.org>
Thanks!
--
Gustavo
> ---
> 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 60198e33d2d5..17477bb2d48f 100644
> --- a/drivers/mtd/nand/raw/mtk_nand.c
> +++ b/drivers/mtd/nand/raw/mtk_nand.c
> @@ -1356,7 +1356,7 @@ static int mtk_nfc_nand_chip_init(struct device *dev, struct mtk_nfc *nfc,
> return -EINVAL;
> }
>
> - chip = devm_kzalloc(dev, sizeof(*chip) + nsels * sizeof(u8),
> + chip = devm_kzalloc(dev, struct_size(chip, sels, nsels),
> GFP_KERNEL);
> if (!chip)
> return -ENOMEM;
> --
> 2.25.1
>
>
Powered by blists - more mailing lists