]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tools/sched_ext: fix strtoul() misuse in scx_hotplug_seq()
authorDavid Carlier <devnexen@gmail.com>
Fri, 27 Feb 2026 18:43:09 +0000 (18:43 +0000)
committerTejun Heo <tj@kernel.org>
Fri, 27 Feb 2026 19:17:44 +0000 (09:17 -1000)
scx_hotplug_seq() uses strtoul() but validates the result with a
negative check (val < 0), which can never be true for an unsigned
return value.

Use the endptr mechanism to verify the entire string was consumed,
and check errno == ERANGE for overflow detection.

Signed-off-by: David Carlier <devnexen@gmail.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
tools/sched_ext/include/scx/compat.h

index 8b4897fc8b9985f58742cab96313f3e2a56cbf13..edccc99c7294ae0c63dd827f125442426bd290d1 100644 (file)
@@ -125,6 +125,7 @@ static inline long scx_hotplug_seq(void)
 {
        int fd;
        char buf[32];
+       char *endptr;
        ssize_t len;
        long val;
 
@@ -137,8 +138,10 @@ static inline long scx_hotplug_seq(void)
        buf[len] = 0;
        close(fd);
 
-       val = strtoul(buf, NULL, 10);
-       SCX_BUG_ON(val < 0, "invalid num hotplug events: %lu", val);
+       errno = 0;
+       val = strtoul(buf, &endptr, 10);
+       SCX_BUG_ON(errno == ERANGE || endptr == buf ||
+                  (*endptr != '\n' && *endptr != '\0'), "invalid num hotplug events: %ld", val);
 
        return val;
 }