Revert "elf: Refuse to dlopen PIE objects [BZ #24323]"
[platform/upstream/glibc.git] / elf / tst-audit25b.c
1 /* Check LD_AUDIT and LD_BIND_NOW.
2    Copyright (C) 2022-2024 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 <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <support/capture_subprocess.h>
27 #include <support/check.h>
28 #include <support/xstdio.h>
29 #include <support/support.h>
30 #include <sys/auxv.h>
31
32 #include "tst-audit25.h"
33
34 static int restart;
35 #define CMDLINE_OPTIONS \
36   { "restart", no_argument, &restart, 1 },
37
38 void tst_audit25mod1_func1 (void);
39 void tst_audit25mod1_func2 (void);
40 void tst_audit25mod2_func1 (void);
41 void tst_audit25mod2_func2 (void);
42
43 static int
44 handle_restart (void)
45 {
46   tst_audit25mod1_func1 ();
47   tst_audit25mod1_func2 ();
48   tst_audit25mod2_func1 ();
49   tst_audit25mod2_func2 ();
50
51   return 0;
52 }
53
54 static int
55 do_test (int argc, char *argv[])
56 {
57   /* We must have either:
58      - One or four parameters left if called initially:
59        + path to ld.so         optional
60        + "--library-path"      optional
61        + the library path      optional
62        + the application name  */
63
64   if (restart)
65     return handle_restart ();
66
67   setenv ("LD_AUDIT", "tst-auditmod25.so", 0);
68
69   char *spargv[9];
70   int i = 0;
71   for (; i < argc - 1; i++)
72     spargv[i] = argv[i + 1];
73   spargv[i++] = (char *) "--direct";
74   spargv[i++] = (char *) "--restart";
75   spargv[i] = NULL;
76
77   {
78     struct support_capture_subprocess result
79       = support_capture_subprogram (spargv[0], spargv);
80     support_capture_subprocess_check (&result, "tst-audit25a", 0,
81                                       sc_allow_stderr);
82
83     /* tst-audit25a and tst-audit25mod1 are built with -Wl,-z,now, but
84        tst-audit25mod2 is built with -Wl,-z,lazy.  So only
85        tst_audit25mod4_func1 (called by tst_audit25mod2_func1) should not
86        have LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  */
87     const char *expected[] =
88       {
89         "la_symbind: tst_audit25mod3_func1 1\n",
90         "la_symbind: tst_audit25mod1_func1 1\n",
91         "la_symbind: tst_audit25mod2_func1 1\n",
92         "la_symbind: tst_audit25mod1_func2 1\n",
93         "la_symbind: tst_audit25mod2_func2 1\n",
94         "la_symbind: tst_audit25mod4_func1 0\n",
95       };
96     compare_output (result.err.buffer, result.err.length,
97                     expected, array_length(expected));
98
99     support_capture_subprocess_free (&result);
100   }
101
102   {
103     setenv ("LD_BIND_NOW", "1", 0);
104     struct support_capture_subprocess result
105       = support_capture_subprogram (spargv[0], spargv);
106     support_capture_subprocess_check (&result, "tst-audit25a", 0,
107                                       sc_allow_stderr);
108
109     /* With LD_BIND_NOW all symbols are expected to have
110        LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT.  Also the resolution
111        order is done in breadth-first order.  */
112     const char *expected[] =
113       {
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     compare_output (result.err.buffer, result.err.length,
122                     expected, array_length(expected));
123
124     support_capture_subprocess_free (&result);
125   }
126
127   return 0;
128 }
129
130 #define TEST_FUNCTION_ARGV do_test
131 #include <support/test-driver.c>