alistair23-linux/drivers/misc
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
..
altera-stapl
c2port
cardreader for-4.17/block-20180402 2018-04-05 14:27:02 -07:00
cb710
cxl cxl: Fix possible deadlock when processing page faults from cxllib 2018-04-04 22:09:33 +10:00
echo misc: Remove Blackfin DSP echo support 2018-03-26 15:56:37 +02:00
eeprom Char/Misc patches for 4.17-rc1 2018-04-04 20:07:20 -07:00
genwqe GenWQE: Fix a typo in two comments 2018-03-27 09:51:22 +02:00
ibmasm
lis3lv02d vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
lkdtm lkdtm: Add missing SPDX-License-Identifier line 2018-03-06 19:18:55 -08:00
mei mei: remove dev_err message on an unsupported ioctl 2018-03-14 19:33:13 +01:00
mic misc: mic: Release reference count and memory for VOP device 2018-03-15 18:12:01 +01:00
ocxl Merge 4.16-rc7 into char-misc-next 2018-03-28 12:27:35 +02:00
sgi-gru
sgi-xp
ti-st
vmw_vmci vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
ad525x_dpot-i2c.c
ad525x_dpot-spi.c
ad525x_dpot.c
ad525x_dpot.h
apds990x.c
apds9802als.c
aspeed-lpc-ctrl.c misc: aspeed-lpc-ctrl: Enable FWH and A2H bridge cycles 2018-03-15 18:20:51 +01:00
aspeed-lpc-snoop.c
atmel-ssc.c
atmel_tclib.c
bh1770glc.c
cs5535-mfgpt.c
ds1682.c
dummy-irq.c
enclosure.c
fsa9480.c
hmc6352.c
hpilo.c vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
hpilo.h
ics932s401.c
ioc4.c
isl29003.c
isl29020.c
Kconfig misc: atmel: Remove CPU_AT32AP700X (AVR32) reference 2018-03-15 14:23:34 +01:00
kgdbts.c
lattice-ecp3-config.c
Makefile lkdtm: Relocate code to subdirectory 2018-03-06 19:18:55 -08:00
pch_phub.c
pci_endpoint_test.c misc: pci_endpoint_test: Handle 64-bit BARs properly 2018-04-03 12:38:06 +01:00
phantom.c vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
pti.c
qcom-coincell.c
spear13xx_pcie_gadget.c
sram-exec.c
sram.c
sram.h
tifm_7xx1.c
tifm_core.c
tsl2550.c
vexpress-syscfg.c treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
vmw_balloon.c