860497a
commit 1bf38e7260c2cd3e40e118c1f1ea28de182dc751
860497a
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
860497a
Date:   Tue Mar 16 12:37:55 2021 +0530
860497a
860497a
    Fix SXID_ERASE behavior in setuid programs (BZ #27471)
860497a
    
860497a
    When parse_tunables tries to erase a tunable marked as SXID_ERASE for
860497a
    setuid programs, it ends up setting the envvar string iterator
860497a
    incorrectly, because of which it may parse the next tunable
860497a
    incorrectly.  Given that currently the implementation allows malformed
860497a
    and unrecognized tunables pass through, it may even allow SXID_ERASE
860497a
    tunables to go through.
860497a
    
860497a
    This change revamps the SXID_ERASE implementation so that:
860497a
    
860497a
    - Only valid tunables are written back to the tunestr string, because
860497a
      of which children of SXID programs will only inherit a clean list of
860497a
      identified tunables that are not SXID_ERASE.
860497a
    
860497a
    - Unrecognized tunables get scrubbed off from the environment and
860497a
      subsequently from the child environment.
860497a
    
860497a
    - This has the side-effect that a tunable that is not identified by
860497a
      the setxid binary, will not be passed on to a non-setxid child even
860497a
      if the child could have identified that tunable.  This may break
860497a
      applications that expect this behaviour but expecting such tunables
860497a
      to cross the SXID boundary is wrong.
860497a
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
860497a
    
860497a
    (cherry picked from commit 2ed18c5b534d9e92fc006202a5af0df6b72e7aca)
860497a
860497a
diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
860497a
index 8b751dcf0deb0d01..8009e54ee5db32be 100644
860497a
--- a/elf/dl-tunables.c
860497a
+++ b/elf/dl-tunables.c
860497a
@@ -174,6 +174,7 @@ parse_tunables (char *tunestr, char *valstring)
860497a
     return;
860497a
 
860497a
   char *p = tunestr;
860497a
+  size_t off = 0;
860497a
 
860497a
   while (true)
860497a
     {
860497a
@@ -187,7 +188,11 @@ parse_tunables (char *tunestr, char *valstring)
860497a
       /* If we reach the end of the string before getting a valid name-value
860497a
 	 pair, bail out.  */
860497a
       if (p[len] == '\0')
860497a
-	return;
860497a
+	{
860497a
+	  if (__libc_enable_secure)
860497a
+	    tunestr[off] = '\0';
860497a
+	  return;
860497a
+	}
860497a
 
860497a
       /* We did not find a valid name-value pair before encountering the
860497a
 	 colon.  */
860497a
@@ -213,35 +218,28 @@ parse_tunables (char *tunestr, char *valstring)
860497a
 
860497a
 	  if (tunable_is_name (cur->name, name))
860497a
 	    {
860497a
-	      /* If we are in a secure context (AT_SECURE) then ignore the tunable
860497a
-		 unless it is explicitly marked as secure.  Tunable values take
860497a
-		 precedence over their envvar aliases.  */
860497a
+	      /* If we are in a secure context (AT_SECURE) then ignore the
860497a
+		 tunable unless it is explicitly marked as secure.  Tunable
860497a
+		 values take precedence over their envvar aliases.  We write
860497a
+		 the tunables that are not SXID_ERASE back to TUNESTR, thus
860497a
+		 dropping all SXID_ERASE tunables and any invalid or
860497a
+		 unrecognized tunables.  */
860497a
 	      if (__libc_enable_secure)
860497a
 		{
860497a
-		  if (cur->security_level == TUNABLE_SECLEVEL_SXID_ERASE)
860497a
+		  if (cur->security_level != TUNABLE_SECLEVEL_SXID_ERASE)
860497a
 		    {
860497a
-		      if (p[len] == '\0')
860497a
-			{
860497a
-			  /* Last tunable in the valstring.  Null-terminate and
860497a
-			     return.  */
860497a
-			  *name = '\0';
860497a
-			  return;
860497a
-			}
860497a
-		      else
860497a
-			{
860497a
-			  /* Remove the current tunable from the string.  We do
860497a
-			     this by overwriting the string starting from NAME
860497a
-			     (which is where the current tunable begins) with
860497a
-			     the remainder of the string.  We then have P point
860497a
-			     to NAME so that we continue in the correct
860497a
-			     position in the valstring.  */
860497a
-			  char *q = &p[len + 1];
860497a
-			  p = name;
860497a
-			  while (*q != '\0')
860497a
-			    *name++ = *q++;
860497a
-			  name[0] = '\0';
860497a
-			  len = 0;
860497a
-			}
860497a
+		      if (off > 0)
860497a
+			tunestr[off++] = ':';
860497a
+
860497a
+		      const char *n = cur->name;
860497a
+
860497a
+		      while (*n != '\0')
860497a
+			tunestr[off++] = *n++;
860497a
+
860497a
+		      tunestr[off++] = '=';
860497a
+
860497a
+		      for (size_t j = 0; j < len; j++)
860497a
+			tunestr[off++] = value[j];
860497a
 		    }
860497a
 
860497a
 		  if (cur->security_level != TUNABLE_SECLEVEL_NONE)
860497a
@@ -254,9 +252,7 @@ parse_tunables (char *tunestr, char *valstring)
860497a
 	    }
860497a
 	}
860497a
 
860497a
-      if (p[len] == '\0')
860497a
-	return;
860497a
-      else
860497a
+      if (p[len] != '\0')
860497a
 	p += len + 1;
860497a
     }
860497a
 }
860497a
diff --git a/elf/tst-env-setuid-tunables.c b/elf/tst-env-setuid-tunables.c
860497a
index 3d523875b1e07a74..05619c9adc8b2698 100644
860497a
--- a/elf/tst-env-setuid-tunables.c
860497a
+++ b/elf/tst-env-setuid-tunables.c
860497a
@@ -45,11 +45,37 @@
860497a
 const char *teststrings[] =
860497a
 {
860497a
   "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.check=2:glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2",
860497a
+  "glibc.malloc.perturb=0x800",
860497a
+  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.perturb=0x800:not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.not_valid.check=2:glibc.malloc.mmap_threshold=4096",
860497a
+  "not_valid.malloc.check=2:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096:glibc.malloc.check=2",
860497a
+  "glibc.malloc.check=4:glibc.malloc.garbage=2:glibc.maoc.mmap_threshold=4096",
860497a
+  ":glibc.malloc.garbage=2:glibc.malloc.check=1",
860497a
+  "glibc.malloc.check=1:glibc.malloc.check=2",
860497a
+  "not_valid.malloc.check=2",
860497a
+  "glibc.not_valid.check=2",
860497a
 };
860497a
 
860497a
 const char *resultstrings[] =
860497a
 {
860497a
   "glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.perturb=0x800",
860497a
+  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.perturb=0x800:glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.mmap_threshold=4096",
860497a
+  "glibc.malloc.mmap_threshold=4096",
860497a
+  "",
860497a
+  "",
860497a
+  "",
860497a
+  "",
860497a
+  "",
860497a
+  "",
860497a
 };
860497a
 
860497a
 static int