From 8e1e967bc8d99a3233d51f67f6b88620cdff78dc Mon Sep 17 00:00:00 2001 From: "Andrew G. Morgan" Date: Sat, 6 Nov 2021 08:02:20 -0700 Subject: [PATCH] setcap: clean up error handling of the ns rootid argument. Bug reported by Artem S. Tashkinov: https://bugzilla.kernel.org/show_bug.cgi?id=214909 Signed-off-by: Andrew G. Morgan --- progs/setcap.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/progs/setcap.c b/progs/setcap.c index 442685d..fe985cd 100644 --- a/progs/setcap.c +++ b/progs/setcap.c @@ -22,6 +22,35 @@ static void usage(void) exit(1); } +/* parse a positive integer with some error handling */ +static unsigned long pos_uint(const char *text, const char *prefix, int *ok) +{ + char *remains; + unsigned long value; + ssize_t len = strlen(text); + + if (len == 0 || *text == '-') { + goto fail; + } + value = strtoul(text, &remains, 0); + if (*remains || value == 0) { + goto fail; + } + if (ok != NULL) { + *ok = 1; + } + return value; + +fail: + if (ok == NULL) { + fprintf(stderr, "%s: want positive integer, got \"%s\"\n", + prefix, text); + exit(1); + } + *ok = 0; + return 0; +} + #define MAXCAP 2048 static int read_caps(int quiet, const char *filename, char *buffer) @@ -93,11 +122,7 @@ int main(int argc, char **argv) exit(1); } --argc; - rootid = (uid_t) atoi(*++argv); - if (rootid+1 < 2) { - fprintf(stderr, "invalid rootid!=0 of '%s'", *argv); - exit(1); - } + rootid = (uid_t) pos_uint(*++argv, "bad ns rootid", NULL); continue; } -- 1.8.3.1