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