linux/drivers/staging/greybus
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
..
Documentation staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
tools vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
Kconfig staging: greybus: enable compile testing of arche driver 2017-05-16 13:39:16 +02:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
arche-apb-ctrl.c staging: greybus: arche-apb-ctrl.c: Fix alignment should match open parenthesis 2017-12-19 14:58:30 +01:00
arche-platform.c staging: greybus: arche-platform.c: Fix alignment should match open parenthesis 2017-12-19 14:58:30 +01:00
arche_platform.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
arpc.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
audio_apbridgea.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_apbridgea.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
audio_codec.c staging: greybus: audio_codec.c: Prefer kernel type 'u32' over 'uint32_t' 2018-01-22 11:49:24 +01:00
audio_codec.h staging: greybus: audio_codec.h Fix alignment should match open parenthesis 2018-01-22 11:49:24 +01:00
audio_gb.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_manager.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_manager.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_manager_module.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_manager_private.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_manager_sysfs.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_module.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
audio_topology.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
authentication.c staging: greybus: authentication.c: Fix alignment should match open parenthesis 2018-01-22 11:48:06 +01:00
bootrom.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
bundle.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
bundle.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
camera.c Staging: greybus: camera: cleanup multiple checks for null pointers 2018-01-09 15:38:48 +01:00
connection.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
connection.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
control.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
control.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
core.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
debugfs.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
es2.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
firmware.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
fw-core.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
fw-download.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
fw-management.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
gb-camera.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
gbphy.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
gbphy.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
gpio.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
greybus.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
greybus_authentication.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
greybus_firmware.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
greybus_id.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
greybus_manifest.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
greybus_protocols.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
greybus_trace.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
hd.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
hd.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
hid.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
i2c.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
interface.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
interface.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
light.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
log.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
loopback.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
manifest.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
manifest.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
module.c treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
module.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
operation.c treewide: setup_timer() -> timer_setup() 2017-11-21 15:57:07 -08:00
operation.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
power_supply.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
pwm.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
raw.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
sdio.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
spi.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
spilib.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
spilib.h staging: greybus: add SPDX identifiers to all greybus driver files 2017-11-11 14:46:20 +01:00
svc.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
svc.h staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
svc_watchdog.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
uart.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
usb.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00
vibrator.c staging: greybus: Remove redundant license text 2017-11-11 14:46:21 +01:00