1
0
Fork 0
alistair23-linux/drivers/net/wireless/intel/iwlwifi
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
..
cfg iwlwifi: bump the max API version for 9000 and 22000 devices 2018-03-28 22:43:39 +03:00
dvm Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next 2018-03-31 23:33:04 -04:00
fw iwlwifi: mvm: fix old scan version sizes 2018-04-24 13:13:08 +03:00
mvm treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
pcie iwlwifi: add a bunch of new 9000 PCI IDs 2018-03-28 22:43:38 +03:00
Kconfig iwlwifi: fix malformed CONFIG_IWLWIFI_PCIE_RTPM default 2018-03-02 10:20:03 +02:00
Makefile iwlwifi: runtime: sync FW and host clocks for logs 2017-12-20 18:28:24 +02:00
iwl-agn-hw.h
iwl-config.h iwlwifi: add shared clock PHY config flag for some devices 2018-03-16 12:34:54 +02:00
iwl-context-info.h iwlwifi: pcie: add context information support 2017-04-11 15:19:34 +03:00
iwl-csr.h iwlwifi: remove dead code for internal devices only 2017-11-03 11:56:08 +02:00
iwl-debug.c
iwl-debug.h iwlwifi: acpi: add common code to read from ACPI 2017-10-06 15:22:28 +03:00
iwl-devtrace-data.h iwlwifi: simplify data tracepoint 2017-06-23 11:57:20 +03:00
iwl-devtrace-io.h iwlwifi: pcie: add MSI-X interrupt tracing 2017-06-29 20:27:48 +03:00
iwl-devtrace-iwlwifi.h net/wireless/iwlwifi: fix iwlwifi_dev_ucode_error tracepoint 2018-03-28 22:55:19 +02:00
iwl-devtrace-msg.h
iwl-devtrace-ucode.h
iwl-devtrace.c net/wireless/iwlwifi: fix iwlwifi_dev_ucode_error tracepoint 2018-03-28 22:55:19 +02:00
iwl-devtrace.h iwlwifi: fix tracing when tx only is enabled 2017-07-21 12:26:38 +03:00
iwl-drv.c wireless: Use octal not symbolic permissions 2018-03-27 11:01:13 +03:00
iwl-drv.h iwlwifi: cleanup references to 8000 family in NVM code 2017-06-05 23:25:21 +03:00
iwl-eeprom-parse.c iwlwifi: advertise maximal MPDU length when Rx MQ is supported 2016-07-01 16:51:22 +03:00
iwl-eeprom-parse.h iwlwifi: remove useless iwl_free_nvm_data() function 2017-06-23 12:02:58 +03:00
iwl-eeprom-read.c
iwl-eeprom-read.h
iwl-fh.h iwlwifi: rename the temporary name of A000 to the official 22000 2017-11-28 16:39:57 +02:00
iwl-io.c iwlwifi: fix nmi triggering from host 2017-08-09 21:14:43 +03:00
iwl-io.h iwlwifi: centralize 64 bit HW registers write 2016-07-06 10:22:08 +03:00
iwl-modparams.h iwlwifi: unify external & internal modparam names 2017-06-29 13:26:25 +03:00
iwl-nvm-parse.c iwlwifi: mvm: query regdb for wmm rule if needed 2018-04-09 18:59:45 +03:00
iwl-nvm-parse.h iwlwifi: mvm: query regdb for wmm rule if needed 2018-04-09 18:59:45 +03:00
iwl-op-mode.h
iwl-phy-db.c iwlwifi: mvm: add documentation for all command IDs 2017-06-29 13:26:24 +03:00
iwl-phy-db.h iwlwifi: mvm: remove redundant alloc_ctx parameter 2016-05-10 22:34:03 +03:00
iwl-prph.h iwlwifi: fix nmi triggering from host 2017-08-09 21:14:43 +03:00
iwl-scd.h
iwl-trans.c iwlwifi: trans: move ref/unref code to the common part of the transport 2017-10-06 14:57:20 +03:00
iwl-trans.h iwlwifi: avoid duplicate sw reset executions in the code 2017-12-20 18:28:24 +02:00