libselinux: avcstat: reject out-of-range interval values
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
@@ -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
Quoted 24 lines · Evgeniy Gorbanyov
strtoul()returns unsigned long, but avcstat stored the result in a signed int and passed it tosleep(). A value aboveINT_MAXbecomes negative, andsleep()then treats it as a large unsigned duration. Compare the fullstrtoul()result withINT_MAXbefore narrowing it.Signed-off-by Evgeniy Gorbanyov <[email protected]>--- libselinux/utils/avcstat.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)libselinux/utils/avcstat.c +1
-0@@ -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
-1usage ();die ("invalid interval \'%s\'" , arg); } --2.50.1
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
@@ -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
Stephen Smalley
maintainerQuoted 11 lines · Evgeniy Gorbanyov
strtoul()returns unsigned long, but avcstat stored the result in a signed int and passed it tosleep(). A value aboveINT_MAXbecomes negative, andsleep()then treats it as a large unsigned duration. Compare the fullstrtoul()result withINT_MAXbefore narrowing it.Clear errno before
strtoul()so a stale ERANGE cannot reject a valid interval.Signed-off-by Evgeniy Gorbanyov <[email protected]>Reviewed-by Stephen Smalley <[email protected]>
Thanks, merged.