Blame SOURCES/sudo-1.8.28-CVE-strtouid.patch

c86b7a
Treat an ID of -1 as invalid since that means "no change".
c86b7a
Fixes CVE-2019-14287.
c86b7a
Found by Joe Vennix from Apple Information Security.
c86b7a
c86b7a
diff -r fcd7a6d8330e lib/util/strtoid.c
c86b7a
--- a/lib/util/strtoid.c	Fri Jan 11 13:31:15 2019 -0700
c86b7a
+++ b/lib/util/strtoid.c	Thu Oct 10 09:52:12 2019 -0600
c86b7a
@@ -1,5 +1,5 @@
c86b7a
 /*
c86b7a
- * Copyright (c) 2013-2016 Todd C. Miller <Todd.Miller@sudo.ws>
c86b7a
+ * Copyright (c) 2013-2019 Todd C. Miller <Todd.Miller@sudo.ws>
c86b7a
  *
c86b7a
  * Permission to use, copy, modify, and distribute this software for any
c86b7a
  * purpose with or without fee is hereby granted, provided that the above
c86b7a
@@ -47,6 +47,27 @@
c86b7a
 #include "sudo_util.h"
c86b7a
 
c86b7a
 /*
c86b7a
+ * Make sure that the ID ends with a valid separator char.
c86b7a
+ */
c86b7a
+static bool
c86b7a
+valid_separator(const char *p, const char *ep, const char *sep)
c86b7a
+{
c86b7a
+    bool valid = false;
c86b7a
+    debug_decl(valid_separator, SUDO_DEBUG_UTIL)
c86b7a
+
c86b7a
+    if (ep != p) {
c86b7a
+	/* check for valid separator (including '\0') */
c86b7a
+	if (sep == NULL)
c86b7a
+	    sep = "";
c86b7a
+	do {
c86b7a
+	    if (*ep == *sep)
c86b7a
+		valid = true;
c86b7a
+	} while (*sep++ != '\0');
c86b7a
+    }
c86b7a
+    debug_return_bool(valid);
c86b7a
+}
c86b7a
+
c86b7a
+/*
c86b7a
  * Parse a uid/gid in string form.
c86b7a
  * If sep is non-NULL, it contains valid separator characters (e.g. comma, space)
c86b7a
  * If endp is non-NULL it is set to the next char after the ID.
c86b7a
@@ -60,38 +81,35 @@ sudo_strtoid_v1(const char *p, const cha
c86b7a
     char *ep;
c86b7a
     id_t ret = 0;
c86b7a
     long long llval;
c86b7a
-    bool valid = false;
c86b7a
     debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL)
c86b7a
 
c86b7a
     /* skip leading space so we can pick up the sign, if any */
c86b7a
     while (isspace((unsigned char)*p))
c86b7a
 	p++;
c86b7a
-    if (sep == NULL)
c86b7a
-	sep = "";
c86b7a
+
c86b7a
+    /* While id_t may be 64-bit signed, uid_t and gid_t are 32-bit unsigned. */
c86b7a
     errno = 0;
c86b7a
     llval = strtoll(p, &ep, 10);
c86b7a
-    if (ep != p) {
c86b7a
-	/* check for valid separator (including '\0') */
c86b7a
-	do {
c86b7a
-	    if (*ep == *sep)
c86b7a
-		valid = true;
c86b7a
-	} while (*sep++ != '\0');
c86b7a
+    if ((errno == ERANGE && llval == LLONG_MAX) || llval > (id_t)UINT_MAX) {
c86b7a
+	errno = ERANGE;
c86b7a
+	if (errstr != NULL)
c86b7a
+	    *errstr = N_("value too large");
c86b7a
+	goto done;
c86b7a
     }
c86b7a
-    if (!valid) {
c86b7a
+    if ((errno == ERANGE && llval == LLONG_MIN) || llval < INT_MIN) {
c86b7a
+	errno = ERANGE;
c86b7a
+	if (errstr != NULL)
c86b7a
+	    *errstr = N_("value too small");
c86b7a
+	goto done;
c86b7a
+    }
c86b7a
+
c86b7a
+    /* Disallow id -1, which means "no change". */
c86b7a
+    if (!valid_separator(p, ep, sep) || llval == -1 || llval == (id_t)UINT_MAX) {
c86b7a
 	if (errstr != NULL)
c86b7a
 	    *errstr = N_("invalid value");
c86b7a
 	errno = EINVAL;
c86b7a
 	goto done;
c86b7a
     }
c86b7a
-    if (errno == ERANGE) {
c86b7a
-	if (errstr != NULL) {
c86b7a
-	    if (llval == LLONG_MAX)
c86b7a
-		*errstr = N_("value too large");
c86b7a
-	    else
c86b7a
-		*errstr = N_("value too small");
c86b7a
-	}
c86b7a
-	goto done;
c86b7a
-    }
c86b7a
     ret = (id_t)llval;
c86b7a
     if (errstr != NULL)
c86b7a
 	*errstr = NULL;
c86b7a
@@ -106,30 +124,15 @@ sudo_strtoid_v1(const char *p, const cha
c86b7a
 {
c86b7a
     char *ep;
c86b7a
     id_t ret = 0;
c86b7a
-    bool valid = false;
c86b7a
     debug_decl(sudo_strtoid, SUDO_DEBUG_UTIL)
c86b7a
 
c86b7a
     /* skip leading space so we can pick up the sign, if any */
c86b7a
     while (isspace((unsigned char)*p))
c86b7a
 	p++;
c86b7a
-    if (sep == NULL)
c86b7a
-	sep = "";
c86b7a
+
c86b7a
     errno = 0;
c86b7a
     if (*p == '-') {
c86b7a
 	long lval = strtol(p, &ep, 10);
c86b7a
-	if (ep != p) {
c86b7a
-	    /* check for valid separator (including '\0') */
c86b7a
-	    do {
c86b7a
-		if (*ep == *sep)
c86b7a
-		    valid = true;
c86b7a
-	    } while (*sep++ != '\0');
c86b7a
-	}
c86b7a
-	if (!valid) {
c86b7a
-	    if (errstr != NULL)
c86b7a
-		*errstr = N_("invalid value");
c86b7a
-	    errno = EINVAL;
c86b7a
-	    goto done;
c86b7a
-	}
c86b7a
 	if ((errno == ERANGE && lval == LONG_MAX) || lval > INT_MAX) {
c86b7a
 	    errno = ERANGE;
c86b7a
 	    if (errstr != NULL)
c86b7a
@@ -142,28 +145,31 @@ sudo_strtoid_v1(const char *p, const cha
c86b7a
 		*errstr = N_("value too small");
c86b7a
 	    goto done;
c86b7a
 	}
c86b7a
-	ret = (id_t)lval;
c86b7a
-    } else {
c86b7a
-	unsigned long ulval = strtoul(p, &ep, 10);
c86b7a
-	if (ep != p) {
c86b7a
-	    /* check for valid separator (including '\0') */
c86b7a
-	    do {
c86b7a
-		if (*ep == *sep)
c86b7a
-		    valid = true;
c86b7a
-	    } while (*sep++ != '\0');
c86b7a
-	}
c86b7a
-	if (!valid) {
c86b7a
+
c86b7a
+	/* Disallow id -1, which means "no change". */
c86b7a
+	if (!valid_separator(p, ep, sep) || lval == -1) {
c86b7a
 	    if (errstr != NULL)
c86b7a
 		*errstr = N_("invalid value");
c86b7a
 	    errno = EINVAL;
c86b7a
 	    goto done;
c86b7a
 	}
c86b7a
+	ret = (id_t)lval;
c86b7a
+    } else {
c86b7a
+	unsigned long ulval = strtoul(p, &ep, 10);
c86b7a
 	if ((errno == ERANGE && ulval == ULONG_MAX) || ulval > UINT_MAX) {
c86b7a
 	    errno = ERANGE;
c86b7a
 	    if (errstr != NULL)
c86b7a
 		*errstr = N_("value too large");
c86b7a
 	    goto done;
c86b7a
 	}
c86b7a
+
c86b7a
+	/* Disallow id -1, which means "no change". */
c86b7a
+	if (!valid_separator(p, ep, sep) || ulval == UINT_MAX) {
c86b7a
+	    if (errstr != NULL)
c86b7a
+		*errstr = N_("invalid value");
c86b7a
+	    errno = EINVAL;
c86b7a
+	    goto done;
c86b7a
+	}
c86b7a
 	ret = (id_t)ulval;
c86b7a
     }
c86b7a
     if (errstr != NULL)