1
0
Fork 0
alistair23-linux/drivers/input
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
..
gameport treewide: setup_timer() -> timer_setup() 2017-11-21 15:57:07 -08:00
joystick Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input 2018-04-05 13:21:57 -07:00
keyboard Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input 2018-04-05 13:21:57 -07:00
misc Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input 2018-04-05 13:21:57 -07:00
mouse Input: alps - fix reporting pressure of v3 trackstick 2018-04-23 16:38:16 -07:00
rmi4 Input: synaptics-rmi4 - fix an unchecked out of memory error path 2018-04-06 15:39:18 -07:00
serio Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input 2018-04-05 13:21:57 -07:00
tablet Input: pegasus_notetaker - do not rely on input_dev->users 2018-03-17 11:05:18 -07:00
touchscreen Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input 2018-05-02 17:34:42 -10:00
Kconfig Input: mousedev - stop offering PS/2 to userspace by default 2017-01-31 11:31:49 -08:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
apm-power.c
evbug.c
evdev.c Revert: Unify CLOCK_MONOTONIC and CLOCK_BOOTTIME 2018-04-26 14:53:32 +02:00
ff-core.c Input: uinput - avoid FF flush when destroying device 2017-09-21 16:31:22 -07:00
ff-memless.c Input: ff_memless - convert to using timer_setup() 2017-10-23 16:31:52 -07:00
input-compat.c Input: extend usable life of event timestamps to 2106 on 32 bit systems 2018-01-09 16:40:30 -08:00
input-compat.h Input: extend usable life of event timestamps to 2106 on 32 bit systems 2018-01-09 16:40:30 -08:00
input-leds.c treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
input-mt.c treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
input-polldev.c Input: remove remaining unneeded MODULE_VERSION() usage 2018-01-16 16:48:25 -08:00
input.c vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
joydev.c vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
matrix-keymap.c Input: matrix-keypad - switch to using generic device properties 2017-01-31 11:31:48 -08:00
mousedev.c vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
sparse-keymap.c Input: remove remaining unneeded MODULE_VERSION() usage 2018-01-16 16:48:25 -08:00