[v2,1/6] elf: Fix audit regression

Message ID 20210719143309.2848878-2-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • Some rtld-audit fixes
Related show

Commit Message

Florian Weimer via Libc-alpha July 19, 2021, 2:33 p.m.
Commit 03e187a41d9 added a regression when an audit module does not have
libc as DT_NEEDED (although unusual it is possible).

Checked on x86_64-linux-gnu.
---
 elf/Makefile         | 12 +++++++++++-
 elf/dl-open.c        | 10 +---------
 elf/tst-audit17.c    | 25 +++++++++++++++++++++++++
 elf/tst-auditmod17.c | 23 +++++++++++++++++++++++
 4 files changed, 60 insertions(+), 10 deletions(-)
 create mode 100644 elf/tst-audit17.c
 create mode 100644 elf/tst-auditmod17.c

-- 
2.30.2

Comments

Florian Weimer via Libc-alpha July 27, 2021, 4:36 p.m. | #1
* Adhemerval Zanella via Libc-alpha:

> diff --git a/elf/dl-open.c b/elf/dl-open.c

> index d2240d8747..218a8312a3 100644

> --- a/elf/dl-open.c

> +++ b/elf/dl-open.c

> @@ -770,15 +770,7 @@ dl_open_worker (void *a)

>    if (!args->libc_already_loaded)

>      {

>        struct link_map *libc_map = GL(dl_ns)[args->nsid].libc_map;

> -#ifdef SHARED

> -      bool initial = libc_map->l_ns == LM_ID_BASE;

> -#else

> -      /* In the static case, there is only one namespace, but it

> -	 contains a secondary libc (the primary libc is statically

> -	 linked).  */

> -      bool initial = false;

> -#endif

> -      _dl_call_libc_early_init (libc_map, initial);

> +      _dl_call_libc_early_init (libc_map, false);

>      }

>  

>    /* Run the initializer functions of new objects.  Temporarily


Maybe add a comment that dlopen cannot be used to load an initial libc
by design?

Rest of the patch looks good to me.

Thanks,
Florian

Patch

diff --git a/elf/Makefile b/elf/Makefile
index 4fe60947ad..3216d67bb4 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -219,7 +219,7 @@  tests += restest1 preloadtest loadfail multiload origtest resolvfail \
 	 tst-dlopen-self tst-auditmany tst-initfinilazyfail tst-dlopenfail \
 	 tst-dlopenfail-2 \
 	 tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \
-	 tst-audit14 tst-audit15 tst-audit16 \
+	 tst-audit14 tst-audit15 tst-audit16 tst-audit17 \
 	 tst-single_threaded tst-single_threaded-pthread \
 	 tst-tls-ie tst-tls-ie-dlmopen argv0test \
 	 tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
@@ -1478,6 +1478,16 @@  $(objpfx)tst-auditlogmod-3.so: $(libsupport)
 $(objpfx)tst-audit16.out: \
   $(objpfx)tst-auditlogmod-1.so $(objpfx)tst-auditlogmod-2.so \
   $(objpfx)tst-auditlogmod-3.so
+$(objpfx)tst-audit17.out: $(objpfx)tst-auditmod17.so
+# The test check if a audit library without libc.so on DT_NEEDED works as
+# intended, so it uses an explicit link rule.
+$(objpfx)tst-auditmod17.so: $(objpfx)tst-auditmod17.os
+	$(CC) -nostdlib -nostartfiles -shared -o $@.new \
+	$(filter-out $(map-file),$^)
+	$(call after-link,$@.new)
+	mv -f $@.new $@
+CFLAGS-.os += $(call elide-stack-protector,.os,tst-auditmod17)
+tst-audit17-ENV = LD_AUDIT=$(objpfx)tst-auditmod17.so
 
 # tst-sonamemove links against an older implementation of the library.
 LDFLAGS-tst-sonamemove-linkmod1.so = \
diff --git a/elf/dl-open.c b/elf/dl-open.c
index d2240d8747..218a8312a3 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -770,15 +770,7 @@  dl_open_worker (void *a)
   if (!args->libc_already_loaded)
     {
       struct link_map *libc_map = GL(dl_ns)[args->nsid].libc_map;
-#ifdef SHARED
-      bool initial = libc_map->l_ns == LM_ID_BASE;
-#else
-      /* In the static case, there is only one namespace, but it
-	 contains a secondary libc (the primary libc is statically
-	 linked).  */
-      bool initial = false;
-#endif
-      _dl_call_libc_early_init (libc_map, initial);
+      _dl_call_libc_early_init (libc_map, false);
     }
 
   /* Run the initializer functions of new objects.  Temporarily
diff --git a/elf/tst-audit17.c b/elf/tst-audit17.c
new file mode 100644
index 0000000000..92986699d4
--- /dev/null
+++ b/elf/tst-audit17.c
@@ -0,0 +1,25 @@ 
+/* Check DT_AUDIT with audit not linked against libc.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+static int
+do_test (void)
+{
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/elf/tst-auditmod17.c b/elf/tst-auditmod17.c
new file mode 100644
index 0000000000..7a4467f597
--- /dev/null
+++ b/elf/tst-auditmod17.c
@@ -0,0 +1,23 @@ 
+/* Check DT_AUDIT with audit not linked against libc.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+unsigned int
+la_version (unsigned int version)
+{
+  return version;
+}