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