1
0
Fork 0
alistair23-linux/sound
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
..
ac97 ALSA: ac97: kconfig: Remove select of undefined symbol AC97 2018-02-12 08:16:39 +01:00
aoa
arm Merge remote-tracking branches 'asoc/topic/ac97', 'asoc/topic/ac97-mfd', 'asoc/topic/amd' and 'asoc/topic/arizona-mfd' into asoc-next 2017-11-10 21:31:02 +00:00
atmel
core treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
drivers ALSA: aloop: Add missing cable lock to ctl API callbacks 2018-04-30 10:06:48 +02:00
firewire ALSA: dice: fix kernel NULL pointer dereference due to invalid calculation for array index 2018-04-29 09:06:39 +02:00
hda ALSA: hda: Copying sync power state helper to core 2018-02-12 13:59:39 +01:00
i2c ASoC: Updates for v4.15 2017-11-13 15:45:57 +01:00
isa ALSA: gus: Delete an error message for a failed memory allocation in snd_gf1_dma_transfer_block() 2017-11-29 09:29:36 +01:00
mips ALSA: sgio2audio: Improve a size determination in snd_sgio2audio_create() 2017-11-29 09:29:31 +01:00
oss vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
parisc
pci ALSA: hda - Fix incorrect usage of IS_REACHABLE() 2018-04-27 17:17:35 +02:00
pcmcia
ppc
sh
soc treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
sparc
spi
synth ASoC: Updates for v4.15 2017-11-13 15:45:57 +01:00
usb ALSA: usx2y: Change return type to vm_fault_t 2018-04-25 08:15:20 +02:00
x86 ALSA: x86: Fix potential crash at error path 2018-02-28 08:46:00 +01:00
Kconfig ASoC: Updates for v4.15 2017-11-13 15:45:57 +01:00
Makefile ASoC: Updates for v4.15 2017-11-13 15:45:57 +01:00
ac97_bus.c
last.c
sound_core.c sound: Remove leftover msnd init declarations 2018-01-11 17:10:34 +01:00