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