e354a5
commit 4c6e0415ef206a595c62d5d37e3b9a821782c533
e354a5
Author: Florian Weimer <fweimer@redhat.com>
e354a5
Date:   Fri Apr 3 13:17:48 2020 +0200
e354a5
e354a5
    elf: Simplify handling of lists of audit strings
e354a5
    
e354a5
    All list elements are colon-separated strings, and there is a hard
e354a5
    upper limit for the number of audit modules, so it is possible to
e354a5
    pre-allocate a fixed-size array of strings to which the LD_AUDIT
e354a5
    environment variable and --audit arguments are added.
e354a5
    
e354a5
    Also eliminate the global variables for the audit list because
e354a5
    the list is only needed briefly during startup.
e354a5
    
e354a5
    There is a slight behavior change: All duplicate LD_AUDIT environment
e354a5
    variables are now processed, not just the last one as before.  However,
e354a5
    such environment vectors are invalid anyway.
e354a5
    
e354a5
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
e354a5
e354a5
diff --git a/elf/rtld.c b/elf/rtld.c
e354a5
index f755dc30331f799f..c39cb8f2cd4bb1cc 100644
e354a5
--- a/elf/rtld.c
e354a5
+++ b/elf/rtld.c
e354a5
@@ -43,6 +43,7 @@
e354a5
 #include <stap-probe.h>
e354a5
 #include <stackinfo.h>
e354a5
 #include <not-cancel.h>
e354a5
+#include <array_length.h>
e354a5
 
e354a5
 #include <assert.h>
e354a5
 
e354a5
@@ -107,8 +108,53 @@ static void print_missing_version (int errcode, const char *objname,
e354a5
 /* Print the various times we collected.  */
e354a5
 static void print_statistics (const hp_timing_t *total_timep);
e354a5
 
e354a5
-/* Add audit objects.  */
e354a5
-static void process_dl_audit (char *str);
e354a5
+/* Length limits for names and paths, to protect the dynamic linker,
e354a5
+   particularly when __libc_enable_secure is active.  */
e354a5
+#ifdef NAME_MAX
e354a5
+# define SECURE_NAME_LIMIT NAME_MAX
e354a5
+#else
e354a5
+# define SECURE_NAME_LIMIT 255
e354a5
+#endif
e354a5
+#ifdef PATH_MAX
e354a5
+# define SECURE_PATH_LIMIT PATH_MAX
e354a5
+#else
e354a5
+# define SECURE_PATH_LIMIT 1024
e354a5
+#endif
e354a5
+
e354a5
+/* Strings containing colon-separated lists of audit modules.  */
e354a5
+struct audit_list
e354a5
+{
e354a5
+  /* Array of strings containing colon-separated path lists.  Each
e354a5
+     audit module needs its own namespace, so pre-allocate the largest
e354a5
+     possible list.  */
e354a5
+  const char *audit_strings[DL_NNS];
e354a5
+
e354a5
+  /* Number of entries added to audit_strings.  */
e354a5
+  size_t length;
e354a5
+
e354a5
+  /* Index into the audit_strings array (for the iteration phase).  */
e354a5
+  size_t current_index;
e354a5
+
e354a5
+  /* Tail of audit_strings[current_index] which still needs
e354a5
+     processing.  */
e354a5
+  const char *current_tail;
e354a5
+
e354a5
+  /* Scratch buffer for returning a name which is part of the strings
e354a5
+     in audit_strings.  */
e354a5
+  char fname[SECURE_NAME_LIMIT];
e354a5
+};
e354a5
+
e354a5
+/* Creates an empty audit list.  */
e354a5
+static void audit_list_init (struct audit_list *);
e354a5
+
e354a5
+/* Add a string to the end of the audit list, for later parsing.  Must
e354a5
+   not be called after audit_list_next.  */
e354a5
+static void audit_list_add_string (struct audit_list *, const char *);
e354a5
+
e354a5
+/* Extract the next audit module from the audit list.  Only modules
e354a5
+   for which dso_name_valid_for_suid is true are returned.  Must be
e354a5
+   called after all the audit_list_add_string calls.  */
e354a5
+static const char *audit_list_next (struct audit_list *);
e354a5
 
e354a5
 /* This is a list of all the modes the dynamic loader can be in.  */
e354a5
 enum mode { normal, list, verify, trace };
e354a5
@@ -116,7 +162,7 @@ enum mode { normal, list, verify, trace };
e354a5
 /* Process all environments variables the dynamic linker must recognize.
e354a5
    Since all of them start with `LD_' we are a bit smarter while finding
e354a5
    all the entries.  */
e354a5
-static void process_envvars (enum mode *modep);
e354a5
+static void process_envvars (enum mode *modep, struct audit_list *);
e354a5
 
e354a5
 #ifdef DL_ARGV_NOT_RELRO
e354a5
 int _dl_argc attribute_hidden;
e354a5
@@ -144,19 +190,6 @@ uintptr_t __pointer_chk_guard_local
e354a5
 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
e354a5
 #endif
e354a5
 
e354a5
-/* Length limits for names and paths, to protect the dynamic linker,
e354a5
-   particularly when __libc_enable_secure is active.  */
e354a5
-#ifdef NAME_MAX
e354a5
-# define SECURE_NAME_LIMIT NAME_MAX
e354a5
-#else
e354a5
-# define SECURE_NAME_LIMIT 255
e354a5
-#endif
e354a5
-#ifdef PATH_MAX
e354a5
-# define SECURE_PATH_LIMIT PATH_MAX
e354a5
-#else
e354a5
-# define SECURE_PATH_LIMIT 1024
e354a5
-#endif
e354a5
-
e354a5
 /* Check that AT_SECURE=0, or that the passed name does not contain
e354a5
    directories and is not overly long.  Reject empty names
e354a5
    unconditionally.  */
e354a5
@@ -174,89 +207,75 @@ dso_name_valid_for_suid (const char *p)
e354a5
   return *p != '\0';
e354a5
 }
e354a5
 
e354a5
-/* LD_AUDIT variable contents.  Must be processed before the
e354a5
-   audit_list below.  */
e354a5
-const char *audit_list_string;
e354a5
-
e354a5
-/* Cyclic list of auditing DSOs.  audit_list->next is the first
e354a5
-   element.  */
e354a5
-static struct audit_list
e354a5
+static void
e354a5
+audit_list_init (struct audit_list *list)
e354a5
 {
e354a5
-  const char *name;
e354a5
-  struct audit_list *next;
e354a5
-} *audit_list;
e354a5
+  list->length = 0;
e354a5
+  list->current_index = 0;
e354a5
+  list->current_tail = NULL;
e354a5
+}
e354a5
 
e354a5
-/* Iterator for audit_list_string followed by audit_list.  */
e354a5
-struct audit_list_iter
e354a5
+static void
e354a5
+audit_list_add_string (struct audit_list *list, const char *string)
e354a5
 {
e354a5
-  /* Tail of audit_list_string still needing processing, or NULL.  */
e354a5
-  const char *audit_list_tail;
e354a5
+  /* Empty strings do not load anything.  */
e354a5
+  if (*string == '\0')
e354a5
+    return;
e354a5
 
e354a5
-  /* The list element returned in the previous iteration.  NULL before
e354a5
-     the first element.  */
e354a5
-  struct audit_list *previous;
e354a5
+  if (list->length == array_length (list->audit_strings))
e354a5
+    _dl_fatal_printf ("Fatal glibc error: Too many audit modules requested\n");
e354a5
 
e354a5
-  /* Scratch buffer for returning a name which is part of
e354a5
-     audit_list_string.  */
e354a5
-  char fname[SECURE_NAME_LIMIT];
e354a5
-};
e354a5
+  list->audit_strings[list->length++] = string;
e354a5
 
e354a5
-/* Initialize an audit list iterator.  */
e354a5
-static void
e354a5
-audit_list_iter_init (struct audit_list_iter *iter)
e354a5
-{
e354a5
-  iter->audit_list_tail = audit_list_string;
e354a5
-  iter->previous = NULL;
e354a5
+  /* Initialize processing of the first string for
e354a5
+     audit_list_next.  */
e354a5
+  if (list->length == 1)
e354a5
+    list->current_tail = string;
e354a5
 }
e354a5
 
e354a5
-/* Iterate through both audit_list_string and audit_list.  */
e354a5
 static const char *
e354a5
-audit_list_iter_next (struct audit_list_iter *iter)
e354a5
+audit_list_next (struct audit_list *list)
e354a5
 {
e354a5
-  if (iter->audit_list_tail != NULL)
e354a5
+  if (list->current_tail == NULL)
e354a5
+    return NULL;
e354a5
+
e354a5
+  while (true)
e354a5
     {
e354a5
-      /* First iterate over audit_list_string.  */
e354a5
-      while (*iter->audit_list_tail != '\0')
e354a5
+      /* Advance to the next string in audit_strings if the current
e354a5
+	 string has been exhausted.  */
e354a5
+      while (*list->current_tail == '\0')
e354a5
 	{
e354a5
-	  /* Split audit list at colon.  */
e354a5
-	  size_t len = strcspn (iter->audit_list_tail, ":");
e354a5
-	  if (len > 0 && len < sizeof (iter->fname))
e354a5
+	  ++list->current_index;
e354a5
+	  if (list->current_index == list->length)
e354a5
 	    {
e354a5
-	      memcpy (iter->fname, iter->audit_list_tail, len);
e354a5
-	      iter->fname[len] = '\0';
e354a5
+	      list->current_tail = NULL;
e354a5
+	      return NULL;
e354a5
 	    }
e354a5
-	  else
e354a5
-	    /* Do not return this name to the caller.  */
e354a5
-	    iter->fname[0] = '\0';
e354a5
-
e354a5
-	  /* Skip over the substring and the following delimiter.  */
e354a5
-	  iter->audit_list_tail += len;
e354a5
-	  if (*iter->audit_list_tail == ':')
e354a5
-	    ++iter->audit_list_tail;
e354a5
-
e354a5
-	  /* If the name is valid, return it.  */
e354a5
-	  if (dso_name_valid_for_suid (iter->fname))
e354a5
-	    return iter->fname;
e354a5
-	  /* Otherwise, wrap around and try the next name.  */
e354a5
+	  list->current_tail = list->audit_strings[list->current_index];
e354a5
 	}
e354a5
-      /* Fall through to the procesing of audit_list.  */
e354a5
-    }
e354a5
 
e354a5
-  if (iter->previous == NULL)
e354a5
-    {
e354a5
-      if (audit_list == NULL)
e354a5
-	/* No pre-parsed audit list.  */
e354a5
-	return NULL;
e354a5
-      /* Start of audit list.  The first list element is at
e354a5
-	 audit_list->next (cyclic list).  */
e354a5
-      iter->previous = audit_list->next;
e354a5
-      return iter->previous->name;
e354a5
+      /* Split the in-string audit list at the next colon colon.  */
e354a5
+      size_t len = strcspn (list->current_tail, ":");
e354a5
+      if (len > 0 && len < sizeof (list->fname))
e354a5
+	{
e354a5
+	  memcpy (list->fname, list->current_tail, len);
e354a5
+	  list->fname[len] = '\0';
e354a5
+	}
e354a5
+      else
e354a5
+	/* Mark the name as unusable for dso_name_valid_for_suid.  */
e354a5
+	list->fname[0] = '\0';
e354a5
+
e354a5
+      /* Skip over the substring and the following delimiter.  */
e354a5
+      list->current_tail += len;
e354a5
+      if (*list->current_tail == ':')
e354a5
+	++list->current_tail;
e354a5
+
e354a5
+      /* If the name is valid, return it.  */
e354a5
+      if (dso_name_valid_for_suid (list->fname))
e354a5
+	return list->fname;
e354a5
+
e354a5
+      /* Otherwise wrap around to find the next list element. .  */
e354a5
     }
e354a5
-  if (iter->previous == audit_list)
e354a5
-    /* Cyclic list wrap-around.  */
e354a5
-    return NULL;
e354a5
-  iter->previous = iter->previous->next;
e354a5
-  return iter->previous->name;
e354a5
 }
e354a5
 
e354a5
 /* Set nonzero during loading and initialization of executable and
e354a5
@@ -1060,15 +1079,13 @@ notify_audit_modules_of_loaded_object (struct link_map *map)
e354a5
 
e354a5
 /* Load all audit modules.  */
e354a5
 static void
e354a5
-load_audit_modules (struct link_map *main_map)
e354a5
+load_audit_modules (struct link_map *main_map, struct audit_list *audit_list)
e354a5
 {
e354a5
   struct audit_ifaces *last_audit = NULL;
e354a5
-  struct audit_list_iter al_iter;
e354a5
-  audit_list_iter_init (&al_iter);
e354a5
 
e354a5
   while (true)
e354a5
     {
e354a5
-      const char *name = audit_list_iter_next (&al_iter);
e354a5
+      const char *name = audit_list_next (audit_list);
e354a5
       if (name == NULL)
e354a5
 	break;
e354a5
       load_audit_module (name, &last_audit);
e354a5
@@ -1100,6 +1117,9 @@ dl_main (const ElfW(Phdr) *phdr,
e354a5
   bool rtld_is_main = false;
e354a5
   void *tcbp = NULL;
e354a5
 
e354a5
+  struct audit_list audit_list;
e354a5
+  audit_list_init (&audit_list);
e354a5
+
e354a5
   GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
e354a5
 
e354a5
 #if defined SHARED && defined _LIBC_REENTRANT \
e354a5
@@ -1113,7 +1133,7 @@ dl_main (const ElfW(Phdr) *phdr,
e354a5
   GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
e354a5
 
e354a5
   /* Process the environment variable which control the behaviour.  */
e354a5
-  process_envvars (&mode);
e354a5
+  process_envvars (&mode, &audit_list);
e354a5
 
e354a5
   /* Set up a flag which tells we are just starting.  */
e354a5
   _dl_starting_up = 1;
e354a5
@@ -1185,7 +1205,7 @@ dl_main (const ElfW(Phdr) *phdr,
e354a5
 	  }
e354a5
 	else if (! strcmp (_dl_argv[1], "--audit") && _dl_argc > 2)
e354a5
 	  {
e354a5
-	    process_dl_audit (_dl_argv[2]);
e354a5
+	    audit_list_add_string (&audit_list, _dl_argv[2]);
e354a5
 
e354a5
 	    _dl_skip_args += 2;
e354a5
 	    _dl_argc -= 2;
e354a5
@@ -1612,8 +1632,7 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
e354a5
 
e354a5
   /* If we have auditing DSOs to load, do it now.  */
e354a5
   bool need_security_init = true;
e354a5
-  if (__glibc_unlikely (audit_list != NULL)
e354a5
-      || __glibc_unlikely (audit_list_string != NULL))
e354a5
+  if (audit_list.length > 0)
e354a5
     {
e354a5
       /* Since we start using the auditing DSOs right away we need to
e354a5
 	 initialize the data structures now.  */
e354a5
@@ -1626,7 +1645,7 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
e354a5
       security_init ();
e354a5
       need_security_init = false;
e354a5
 
e354a5
-      load_audit_modules (main_map);
e354a5
+      load_audit_modules (main_map, &audit_list);
e354a5
     }
e354a5
 
e354a5
   /* Keep track of the currently loaded modules to count how many
e354a5
@@ -2500,30 +2519,6 @@ a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
e354a5
     }
e354a5
 }
e354a5
 
e354a5
-static void
e354a5
-process_dl_audit (char *str)
e354a5
-{
e354a5
-  /* The parameter is a colon separated list of DSO names.  */
e354a5
-  char *p;
e354a5
-
e354a5
-  while ((p = (strsep) (&str, ":")) != NULL)
e354a5
-    if (dso_name_valid_for_suid (p))
e354a5
-      {
e354a5
-	/* This is using the local malloc, not the system malloc.  The
e354a5
-	   memory can never be freed.  */
e354a5
-	struct audit_list *newp = malloc (sizeof (*newp));
e354a5
-	newp->name = p;
e354a5
-
e354a5
-	if (audit_list == NULL)
e354a5
-	  audit_list = newp->next = newp;
e354a5
-	else
e354a5
-	  {
e354a5
-	    newp->next = audit_list->next;
e354a5
-	    audit_list = audit_list->next = newp;
e354a5
-	  }
e354a5
-      }
e354a5
-}
e354a5
-
e354a5
 /* Process all environments variables the dynamic linker must recognize.
e354a5
    Since all of them start with `LD_' we are a bit smarter while finding
e354a5
    all the entries.  */
e354a5
@@ -2531,7 +2526,7 @@ extern char **_environ attribute_hidden;
e354a5
 
e354a5
 
e354a5
 static void
e354a5
-process_envvars (enum mode *modep)
e354a5
+process_envvars (enum mode *modep, struct audit_list *audit_list)
e354a5
 {
e354a5
   char **runp = _environ;
e354a5
   char *envline;
e354a5
@@ -2571,7 +2566,7 @@ process_envvars (enum mode *modep)
e354a5
 	      break;
e354a5
 	    }
e354a5
 	  if (memcmp (envline, "AUDIT", 5) == 0)
e354a5
-	    audit_list_string = &envline[6];
e354a5
+	    audit_list_add_string (audit_list, &envline[6]);
e354a5
 	  break;
e354a5
 
e354a5
 	case 7: