446cf2
commit 8f7a75d700af809eeb4363895078fabfb3a9d7c3
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Mon Feb 17 16:49:40 2020 +0100
446cf2
446cf2
    elf: Implement DT_AUDIT, DT_DEPAUDIT support [BZ #24943]
446cf2
    
446cf2
    binutils ld has supported --audit, --depaudit for a long time,
446cf2
    only support in glibc has been missing.
446cf2
    
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
446cf2
Conflicts:
446cf2
	elf/Makefile
446cf2
	  (Test backport differences.)
446cf2
446cf2
diff --git a/elf/Makefile b/elf/Makefile
446cf2
index 5b0aeccb0a53c182..a6601ba84c8f4017 100644
446cf2
--- a/elf/Makefile
446cf2
+++ b/elf/Makefile
446cf2
@@ -195,7 +195,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
446cf2
 	 tst-sonamemove-link tst-sonamemove-dlopen \
446cf2
 	 tst-auditmany tst-initfinilazyfail \
446cf2
 	 tst-dlopenfail tst-dlopenfail-2 \
446cf2
-	 tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen
446cf2
+	 tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \
446cf2
+	 tst-audit14 tst-audit15 tst-audit16
446cf2
 #	 reldep9
446cf2
 tests-internal += loadtest unload unload2 circleload1 \
446cf2
 	 neededtest neededtest2 neededtest3 neededtest4 \
446cf2
@@ -310,7 +311,8 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
446cf2
 		tst-initlazyfailmod tst-finilazyfailmod \
446cf2
 		tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2 \
446cf2
 		tst-dlopenfailmod3 \
446cf2
-		tst-filterobj-flt tst-filterobj-aux tst-filterobj-filtee
446cf2
+		tst-filterobj-flt tst-filterobj-aux tst-filterobj-filtee \
446cf2
+		tst-auditlogmod-1 tst-auditlogmod-2 tst-auditlogmod-3
446cf2
 
446cf2
 # Most modules build with _ISOMAC defined, but those filtered out
446cf2
 # depend on internal headers.
446cf2
@@ -1453,6 +1455,22 @@ $(objpfx)tst-auditmany.out: $(objpfx)tst-auditmanymod1.so \
446cf2
 tst-auditmany-ENV = \
446cf2
   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
446cf2
 
446cf2
+LDFLAGS-tst-audit14 = -Wl,--audit=tst-auditlogmod-1.so
446cf2
+$(objpfx)tst-auditlogmod-1.so: $(libsupport)
446cf2
+$(objpfx)tst-audit14.out: $(objpfx)tst-auditlogmod-1.so
446cf2
+LDFLAGS-tst-audit15 = \
446cf2
+  -Wl,--audit=tst-auditlogmod-1.so,--depaudit=tst-auditlogmod-2.so
446cf2
+$(objpfx)tst-auditlogmod-2.so: $(libsupport)
446cf2
+$(objpfx)tst-audit15.out: \
446cf2
+  $(objpfx)tst-auditlogmod-1.so $(objpfx)tst-auditlogmod-2.so
446cf2
+LDFLAGS-tst-audit16 = \
446cf2
+  -Wl,--audit=tst-auditlogmod-1.so:tst-auditlogmod-2.so \
446cf2
+  -Wl,--depaudit=tst-auditlogmod-3.so
446cf2
+$(objpfx)tst-auditlogmod-3.so: $(libsupport)
446cf2
+$(objpfx)tst-audit16.out: \
446cf2
+  $(objpfx)tst-auditlogmod-1.so $(objpfx)tst-auditlogmod-2.so \
446cf2
+  $(objpfx)tst-auditlogmod-3.so
446cf2
+
446cf2
 # tst-sonamemove links against an older implementation of the library.
446cf2
 LDFLAGS-tst-sonamemove-linkmod1.so = \
446cf2
   -Wl,--version-script=tst-sonamemove-linkmod1.map \
446cf2
diff --git a/elf/rtld.c b/elf/rtld.c
446cf2
index c39cb8f2cd4bb1cc..d44facf5343b3301 100644
446cf2
--- a/elf/rtld.c
446cf2
+++ b/elf/rtld.c
446cf2
@@ -151,9 +151,17 @@ static void audit_list_init (struct audit_list *);
446cf2
    not be called after audit_list_next.  */
446cf2
 static void audit_list_add_string (struct audit_list *, const char *);
446cf2
 
446cf2
+/* Add the audit strings from the link map, found in the dynamic
446cf2
+   segment at TG (either DT_AUDIT and DT_DEPAUDIT).  Must be called
446cf2
+   before audit_list_next.  */
446cf2
+static void audit_list_add_dynamic_tag (struct audit_list *,
446cf2
+					struct link_map *,
446cf2
+					unsigned int tag);
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
+   called after all the audit_list_add_string,
446cf2
+   audit_list_add_dynamic_tags 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
@@ -233,6 +241,16 @@ audit_list_add_string (struct audit_list *list, const char *string)
446cf2
     list->current_tail = string;
446cf2
 }
446cf2
 
446cf2
+static void
446cf2
+audit_list_add_dynamic_tag (struct audit_list *list, struct link_map *main_map,
446cf2
+			    unsigned int tag)
446cf2
+{
446cf2
+  ElfW(Dyn) *info = main_map->l_info[ADDRIDX (tag)];
446cf2
+  const char *strtab = (const char *) D_PTR (main_map, l_info[DT_STRTAB]);
446cf2
+  if (info != NULL)
446cf2
+    audit_list_add_string (list, strtab + info->d_un.d_val);
446cf2
+}
446cf2
+
446cf2
 static const char *
446cf2
 audit_list_next (struct audit_list *list)
446cf2
 {
446cf2
@@ -1630,6 +1648,9 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
446cf2
     /* Assign a module ID.  Do this before loading any audit modules.  */
446cf2
     GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
446cf2
 
446cf2
+  audit_list_add_dynamic_tag (&audit_list, main_map, DT_AUDIT);
446cf2
+  audit_list_add_dynamic_tag (&audit_list, main_map, DT_DEPAUDIT);
446cf2
+
446cf2
   /* If we have auditing DSOs to load, do it now.  */
446cf2
   bool need_security_init = true;
446cf2
   if (audit_list.length > 0)
446cf2
diff --git a/elf/tst-audit14.c b/elf/tst-audit14.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..27ff8db9480a98cc
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-audit14.c
446cf2
@@ -0,0 +1,46 @@
446cf2
+/* Main program with DT_AUDIT.  One audit module.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <stdlib.h>
446cf2
+#include <string.h>
446cf2
+#include <support/check.h>
446cf2
+#include <support/xstdio.h>
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  /* Verify what the audit module has written.  This test assumes that
446cf2
+     standard output has been redirected to a regular file.  */
446cf2
+  FILE *fp = xfopen ("/dev/stdout", "r");
446cf2
+
446cf2
+  char *buffer = NULL;
446cf2
+  size_t buffer_length = 0;
446cf2
+  size_t line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  const char *message = "info: tst-auditlogmod-1.so loaded\n";
446cf2
+  TEST_COMPARE_BLOB (message, strlen (message), buffer, line_length);
446cf2
+
446cf2
+  /* No more audit module output.  */
446cf2
+  line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  TEST_COMPARE_BLOB ("", 0, buffer, line_length);
446cf2
+
446cf2
+  free (buffer);
446cf2
+  xfclose (fp);
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/elf/tst-audit15.c b/elf/tst-audit15.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..cbd1589ec40653d1
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-audit15.c
446cf2
@@ -0,0 +1,50 @@
446cf2
+/* Main program with DT_AUDIT and DT_DEPAUDIT.  Two audit modules.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <stdlib.h>
446cf2
+#include <string.h>
446cf2
+#include <support/check.h>
446cf2
+#include <support/xstdio.h>
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  /* Verify what the audit modules have written.  This test assumes
446cf2
+     that standard output has been redirected to a regular file.  */
446cf2
+  FILE *fp = xfopen ("/dev/stdout", "r");
446cf2
+
446cf2
+  char *buffer = NULL;
446cf2
+  size_t buffer_length = 0;
446cf2
+  size_t line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  const char *message = "info: tst-auditlogmod-1.so loaded\n";
446cf2
+  TEST_COMPARE_BLOB (message, strlen (message), buffer, line_length);
446cf2
+
446cf2
+  line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  message = "info: tst-auditlogmod-2.so loaded\n";
446cf2
+  TEST_COMPARE_BLOB (message, strlen (message), buffer, line_length);
446cf2
+
446cf2
+  /* No more audit module output.  */
446cf2
+  line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  TEST_COMPARE_BLOB ("", 0, buffer, line_length);
446cf2
+
446cf2
+  free (buffer);
446cf2
+  xfclose (fp);
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/elf/tst-audit16.c b/elf/tst-audit16.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..dd6ce189ea6fffa3
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-audit16.c
446cf2
@@ -0,0 +1,54 @@
446cf2
+/* Main program with DT_AUDIT and DT_DEPAUDIT.  Three audit modules.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <stdlib.h>
446cf2
+#include <string.h>
446cf2
+#include <support/check.h>
446cf2
+#include <support/xstdio.h>
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  /* Verify what the audit modules have written.  This test assumes
446cf2
+     that standard output has been redirected to a regular file.  */
446cf2
+  FILE *fp = xfopen ("/dev/stdout", "r");
446cf2
+
446cf2
+  char *buffer = NULL;
446cf2
+  size_t buffer_length = 0;
446cf2
+  size_t line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  const char *message = "info: tst-auditlogmod-1.so loaded\n";
446cf2
+  TEST_COMPARE_BLOB (message, strlen (message), buffer, line_length);
446cf2
+
446cf2
+  line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  message = "info: tst-auditlogmod-2.so loaded\n";
446cf2
+  TEST_COMPARE_BLOB (message, strlen (message), buffer, line_length);
446cf2
+
446cf2
+  line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  message = "info: tst-auditlogmod-3.so loaded\n";
446cf2
+  TEST_COMPARE_BLOB (message, strlen (message), buffer, line_length);
446cf2
+
446cf2
+  /* No more audit module output.  */
446cf2
+  line_length = xgetline (&buffer, &buffer_length, fp);
446cf2
+  TEST_COMPARE_BLOB ("", 0, buffer, line_length);
446cf2
+
446cf2
+  free (buffer);
446cf2
+  xfclose (fp);
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/elf/tst-auditlogmod-1.c b/elf/tst-auditlogmod-1.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..db9dac4c01a58dcd
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-auditlogmod-1.c
446cf2
@@ -0,0 +1,27 @@
446cf2
+/* Audit module which logs that it was loaded.  Variant 1.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <link.h>
446cf2
+#include <support/support.h>
446cf2
+
446cf2
+unsigned int
446cf2
+la_version (unsigned int v)
446cf2
+{
446cf2
+  write_message ("info: tst-auditlogmod-1.so loaded\n");
446cf2
+  return LAV_CURRENT;
446cf2
+}
446cf2
diff --git a/elf/tst-auditlogmod-2.c b/elf/tst-auditlogmod-2.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..871c22641c47fed0
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-auditlogmod-2.c
446cf2
@@ -0,0 +1,27 @@
446cf2
+/* Audit module which logs that it was loaded.  Variant 2.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <link.h>
446cf2
+#include <support/support.h>
446cf2
+
446cf2
+unsigned int
446cf2
+la_version (unsigned int v)
446cf2
+{
446cf2
+  write_message ("info: tst-auditlogmod-2.so loaded\n");
446cf2
+  return LAV_CURRENT;
446cf2
+}
446cf2
diff --git a/elf/tst-auditlogmod-3.c b/elf/tst-auditlogmod-3.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..da2ee19d4ab9e861
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-auditlogmod-3.c
446cf2
@@ -0,0 +1,27 @@
446cf2
+/* Audit module which logs that it was loaded.  Variant 3.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <link.h>
446cf2
+#include <support/support.h>
446cf2
+
446cf2
+unsigned int
446cf2
+la_version (unsigned int v)
446cf2
+{
446cf2
+  write_message ("info: tst-auditlogmod-3.so loaded\n");
446cf2
+  return LAV_CURRENT;
446cf2
+}