1
0
Fork 0
alistair23-linux/drivers
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
..
accessibility
acpi ACPI fixes for 4.17-rc3 2018-04-26 11:06:36 -07:00
amba ARM: amba: Fix race condition with driver_override 2018-04-26 10:35:04 +02:00
android ANDROID: binder: prevent transactions into own process. 2018-04-23 12:12:41 +02:00
ata Merge branch 'for-4.17' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/libata 2018-04-03 17:42:25 -07:00
atm atm: iphase: fix spelling mistake: "Tansmit" -> "Transmit" 2018-04-19 13:41:49 -04:00
auxdisplay
base device: Use overflow helpers for devm_kmalloc() 2018-06-05 12:16:51 -07:00
bcma
block for-linus-20180425 2018-04-25 21:05:15 -07:00
bluetooth Bluetooth: Set HCI_QUIRK_SIMULTANEOUS_DISCOVERY for BTUSB_QCA_ROME 2018-04-01 21:43:02 +03:00
bus HISI LPC: Add Kconfig MFD_CORE dependency 2018-04-26 16:53:23 +02:00
cdrom cdrom: information leak in cdrom_ioctl_media_changed() 2018-04-18 08:21:32 -06:00
char virtio: fixups 2018-04-26 16:36:11 -07:00
clk treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
clocksource clocksource/imx-tpm: Correct -ETIME return condition check 2018-04-19 13:21:35 +02:00
connector
cpufreq cpufreq / CPPC: Set platform specific transition_delay_us 2018-04-30 10:14:08 +02:00
cpuidle cpuidle: menu: Avoid selecting shallow states with stopped tick 2018-04-09 11:54:57 +02:00
crypto .gitignore: move *-asn1.[ch] patterns to the top-level .gitignore 2018-04-07 19:04:02 +09:00
dax treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
dca
devfreq
dio
dma treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
dma-buf
edac * Add NVDIMM support to EDAC (Tony Luck) 2018-04-05 14:21:13 -07:00
eisa
extcon Char/Misc patches for 4.17-rc1 2018-04-04 20:07:20 -07:00
firewire treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
firmware firmware: arm_scmi: remove redundant null check on array 2018-04-16 10:15:58 +01:00
fmc treewide: Fix typos in printk 2018-03-27 09:51:22 +02:00
fpga fpga-manager: altera-ps-spi: preserve nCONFIG state 2018-04-23 13:27:05 +02:00
fsi
gpio treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
gpu treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
hid HID: i2c-hid: fix inverted return value from i2c_hid_command() 2018-04-19 09:25:15 +02:00
hsi
hv ARM: 2018-04-09 11:42:31 -07:00
hwmon hwmon: (k10temp) Add support for AMD Ryzen w/ Vega graphics 2018-04-25 05:31:06 -07:00
hwspinlock treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
hwtracing Char/Misc patches for 4.17-rc1 2018-04-04 20:07:20 -07:00
i2c i2c: sprd: Fix the i2c count issue 2018-04-27 14:12:43 +02:00
ide for-4.17/block-20180402 2018-04-05 14:27:02 -07:00
idle
iio This is the bulk of GPIO changes for the v4.17 kernel cycle: 2018-04-05 09:51:41 -07:00
infiniband treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
input treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
iommu iommu: rockchip: fix building without CONFIG_OF 2018-05-03 16:36:07 +02:00
ipack
irqchip irqchip/qcom: Fix check for spurious interrupts 2018-05-02 15:56:10 +02:00
isdn mISDN: Remove VLAs 2018-04-12 21:46:10 -04:00
leds
lightnvm lightnvm: pblk: remove some unnecessary NULL checks 2018-03-29 17:29:09 -06:00
macintosh powerpc updates for 4.17 2018-04-07 12:08:19 -07:00
mailbox
mcb
md treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
media MAINTAINERS & files: Canonize the e-mails I use at files 2018-05-04 06:21:06 -04:00
memory ARM: OMAP2+: Fix build when using split object directories 2018-04-18 10:07:13 -07:00
memstick
message scsi: mptsas: Disable WRITE SAME 2018-04-18 23:37:25 -04:00
mfd platform/chrome: mfd/cros_ec_dev: Add sysfs entry to set keyboard wake lid angle 2018-04-10 22:25:07 -07:00
misc treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
mmc MMC host: 2018-04-20 10:41:31 -07:00
mtd mtd: rawnand: marvell: fix the chip-select DT parsing logic 2018-04-26 19:06:42 +02:00
mux
net treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
nfc
ntb
nubus
nvdimm Revert "libnvdimm, of_pmem: workaround OF_NUMA=n build error" 2018-04-19 15:10:56 -07:00
nvme First pull request for 4.17-rc 2018-05-04 20:51:10 -10:00
nvmem Char/Misc patches for 4.17-rc1 2018-04-04 20:07:20 -07:00
of earlycon: Use a pointer table to fix __earlycon_table stride 2018-04-23 10:06:59 +02:00
opp
oprofile oprofilefs: don't oops on allocation failure 2018-03-29 15:07:48 -04:00
parisc parisc: Fix section mismatches 2018-05-02 21:47:35 +02:00
parport Char/Misc patches for 4.17-rc1 2018-04-04 20:07:20 -07:00
pci pci-v4.17-fixes-1 2018-04-26 16:28:24 -07:00
pcmcia Merge branch 'for-linus-sa1100' of git://git.armlinux.org.uk/~rmk/linux-arm 2018-04-09 09:26:36 -07:00
perf ARM: SoC driver updates for 4.17 2018-04-05 21:29:35 -07:00
phy ARM: SoC platform updates for 4.17 2018-04-05 21:21:08 -07:00
pinctrl This is the bulk of GPIO changes for the v4.17 kernel cycle: 2018-04-05 09:51:41 -07:00
platform platform/x86: Kconfig: Fix dell-laptop dependency chain. 2018-05-04 22:20:14 +02:00
pnp
power ARM: SoC platform updates for 4.17 2018-04-05 21:21:08 -07:00
powercap
pps
ps3
ptp
pwm pwm: Changes for v4.17-rc1 2018-04-13 15:46:21 -07:00
rapidio rapidio: fix rio_dma_transfer error handling 2018-04-20 17:18:35 -07:00
ras
regulator Merge remote-tracking branches 'regulator/topic/88pg86x', 'regulator/topic/dt', 'regulator/topic/formatting' and 'regulator/topic/gpio' into regulator-next 2018-03-28 10:33:53 +08:00
remoteproc remoteproc: qcom: Fix potential device node leaks 2018-04-25 16:46:55 -07:00
reset treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
rpmsg rpmsg: added MODULE_ALIAS for rpmsg_char 2018-04-25 16:46:55 -07:00
rtc rtc: opal: Fix OPAL RTC driver OPAL_BUSY loops 2018-04-25 13:24:13 +10:00
s390 treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
sbus sparc64: Fix mistake in oradax license text 2018-04-30 16:06:01 -04:00
scsi scsi: isci: Fix infinite loop in while loop 2018-04-20 19:23:32 -04:00
sfi
sh
siox
slimbus slimbus: Fix out-of-bounds access in slim_slicesize() 2018-04-23 13:40:15 +02:00
sn
soc soc: bcm: raspberrypi-power: Fix use of __packed 2018-04-16 15:15:23 -07:00
soundwire
spi spi: SPI updates for v4.17 2018-04-03 12:06:21 -07:00
spmi
ssb
staging treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
target scsi: target: Fix fortify_panic kernel exception 2018-04-20 19:24:01 -04:00
tc
tee
thermal Merge branches 'thermal-core' and 'thermal-soc' into next 2018-04-13 14:11:53 +08:00
thunderbolt
tty tty: Use __GFP_NOFAIL for tty_ldisc_get() 2018-04-25 15:03:44 +02:00
uio uio_hv_generic: fix subchannel ring mmap 2018-04-23 12:43:48 +02:00
usb treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
uwb
vfio VFIO updates for v4.17-rc1 2018-04-06 19:44:27 -07:00
vhost vhost: return bool from *_access_ok() functions 2018-04-11 10:54:06 -04:00
video fbdev changes for v4.17: 2018-04-10 10:20:00 -07:00
virt virt: vbox: Log an error when we fail to get the host version 2018-04-23 13:41:55 +02:00
virtio virtio: feature 2018-04-11 18:58:27 -07:00
visorbus
vlynq
vme
w1
watchdog aspeed: watchdog: Set bootstatus during probe 2018-04-16 10:22:40 +02:00
xen xen: fixes and one header update for 4.17-rc2 2018-04-20 08:36:04 -07:00
zorro treewide: Use struct_size() for kmalloc()-family 2018-06-06 11:15:43 -07:00
Kconfig hwtracing: Add HW tracing support menu 2018-03-29 13:38:10 +03:00
Makefile