Commit ac68b1b3 authored by Luis R. Rodriguez's avatar Luis R. Rodriguez Committed by Linus Torvalds

lib/test_kmod.c: fix limit check on number of test devices created

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.

Fixes: d9c6a72d ("kmod: add test driver to stress test the module loader")
Reported-by: default avatarDan Carpenter <>
Signed-off-by: default avatarLuis R. Rodriguez <>
Acked-by: default avatarKees Cook <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 0627be7d
......@@ -1141,7 +1141,7 @@ static struct kmod_test_device *register_test_dev_kmod(void)
/* 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;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment