1From 7120e38a3d1b3a3fb0d687a0f811f54beaf90d09 Mon Sep 17 00:00:00 2001
2From: Florian Weimer <fweimer@redhat.com>
3Date: Sat, 2 Nov 2019 21:55:35 +0100
4Subject: [PATCH 12/20] Properly initialize audit cookie for the dynamic loader
5 [BZ #25157]
6
7The l_audit array is indexed by audit module, not audit function.
8
9Change-Id: I180eb3573dc1c57433750f5d8cb18271460ba5f2
10(cherry picked from commit c7bf5ceab6ec776ac7350d3b0190776bf532ac54)
11Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
12---
13 elf/rtld.c              | 14 ++++-----
14 elf/tst-auditmany.c     | 26 +++++++++++++++++
15 elf/tst-auditmanymod.h  | 64 +++++++++++++++++++++++++++++++++++++++++
16 elf/tst-auditmanymod1.c | 20 +++++++++++++
17 elf/tst-auditmanymod2.c | 20 +++++++++++++
18 elf/tst-auditmanymod3.c | 20 +++++++++++++
19 elf/tst-auditmanymod4.c | 20 +++++++++++++
20 elf/tst-auditmanymod5.c | 20 +++++++++++++
21 elf/tst-auditmanymod6.c | 20 +++++++++++++
22 elf/tst-auditmanymod7.c | 20 +++++++++++++
23 elf/tst-auditmanymod8.c | 20 +++++++++++++
24 elf/tst-auditmanymod9.c | 20 +++++++++++++
25 12 files changed, 277 insertions(+), 7 deletions(-)
26 create mode 100644 elf/tst-auditmany.c
27 create mode 100644 elf/tst-auditmanymod.h
28 create mode 100644 elf/tst-auditmanymod1.c
29 create mode 100644 elf/tst-auditmanymod2.c
30 create mode 100644 elf/tst-auditmanymod3.c
31 create mode 100644 elf/tst-auditmanymod4.c
32 create mode 100644 elf/tst-auditmanymod5.c
33 create mode 100644 elf/tst-auditmanymod6.c
34 create mode 100644 elf/tst-auditmanymod7.c
35 create mode 100644 elf/tst-auditmanymod8.c
36 create mode 100644 elf/tst-auditmanymod9.c
37
38diff --git a/elf/rtld.c b/elf/rtld.c
39index 10a08ec5..63525ad5 100644
40--- a/elf/rtld.c
41+++ b/elf/rtld.c
42@@ -990,13 +990,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
43
44       /* Store the pointer.  */
45       if (err_str == NULL && largs.result != NULL)
46-	{
47-	  newp->fptr[cnt] = largs.result;
48-
49-	  /* The dynamic linker link map is statically allocated,
50-	     initialize the data now.  */
51-	  GL(dl_rtld_map).l_audit[cnt].cookie = (intptr_t) &GL(dl_rtld_map);
52-	}
53+	newp->fptr[cnt] = largs.result;
54       else
55 	newp->fptr[cnt] = NULL;
56       ++cnt;
57@@ -1012,6 +1006,12 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
58     *last_audit = GLRO(dl_audit) = &newp->ifaces;
59   else
60     *last_audit = (*last_audit)->next = &newp->ifaces;
61+
62+  /* The dynamic linker link map is statically allocated, initialize
63+     the data now.  */
64+  GL (dl_rtld_map).l_audit[GLRO (dl_naudit)].cookie
65+    = (intptr_t) &GL (dl_rtld_map);
66+
67   ++GLRO(dl_naudit);
68
69   /* Mark the DSO as being used for auditing.  */
70diff --git a/elf/tst-auditmany.c b/elf/tst-auditmany.c
71new file mode 100644
72index 00000000..9d68105b
73--- /dev/null
74+++ b/elf/tst-auditmany.c
75@@ -0,0 +1,26 @@
76+/* Check cookie initialization for many auditors.  Main program.
77+   Copyright (C) 2019 Free Software Foundation, Inc.
78+   This file is part of the GNU C Library.
79+
80+   The GNU C Library is free software; you can redistribute it and/or
81+   modify it under the terms of the GNU Lesser General Public
82+   License as published by the Free Software Foundation; either
83+   version 2.1 of the License, or (at your option) any later version.
84+
85+   The GNU C Library is distributed in the hope that it will be useful,
86+   but WITHOUT ANY WARRANTY; without even the implied warranty of
87+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
88+   Lesser General Public License for more details.
89+
90+   You should have received a copy of the GNU Lesser General Public
91+   License along with the GNU C Library; if not, see
92+   <https://www.gnu.org/licenses/>.  */
93+
94+/* It does not make sense to use the test harness for this test
95+   because the testing happens in auditors.  */
96+
97+int
98+main (void)
99+{
100+  return 0;
101+}
102diff --git a/elf/tst-auditmanymod.h b/elf/tst-auditmanymod.h
103new file mode 100644
104index 00000000..d1d89e08
105--- /dev/null
106+++ b/elf/tst-auditmanymod.h
107@@ -0,0 +1,64 @@
108+/* Check cookie initialization for many auditors.  Auditor template.
109+   Copyright (C) 2019 Free Software Foundation, Inc.
110+   This file is part of the GNU C Library.
111+
112+   The GNU C Library is free software; you can redistribute it and/or
113+   modify it under the terms of the GNU Lesser General Public
114+   License as published by the Free Software Foundation; either
115+   version 2.1 of the License, or (at your option) any later version.
116+
117+   The GNU C Library is distributed in the hope that it will be useful,
118+   but WITHOUT ANY WARRANTY; without even the implied warranty of
119+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
120+   Lesser General Public License for more details.
121+
122+   You should have received a copy of the GNU Lesser General Public
123+   License along with the GNU C Library; if not, see
124+   <https://www.gnu.org/licenses/>.  */
125+
126+/* The macro MOD must be defined to the number of this auditor (an
127+   integer) before including this file.  */
128+
129+#include <link.h>
130+#include <stdio.h>
131+#include <unistd.h>
132+
133+/* Error counter for delayed error reporting.  */
134+static int errors;
135+
136+unsigned int
137+la_version (unsigned int version)
138+{
139+  return version;
140+}
141+
142+unsigned int
143+la_objopen (struct link_map *map, Lmid_t lmid,
144+            uintptr_t *cookie)
145+{
146+  struct link_map *cookie_map = (struct link_map *) *cookie;
147+  printf ("info: %d, la_objopen: map=%p name=%s cookie=%p:%p diff=%td\n",
148+          MOD, map, map->l_name, cookie, cookie_map,
149+          (char *) cookie - (char *) map);
150+  fflush (stdout);
151+  if (map != cookie_map)
152+    {
153+      printf ("error: %d, la_objopen:"
154+              " map address does not match cookie value\n",
155+              MOD);
156+      fflush (stdout);
157+      ++errors;
158+    }
159+  return 0;
160+}
161+
162+extern unsigned int
163+la_objclose (uintptr_t *__cookie)
164+{
165+  if (errors != 0)
166+    {
167+      printf ("error: exiting due to previous errors");
168+      _exit (1);
169+    }
170+  return 0;
171+}
172diff --git a/elf/tst-auditmanymod1.c b/elf/tst-auditmanymod1.c
173new file mode 100644
174index 00000000..c7de49d4
175--- /dev/null
176+++ b/elf/tst-auditmanymod1.c
177@@ -0,0 +1,20 @@
178+/* Check cookie initialization for many auditors.  Auditor 1.
179+   Copyright (C) 2019 Free Software Foundation, Inc.
180+   This file is part of the GNU C Library.
181+
182+   The GNU C Library is free software; you can redistribute it and/or
183+   modify it under the terms of the GNU Lesser General Public
184+   License as published by the Free Software Foundation; either
185+   version 2.1 of the License, or (at your option) any later version.
186+
187+   The GNU C Library is distributed in the hope that it will be useful,
188+   but WITHOUT ANY WARRANTY; without even the implied warranty of
189+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
190+   Lesser General Public License for more details.
191+
192+   You should have received a copy of the GNU Lesser General Public
193+   License along with the GNU C Library; if not, see
194+   <https://www.gnu.org/licenses/>.  */
195+
196+#define MOD 1
197+#include "tst-auditmanymod.h"
198diff --git a/elf/tst-auditmanymod2.c b/elf/tst-auditmanymod2.c
199new file mode 100644
200index 00000000..4254f022
201--- /dev/null
202+++ b/elf/tst-auditmanymod2.c
203@@ -0,0 +1,20 @@
204+/* Check cookie initialization for many auditors.  Auditor 2.
205+   Copyright (C) 2019 Free Software Foundation, Inc.
206+   This file is part of the GNU C Library.
207+
208+   The GNU C Library is free software; you can redistribute it and/or
209+   modify it under the terms of the GNU Lesser General Public
210+   License as published by the Free Software Foundation; either
211+   version 2.1 of the License, or (at your option) any later version.
212+
213+   The GNU C Library is distributed in the hope that it will be useful,
214+   but WITHOUT ANY WARRANTY; without even the implied warranty of
215+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
216+   Lesser General Public License for more details.
217+
218+   You should have received a copy of the GNU Lesser General Public
219+   License along with the GNU C Library; if not, see
220+   <https://www.gnu.org/licenses/>.  */
221+
222+#define MOD 2
223+#include "tst-auditmanymod.h"
224diff --git a/elf/tst-auditmanymod3.c b/elf/tst-auditmanymod3.c
225new file mode 100644
226index 00000000..ee90f4eb
227--- /dev/null
228+++ b/elf/tst-auditmanymod3.c
229@@ -0,0 +1,20 @@
230+/* Check cookie initialization for many auditors.  Auditor 3.
231+   Copyright (C) 2019 Free Software Foundation, Inc.
232+   This file is part of the GNU C Library.
233+
234+   The GNU C Library is free software; you can redistribute it and/or
235+   modify it under the terms of the GNU Lesser General Public
236+   License as published by the Free Software Foundation; either
237+   version 2.1 of the License, or (at your option) any later version.
238+
239+   The GNU C Library is distributed in the hope that it will be useful,
240+   but WITHOUT ANY WARRANTY; without even the implied warranty of
241+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
242+   Lesser General Public License for more details.
243+
244+   You should have received a copy of the GNU Lesser General Public
245+   License along with the GNU C Library; if not, see
246+   <https://www.gnu.org/licenses/>.  */
247+
248+#define MOD 3
249+#include "tst-auditmanymod.h"
250diff --git a/elf/tst-auditmanymod4.c b/elf/tst-auditmanymod4.c
251new file mode 100644
252index 00000000..6379fa1d
253--- /dev/null
254+++ b/elf/tst-auditmanymod4.c
255@@ -0,0 +1,20 @@
256+/* Check cookie initialization for many auditors.  Auditor 4.
257+   Copyright (C) 2019 Free Software Foundation, Inc.
258+   This file is part of the GNU C Library.
259+
260+   The GNU C Library is free software; you can redistribute it and/or
261+   modify it under the terms of the GNU Lesser General Public
262+   License as published by the Free Software Foundation; either
263+   version 2.1 of the License, or (at your option) any later version.
264+
265+   The GNU C Library is distributed in the hope that it will be useful,
266+   but WITHOUT ANY WARRANTY; without even the implied warranty of
267+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
268+   Lesser General Public License for more details.
269+
270+   You should have received a copy of the GNU Lesser General Public
271+   License along with the GNU C Library; if not, see
272+   <https://www.gnu.org/licenses/>.  */
273+
274+#define MOD 4
275+#include "tst-auditmanymod.h"
276diff --git a/elf/tst-auditmanymod5.c b/elf/tst-auditmanymod5.c
277new file mode 100644
278index 00000000..17c0f617
279--- /dev/null
280+++ b/elf/tst-auditmanymod5.c
281@@ -0,0 +1,20 @@
282+/* Check cookie initialization for many auditors.  Auditor 5.
283+   Copyright (C) 2019 Free Software Foundation, Inc.
284+   This file is part of the GNU C Library.
285+
286+   The GNU C Library is free software; you can redistribute it and/or
287+   modify it under the terms of the GNU Lesser General Public
288+   License as published by the Free Software Foundation; either
289+   version 2.1 of the License, or (at your option) any later version.
290+
291+   The GNU C Library is distributed in the hope that it will be useful,
292+   but WITHOUT ANY WARRANTY; without even the implied warranty of
293+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
294+   Lesser General Public License for more details.
295+
296+   You should have received a copy of the GNU Lesser General Public
297+   License along with the GNU C Library; if not, see
298+   <https://www.gnu.org/licenses/>.  */
299+
300+#define MOD 5
301+#include "tst-auditmanymod.h"
302diff --git a/elf/tst-auditmanymod6.c b/elf/tst-auditmanymod6.c
303new file mode 100644
304index 00000000..86bc6801
305--- /dev/null
306+++ b/elf/tst-auditmanymod6.c
307@@ -0,0 +1,20 @@
308+/* Check cookie initialization for many auditors.  Auditor 6.
309+   Copyright (C) 2019 Free Software Foundation, Inc.
310+   This file is part of the GNU C Library.
311+
312+   The GNU C Library is free software; you can redistribute it and/or
313+   modify it under the terms of the GNU Lesser General Public
314+   License as published by the Free Software Foundation; either
315+   version 2.1 of the License, or (at your option) any later version.
316+
317+   The GNU C Library is distributed in the hope that it will be useful,
318+   but WITHOUT ANY WARRANTY; without even the implied warranty of
319+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
320+   Lesser General Public License for more details.
321+
322+   You should have received a copy of the GNU Lesser General Public
323+   License along with the GNU C Library; if not, see
324+   <https://www.gnu.org/licenses/>.  */
325+
326+#define MOD 6
327+#include "tst-auditmanymod.h"
328diff --git a/elf/tst-auditmanymod7.c b/elf/tst-auditmanymod7.c
329new file mode 100644
330index 00000000..92b0bf60
331--- /dev/null
332+++ b/elf/tst-auditmanymod7.c
333@@ -0,0 +1,20 @@
334+/* Check cookie initialization for many auditors.  Auditor 7.
335+   Copyright (C) 2019 Free Software Foundation, Inc.
336+   This file is part of the GNU C Library.
337+
338+   The GNU C Library is free software; you can redistribute it and/or
339+   modify it under the terms of the GNU Lesser General Public
340+   License as published by the Free Software Foundation; either
341+   version 2.1 of the License, or (at your option) any later version.
342+
343+   The GNU C Library is distributed in the hope that it will be useful,
344+   but WITHOUT ANY WARRANTY; without even the implied warranty of
345+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
346+   Lesser General Public License for more details.
347+
348+   You should have received a copy of the GNU Lesser General Public
349+   License along with the GNU C Library; if not, see
350+   <https://www.gnu.org/licenses/>.  */
351+
352+#define MOD 7
353+#include "tst-auditmanymod.h"
354diff --git a/elf/tst-auditmanymod8.c b/elf/tst-auditmanymod8.c
355new file mode 100644
356index 00000000..d42f884d
357--- /dev/null
358+++ b/elf/tst-auditmanymod8.c
359@@ -0,0 +1,20 @@
360+/* Check cookie initialization for many auditors.  Auditor 8.
361+   Copyright (C) 2019 Free Software Foundation, Inc.
362+   This file is part of the GNU C Library.
363+
364+   The GNU C Library is free software; you can redistribute it and/or
365+   modify it under the terms of the GNU Lesser General Public
366+   License as published by the Free Software Foundation; either
367+   version 2.1 of the License, or (at your option) any later version.
368+
369+   The GNU C Library is distributed in the hope that it will be useful,
370+   but WITHOUT ANY WARRANTY; without even the implied warranty of
371+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
372+   Lesser General Public License for more details.
373+
374+   You should have received a copy of the GNU Lesser General Public
375+   License along with the GNU C Library; if not, see
376+   <https://www.gnu.org/licenses/>.  */
377+
378+#define MOD 8
379+#include "tst-auditmanymod.h"
380diff --git a/elf/tst-auditmanymod9.c b/elf/tst-auditmanymod9.c
381new file mode 100644
382index 00000000..6bee81d6
383--- /dev/null
384+++ b/elf/tst-auditmanymod9.c
385@@ -0,0 +1,20 @@
386+/* Check cookie initialization for many auditors.  Auditor 9.
387+   Copyright (C) 2019 Free Software Foundation, Inc.
388+   This file is part of the GNU C Library.
389+
390+   The GNU C Library is free software; you can redistribute it and/or
391+   modify it under the terms of the GNU Lesser General Public
392+   License as published by the Free Software Foundation; either
393+   version 2.1 of the License, or (at your option) any later version.
394+
395+   The GNU C Library is distributed in the hope that it will be useful,
396+   but WITHOUT ANY WARRANTY; without even the implied warranty of
397+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
398+   Lesser General Public License for more details.
399+
400+   You should have received a copy of the GNU Lesser General Public
401+   License along with the GNU C Library; if not, see
402+   <https://www.gnu.org/licenses/>.  */
403+
404+#define MOD 9
405+#include "tst-auditmanymod.h"
406--
4072.20.1
408
409