linux/drivers/reset
Kees Cook acafe7e302 treewide: Use struct_size() for kmalloc()-family
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct foo {
    int stuff;
    void *entry[];
};

instance = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);

This patch makes the changes for kmalloc()-family (and kvmalloc()-family)
uses. It was done via automatic conversion with manual review for the
"CHECKME" non-standard cases noted below, using the following Coccinelle
script:

// pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
//                      sizeof *pkey_cache->table, GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
+ alloc(struct_size(VAR, ELEMENT, COUNT), GFP)

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
+ alloc(struct_size(VAR, ELEMENT, COUNT), GFP)

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@

- alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
+ alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-06 11:15:43 -07:00
..
hisilicon reset: hi6220: Set module license so that it can be loaded 2017-05-24 10:53:41 +02:00
sti reset: sti: Use devm_kcalloc() in syscfg_reset_controller_register() 2017-05-15 08:59:08 +02:00
tegra reset: Add Tegra BPMP reset driver 2016-11-18 14:34:56 +01:00
core.c treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
Kconfig reset: stm32mp1: Enable stm32mp1 reset driver 2018-03-27 10:44:04 +02:00
Makefile reset: stm32mp1: Enable stm32mp1 reset driver 2018-03-27 10:44:04 +02:00
reset-a10sr.c reset: Add Altera Arria10 SR Reset Controller 2017-03-15 12:19:11 +01:00
reset-ath79.c reset: ath79: make it explicitly non-modular 2017-03-08 09:36:45 +01:00
reset-axs10x.c ARC: reset: introduce AXS10x reset driver 2017-09-18 18:41:27 +02:00
reset-berlin.c reset: berlin: make it explicitly non-modular 2016-10-24 10:43:40 +02:00
reset-hsdk.c ARC: reset: remove the misleading v1 suffix all over 2017-09-18 13:02:03 +02:00
reset-imx7.c reset: Add i.MX7 SRC reset driver 2017-03-15 12:18:49 +01:00
reset-lantiq.c reset: Add a reset controller driver for the Lantiq XWAY based SoCs 2017-09-04 21:19:02 +02:00
reset-lpc18xx.c reset: lpc18xx: make it explicitly non-modular 2016-10-24 10:45:38 +02:00
reset-meson.c reset: meson: enable level reset support on Meson8b 2018-02-16 15:32:09 +01:00
reset-oxnas.c reset: oxnas: make it explicitly non-modular 2017-03-08 09:36:45 +01:00
reset-pistachio.c reset: pistachio: make it explicitly non-modular 2017-03-08 09:36:45 +01:00
reset-simple.c reset: simple: Enable for ASPEED systems 2018-02-20 17:42:29 +01:00
reset-simple.h reset: socfpga: use the reset-simple driver 2017-10-18 15:51:02 +02:00
reset-stm32mp1.c reset: stm32mp1: Enable stm32mp1 reset driver 2018-03-27 10:44:04 +02:00
reset-sunxi.c reset: sunxi: use reset-simple driver 2017-10-17 15:35:24 +02:00
reset-ti-sci.c reset: Add the TI SCI reset driver 2017-06-06 09:44:17 +02:00
reset-ti-syscon.c reset: constify reset_control_ops structures 2017-01-12 11:12:07 +01:00
reset-uniphier.c reset: uniphier: add ethernet reset control support for PXs3 2018-03-27 10:44:04 +02:00
reset-zynq.c reset: zynq: make it explicitly non-modular 2016-10-24 10:45:32 +02:00