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