4c1956
Downstream-only patch to change rtld_active () to return true during
4c1956
early audit operations.  GLRO (_dl_profile_output) is initialized much
4c1956
earlier than GLRO (dl_init_all_dirs), before auditors run, so it is a
4c1956
good replacement.
4c1956
4c1956
This is addressed downstream very differently, in this commit:
4c1956
4c1956
commit 8dcb6d0af07fda3607b541857e4f3970a74ed55b
4c1956
Author: Florian Weimer <fweimer@redhat.com>
4c1956
Date:   Tue Apr 26 14:23:02 2022 +0200
4c1956
4c1956
    dlfcn: Do not use rtld_active () to determine ld.so state (bug 29078)
4c1956
4c1956
    When audit modules are loaded, ld.so initialization is not yet
4c1956
    complete, and rtld_active () returns false even though ld.so is
4c1956
    mostly working.  Instead, the static dlopen hook is used, but that
4c1956
    does not work at all because this is not a static dlopen situation.
4c1956
4c1956
    Commit 466c1ea15f461edb8e3ffaf5d86d708876343bbf ("dlfcn: Rework
4c1956
    static dlopen hooks") moved the hook pointer into _rtld_global_ro,
4c1956
    which means that separate protection is not needed anymore and the
4c1956
    hook pointer can be checked directly.
4c1956
4c1956
    The guard for disabling libio vtable hardening in _IO_vtable_check
4c1956
    should stay for now.
4c1956
4c1956
    Fixes commit 8e1472d2c1e25e6eabc2059170731365f6d5b3d1 ("ld.so:
4c1956
    Examine GLRO to detect inactive loader [BZ #20204]").
4c1956
4c1956
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
4c1956
4c1956
diff --git a/elf/rtld.c b/elf/rtld.c
4c1956
index 22cceeab40319582..b47e84ca2fb6f03c 100644
4c1956
--- a/elf/rtld.c
4c1956
+++ b/elf/rtld.c
4c1956
@@ -2352,9 +2352,7 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
4c1956
   GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
4c1956
 
4c1956
   /* Remember the last search directory added at startup, now that
4c1956
-     malloc will no longer be the one from dl-minimal.c.  As a side
4c1956
-     effect, this marks ld.so as initialized, so that the rtld_active
4c1956
-     function returns true from now on.  */
4c1956
+     malloc will no longer be the one from dl-minimal.c.  */
4c1956
   GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
4c1956
 
4c1956
   /* Print scope information.  */
4c1956
@@ -2675,7 +2673,9 @@ process_envvars (struct dl_main_state *state)
4c1956
   char *envline;
4c1956
   char *debug_output = NULL;
4c1956
 
4c1956
-  /* This is the default place for profiling data file.  */
4c1956
+  /* This is the default place for profiling data file.  As a side
4c1956
+     effect, this marks ld.so as initialized, so that the rtld_active
4c1956
+     function returns true from now on.  */
4c1956
   GLRO(dl_profile_output)
4c1956
     = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
4c1956
 
4c1956
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
4c1956
index d4f70211c34d1c59..9dec9e3d3b6d6aa2 100644
4c1956
--- a/sysdeps/generic/ldsodefs.h
4c1956
+++ b/sysdeps/generic/ldsodefs.h
4c1956
@@ -600,18 +600,18 @@ struct rtld_global_ro
4c1956
 
4c1956
   /* Name of the shared object to be profiled (if any).  */
4c1956
   EXTERN const char *_dl_profile;
4c1956
-  /* Filename of the output file.  */
4c1956
+  /* Filename of the output file.  This is assigned a
4c1956
+     non-NULL pointer by the ld.so startup code (after initialization
4c1956
+     to NULL), so this can also serve as an indicator whether a copy
4c1956
+     of ld.so is initialized and active.  See the rtld_active function
4c1956
+     below.  */
4c1956
   EXTERN const char *_dl_profile_output;
4c1956
   /* Name of the object we want to trace the prelinking.  */
4c1956
   EXTERN const char *_dl_trace_prelink;
4c1956
   /* Map of shared object to be prelink traced.  */
4c1956
   EXTERN struct link_map *_dl_trace_prelink_map;
4c1956
 
4c1956
-  /* All search directories defined at startup.  This is assigned a
4c1956
-     non-NULL pointer by the ld.so startup code (after initialization
4c1956
-     to NULL), so this can also serve as an indicator whether a copy
4c1956
-     of ld.so is initialized and active.  See the rtld_active function
4c1956
-     below.  */
4c1956
+  /* All search directories defined at startup.  */
4c1956
   EXTERN struct r_search_path_elem *_dl_init_all_dirs;
4c1956
 
4c1956
 #ifdef NEED_DL_SYSINFO
4c1956
@@ -1259,9 +1259,9 @@ static inline bool
4c1956
 rtld_active (void)
4c1956
 {
4c1956
   /* The default-initialized variable does not have a non-zero
4c1956
-     dl_init_all_dirs member, so this allows us to recognize an
4c1956
+     dl_profile_output member, so this allows us to recognize an
4c1956
      initialized and active ld.so copy.  */
4c1956
-  return GLRO(dl_init_all_dirs) != NULL;
4c1956
+  return GLRO(dl_profile_output) != NULL;
4c1956
 }
4c1956
 
4c1956
 static inline struct auditstate *