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