1
0
Fork 0
alistair23-linux/arch/um/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
..
Makefile High Performance UML Vector Network Driver 2018-02-19 19:38:51 +01:00
chan.h um: Use tty_port in SIGWINCH handler 2013-03-11 10:08:04 +01:00
chan_kern.c Epoll based IRQ controller 2018-02-19 19:38:51 +01:00
chan_user.c um: Use tty_port in SIGWINCH handler 2013-03-11 10:08:04 +01:00
chan_user.h um: Use tty_port in SIGWINCH handler 2013-03-11 10:08:04 +01:00
cow.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
cow_sys.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
cow_user.c um: switch cow_user.h to htobe{32,64}/betoh{32,64} 2012-04-10 00:13:45 +02:00
daemon.h um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
daemon_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
daemon_user.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
fd.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
harddog_kern.c Replace <asm/uaccess.h> with <linux/uaccess.h> globally 2016-12-24 11:46:01 -08:00
harddog_user.c um: Do not use stdin and stdout identifiers for struct members 2015-06-25 22:42:19 +02:00
hostaudio_kern.c um: annotate ->poll() instances 2017-11-27 16:19:59 -05:00
line.c Epoll based IRQ controller 2018-02-19 19:38:51 +01:00
line.h um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
mconsole.h um: Stop abusing __KERNEL__ 2015-05-31 22:05:32 +02:00
mconsole_kern.c mconsole_proc(): don't mess with file->f_pos 2018-02-09 19:28:01 -08:00
mconsole_kern.h um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
mconsole_user.c um: -include user.h for USER_OBJ, trim includes 2011-11-02 14:14:44 +01:00
mmapper_kern.c Replace <asm/uaccess.h> with <linux/uaccess.h> globally 2016-12-24 11:46:01 -08:00
net_kern.c High Performance UML Vector Network Driver 2018-02-19 19:38:51 +01:00
net_user.c um: fix returns without va_end 2015-12-08 22:26:00 +01:00
null.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
pcap_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
pcap_user.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
pcap_user.h um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
port.h uml: remove useless comments 2009-04-01 08:59:17 -07:00
port_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
port_user.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
pty.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
random.c Epoll based IRQ controller 2018-02-19 19:38:51 +01:00
slip.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
slip_common.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
slip_common.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
slip_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
slip_user.c um: Do not use stdin and stdout identifiers for struct members 2015-06-25 22:42:19 +02:00
slirp.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
slirp_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
slirp_user.c um: Do not use stdin and stdout identifiers for struct members 2015-06-25 22:42:19 +02:00
ssl.c um: Use tty_port_operations->destruct 2013-03-11 10:08:03 +01:00
ssl.h uml: remove useless comments 2009-04-01 08:59:17 -07:00
stderr_console.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
stdio_console.c um: console: Ignore console= option 2017-07-05 23:18:48 +02:00
stdio_console.h uml: remove useless comments 2009-04-01 08:59:17 -07:00
tty.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
ubd.h um: UBD Improvements 2016-12-14 22:46:55 +01:00
ubd_kern.c treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
ubd_user.c um: UBD Improvements 2016-12-14 22:46:55 +01:00
umcast.h um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
umcast_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
umcast_user.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
vde.h uml: add VDE networking support 2007-10-16 09:43:05 -07:00
vde_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
vde_user.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
vector_kern.c treewide: kmalloc() -> kmalloc_array() 2018-06-12 16:19:22 -07:00
vector_kern.h Fix vector raw inintialization logic 2018-03-29 22:18:02 +02:00
vector_transports.c net: in virtio_net_hdr only add VLAN_HLEN to csum_start if payload holds vlan 2018-06-07 16:15:38 -04:00
vector_user.c Fix vector raw inintialization logic 2018-03-29 22:18:02 +02:00
vector_user.h Fix vector raw inintialization logic 2018-03-29 22:18:02 +02:00
xterm.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00
xterm.h uml: remove useless comments 2009-04-01 08:59:17 -07:00
xterm_kern.c um: get rid of pointless include "..." where include <...> will do 2012-10-09 22:28:45 +02:00