1/* Check LD_AUDIT and LD_BIND_NOW.
2 Copyright (C) 2022 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 <errno.h>
20#include <getopt.h>
21#include <limits.h>
22#include <inttypes.h>
23#include <string.h>
24#include <stdlib.h>
25#include <support/capture_subprocess.h>
26#include <support/check.h>
27#include <support/xstdio.h>
28#include <support/support.h>
29#include <sys/auxv.h>
30
31static int restart;
32#define CMDLINE_OPTIONS \
33 { "restart", no_argument, &restart, 1 },
34
35void tst_audit25mod1_func1 (void);
36void tst_audit25mod1_func2 (void);
37void tst_audit25mod2_func1 (void);
38void tst_audit25mod2_func2 (void);
39
40static int
41handle_restart (void)
42{
43 tst_audit25mod1_func1 ();
44 tst_audit25mod1_func2 ();
45 tst_audit25mod2_func1 ();
46 tst_audit25mod2_func2 ();
47
48 return 0;
49}
50
51static inline bool
52startswith (const char *str, const char *pre)
53{
54 size_t lenpre = strlen (s: pre);
55 size_t lenstr = strlen (s: str);
56 return lenstr >= lenpre && memcmp (s1: pre, s2: str, n: lenpre) == 0;
57}
58
59static int
60do_test (int argc, char *argv[])
61{
62 /* We must have either:
63 - One or four 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 setenv (name: "LD_AUDIT", value: "tst-auditmod25.so", replace: 0);
73
74 char *spargv[9];
75 int i = 0;
76 for (; i < argc - 1; i++)
77 spargv[i] = argv[i + 1];
78 spargv[i++] = (char *) "--direct";
79 spargv[i++] = (char *) "--restart";
80 spargv[i] = NULL;
81
82 {
83 struct support_capture_subprocess result
84 = support_capture_subprogram (file: spargv[0], argv: spargv);
85 support_capture_subprocess_check (&result, context: "tst-audit25a", status_or_signal: 0,
86 allowed: sc_allow_stderr);
87
88 /* tst-audit25a and tst-audit25mod1 are built with -Wl,-z,now, but
89 tst-audit25mod2 is built with -Wl,-z,lazy. So only
90 tst_audit25mod4_func1 (called by tst_audit25mod2_func1) should not
91 have LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT. */
92 TEST_COMPARE_STRING (result.err.buffer,
93 "la_symbind: tst_audit25mod3_func1 1\n"
94 "la_symbind: tst_audit25mod1_func1 1\n"
95 "la_symbind: tst_audit25mod2_func1 1\n"
96 "la_symbind: tst_audit25mod1_func2 1\n"
97 "la_symbind: tst_audit25mod2_func2 1\n"
98 "la_symbind: tst_audit25mod4_func1 0\n");
99
100 support_capture_subprocess_free (&result);
101 }
102
103 {
104 setenv (name: "LD_BIND_NOW", value: "1", replace: 0);
105 struct support_capture_subprocess result
106 = support_capture_subprogram (file: spargv[0], argv: spargv);
107 support_capture_subprocess_check (&result, context: "tst-audit25a", status_or_signal: 0,
108 allowed: sc_allow_stderr);
109
110 /* With LD_BIND_NOW all symbols are expected to have
111 LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT. Also the resolution
112 order is done in breadth-first order. */
113 TEST_COMPARE_STRING (result.err.buffer,
114 "la_symbind: tst_audit25mod4_func1 1\n"
115 "la_symbind: tst_audit25mod3_func1 1\n"
116 "la_symbind: tst_audit25mod1_func1 1\n"
117 "la_symbind: tst_audit25mod2_func1 1\n"
118 "la_symbind: tst_audit25mod1_func2 1\n"
119 "la_symbind: tst_audit25mod2_func2 1\n");
120
121 support_capture_subprocess_free (&result);
122 }
123
124 return 0;
125}
126
127#define TEST_FUNCTION_ARGV do_test
128#include <support/test-driver.c>
129

source code of glibc/elf/tst-audit25b.c