]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
bits: prevent unsigned integer underflow and long-lived loop
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Mon, 6 Apr 2026 21:15:20 +0000 (17:15 -0400)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 7 Apr 2026 14:46:10 +0000 (10:46 -0400)
If 0 is allowed for --width it will culminate to a wraparound
due to an unsigned integer underflow when a size_t for-loop
control variable, namely 'n', is setup. n is the result of
cpuset_nbits(size) - 1, where size is set by cpuset_alloc()
which was called with 0 (width) for the @ncpus parameter that
will make it so that @size remains 0 as the calculated memory
allocation size yields zero as well. Therefore the sum for 'n'
will be -1 that wraps around to UINT_MAX and end creates a
long-lived for loop.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
text-utils/bits.c

index 97a77a82f72b3bedc34f4df8923ecf7122c03bc9..dbeaa09b765c57fc9083a0ad32b0cd4e7a97f200 100644 (file)
@@ -17,6 +17,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -283,6 +284,8 @@ int main(int argc, char **argv)
                        /* allow up to 128k masks */
                        width = str2unum_or_err(optarg,
                                10, _("invalid --width"), 128 * 1024);
+                       if (width == 0)
+                               errx(EXIT_FAILURE, _("invalid --width"));
                        break;
                case 'V':
                        print_version(EXIT_SUCCESS);