Skip to content

Commit ed3ce71

Browse files
committed
elf: Move la_activity (LA_ACT_ADD) after _dl_add_to_namespace_list() (BZ #28062)
It ensures that the the namespace is guaranteed to not be empty. Checked on x86_64-linux-gnu. Reviewed-by: Florian Weimer <[email protected]>
1 parent bdeb7a8 commit ed3ce71

File tree

5 files changed

+271
-37
lines changed

5 files changed

+271
-37
lines changed

elf/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
229229
tst-tls-ie tst-tls-ie-dlmopen argv0test \
230230
tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
231231
tst-tls20 tst-tls21 tst-dlmopen-dlerror tst-dlmopen-gethostbyname \
232-
tst-dl-is_dso tst-ro-dynamic
232+
tst-dl-is_dso tst-ro-dynamic \
233+
tst-audit18 \
233234
# reldep9
234235
tests-internal += loadtest unload unload2 circleload1 \
235236
neededtest neededtest2 neededtest3 neededtest4 \
@@ -370,6 +371,8 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
370371
tst-tls20mod-bad tst-tls21mod tst-dlmopen-dlerror-mod \
371372
tst-auxvalmod \
372373
tst-dlmopen-gethostbyname-mod tst-ro-dynamic-mod \
374+
tst-auditmod18 \
375+
tst-audit18mod \
373376

374377
# Most modules build with _ISOMAC defined, but those filtered out
375378
# depend on internal headers.
@@ -1543,6 +1546,10 @@ $(objpfx)tst-audit16-cmp.out: tst-audit16.exp $(objpfx)tst-audit16.out
15431546
cmp $^ > $@; \
15441547
$(evaluate-test)
15451548

1549+
$(objpfx)tst-audit18.out: $(objpfx)tst-auditmod18.so \
1550+
$(objpfx)tst-audit18mod.so
1551+
tst-audit18-ARGS = -- $(host-test-program-cmd)
1552+
15461553
# tst-sonamemove links against an older implementation of the library.
15471554
LDFLAGS-tst-sonamemove-linkmod1.so = \
15481555
-Wl,--version-script=tst-sonamemove-linkmod1.map \

elf/dl-load.c

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,42 +1058,6 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
10581058
/* This is the ELF header. We read it in `open_verify'. */
10591059
header = (void *) fbp->buf;
10601060

1061-
/* Signal that we are going to add new objects. */
1062-
if (r->r_state == RT_CONSISTENT)
1063-
{
1064-
#ifdef SHARED
1065-
/* Auditing checkpoint: we are going to add new objects. */
1066-
if ((mode & __RTLD_AUDIT) == 0
1067-
&& __glibc_unlikely (GLRO(dl_naudit) > 0))
1068-
{
1069-
struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
1070-
/* Do not call the functions for any auditing object. */
1071-
if (head->l_auditing == 0)
1072-
{
1073-
struct audit_ifaces *afct = GLRO(dl_audit);
1074-
for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1075-
{
1076-
if (afct->activity != NULL)
1077-
afct->activity (&link_map_audit_state (head, cnt)->cookie,
1078-
LA_ACT_ADD);
1079-
1080-
afct = afct->next;
1081-
}
1082-
}
1083-
}
1084-
#endif
1085-
1086-
/* Notify the debugger we have added some objects. We need to
1087-
call _dl_debug_initialize in a static program in case dynamic
1088-
linking has not been used before. */
1089-
r->r_state = RT_ADD;
1090-
_dl_debug_state ();
1091-
LIBC_PROBE (map_start, 2, nsid, r);
1092-
make_consistent = true;
1093-
}
1094-
else
1095-
assert (r->r_state == RT_ADD);
1096-
10971061
/* Enter the new object in the list of loaded objects. */
10981062
l = _dl_new_object (realname, name, l_type, loader, mode, nsid);
10991063
if (__glibc_unlikely (l == NULL))
@@ -1515,6 +1479,44 @@ cannot enable executable stack as shared object requires");
15151479
/* Now that the object is fully initialized add it to the object list. */
15161480
_dl_add_to_namespace_list (l, nsid);
15171481

1482+
/* Signal that we are going to add new objects. */
1483+
if (r->r_state == RT_CONSISTENT)
1484+
{
1485+
#ifdef SHARED
1486+
/* Auditing checkpoint: we are going to add new objects. Since this
1487+
is called after _dl_add_to_namespace_list the namespace is guaranteed
1488+
to not be empty. */
1489+
if ((mode & __RTLD_AUDIT) == 0
1490+
&& __glibc_unlikely (GLRO(dl_naudit) > 0))
1491+
{
1492+
struct link_map *head = GL(dl_ns)[nsid]._ns_loaded;
1493+
/* Do not call the functions for any auditing object. */
1494+
if (head->l_auditing == 0)
1495+
{
1496+
struct audit_ifaces *afct = GLRO(dl_audit);
1497+
for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1498+
{
1499+
if (afct->activity != NULL)
1500+
afct->activity (&link_map_audit_state (head, cnt)->cookie,
1501+
LA_ACT_ADD);
1502+
1503+
afct = afct->next;
1504+
}
1505+
}
1506+
}
1507+
#endif
1508+
1509+
/* Notify the debugger we have added some objects. We need to
1510+
call _dl_debug_initialize in a static program in case dynamic
1511+
linking has not been used before. */
1512+
r->r_state = RT_ADD;
1513+
_dl_debug_state ();
1514+
LIBC_PROBE (map_start, 2, nsid, r);
1515+
make_consistent = true;
1516+
}
1517+
else
1518+
assert (r->r_state == RT_ADD);
1519+
15181520
#ifdef SHARED
15191521
/* Auditing checkpoint: we have a new object. */
15201522
if (__glibc_unlikely (GLRO(dl_naudit) > 0)

elf/tst-audit18.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Check DT_AUDIT with dlmopen.
2+
Copyright (C) 2021 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<https://www.gnu.org/licenses/>. */
18+
19+
#include <array_length.h>
20+
#include <getopt.h>
21+
#include <string.h>
22+
#include <stdlib.h>
23+
#include <unistd.h>
24+
#include <gnu/lib-names.h>
25+
#include <support/capture_subprocess.h>
26+
#include <support/check.h>
27+
#include <support/xdlfcn.h>
28+
#include <support/xstdio.h>
29+
#include <support/support.h>
30+
31+
static int restart;
32+
#define CMDLINE_OPTIONS \
33+
{ "restart", no_argument, &restart, 1 },
34+
35+
static int
36+
handle_restart (void)
37+
{
38+
{
39+
void *h = xdlmopen (LM_ID_NEWLM, LIBC_SO, RTLD_NOW);
40+
41+
pid_t (*s) (void) = xdlsym (h, "getpid");
42+
TEST_COMPARE (s (), getpid ());
43+
44+
xdlclose (h);
45+
}
46+
47+
{
48+
void *h = xdlmopen (LM_ID_NEWLM, "tst-audit18mod.so", RTLD_NOW);
49+
50+
int (*foo) (void) = xdlsym (h, "foo");
51+
TEST_COMPARE (foo (), 10);
52+
53+
xdlclose (h);
54+
}
55+
56+
return 0;
57+
}
58+
59+
static int
60+
do_test (int argc, char *argv[])
61+
{
62+
/* We must have either:
63+
- One our fource parameters left if called initially:
64+
+ path to ld.so optional
65+
+ "--library-path" optional
66+
+ the library path optional
67+
+ the application name */
68+
69+
if (restart)
70+
return handle_restart ();
71+
72+
char *spargv[9];
73+
int i = 0;
74+
for (; i < argc - 1; i++)
75+
spargv[i] = argv[i + 1];
76+
spargv[i++] = (char *) "--direct";
77+
spargv[i++] = (char *) "--restart";
78+
spargv[i] = NULL;
79+
80+
setenv ("LD_AUDIT", "tst-auditmod18.so", 0);
81+
struct support_capture_subprocess result
82+
= support_capture_subprogram (spargv[0], spargv);
83+
support_capture_subprocess_check (&result, "tst-audit18", 0, sc_allow_stderr);
84+
85+
struct
86+
{
87+
const char *name;
88+
bool found;
89+
} audit_iface[] =
90+
{
91+
{ "la_version", false },
92+
{ "la_objsearch", false },
93+
{ "la_activity", false },
94+
{ "la_objopen", false },
95+
{ "la_objclose", false },
96+
{ "la_preinit", false },
97+
#if __WORDSIZE == 32
98+
{ "la_symbind32", false },
99+
#elif __WORDSIZE == 64
100+
{ "la_symbind64", false },
101+
#endif
102+
};
103+
104+
/* Some hooks are called more than once but the test only check if any
105+
is called at least once. */
106+
FILE *out = fmemopen (result.err.buffer, result.err.length, "r");
107+
TEST_VERIFY (out != NULL);
108+
char *buffer = NULL;
109+
size_t buffer_length = 0;
110+
while (xgetline (&buffer, &buffer_length, out))
111+
{
112+
for (int i = 0; i < array_length (audit_iface); i++)
113+
if (strncmp (buffer, audit_iface[i].name,
114+
strlen (audit_iface[i].name)) == 0)
115+
audit_iface[i].found = true;
116+
}
117+
free (buffer);
118+
xfclose (out);
119+
120+
for (int i = 0; i < array_length (audit_iface); i++)
121+
TEST_COMPARE (audit_iface[i].found, true);
122+
123+
support_capture_subprocess_free (&result);
124+
125+
return 0;
126+
}
127+
128+
#define TEST_FUNCTION_ARGV do_test
129+
#include <support/test-driver.c>

elf/tst-audit18mod.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Check DT_AUDIT with dlmopen.
2+
Copyright (C) 2021 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<https://www.gnu.org/licenses/>. */
18+
19+
int
20+
foo (void)
21+
{
22+
return 10;
23+
}

elf/tst-auditmod18.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Check DT_AUDIT with dlmopen.
2+
Copyright (C) 2021 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<https://www.gnu.org/licenses/>. */
18+
19+
#include <stdio.h>
20+
#include <link.h>
21+
22+
unsigned int
23+
la_version (unsigned int version)
24+
{
25+
fprintf (stderr, "%s\n", __func__);
26+
return LAV_CURRENT;
27+
}
28+
29+
char *
30+
la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
31+
{
32+
fprintf (stderr, "%s\n", __func__);
33+
return (char *) name;
34+
}
35+
36+
void
37+
la_activity (uintptr_t *cookie, unsigned int flag)
38+
{
39+
fprintf (stderr, "%s\n", __func__);
40+
}
41+
42+
unsigned int
43+
la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
44+
{
45+
fprintf (stderr, "%s\n", __func__);
46+
return LA_FLG_BINDTO | LA_FLG_BINDFROM;
47+
}
48+
49+
unsigned int
50+
la_objclose (uintptr_t *cookie)
51+
{
52+
fprintf (stderr, "%s\n", __func__);
53+
return 0;
54+
}
55+
56+
void
57+
la_preinit (uintptr_t *cookie)
58+
{
59+
fprintf (stderr, "%s\n", __func__);
60+
}
61+
62+
uintptr_t
63+
#if __ELF_NATIVE_CLASS == 32
64+
la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
65+
uintptr_t *defcook, unsigned int *flags, const char *symname)
66+
#else
67+
la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
68+
uintptr_t *defcook, unsigned int *flags, const char *symname)
69+
#endif
70+
{
71+
fprintf (stderr, "%s\n", __func__);
72+
return sym->st_value;
73+
}

0 commit comments

Comments
 (0)