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