29e444
From 207e77fd3f0a94acdf0557608dd4f10ce0e0f22f Mon Sep 17 00:00:00 2001
29e444
From: Andreas Schwab <schwab@redhat.com>
29e444
Date: Mon, 9 May 2011 10:55:58 +0200
29e444
Subject: [PATCH] Never leave $ORIGIN unexpanded
29e444
29e444
* elf/dl-load.c (is_dst): Remove parameter secure, all callers
29e444
changed.  Move check for valid use of $ORIGIN ...
29e444
(_dl_dst_substitute): ... here.  Reset check_for_trusted when a
29e444
path element is skipped.
29e444
29e444
---
29e444
 ChangeLog     |    7 +++++++
29e444
 elf/dl-load.c |   34 ++++++++++++++++------------------
29e444
 2 files changed, 23 insertions(+), 18 deletions(-)
29e444
12745e
diff --git glibc-2.17-c758a686/elf/dl-load.c glibc-2.17-c758a686/elf/dl-load.c
29e444
index 18a83d2..6e16a9a 100644
12745e
--- glibc-2.17-c758a686/elf/dl-load.c
12745e
+++ glibc-2.17-c758a686/elf/dl-load.c
29e444
@@ -249,8 +249,7 @@ is_trusted_path_normalize (const char *path, size_t len)
29e444
 
29e444
 
29e444
 static size_t
29e444
-is_dst (const char *start, const char *name, const char *str,
29e444
-	int is_path, int secure)
29e444
+is_dst (const char *start, const char *name, const char *str, int is_path)
29e444
 {
29e444
   size_t len;
29e444
   bool is_curly = false;
29e444
@@ -279,12 +278,6 @@ is_dst (const char *start, const char *name, const char *str,
29e444
 	   && (!is_path || name[len] != ':'))
29e444
     return 0;
29e444
 
29e444
-  if (__builtin_expect (secure, 0)
29e444
-      && ((name[len] != '\0' && name[len] != '/'
29e444
-	   && (!is_path || name[len] != ':'))
29e444
-	  || (name != start + 1 && (!is_path || name[-2] != ':'))))
29e444
-    return 0;
29e444
-
29e444
   return len;
29e444
 }
29e444
 
29e444
@@ -299,13 +292,10 @@ _dl_dst_count (const char *name, int is_path)
29e444
     {
29e444
       size_t len;
29e444
 
29e444
-      /* $ORIGIN is not expanded for SUID/GUID programs (except if it
29e444
-	 is $ORIGIN alone) and it must always appear first in path.  */
29e444
       ++name;
29e444
-      if ((len = is_dst (start, name, "ORIGIN", is_path,
29e444
-			 INTUSE(__libc_enable_secure))) != 0
29e444
-	  || (len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0
29e444
-	  || (len = is_dst (start, name, "LIB", is_path, 0)) != 0)
29e444
+      if ((len = is_dst (start, name, "ORIGIN", is_path)) != 0
29e444
+	  || (len = is_dst (start, name, "PLATFORM", is_path)) != 0
29e444
+	  || (len = is_dst (start, name, "LIB", is_path)) != 0)
29e444
 	++cnt;
29e444
 
29e444
       name = strchr (name + len, '$');
29e444
@@ -338,9 +328,16 @@ _dl_dst_substitute (struct link_map *l, const char *name, char *result,
29e444
 	  size_t len;
29e444
 
29e444
 	  ++name;
29e444
-	  if ((len = is_dst (start, name, "ORIGIN", is_path,
29e444
-			     INTUSE(__libc_enable_secure))) != 0)
29e444
+	  if ((len = is_dst (start, name, "ORIGIN", is_path)) != 0)
29e444
 	    {
29e444
+	      /* For SUID/GUID programs $ORIGIN must always appear
29e444
+		 first in a path element.  */
29e444
+	      if (__builtin_expect (INTUSE(__libc_enable_secure), 0)
29e444
+		  && ((name[len] != '\0' && name[len] != '/'
29e444
+		       && (!is_path || name[len] != ':'))
29e444
+		      || (name != start + 1 && (!is_path || name[-2] != ':'))))
29e444
+		repl = (const char *) -1;
29e444
+	      else
29e444
 #ifndef SHARED
29e444
 	      if (l == NULL)
29e444
 		repl = _dl_get_origin ();
29e444
@@ -351,9 +348,9 @@ _dl_dst_substitute (struct link_map *l, const char *name, char *result,
29e444
 	      check_for_trusted = (INTUSE(__libc_enable_secure)
29e444
 				   && l->l_type == lt_executable);
29e444
 	    }
29e444
-	  else if ((len = is_dst (start, name, "PLATFORM", is_path, 0)) != 0)
29e444
+	  else if ((len = is_dst (start, name, "PLATFORM", is_path)) != 0)
29e444
 	    repl = GLRO(dl_platform);
29e444
-	  else if ((len = is_dst (start, name, "LIB", is_path, 0)) != 0)
29e444
+	  else if ((len = is_dst (start, name, "LIB", is_path)) != 0)
29e444
 	    repl = DL_DST_LIB;
29e444
 
29e444
 	  if (repl != NULL && repl != (const char *) -1)
29e444
@@ -373,6 +370,7 @@ _dl_dst_substitute (struct link_map *l, const char *name, char *result,
29e444
 		 element, but keep an empty element at the end.  */
29e444
 	      if (wp == result && is_path && *name == ':' && name[1] != '\0')
29e444
 		++name;
29e444
+	      check_for_trusted = false;
29e444
 	    }
29e444
 	  else
29e444
 	    /* No DST we recognize.  */