1
0
Fork 0
alistair23-linux/drivers/net
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
..
appletalk
arcnet
bonding bonding: do not set slave_dev npinfo before slave_enable_netpoll in bond_enslave 2018-04-23 11:52:35 -04:00
caif drivers/net: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
can Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-04-03 14:04:18 -07:00
dsa net: dsa: mv88e6xxx: Fix receive time stamp race condition. 2018-04-12 22:05:58 -04:00
ethernet treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
fddi
fjes
hamradio drivers/net: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
hippi
hyperv hv_netvsc: Pass net_device parameter to revoke and teardown functions 2018-04-05 22:21:00 -04:00
ieee802154 Merge branch 'ieee802154-for-davem-2018-03-29' of git://git.kernel.org/pub/scm/linux/kernel/git/sschmidt/wpan-next 2018-03-30 13:00:11 -04:00
ipvlan net: Drop pernet_operations::async 2018-03-27 13:18:09 -04:00
netdevsim devlink: convert occ_get op to separate registration 2018-04-08 12:45:57 -04:00
phy net: phy: allow scanning busses with missing phys 2018-04-25 12:59:42 -04:00
plip
ppp pppoe: check sockaddr length in pppoe_connect() 2018-04-23 21:12:15 -04:00
slip slip: Check if rstate is initialized before uncompressing 2018-04-11 10:33:46 -04:00
team team: fix netconsole setup over team 2018-04-24 09:36:21 -04:00
usb qmi_wwan: do not steal interfaces from class drivers 2018-05-03 11:25:03 -04:00
vmxnet3 vmxnet3: fix incorrect dereference when rxvlan is disabled 2018-04-19 13:59:05 -04:00
wan hdlc_ppp: carrier detect ok, don't turn off negotiation 2018-02-26 14:38:12 -05:00
wimax net: drivers/net: Remove unnecessary skb_copy_expand OOM messages 2018-03-15 14:28:03 -04:00
wireless treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
xen-netback drivers/net: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00
Kconfig netdevsim: Add simple FIB resource controller via devlink 2018-03-29 14:10:31 -04:00
LICENSE.SRC
Makefile net: remove cris etrax ethernet driver 2018-03-26 15:56:24 +02:00
Space.c net/mac89x0: Convert to platform_driver 2018-03-01 21:21:36 -05:00
dummy.c net: Do not take net_rwsem in __rtnl_link_unregister() 2018-03-31 22:24:58 -04:00
eql.c
geneve.c net: Drop pernet_operations::async 2018-03-27 13:18:09 -04:00
gtp.c net: Drop pernet_operations::async 2018-03-27 13:18:09 -04:00
ifb.c net: Do not take net_rwsem in __rtnl_link_unregister() 2018-03-31 22:24:58 -04:00
loopback.c net: Drop pernet_operations::async 2018-03-27 13:18:09 -04:00
macsec.c Revert "macsec: missing dev_put() on error in macsec_newlink()" 2018-04-16 10:01:12 -04:00
macvlan.c macvlan: filter out unsupported feature flags 2018-03-11 22:46:16 -04:00
macvtap.c
mdio.c
mii.c
netconsole.c
nlmon.c
ntb_netdev.c
rionet.c
sb1000.c
sungem_phy.c
tap.c vfs: do bulk POLL* -> EPOLL* replacement 2018-02-11 14:34:03 -08:00
thunderbolt.c net: thunderbolt: Run disconnect flow asynchronously when logout is received 2018-02-12 12:03:04 -05:00
tun.c tun: fix vlan packet truncation 2018-04-18 13:50:29 -04:00
veth.c
virtio_net.c virtio_net: sparse annotation fix 2018-04-19 16:33:20 -04:00
vrf.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2018-04-01 19:49:34 -04:00
vsockmon.c
vxlan.c net: Drop pernet_operations::async 2018-03-27 13:18:09 -04:00
xen-netfront.c drivers/net: Use octal not symbolic permissions 2018-03-26 12:07:49 -04:00