Skip to content

Commit a08b8f8

Browse files
Erick Archerdtor
authored andcommitted
Input: ff-core - prefer struct_size over open coded arithmetic
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "ff" variable is a pointer to "struct ff_device" and this structure ends in a flexible array: struct ff_device { [...] struct file *effect_owners[] __counted_by(max_effects); }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the calculation "size + count * size" in the kzalloc() function. The struct_size() helper returns SIZE_MAX on overflow. So, refactor the comparison to take advantage of this. This way, the code is more readable and safer. This code was detected with the help of Coccinelle, and audited and modified manually. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] Link: KSPP#160 [2] Signed-off-by: Erick Archer <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/AS8PR02MB72371E646714BAE2E51A6A378B152@AS8PR02MB7237.eurprd02.prod.outlook.com Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 7b4e0b3 commit a08b8f8

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/input/ff-core.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
/* #define DEBUG */
1010

1111
#include <linux/input.h>
12+
#include <linux/limits.h>
1213
#include <linux/module.h>
1314
#include <linux/mutex.h>
15+
#include <linux/overflow.h>
1416
#include <linux/sched.h>
1517
#include <linux/slab.h>
1618

@@ -315,9 +317,8 @@ int input_ff_create(struct input_dev *dev, unsigned int max_effects)
315317
return -EINVAL;
316318
}
317319

318-
ff_dev_size = sizeof(struct ff_device) +
319-
max_effects * sizeof(struct file *);
320-
if (ff_dev_size < max_effects) /* overflow */
320+
ff_dev_size = struct_size(ff, effect_owners, max_effects);
321+
if (ff_dev_size == SIZE_MAX) /* overflow */
321322
return -EINVAL;
322323

323324
ff = kzalloc(ff_dev_size, GFP_KERNEL);

0 commit comments

Comments
 (0)