a5f861
This patch was developed under embargo and cannot reference an upstream
a5f861
commit. To find the associated commit please review the upstream git
a5f861
log for CVE-2023-4911 to identify the relevant commits.
a5f861
a5f861
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
a5f861
Date:   Tue Sep 19 18:39:32 2023 -0400
a5f861
a5f861
    tunables: Terminate if end of input is reached (CVE-2023-4911)
a5f861
    
a5f861
    The string parsing routine may end up writing beyond bounds of tunestr
a5f861
    if the input tunable string is malformed, of the form name=name=val.
a5f861
    This gets processed twice, first as name=name=val and next as name=val,
a5f861
    resulting in tunestr being name=name=val:name=val, thus overflowing
a5f861
    tunestr.
a5f861
    
a5f861
    Terminate the parsing loop at the first instance itself so that tunestr
a5f861
    does not overflow.
a5f861
    
a5f861
    This also fixes up tst-env-setuid-tunables to actually handle failures
a5f861
    correct and add new tests to validate the fix for this CVE.
a5f861
    
a5f861
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
a5f861
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
a5f861
a5f861
Conflicts:
a5f861
	NEWS
a5f861
	(Dropped)
a5f861
	elf/tst-env-setuid-tunables.c
a5f861
	(Trivial conflict at HAVE_TUNABLES)
a5f861
a5f861
diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
a5f861
index 3c84809d44381241..2c878e08ea197b29 100644
a5f861
--- a/elf/dl-tunables.c
a5f861
+++ b/elf/dl-tunables.c
a5f861
@@ -193,11 +193,7 @@ parse_tunables (char *tunestr, char *valstring)
a5f861
       /* If we reach the end of the string before getting a valid name-value
a5f861
 	 pair, bail out.  */
a5f861
       if (p[len] == '\0')
a5f861
-	{
a5f861
-	  if (__libc_enable_secure)
a5f861
-	    tunestr[off] = '\0';
a5f861
-	  return;
a5f861
-	}
a5f861
+	break;
a5f861
 
a5f861
       /* We did not find a valid name-value pair before encountering the
a5f861
 	 colon.  */
a5f861
@@ -257,9 +253,16 @@ parse_tunables (char *tunestr, char *valstring)
a5f861
 	    }
a5f861
 	}
a5f861
 
a5f861
-      if (p[len] != '\0')
a5f861
-	p += len + 1;
a5f861
+      /* We reached the end while processing the tunable string.  */
a5f861
+      if (p[len] == '\0')
a5f861
+	break;
a5f861
+
a5f861
+      p += len + 1;
a5f861
     }
a5f861
+
a5f861
+  /* Terminate tunestr before we leave.  */
a5f861
+  if (__libc_enable_secure)
a5f861
+    tunestr[off] = '\0';
a5f861
 }
a5f861
 #endif
a5f861
 
a5f861
diff --git a/elf/tst-env-setuid-tunables.c b/elf/tst-env-setuid-tunables.c
a5f861
index 0b9b075c40598c6f..8b0861c4ad853040 100644
a5f861
--- a/elf/tst-env-setuid-tunables.c
a5f861
+++ b/elf/tst-env-setuid-tunables.c
a5f861
@@ -52,6 +52,8 @@ const char *teststrings[] =
a5f861
   "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
a5f861
   "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
a5f861
   "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
a5f861
+  "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
a5f861
+  "glibc.malloc.check=2",
a5f861
   "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
a5f861
   "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
a5f861
   ":glibc.malloc.garbage=2:glibc.malloc.check=1",
a5f861
@@ -70,6 +72,8 @@ const char *resultstrings[] =
a5f861
   "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
a5f861
   "glibc.malloc.mmap_threshold=4096",
a5f861
   "glibc.malloc.mmap_threshold=4096",
a5f861
+  "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096",
a5f861
+  "",
a5f861
   "",
a5f861
   "",
a5f861
   "",
a5f861
@@ -84,11 +88,18 @@ test_child (int off)
a5f861
   const char *val = getenv ("GLIBC_TUNABLES");
a5f861
 
a5f861
 #if HAVE_TUNABLES
a5f861
+  printf ("    [%d] GLIBC_TUNABLES is %s\n", off, val);
a5f861
+  fflush (stdout);
a5f861
   if (val != NULL && strcmp (val, resultstrings[off]) == 0)
a5f861
     return 0;
a5f861
 
a5f861
   if (val != NULL)
a5f861
-    printf ("[%d] Unexpected GLIBC_TUNABLES VALUE %s\n", off, val);
a5f861
+    printf ("    [%d] Unexpected GLIBC_TUNABLES VALUE %s, expected %s\n",
a5f861
+	    off, val, resultstrings[off]);
a5f861
+  else
a5f861
+    printf ("    [%d] GLIBC_TUNABLES environment variable absent\n", off);
a5f861
+
a5f861
+  fflush (stdout);
a5f861
 
a5f861
   return 1;
a5f861
 #else
a5f861
@@ -117,21 +128,26 @@ do_test (int argc, char **argv)
a5f861
       if (ret != 0)
a5f861
 	exit (1);
a5f861
 
a5f861
-      exit (EXIT_SUCCESS);
a5f861
+      /* Special return code to make sure that the child executed all the way
a5f861
+	 through.  */
a5f861
+      exit (42);
a5f861
     }
a5f861
   else
a5f861
     {
a5f861
-      int ret = 0;
a5f861
-
a5f861
       /* Spawn tests.  */
a5f861
       for (int i = 0; i < array_length (teststrings); i++)
a5f861
 	{
a5f861
 	  char buf[INT_BUFSIZE_BOUND (int)];
a5f861
 
a5f861
-	  printf ("Spawned test for %s (%d)\n", teststrings[i], i);
a5f861
+	  printf ("[%d] Spawned test for %s\n", i, teststrings[i]);
a5f861
 	  snprintf (buf, sizeof (buf), "%d\n", i);
a5f861
+	  fflush (stdout);
a5f861
 	  if (setenv ("GLIBC_TUNABLES", teststrings[i], 1) != 0)
a5f861
-	    exit (1);
a5f861
+	    {
a5f861
+	      printf ("    [%d] Failed to set GLIBC_TUNABLES: %m", i);
a5f861
+	      support_record_failure ();
a5f861
+	      continue;
a5f861
+	    }
a5f861
 
a5f861
 	  int status = support_capture_subprogram_self_sgid (buf);
a5f861
 
a5f861
@@ -139,9 +155,14 @@ do_test (int argc, char **argv)
a5f861
 	  if (WEXITSTATUS (status) == EXIT_UNSUPPORTED)
a5f861
 	    return EXIT_UNSUPPORTED;
a5f861
 
a5f861
-	  ret |= status;
a5f861
+	  if (WEXITSTATUS (status) != 42)
a5f861
+	    {
a5f861
+	      printf ("    [%d] child failed with status %d\n", i,
a5f861
+		      WEXITSTATUS (status));
a5f861
+	      support_record_failure ();
a5f861
+	    }
a5f861
 	}
a5f861
-      return ret;
a5f861
+      return 0;
a5f861
     }
a5f861
 }
a5f861