1
0
Fork 0
alistair23-linux/drivers/net/wireless
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
..
admtek net/wireless: fix spaces and grammar copy/paste in vendor Kconfig help text 2018-03-13 18:52:25 +02:00
ath Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2018-04-05 11:56:35 -07:00
atmel Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2018-04-05 11:56:35 -07:00
broadcom brcmfmac: fix firmware request processing if nvram load fails 2018-04-09 18:50:31 +03:00
cisco Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-04-03 14:04:18 -07:00
intel treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
intersil wireless: Use octal not symbolic permissions 2018-03-27 11:01:13 +03:00
marvell mwifiex: remove warnings in mwifiex_cmd_append_11n_tlv() 2018-03-27 11:03:24 +03:00
mediatek treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
quantenna net/wireless: fix spaces and grammar copy/paste in vendor Kconfig help text 2018-03-13 18:52:25 +02:00
ralink wireless: Use octal not symbolic permissions 2018-03-27 11:01:13 +03:00
realtek wireless-drivers fixes for 4.17 2018-04-27 13:48:37 -04:00
rsi rsi: fix kernel panic observed on 64bit machine 2018-03-27 11:53:52 +03:00
st wireless: Use octal not symbolic permissions 2018-03-27 11:01:13 +03:00
ti wireless: Use octal not symbolic permissions 2018-03-27 11:01:13 +03:00
zydas net/wireless: fix spaces and grammar copy/paste in vendor Kconfig help text 2018-03-13 18:52:25 +02:00
Kconfig qtnfmac: introduce new FullMAC driver for Quantenna chipsets 2017-05-24 17:04:13 +03:00
Makefile License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
mac80211_hwsim.c mac80211_hwsim: use DEFINE_IDA 2018-04-11 10:28:39 -07:00
mac80211_hwsim.h mac80211_hwsim: add permanent mac address option for new radios 2018-01-31 12:45:35 +01:00
ray_cs.c wireless: Use octal not symbolic permissions 2018-03-27 11:01:13 +03:00
ray_cs.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
rayctl.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
rndis_wlan.c Another set of patches for -next: 2017-04-28 14:41:15 -04:00
wl3501.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
wl3501_cs.c wl3501_cs: fix spelling mistake: "Insupported" -> "Unsupported" 2017-07-28 18:06:31 +03:00