abe59f
commit bfb5ed5df3dd4d9507b4922248dc445b690d19c0
abe59f
Author: H.J. Lu <hjl.tools@gmail.com>
abe59f
Date:   Fri Oct 15 10:44:49 2021 -0700
abe59f
abe59f
    elf: Also try DT_RUNPATH for LD_AUDIT dlopen [BZ #28455]
abe59f
    
abe59f
    DT_RUNPATH is only used to find the immediate dependencies of the
abe59f
    executable or shared object containing the DT_RUNPATH entry.  Update
abe59f
    LD_AUDIT dlopen call to try the DT_RUNPATH entry of the executable.
abe59f
    
abe59f
    Add tst-audit14a, which is copied from tst-audit14, to DT_RUNPATH and
abe59f
    build tst-audit14 with -Wl,--disable-new-dtags to test DT_RPATH.
abe59f
    
abe59f
    This partially fixes BZ #28455.
abe59f
abe59f
Conflicts:
abe59f
	elf/Makefile
abe59f
		Rewrite test inclusion to use older stdout pattern.
abe59f
abe59f
diff --git a/elf/Makefile b/elf/Makefile
abe59f
index 914cb5ad2f2c3aea..4ec4e9a049156755 100644
abe59f
--- a/elf/Makefile
abe59f
+++ b/elf/Makefile
abe59f
@@ -227,6 +227,7 @@ tests += \
abe59f
   tst-audit12 \
abe59f
   tst-audit13 \
abe59f
   tst-audit14 \
abe59f
+  tst-audit14a \
abe59f
   tst-audit15 \
abe59f
   tst-audit16 \
abe59f
   tst-audit17 \
abe59f
@@ -1788,9 +1789,11 @@ $(objpfx)tst-auditmany.out: $(objpfx)tst-auditmanymod1.so \
abe59f
 tst-auditmany-ENV = \
abe59f
   LD_AUDIT=tst-auditmanymod1.so:tst-auditmanymod2.so:tst-auditmanymod3.so:tst-auditmanymod4.so:tst-auditmanymod5.so:tst-auditmanymod6.so:tst-auditmanymod7.so:tst-auditmanymod8.so:tst-auditmanymod9.so
abe59f
 
abe59f
-LDFLAGS-tst-audit14 = -Wl,--audit=tst-auditlogmod-1.so
abe59f
+LDFLAGS-tst-audit14 = -Wl,--audit=tst-auditlogmod-1.so,--disable-new-dtags
abe59f
 $(objpfx)tst-auditlogmod-1.so: $(libsupport)
abe59f
 $(objpfx)tst-audit14.out: $(objpfx)tst-auditlogmod-1.so
abe59f
+LDFLAGS-tst-audit14a = -Wl,--audit=tst-auditlogmod-1.so,--enable-new-dtags
abe59f
+$(objpfx)tst-audit14a.out: $(objpfx)tst-auditlogmod-1.so
abe59f
 LDFLAGS-tst-audit15 = \
abe59f
   -Wl,--audit=tst-auditlogmod-1.so,--depaudit=tst-auditlogmod-2.so
abe59f
 $(objpfx)tst-auditlogmod-2.so: $(libsupport)
abe59f
diff --git a/elf/dl-load.c b/elf/dl-load.c
abe59f
index 1613217a236c7fc3..0b45e6e3db31c70d 100644
abe59f
--- a/elf/dl-load.c
abe59f
+++ b/elf/dl-load.c
abe59f
@@ -2042,6 +2042,21 @@ _dl_map_object (struct link_map *loader, const char *name,
abe59f
 			    &main_map->l_rpath_dirs,
abe59f
 			    &realname, &fb, loader ?: main_map, LA_SER_RUNPATH,
abe59f
 			    &found_other_class);
abe59f
+
abe59f
+	  /* Also try DT_RUNPATH in the executable for LD_AUDIT dlopen
abe59f
+	     call.  */
abe59f
+	  if (__glibc_unlikely (mode & __RTLD_AUDIT)
abe59f
+	      && fd == -1 && !did_main_map
abe59f
+	      && main_map != NULL && main_map->l_type != lt_loaded)
abe59f
+	    {
abe59f
+	      struct r_search_path_struct l_rpath_dirs;
abe59f
+	      l_rpath_dirs.dirs = NULL;
abe59f
+	      if (cache_rpath (main_map, &l_rpath_dirs,
abe59f
+			       DT_RUNPATH, "RUNPATH"))
abe59f
+		fd = open_path (name, namelen, mode, &l_rpath_dirs,
abe59f
+				&realname, &fb, loader ?: main_map,
abe59f
+				LA_SER_RUNPATH, &found_other_class);
abe59f
+	    }
abe59f
 	}
abe59f
 
abe59f
       /* Try the LD_LIBRARY_PATH environment variable.  */
abe59f
diff --git a/elf/tst-audit14a.c b/elf/tst-audit14a.c
abe59f
new file mode 100644
abe59f
index 0000000000000000..c6232eacf2946e4e
abe59f
--- /dev/null
abe59f
+++ b/elf/tst-audit14a.c
abe59f
@@ -0,0 +1 @@
abe59f
+#include "tst-audit14.c"