2024-02-11 09:17:44

by Erick Archer

[permalink] [raw]
Subject: [PATCH v2] mtd: rawnand: Prefer struct_size over open coded arithmetic

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 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]
Reviewed-by: Gustavo A. R. Silva <[email protected]>
Signed-off-by: Erick Archer <[email protected]>
---
Changes in v2:
- Add the "Reviewed-by:" tag.
- Fix a spelling error in the commit message. Change "more safer" for
"safer" (Uwe Kleine-König)
---
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



2024-02-11 11:52:07

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2] mtd: rawnand: Prefer struct_size over open coded arithmetic

On Sun, Feb 11, 2024 at 10:16:33AM +0100, 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 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]
> Reviewed-by: Gustavo A. R. Silva <[email protected]>
> Signed-off-by: Erick Archer <[email protected]>

Acked-by: Uwe Kleine-K?nig <[email protected]>

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.14 kB)
signature.asc (499.00 B)
Download all attachments
Subject: Re: [PATCH v2] mtd: rawnand: Prefer struct_size over open coded arithmetic

Il 11/02/24 10:16, Erick Archer ha scritto:
> 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 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]
> Reviewed-by: Gustavo A. R. Silva <[email protected]>
> Signed-off-by: Erick Archer <[email protected]>

Just noticed that there was a v2 already addressing the commit description issue.

Reviewed-by: AngeloGioacchino Del Regno <[email protected]>



2024-02-12 15:51:06

by Matthias Brugger

[permalink] [raw]
Subject: Re: [PATCH v2] mtd: rawnand: Prefer struct_size over open coded arithmetic



On 11/02/2024 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 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]
> Reviewed-by: Gustavo A. R. Silva <[email protected]>
> Signed-off-by: Erick Archer <[email protected]>

Reviewed-by: Matthias Brugger <[email protected]>

> ---
> Changes in v2:
> - Add the "Reviewed-by:" tag.
> - Fix a spelling error in the commit message. Change "more safer" for
> "safer" (Uwe Kleine-König)
> ---
> 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
>

2024-02-26 10:56:11

by Miquel Raynal

[permalink] [raw]
Subject: Re: [PATCH v2] mtd: rawnand: Prefer struct_size over open coded arithmetic

On Sun, 2024-02-11 at 09:16:33 UTC, 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 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]
> Reviewed-by: Gustavo A. R. Silva <[email protected]>
> Signed-off-by: Erick Archer <[email protected]>
> Acked-by: Uwe Kleine-König <[email protected]>
> Reviewed-by: AngeloGioacchino Del Regno <[email protected]>
> Reviewed-by: Matthias Brugger <[email protected]>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git nand/next, thanks.

Miquel