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