Skip to content

Commit

Permalink
lib/test_kmod.c: fix limit check on number of test devices created
Browse files Browse the repository at this point in the history
As reported by Dan the parentheses is in the wrong place, and since
unlikely() call returns either 0 or 1 it's never less than zero.  The
second issue is that signed integer overflows like "INT_MAX + 1" are
undefined behavior.

Since num_test_devs represents the number of devices, we want to stop
prior to hitting the max, and not rely on the wrap arround at all.  So
just cap at num_test_devs + 1, prior to assigning a new device.

Link: http://lkml.kernel.org/r/[email protected]
Fixes: d9c6a72 ("kmod: add test driver to stress test the module loader")
Reported-by: Dan Carpenter <[email protected]>
Signed-off-by: Luis R. Rodriguez <[email protected]>
Acked-by: Kees Cook <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
mcgrof authored and torvalds committed Mar 10, 2018
1 parent 0627be7 commit ac68b1b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/test_kmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ static struct kmod_test_device *register_test_dev_kmod(void)
mutex_lock(&reg_dev_mutex);

/* int should suffice for number of devices, test for wrap */
if (unlikely(num_test_devs + 1) < 0) {
if (num_test_devs + 1 == INT_MAX) {
pr_err("reached limit of number of test devices\n");
goto out;
}
Expand Down

0 comments on commit ac68b1b

Please sign in to comment.