elf: Make glibc.rtld.enable_secure ignore alias environment variables
[glibc.git] / elf / dl-tunables.c
blob147cc4cf23f5ad24f445f5ac7625c52d5e448691
1 /* The tunable framework. See the README.tunables to know how to use the
2 tunable in a glibc module.
4 Copyright (C) 2016-2024 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
21 /* Mark symbols hidden in static PIE for early self relocation to work. */
22 #if BUILD_PIE_DEFAULT
23 # pragma GCC visibility push(hidden)
24 #endif
25 #include <startup.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <sysdep.h>
31 #include <fcntl.h>
32 #include <ldsodefs.h>
33 #include <array_length.h>
34 #include <dl-minimal-malloc.h>
35 #include <dl-symbol-redir-ifunc.h>
37 #define TUNABLES_INTERNAL 1
38 #include "dl-tunables.h"
40 static char **
41 get_next_env (char **envp, char **name, char **val, char ***prev_envp)
43 while (envp != NULL && *envp != NULL)
45 char **prev = envp;
46 char *envline = *envp++;
47 int len = 0;
49 while (envline[len] != '\0' && envline[len] != '=')
50 len++;
52 /* Just the name and no value, go to the next one. */
53 if (envline[len] == '\0')
54 continue;
56 *name = envline;
57 *val = &envline[len + 1];
58 *prev_envp = prev;
60 return envp;
63 return NULL;
66 static void
67 do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp,
68 const tunable_num_t *minp,
69 const tunable_num_t *maxp)
71 tunable_num_t val, min, max;
73 switch (cur->type.type_code)
75 case TUNABLE_TYPE_STRING:
76 cur->val.strval = valp->strval;
77 cur->initialized = true;
78 return;
79 case TUNABLE_TYPE_INT_32:
80 val = (int32_t) valp->numval;
81 break;
82 case TUNABLE_TYPE_UINT_64:
83 val = (int64_t) valp->numval;
84 break;
85 case TUNABLE_TYPE_SIZE_T:
86 val = (size_t) valp->numval;
87 break;
88 default:
89 __builtin_unreachable ();
92 bool unsigned_cmp = unsigned_tunable_type (cur->type.type_code);
94 min = minp != NULL ? *minp : cur->type.min;
95 max = maxp != NULL ? *maxp : cur->type.max;
97 /* We allow only increasingly restrictive bounds. */
98 if (tunable_val_lt (min, cur->type.min, unsigned_cmp))
99 min = cur->type.min;
101 if (tunable_val_gt (max, cur->type.max, unsigned_cmp))
102 max = cur->type.max;
104 /* Skip both bounds if they're inconsistent. */
105 if (tunable_val_gt (min, max, unsigned_cmp))
107 min = cur->type.min;
108 max = cur->type.max;
111 /* Bail out if the bounds are not valid. */
112 if (tunable_val_lt (val, min, unsigned_cmp)
113 || tunable_val_lt (max, val, unsigned_cmp))
114 return;
116 cur->val.numval = val;
117 cur->type.min = min;
118 cur->type.max = max;
119 cur->initialized = true;
122 static bool
123 tunable_parse_num (const char *strval, size_t len, tunable_num_t *val)
125 char *endptr = NULL;
126 uint64_t numval = _dl_strtoul (strval, &endptr);
127 if (endptr != strval + len)
128 return false;
129 *val = (tunable_num_t) numval;
130 return true;
133 /* Validate range of the input value and initialize the tunable CUR if it looks
134 good. */
135 static bool
136 tunable_initialize (tunable_t *cur, const char *strval, size_t len)
138 tunable_val_t val = { 0 };
140 if (cur->type.type_code != TUNABLE_TYPE_STRING)
142 if (!tunable_parse_num (strval, len, &val.numval))
143 return false;
145 else
146 val.strval = (struct tunable_str_t) { strval, len };
147 do_tunable_update_val (cur, &val, NULL, NULL);
149 return true;
152 bool
153 __tunable_is_initialized (tunable_id_t id)
155 return tunable_list[id].initialized;
157 rtld_hidden_def (__tunable_is_initialized)
159 void
160 __tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp,
161 tunable_num_t *maxp)
163 tunable_t *cur = &tunable_list[id];
165 do_tunable_update_val (cur, valp, minp, maxp);
168 struct tunable_toset_t
170 tunable_t *t;
171 const char *value;
172 size_t len;
175 enum { tunables_list_size = array_length (tunable_list) };
177 /* Parse the tunable string VALSTRING and set TUNABLES with the found tunables
178 and their respective values. The VALSTRING is parsed in place, with the
179 tunable start and size recorded in TUNABLES.
180 Return the number of tunables found (including 0 if the string is empty)
181 or -1 if for an ill-formatted definition. */
182 static int
183 parse_tunables_string (const char *valstring, struct tunable_toset_t *tunables)
185 if (valstring == NULL || *valstring == '\0')
186 return 0;
188 const char *p = valstring;
189 bool done = false;
190 int ntunables = 0;
192 while (!done)
194 const char *name = p;
196 /* First, find where the name ends. */
197 while (*p != '=' && *p != ':' && *p != '\0')
198 p++;
200 /* If we reach the end of the string before getting a valid name-value
201 pair, bail out. */
202 if (*p == '\0')
203 return -1;
205 /* We did not find a valid name-value pair before encountering the
206 colon. */
207 if (*p == ':')
209 p++;
210 continue;
213 /* Skip the '='. */
214 p++;
216 const char *value = p;
218 while (*p != '=' && *p != ':' && *p != '\0')
219 p++;
221 if (*p == '=')
222 return -1;
223 else if (*p == '\0')
224 done = true;
226 /* Add the tunable if it exists. */
227 for (size_t i = 0; i < tunables_list_size; i++)
229 tunable_t *cur = &tunable_list[i];
231 if (tunable_is_name (cur->name, name))
233 tunables[i] = (struct tunable_toset_t) { cur, value, p - value };
234 break;
239 return ntunables;
242 static void
243 parse_tunable_print_error (const struct tunable_toset_t *toset)
245 _dl_error_printf ("WARNING: ld.so: invalid GLIBC_TUNABLES value `%.*s' "
246 "for option `%s': ignored.\n",
247 (int) toset->len,
248 toset->value,
249 toset->t->name);
252 static void
253 parse_tunables (const char *valstring)
255 struct tunable_toset_t tunables[tunables_list_size] = { 0 };
256 if (parse_tunables_string (valstring, tunables) == -1)
258 _dl_error_printf (
259 "WARNING: ld.so: invalid GLIBC_TUNABLES `%s': ignored.\n", valstring);
260 return;
263 /* Ignore tunables if enable_secure is set */
264 struct tunable_toset_t *tsec =
265 &tunables[TUNABLE_ENUM_NAME(glibc, rtld, enable_secure)];
266 if (tsec->t != NULL)
268 tunable_num_t val;
269 if (!tunable_parse_num (tsec->value, tsec->len, &val))
270 parse_tunable_print_error (tsec);
271 else if (val == 1)
273 __libc_enable_secure = 1;
274 return;
278 for (int i = 0; i < tunables_list_size; i++)
280 if (tunables[i].t == NULL)
281 continue;
283 if (!tunable_initialize (tunables[i].t, tunables[i].value,
284 tunables[i].len))
285 parse_tunable_print_error (&tunables[i]);
289 /* Initialize the tunables list from the environment. For now we only use the
290 ENV_ALIAS to find values. Later we will also use the tunable names to find
291 values. */
292 void
293 __tunables_init (char **envp)
295 char *envname = NULL;
296 char *envval = NULL;
297 char **prev_envp = envp;
299 /* Ignore tunables for AT_SECURE programs. */
300 if (__libc_enable_secure)
301 return;
303 enum { tunable_num_env_alias = array_length (tunable_env_alias_list) };
304 struct tunable_toset_t tunables_env_alias[tunable_num_env_alias] = { 0 };
306 while ((envp = get_next_env (envp, &envname, &envval, &prev_envp)) != NULL)
308 /* The environment variable is allocated on the stack by the kernel, so
309 it is safe to keep the references to the suboptions for later parsing
310 of string tunables. */
311 if (tunable_is_name ("GLIBC_TUNABLES", envname))
313 parse_tunables (envval);
314 continue;
317 for (int i = 0; i < tunable_num_env_alias; i++)
319 tunable_t *cur = &tunable_list[tunable_env_alias_list[i]];
320 const char *name = cur->env_alias;
322 if (name[0] == '\0')
323 continue;
325 if (tunable_is_name (name, envname))
327 size_t envvallen = 0;
328 /* The environment variable is always null-terminated. */
329 for (const char *p = envval; *p != '\0'; p++, envvallen++);
331 tunables_env_alias[i] =
332 (struct tunable_toset_t) { cur, envval, envvallen };
333 break;
338 /* Check if glibc.rtld.enable_secure was set and skip over the environment
339 variables aliases. */
340 if (__libc_enable_secure)
341 return;
343 for (int i = 0; i < tunable_num_env_alias; i++)
345 /* Skip over tunables that have either been set or already initialized. */
346 if (tunables_env_alias[i].t == NULL
347 || tunables_env_alias[i].t->initialized)
348 continue;
350 if (!tunable_initialize (tunables_env_alias[i].t,
351 tunables_env_alias[i].value,
352 tunables_env_alias[i].len))
353 parse_tunable_print_error (&tunables_env_alias[i]);
357 void
358 __tunables_print (void)
360 for (int i = 0; i < array_length (tunable_list); i++)
362 const tunable_t *cur = &tunable_list[i];
363 if (cur->type.type_code == TUNABLE_TYPE_STRING
364 && cur->val.strval.str == NULL)
365 _dl_printf ("%s:\n", cur->name);
366 else
368 _dl_printf ("%s: ", cur->name);
369 switch (cur->type.type_code)
371 case TUNABLE_TYPE_INT_32:
372 _dl_printf ("%d (min: %d, max: %d)\n",
373 (int) cur->val.numval,
374 (int) cur->type.min,
375 (int) cur->type.max);
376 break;
377 case TUNABLE_TYPE_UINT_64:
378 _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n",
379 (long int) cur->val.numval,
380 (long int) cur->type.min,
381 (long int) cur->type.max);
382 break;
383 case TUNABLE_TYPE_SIZE_T:
384 _dl_printf ("0x%zx (min: 0x%zx, max: 0x%zx)\n",
385 (size_t) cur->val.numval,
386 (size_t) cur->type.min,
387 (size_t) cur->type.max);
388 break;
389 case TUNABLE_TYPE_STRING:
390 _dl_printf ("%.*s\n",
391 (int) cur->val.strval.len,
392 cur->val.strval.str);
393 break;
394 default:
395 __builtin_unreachable ();
401 void
402 __tunable_get_default (tunable_id_t id, void *valp)
404 tunable_t *cur = &tunable_list[id];
406 switch (cur->type.type_code)
408 case TUNABLE_TYPE_UINT_64:
410 *((uint64_t *) valp) = (uint64_t) cur->def.numval;
411 break;
413 case TUNABLE_TYPE_INT_32:
415 *((int32_t *) valp) = (int32_t) cur->def.numval;
416 break;
418 case TUNABLE_TYPE_SIZE_T:
420 *((size_t *) valp) = (size_t) cur->def.numval;
421 break;
423 case TUNABLE_TYPE_STRING:
425 *((const struct tunable_str_t **)valp) = &cur->def.strval;
426 break;
428 default:
429 __builtin_unreachable ();
432 rtld_hidden_def (__tunable_get_default)
434 /* Set the tunable value. This is called by the module that the tunable exists
435 in. */
436 void
437 __tunable_get_val (tunable_id_t id, void *valp, tunable_callback_t callback)
439 tunable_t *cur = &tunable_list[id];
441 switch (cur->type.type_code)
443 case TUNABLE_TYPE_UINT_64:
445 *((uint64_t *) valp) = (uint64_t) cur->val.numval;
446 break;
448 case TUNABLE_TYPE_INT_32:
450 *((int32_t *) valp) = (int32_t) cur->val.numval;
451 break;
453 case TUNABLE_TYPE_SIZE_T:
455 *((size_t *) valp) = (size_t) cur->val.numval;
456 break;
458 case TUNABLE_TYPE_STRING:
460 *((const struct tunable_str_t **) valp) = &cur->val.strval;
461 break;
463 default:
464 __builtin_unreachable ();
467 if (cur->initialized && callback != NULL)
468 callback (&cur->val);
471 rtld_hidden_def (__tunable_get_val)