elf: Make glibc.rtld.enable_secure ignore alias environment variables
[glibc.git] / elf / tst-tunables-enable_secure.c
blob99a065e8db53271437d50e23b55e642a9e096bfa
1 /* Check GLIBC_TUNABLES parsing for enable_secure.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <array_length.h>
20 /* The test uses the tunable_env_alias_list size, which is only exported for
21 ld.so. This will result in a copy of tunable_list and
22 tunable_env_alias_list, which is ununsed by the test itself. */
23 #define TUNABLES_INTERNAL 1
24 #include <dl-tunables.h>
25 #include <getopt.h>
26 #include <intprops.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <support/capture_subprocess.h>
30 #include <support/check.h>
31 #include <sys/auxv.h>
32 #include <unistd.h>
34 static int restart;
35 #define CMDLINE_OPTIONS \
36 { "restart", no_argument, &restart, 1 },
38 static const struct test_t
40 const char *env;
41 const char *extraenv;
42 bool check_multiple;
43 int32_t expected_malloc_check;
44 int32_t expected_enable_secure;
45 } tests[] =
47 /* Expected tunable format. */
48 /* Tunables should be ignored if enable_secure is set. */
50 "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1",
51 NULL,
52 false,
56 /* Tunables should be ignored if enable_secure is set. */
58 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
59 NULL,
60 false,
64 /* Tunables should be set if enable_secure is unset. */
66 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2",
67 NULL,
68 false,
72 /* Tunables should be ignored if enable_secure is set. */
74 "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1",
75 "MALLOC_CHECK_=2",
76 false,
80 /* Same as before, but with enviroment alias prior GLIBC_TUNABLES. */
82 "MALLOC_CHECK_=2",
83 "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1",
84 false,
88 /* Tunables should be ignored if enable_secure is set. */
90 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
91 "MALLOC_CHECK_=2",
92 false,
97 "MALLOC_CHECK_=2",
98 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
99 false,
103 /* Tunables should be set if enable_secure is unset. */
105 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2",
106 /* Tunable have precedence over the environment variable. */
107 "MALLOC_CHECK_=1",
108 false,
113 "MALLOC_CHECK_=1",
114 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2",
115 /* Tunable have precedence over the environment variable. */
116 false,
120 /* Tunables should be set if enable_secure is unset. */
122 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0",
123 /* Tunable have precedence over the environment variable. */
124 "MALLOC_CHECK_=1",
125 false,
129 /* Tunables should be set if enable_secure is unset. */
131 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0",
132 /* Tunable have precedence over the environment variable. */
133 "MALLOC_CHECK_=1",
134 false,
138 /* Check with tunables environment variable alias set multiple times. */
140 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
141 "MALLOC_CHECK_=2",
142 true,
146 /* Tunables should be set if enable_secure is unset. */
148 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0",
149 /* Tunable have precedence over the environment variable. */
150 "MALLOC_CHECK_=1",
151 true,
157 static int
158 handle_restart (int i)
160 if (tests[i].expected_enable_secure == 1)
161 TEST_COMPARE (1, __libc_enable_secure);
162 else
163 TEST_COMPARE (tests[i].expected_enable_secure,
164 TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t,
165 NULL));
166 TEST_COMPARE (tests[i].expected_malloc_check,
167 TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL));
168 return 0;
171 static int
172 do_test (int argc, char *argv[])
174 /* We must have either:
175 - One or four parameters left if called initially:
176 + path to ld.so optional
177 + "--library-path" optional
178 + the library path optional
179 + the application name
180 + the test to check */
182 TEST_VERIFY_EXIT (argc == 2 || argc == 5);
184 if (restart)
185 return handle_restart (atoi (argv[1]));
187 char nteststr[INT_BUFSIZE_BOUND (int)];
189 char *spargv[10];
191 int i = 0;
192 for (; i < argc - 1; i++)
193 spargv[i] = argv[i + 1];
194 spargv[i++] = (char *) "--direct";
195 spargv[i++] = (char *) "--restart";
196 spargv[i++] = nteststr;
197 spargv[i] = NULL;
200 enum { tunable_num_env_alias = array_length (tunable_env_alias_list) };
202 for (int i = 0; i < array_length (tests); i++)
204 snprintf (nteststr, sizeof nteststr, "%d", i);
206 printf ("[%d] Spawned test for %s\n", i, tests[i].env);
207 setenv ("GLIBC_TUNABLES", tests[i].env, 1);
209 char *envp[2 + tunable_num_env_alias + 1] =
211 (char *) tests[i].env,
212 (char *) tests[i].extraenv,
213 NULL,
215 if (tests[i].check_multiple)
217 int j;
218 for (j=0; j < tunable_num_env_alias; j++)
219 envp[j + 2] = (char *) tests[i].extraenv;
220 envp[j + 2] = NULL;
223 struct support_capture_subprocess result
224 = support_capture_subprogram (spargv[0], spargv, envp);
225 support_capture_subprocess_check (&result, "tst-tunables-enable_secure",
226 0, sc_allow_stderr);
227 support_capture_subprocess_free (&result);
230 return 0;
233 #define TEST_FUNCTION_ARGV do_test
234 #include <support/test-driver.c>