linux/drivers
Kees Cook 6da2ec5605 treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:

        kmalloc(a * b, gfp)

with:
        kmalloc_array(a * b, gfp)

as well as handling cases of:

        kmalloc(a * b * c, gfp)

with:

        kmalloc(array3_size(a, b, c), gfp)

as it's slightly less ugly than:

        kmalloc_array(array_size(a, b), c, gfp)

This does, however, attempt to ignore constant size factors like:

        kmalloc(4 * 1024, gfp)

though any constants defined via macros get caught up in the conversion.

Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.

The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().

The Coccinelle script used for this was:

// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@

(
  kmalloc(
-	(sizeof(TYPE)) * E
+	sizeof(TYPE) * E
  , ...)
|
  kmalloc(
-	(sizeof(THING)) * E
+	sizeof(THING) * E
  , ...)
)

// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@

(
  kmalloc(
-	sizeof(u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * (COUNT)
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(__u8) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(char) * COUNT
+	COUNT
  , ...)
|
  kmalloc(
-	sizeof(unsigned char) * COUNT
+	COUNT
  , ...)
)

// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@

(
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_ID)
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_ID
+	COUNT_ID, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (COUNT_CONST)
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * COUNT_CONST
+	COUNT_CONST, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_ID)
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_ID
+	COUNT_ID, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (COUNT_CONST)
+	COUNT_CONST, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * COUNT_CONST
+	COUNT_CONST, sizeof(THING)
  , ...)
)

// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@

- kmalloc
+ kmalloc_array
  (
-	SIZE * COUNT
+	COUNT, SIZE
  , ...)

// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@

(
  kmalloc(
-	sizeof(TYPE) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(TYPE) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(TYPE))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * (COUNT) * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * (STRIDE)
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
|
  kmalloc(
-	sizeof(THING) * COUNT * STRIDE
+	array3_size(COUNT, STRIDE, sizeof(THING))
  , ...)
)

// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@

(
  kmalloc(
-	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(THING1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * COUNT
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
|
  kmalloc(
-	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
  , ...)
)

// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@

(
  kmalloc(
-	(COUNT) * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * STRIDE * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	(COUNT) * (STRIDE) * (SIZE)
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
|
  kmalloc(
-	COUNT * STRIDE * SIZE
+	array3_size(COUNT, STRIDE, SIZE)
  , ...)
)

// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@

(
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(
-	(E1) * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * E3
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	(E1) * (E2) * (E3)
+	array3_size(E1, E2, E3)
  , ...)
|
  kmalloc(
-	E1 * E2 * E3
+	array3_size(E1, E2, E3)
  , ...)
)

// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@

(
  kmalloc(sizeof(THING) * C2, ...)
|
  kmalloc(sizeof(TYPE) * C2, ...)
|
  kmalloc(C1 * C2 * C3, ...)
|
  kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * (E2)
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(TYPE) * E2
+	E2, sizeof(TYPE)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * (E2)
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	sizeof(THING) * E2
+	E2, sizeof(THING)
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * E2
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	(E1) * (E2)
+	E1, E2
  , ...)
|
- kmalloc
+ kmalloc_array
  (
-	E1 * E2
+	E1, E2
  , ...)
)

Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 16:19:22 -07:00
..
accessibility
acpi treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
amba Merge branch 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm 2018-06-06 13:49:25 -07:00
android
ata SCSI misc on 20180610 2018-06-10 13:01:12 -07:00
atm treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
auxdisplay treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
base arm64 updates for 4.18: 2018-06-08 11:10:58 -07:00
bcma dma-mapping updates for 4.18: 2018-06-04 10:58:12 -07:00
block treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
bluetooth
bus
cdrom treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
char treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
clk This time we have a good set of changes to the core framework that do some 2018-06-09 12:06:24 -07:00
clocksource This time we have a good set of changes to the core framework that do some 2018-06-09 12:06:24 -07:00
connector Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-06-06 18:39:49 -07:00
cpufreq treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
cpuidle powerpc updates for 4.18 2018-06-07 10:23:33 -07:00
crypto treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
dax libnvdimm for 4.18 2018-06-08 17:21:52 -07:00
dca
devfreq
dio
dma treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
dma-buf
edac
eisa
extcon
firewire treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
firmware Char/Misc driver patches for 4.18-rc1 2018-06-05 16:20:22 -07:00
fmc
fpga
fsi
gpio This is the bulk of GPIO changes for the v4.18 development 2018-06-08 10:31:52 -07:00
gpu treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
hid treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
hsi
hv SCSI misc on 20180610 2018-06-10 13:01:12 -07:00
hwmon powerpc updates for 4.18 2018-06-07 10:23:33 -07:00
hwspinlock treewide: Use struct_size() for devm_kmalloc() and friends 2018-06-06 11:15:43 -07:00
hwtracing Char/Misc driver patches for 4.18-rc1 2018-06-05 16:20:22 -07:00
i2c treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
ide treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
idle
iio Staging/IIO patches for 4.18-rc1 2018-06-09 10:32:39 -07:00
infiniband treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
input treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
iommu Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2018-06-10 09:44:53 -07:00
ipack
irqchip treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
isdn treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
leds leds: Use struct_size() in allocation 2018-06-12 16:19:22 -07:00
lightnvm treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
macintosh powerpc updates for 4.18 2018-06-07 10:23:33 -07:00
mailbox - Remove HAS_DMA config dependencies 2018-06-07 13:35:59 -07:00
mcb
md treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
media treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
memory
memstick treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
message treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
mfd - New Device Support 2018-06-11 07:20:17 -07:00
misc treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
mmc MMC core: 2018-06-05 16:11:43 -07:00
mtd treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
mux
net treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
nfc
ntb
nubus Char/Misc driver patches for 4.18-rc1 2018-06-05 16:20:22 -07:00
nvdimm Merge branch 'for-4.18/mcsafe' into libnvdimm-for-next 2018-06-08 15:16:44 -07:00
nvme for-linus-20180608 2018-06-08 13:36:19 -07:00
nvmem
of DeviceTree updates for v4.18: 2018-06-07 14:06:31 -07:00
opp
oprofile
parisc dma-mapping updates for 4.18: 2018-06-04 10:58:12 -07:00
parport
pci pci-v4.18-changes 2018-06-07 12:45:58 -07:00
pcmcia treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
perf
phy Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next 2018-06-06 18:39:49 -07:00
pinctrl treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
platform - New Device Support 2018-06-11 07:20:17 -07:00
pnp media updates for v4.18-rc1 2018-06-07 12:34:37 -07:00
power power supply and reset changes for the v4.18 series 2018-06-09 12:11:09 -07:00
powercap
pps
ps3
ptp
pwm pwm: stm32: Initialize raw local variables 2018-06-04 07:13:40 +01:00
rapidio
ras
regulator regulator: Updates for v4.18 2018-06-08 13:08:57 -07:00
remoteproc
reset - Introduce arithmetic overflow test helper functions (Rasmus) 2018-06-06 17:27:14 -07:00
rpmsg
rtc - New Device Support 2018-06-11 07:20:17 -07:00
s390 treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
sbus
scsi treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
sfi
sh
siox
slimbus
sn
soc treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
soundwire Char/Misc driver patches for 4.18-rc1 2018-06-05 16:20:22 -07:00
spi Power management updates for 4.18-rc1 2018-06-05 09:38:39 -07:00
spmi
ssb
staging treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
target SCSI misc on 20180610 2018-06-10 13:01:12 -07:00
tc
tee
thermal - Introduce arithmetic overflow test helper functions (Rasmus) 2018-06-06 17:27:14 -07:00
thunderbolt
tty treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
uio
usb treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
uwb treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
vfio Merge branch 'work.aio-1' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs 2018-06-04 13:57:43 -07:00
vhost treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
video treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
virt treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
virtio treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
visorbus
vlynq
vme
w1 Char/Misc driver patches for 4.18-rc1 2018-06-05 16:20:22 -07:00
watchdog
xen treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
zorro - Introduce arithmetic overflow test helper functions (Rasmus) 2018-06-06 17:27:14 -07:00
Kconfig
Makefile