Skip to contentSkip to conversation

test_kmod: fix limit check on number of test devices created

PATCH In review Luis R. Rodriguez 2 participants 2 messages Feb 24, 2018 – Feb 27, 2018

Subsystems (2)
lore
Share & tools
lore

Luis R. Rodriguez

author maintainer
#0 lore raw

PATCH 1/1 test_kmod: 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 d9c6a72d6fa2 ("kmod: add test driver to stress test the module loader")
---
 lib/test_kmod.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_kmod.c +1 -1
index e372b97eee13..0e5b7a61460b 100644
@@ -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;
 	}
Signature · 1 line
2.16.2
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Luis R. Rodriguez

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.

Quoted 7 lines

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 d9c6a72d6fa2 ("kmod: add test driver to stress test the module loader")

-Kees

Quoted 20 lines
---
 lib/test_kmod.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_kmod.c +1 -2
index e372b97eee13..0e5b7a61460b 100644
@@ -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;
        }
--

2.16.2

Signature · 2 lines
Kees Cook
Pixel Security