1 /* Check GLIBC_TUNABLES parsing for enable_secure.
2 Copyright (C) 2024-2025 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>
29 #include <support/capture_subprocess.h>
30 #include <support/check.h>
35 #define CMDLINE_OPTIONS \
36 { "restart", no_argument, &restart, 1 },
38 static const struct test_t
43 int32_t expected_malloc_check
;
44 int32_t expected_enable_secure
;
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",
56 /* Tunables should be ignored if enable_secure is set. */
58 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
64 /* Tunables should be set if enable_secure is unset. */
66 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2",
72 /* Tunables should be ignored if enable_secure is set. */
74 "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1",
80 /* Same as before, but with enviroment alias prior GLIBC_TUNABLES. */
83 "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1",
88 /* Tunables should be ignored if enable_secure is set. */
90 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
98 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
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. */
114 "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2",
115 /* Tunable have precedence over the environment variable. */
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. */
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. */
138 /* Check with tunables environment variable alias set multiple times. */
140 "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2",
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. */
158 handle_restart (int i
)
160 if (tests
[i
].expected_enable_secure
== 1)
161 TEST_COMPARE (1, __libc_enable_secure
);
163 TEST_COMPARE (tests
[i
].expected_enable_secure
,
164 TUNABLE_GET_FULL (glibc
, rtld
, enable_secure
, int32_t,
166 TEST_COMPARE (tests
[i
].expected_malloc_check
,
167 TUNABLE_GET_FULL (glibc
, malloc
, check
, int32_t, NULL
));
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);
185 return handle_restart (atoi (argv
[1]));
187 char nteststr
[INT_BUFSIZE_BOUND (int)];
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
;
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
,
215 if (tests
[i
].check_multiple
)
218 for (j
=0; j
< tunable_num_env_alias
; j
++)
219 envp
[j
+ 2] = (char *) tests
[i
].extraenv
;
223 struct support_capture_subprocess result
224 = support_capture_subprogram (spargv
[0], spargv
, envp
);
225 support_capture_subprocess_check (&result
, "tst-tunables-enable_secure",
227 support_capture_subprocess_free (&result
);
233 #define TEST_FUNCTION_ARGV do_test
234 #include <support/test-driver.c>