treewide: Use struct_size() for devm_kmalloc() and friends
authorKees Cook <keescook@chromium.org>
Tue, 8 May 2018 23:08:53 +0000 (16:08 -0700)
committerKees Cook <keescook@chromium.org>
Wed, 6 Jun 2018 18:15:43 +0000 (11:15 -0700)
commit0ed2dd03b94b7b7f66e23f25073b5385d0416589
tree5b46cbc179ffcdaf40715d53b8308f5a937b5c40
parentb4b06db115bbbc10252287ae2d326fb5ecefaf18
treewide: Use struct_size() for devm_kmalloc() and friends

Replaces open-coded struct size calculations with struct_size() for
devm_*, f2fs_*, and sock_* allocations. Automatically generated (and
manually adjusted) from the following Coccinelle script:

// Direct reference to struct field.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

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

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

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

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@

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

Signed-off-by: Kees Cook <keescook@chromium.org>
32 files changed:
crypto/af_alg.c
drivers/clk/bcm/clk-bcm2835-aux.c
drivers/clk/bcm/clk-bcm2835.c
drivers/clk/clk-s2mps11.c
drivers/clk/clk-scmi.c
drivers/clk/davinci/da8xx-cfgchip.c
drivers/clk/mvebu/armada-37xx-periph.c
drivers/clk/mvebu/armada-37xx-tbg.c
drivers/clk/qcom/clk-spmi-pmic-div.c
drivers/clk/samsung/clk-exynos-audss.c
drivers/clk/samsung/clk-exynos5433.c
drivers/clk/samsung/clk-s3c2410-dclk.c
drivers/clk/samsung/clk-s5pv210-audss.c
drivers/dma/bcm-sba-raid.c
drivers/dma/nbpfaxi.c
drivers/dma/sprd-dma.c
drivers/gpio/gpio-uniphier.c
drivers/hwspinlock/sirf_hwspinlock.c
drivers/input/keyboard/cap11xx.c
drivers/mfd/qcom-pm8xxx.c
drivers/misc/cb710/core.c
drivers/mtd/spi-nor/aspeed-smc.c
drivers/net/can/peak_canfd/peak_pciefd_main.c
drivers/pinctrl/samsung/pinctrl-s3c64xx.c
drivers/pinctrl/uniphier/pinctrl-uniphier-core.c
drivers/regulator/mc13783-regulator.c
drivers/regulator/mc13892-regulator.c
drivers/rtc/rtc-ac100.c
drivers/soc/actions/owl-sps.c
drivers/soc/rockchip/pm_domains.c
drivers/thermal/qcom/tsens.c
sound/soc/qcom/apq8016_sbc.c