e354a5
commit c7bf5ceab6ec776ac7350d3b0190776bf532ac54
e354a5
Author: Florian Weimer <fweimer@redhat.com>
e354a5
Date:   Sat Nov 2 21:55:35 2019 +0100
e354a5
e354a5
    Properly initialize audit cookie for the dynamic loader [BZ #25157]
e354a5
    
e354a5
    The l_audit array is indexed by audit module, not audit function.
e354a5
    
e354a5
    Change-Id: I180eb3573dc1c57433750f5d8cb18271460ba5f2
e354a5
e354a5
Conflicts:
e354a5
	elf/Makefile
e354a5
	  (Test backport differences.)
e354a5
e354a5
diff --git a/elf/Makefile b/elf/Makefile
e354a5
index 4e1356b9172aee02..4ab73dc48d9ac126 100644
e354a5
--- a/elf/Makefile
e354a5
+++ b/elf/Makefile
e354a5
@@ -192,7 +192,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
e354a5
 	 tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \
e354a5
 	 tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note \
e354a5
 	 tst-audit13 \
e354a5
-	 tst-sonamemove-link tst-sonamemove-dlopen tst-initfinilazyfail \
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
 #	 reldep9
e354a5
@@ -303,6 +304,9 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
e354a5
 		tst-absolute-zero-lib tst-big-note-lib \
e354a5
 		tst-audit13mod1 tst-sonamemove-linkmod1 \
e354a5
 		tst-sonamemove-runmod1 tst-sonamemove-runmod2 \
e354a5
+		tst-auditmanymod1 tst-auditmanymod2 tst-auditmanymod3 \
e354a5
+		tst-auditmanymod4 tst-auditmanymod5 tst-auditmanymod6 \
e354a5
+		tst-auditmanymod7 tst-auditmanymod8 tst-auditmanymod9 \
e354a5
 		tst-initlazyfailmod tst-finilazyfailmod \
e354a5
 		tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2 \
e354a5
 		tst-dlopenfailmod3 \
e354a5
@@ -1433,6 +1437,14 @@ $(objpfx)tst-audit13.out: $(objpfx)tst-audit13mod1.so
e354a5
 LDFLAGS-tst-audit13mod1.so = -Wl,-z,lazy
e354a5
 tst-audit13-ENV = LD_AUDIT=$(objpfx)tst-audit13mod1.so
e354a5
 
e354a5
+$(objpfx)tst-auditmany.out: $(objpfx)tst-auditmanymod1.so \
e354a5
+  $(objpfx)tst-auditmanymod2.so $(objpfx)tst-auditmanymod3.so \
e354a5
+  $(objpfx)tst-auditmanymod4.so $(objpfx)tst-auditmanymod5.so \
e354a5
+  $(objpfx)tst-auditmanymod6.so $(objpfx)tst-auditmanymod7.so \
e354a5
+  $(objpfx)tst-auditmanymod8.so	$(objpfx)tst-auditmanymod9.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
 # 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 ffbd8f4553bb3425..f557f39a70669c09 100644
e354a5
--- a/elf/rtld.c
e354a5
+++ b/elf/rtld.c
e354a5
@@ -1008,13 +1008,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
e354a5
 
e354a5
       /* Store the pointer.  */
e354a5
       if (err_str == NULL && largs.result != NULL)
e354a5
-	{
e354a5
-	  newp->fptr[cnt] = largs.result;
e354a5
-
e354a5
-	  /* The dynamic linker link map is statically allocated,
e354a5
-	     initialize the data now.  */
e354a5
-	  GL(dl_rtld_map).l_audit[cnt].cookie = (intptr_t) &GL(dl_rtld_map);
e354a5
-	}
e354a5
+	newp->fptr[cnt] = largs.result;
e354a5
       else
e354a5
 	newp->fptr[cnt] = NULL;
e354a5
       ++cnt;
e354a5
@@ -1030,6 +1024,12 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
e354a5
     *last_audit = GLRO(dl_audit) = &newp->ifaces;
e354a5
   else
e354a5
     *last_audit = (*last_audit)->next = &newp->ifaces;
e354a5
+
e354a5
+  /* The dynamic linker link map is statically allocated, initialize
e354a5
+     the data now.  */
e354a5
+  GL (dl_rtld_map).l_audit[GLRO (dl_naudit)].cookie
e354a5
+    = (intptr_t) &GL (dl_rtld_map);
e354a5
+
e354a5
   ++GLRO(dl_naudit);
e354a5
 
e354a5
   /* Mark the DSO as being used for auditing.  */
e354a5
diff --git a/elf/tst-auditmany.c b/elf/tst-auditmany.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..9d68105b9e707b46
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmany.c
e354a5
@@ -0,0 +1,26 @@
e354a5
+/* Check cookie initialization for many auditors.  Main program.
e354a5
+   Copyright (C) 2019 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
+/* It does not make sense to use the test harness for this test
e354a5
+   because the testing happens in auditors.  */
e354a5
+
e354a5
+int
e354a5
+main (void)
e354a5
+{
e354a5
+  return 0;
e354a5
+}
e354a5
diff --git a/elf/tst-auditmanymod.h b/elf/tst-auditmanymod.h
e354a5
new file mode 100644
e354a5
index 0000000000000000..d1d89e08431ce32f
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod.h
e354a5
@@ -0,0 +1,64 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor template.
e354a5
+   Copyright (C) 2019 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
+/* The macro MOD must be defined to the number of this auditor (an
e354a5
+   integer) before including this file.  */
e354a5
+
e354a5
+#include <link.h>
e354a5
+#include <stdio.h>
e354a5
+#include <unistd.h>
e354a5
+
e354a5
+/* Error counter for delayed error reporting.  */
e354a5
+static int errors;
e354a5
+
e354a5
+unsigned int
e354a5
+la_version (unsigned int version)
e354a5
+{
e354a5
+  return version;
e354a5
+}
e354a5
+
e354a5
+unsigned int
e354a5
+la_objopen (struct link_map *map, Lmid_t lmid,
e354a5
+            uintptr_t *cookie)
e354a5
+{
e354a5
+  struct link_map *cookie_map = (struct link_map *) *cookie;
e354a5
+  printf ("info: %d, la_objopen: map=%p name=%s cookie=%p:%p diff=%td\n",
e354a5
+          MOD, map, map->l_name, cookie, cookie_map,
e354a5
+          (char *) cookie - (char *) map);
e354a5
+  fflush (stdout);
e354a5
+  if (map != cookie_map)
e354a5
+    {
e354a5
+      printf ("error: %d, la_objopen:"
e354a5
+              " map address does not match cookie value\n",
e354a5
+              MOD);
e354a5
+      fflush (stdout);
e354a5
+      ++errors;
e354a5
+    }
e354a5
+  return 0;
e354a5
+}
e354a5
+
e354a5
+extern unsigned int
e354a5
+la_objclose (uintptr_t *__cookie)
e354a5
+{
e354a5
+  if (errors != 0)
e354a5
+    {
e354a5
+      printf ("error: exiting due to previous errors");
e354a5
+      _exit (1);
e354a5
+    }
e354a5
+  return 0;
e354a5
+}
e354a5
diff --git a/elf/tst-auditmanymod1.c b/elf/tst-auditmanymod1.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..c7de49d446a7e52d
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod1.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 1.
e354a5
+   Copyright (C) 2019 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
+#define MOD 1
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod2.c b/elf/tst-auditmanymod2.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..4254f022a177b844
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod2.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 2.
e354a5
+   Copyright (C) 2019 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
+#define MOD 2
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod3.c b/elf/tst-auditmanymod3.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..ee90f4eb3a5c1b35
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod3.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 3.
e354a5
+   Copyright (C) 2019 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
+#define MOD 3
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod4.c b/elf/tst-auditmanymod4.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..6379fa1d55014998
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod4.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 4.
e354a5
+   Copyright (C) 2019 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
+#define MOD 4
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod5.c b/elf/tst-auditmanymod5.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..17c0f617aa4d1893
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod5.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 5.
e354a5
+   Copyright (C) 2019 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
+#define MOD 5
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod6.c b/elf/tst-auditmanymod6.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..86bc6801a4454742
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod6.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 6.
e354a5
+   Copyright (C) 2019 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
+#define MOD 6
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod7.c b/elf/tst-auditmanymod7.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..92b0bf6006876dff
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod7.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 7.
e354a5
+   Copyright (C) 2019 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
+#define MOD 7
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod8.c b/elf/tst-auditmanymod8.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..d42f884d2f24f4c0
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod8.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 8.
e354a5
+   Copyright (C) 2019 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
+#define MOD 8
e354a5
+#include "tst-auditmanymod.h"
e354a5
diff --git a/elf/tst-auditmanymod9.c b/elf/tst-auditmanymod9.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..6bee81d69c6d3c22
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-auditmanymod9.c
e354a5
@@ -0,0 +1,20 @@
e354a5
+/* Check cookie initialization for many auditors.  Auditor 9.
e354a5
+   Copyright (C) 2019 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
+#define MOD 9
e354a5
+#include "tst-auditmanymod.h"