1
0
Fork 0
alistair23-linux/drivers/net/ethernet
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
..
3com
8390 Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-04-03 14:04:18 -07:00
adaptec
aeroflex
agere
alacritech
allwinner
alteon
altera ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
amazon net: ena: Eliminate duplicate barriers on weakly-ordered archs 2018-03-26 12:47:56 -04:00
amd amd-xgbe: Only use the SFP supported transceiver signals 2018-04-23 21:24:22 -04:00
apm
apple net/macmace: Drop redundant MACH_IS_MAC test 2018-02-28 12:00:27 -05:00
aquantia net: aquantia: oops when shutdown on already stopped device 2018-04-11 10:41:36 -04:00
arc net: ethernet: arc: Fix a potential memory leak if an optional regulator is deferred 2018-03-20 12:06:23 -04:00
atheros
aurora
broadcom net: systemport: fix spelling mistake: "asymetric" -> "asymmetric" 2018-04-29 22:49:03 -04:00
brocade ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
cadence treewide: fix up files incorrectly marked executable 2018-04-07 13:31:23 -07:00
calxeda
cavium net: thunderx: rework mac addresses list to u64 array 2018-04-09 10:59:38 -04:00
chelsio cxgb4: LLD driver changes to support TLS 2018-03-31 23:37:32 -04:00
cirrus net/mac89x0: Replace custom debug logging with netif_* calls 2018-03-01 21:21:36 -05:00
cisco enic: drop IP proto check for vxlan tunnel delete 2018-03-16 09:50:40 -04:00
cortina net: gemini: fix memory leak 2018-03-20 12:08:45 -04:00
davicom treewide: simplify Kconfig dependencies for removed archs 2018-03-26 15:55:57 +02:00
dec
dlink
emulex ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
ezchip
faraday net: faraday add nds32 support. 2018-02-22 10:44:36 +08:00
freescale net: ethernet: ucc: fix spelling mistake: "tx-late-collsion" -> "tx-late-collision" 2018-04-30 09:29:39 -04:00
fujitsu
hisilicon net: hns: Avoid action name truncation 2018-04-19 16:30:26 -04:00
hp
huawei
i825xx
ibm ibmvnic: Clean actual number of RX or TX pools 2018-04-22 21:13:30 -04:00
intel ice: Fix insufficient memory issue in ice_aq_manage_mac_read 2018-04-24 12:27:49 -07:00
marvell net: mvpp2: Fix clock resource by adding missing mg_core_clk 2018-04-27 11:22:55 -04:00
mediatek net: mediatek: Explicitly include pinctrl headers 2018-02-05 09:41:54 -08:00
mellanox treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
micrel
microchip lan743x: make functions lan743x_csr_read and lan743x_csr_read static 2018-03-12 11:12:38 -04:00
moxa
myricom ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
natsemi Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2018-03-23 11:31:58 -04:00
neterion
netronome nfp: flower: set tunnel ttl value to net default 2018-05-01 18:59:57 -04:00
ni net: ethernet: nixge: Add support for National Instruments XGE netdev 2018-03-29 11:51:00 -04:00
nuvoton
nvidia forcedeth: remove duplicate structure member in rx 2018-01-23 11:11:41 -05:00
nxp
oki-semi pch_gbe: Deprecate pci_get_bus_and_slot() 2018-01-17 08:16:46 -06:00
packetengines
pasemi
qlogic qed: fix spelling mistake: "offloded" -> "offloaded" 2018-05-03 14:45:51 -04:00
qualcomm net: qualcomm: rmnet: Fix warning seen with fill_info 2018-04-18 21:23:06 -04:00
rdc
realtek 8139too: Use disable_irq_nosync() in rtl8139_poll_controller() 2018-05-02 13:22:06 -04:00
renesas sh_eth: kill useless check in __sh_eth_get_regs() 2018-04-01 14:09:53 -04:00
rocker rocker: fix possible null pointer dereference in rocker_router_fib_event_work 2018-02-01 09:50:52 -05:00
samsung ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
seeq
sfc sfc: fix ARFS expiry check on EF10 2018-04-27 20:21:07 -04:00
sgi
silan
sis
smsc arch: remove obsolete architecture ports 2018-04-02 20:20:12 -07:00
socionext net: ethernet: ave: add UniPhier PXs3 support 2018-03-26 11:29:10 -04:00
stmicro net: stmmac: Disable ACS Feature for GMAC >= 4 2018-04-19 13:33:44 -04:00
sun ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
synopsys
tehuti
ti net: ethernet: ti: cpsw: fix packet leaking in dual_mac mode 2018-05-02 11:08:23 -04:00
toshiba
tundra net: tsi108: Use DMA API properly 2018-01-17 18:04:43 -06:00
via
wiznet
xilinx
xircom
xscale
Kconfig Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-04-03 14:04:18 -07:00
Makefile Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-04-03 14:04:18 -07:00
dnet.c
dnet.h
ec_bhf.c ethernet: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
ethoc.c
fealnx.c
jme.c net: jme: remove unused initialization of 'rxdesc' 2018-02-01 14:54:28 -05:00
jme.h
korina.c
lantiq_etop.c
netx-eth.c