Skip to contentSkip to conversation

libselinux: avcstat: reject out-of-range interval values

PATCH Superseded Evgeniy Gorbanyov 2 participants 4 messages Oct 8 – Oct 9 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: avcstat: reject out-of-range interval values

strtoul() returns unsigned long, but avcstat stored the result in a signed int and passed it to sleep(). A value above INT_MAX becomes negative, and sleep() then treats it as a large unsigned duration. Compare the full strtoul() result with INT_MAX before narrowing it.

---
 libselinux/utils/avcstat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/utils/avcstat.c +3 -2
index 640a7a05..0c534f51 100644
@@ -13,6 +13,7 @@
 #include <libgen.h>
 #include <stdarg.h>
 #include <errno.h>
+#include <limits.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -122,9 +123,9 @@ int main(int argc, char **argv)
 
 	if (optind < argc) {
 		char *arg = argv[optind];
-		unsigned int n = strtoul(arg, NULL, 10);
+		unsigned long n = strtoul(arg, NULL, 10);
 
-		if (errno == ERANGE) {
+		if (errno == ERANGE || n > INT_MAX) {
 			usage();
 			die("invalid interval \'%s\'", arg);
 		}
Signature · 1 line
2.50.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Evgeniy Gorbanyov

strtoul() returns unsigned long, but avcstat stored the result in a signed int and passed it to sleep(). A value above INT_MAX becomes negative, and sleep() then treats it as a large unsigned duration. Compare the full strtoul() result with INT_MAX before narrowing it.

---
 libselinux/utils/avcstat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/utils/avcstat.c +1 -0
index 640a7a05..0c534f51 100644
@@ -13,6 +13,7 @@
 #include <libgen.h>
 #include <stdarg.h>
 #include <errno.h>
+#include <limits.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>

libselinux/utils/avcstat.c int main(int argc, char **argv) · line 128

Quoted 8 lines
libselinux/utils/avcstat.c +2 -2
@@ -122,9 +123,9 @@ int main(int argc, char **argv)
 
        if (optind < argc) {
                char *arg = argv[optind];
-               unsigned int n = strtoul(arg, NULL, 10);
+               unsigned long n = strtoul(arg, NULL, 10);
 
-               if (errno == ERANGE) {
+               if (errno == ERANGE || n > INT_MAX) {

Since you are touching this: errno is checked for ERANGE but never cleared before the strtoul() call so a stale ERANGE from earlier would reject a valid interval. Pre-existing and probably not reachable with the current call sequence, but addin errno = 0; before strtoul() would make the check future-proof. Up to you whether you fold it in and or leave it for a follow-up.

Otherwise,

Quoted 7 lines
libselinux/utils/avcstat.c +0 -1
                        usage();
                        die("invalid interval \'%s\'", arg);
                }
--

2.50.1

#2 lore raw

PATCH v2 1/1 libselinux: avcstat: reject out-of-range interval values

strtoul() returns unsigned long, but avcstat stored the result in a signed int and passed it to sleep(). A value above INT_MAX becomes negative, and sleep() then treats it as a large unsigned duration. Compare the full strtoul() result with INT_MAX before narrowing it.

Clear errno before strtoul() so a stale ERANGE cannot reject a valid interval.

---
 libselinux/utils/avcstat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
libselinux/utils/avcstat.c +6 -2
index 640a7a05..c7658792 100644
@@ -13,6 +13,7 @@
 #include <libgen.h>
 #include <stdarg.h>
 #include <errno.h>
+#include <limits.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -122,9 +123,12 @@ int main(int argc, char **argv)
 
 	if (optind < argc) {
 		char *arg = argv[optind];
-		unsigned int n = strtoul(arg, NULL, 10);
+		unsigned long n;
 
-		if (errno == ERANGE) {
+		errno = 0;
+		n = strtoul(arg, NULL, 10);
+
+		if (errno == ERANGE || n > INT_MAX) {
 			usage();
 			die("invalid interval \'%s\'", arg);
 		}
Signature · 1 line
2.50.1
maintainer
#3 lore raw
Quoted 11 lines · Evgeniy Gorbanyov

strtoul() returns unsigned long, but avcstat stored the result in a signed int and passed it to sleep(). A value above INT_MAX becomes negative, and sleep() then treats it as a large unsigned duration. Compare the full strtoul() result with INT_MAX before narrowing it.

Clear errno before strtoul() so a stale ERANGE cannot reject a valid interval.

Thanks, merged.