]>
Commit | Line | Data |
---|---|---|
67e58f39 SP |
1 | /* The tunable framework. See the README.tunables to know how to use the |
2 | tunable in a glibc module. | |
3 | ||
dff8da6b | 4 | Copyright (C) 2016-2024 Free Software Foundation, Inc. |
67e58f39 SP |
5 | This file is part of the GNU C Library. |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with the GNU C Library; if not, see | |
5a82c748 | 19 | <https://www.gnu.org/licenses/>. */ |
67e58f39 | 20 | |
47618209 SN |
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 | |
086df229 | 25 | #include <startup.h> |
67e58f39 SP |
26 | #include <stdint.h> |
27 | #include <stdbool.h> | |
28 | #include <unistd.h> | |
29 | #include <stdlib.h> | |
67e58f39 SP |
30 | #include <sysdep.h> |
31 | #include <fcntl.h> | |
32 | #include <ldsodefs.h> | |
86f65dff | 33 | #include <array_length.h> |
b05fae4d | 34 | #include <dl-minimal-malloc.h> |
67e58f39 SP |
35 | |
36 | #define TUNABLES_INTERNAL 1 | |
37 | #include "dl-tunables.h" | |
38 | ||
67e58f39 | 39 | static char ** |
2a969b53 | 40 | get_next_env (char **envp, char **name, char **val, char ***prev_envp) |
67e58f39 SP |
41 | { |
42 | while (envp != NULL && *envp != NULL) | |
43 | { | |
8b9e9c3c | 44 | char **prev = envp; |
41389c40 | 45 | char *envline = *envp++; |
67e58f39 SP |
46 | int len = 0; |
47 | ||
48 | while (envline[len] != '\0' && envline[len] != '=') | |
49 | len++; | |
50 | ||
51 | /* Just the name and no value, go to the next one. */ | |
52 | if (envline[len] == '\0') | |
53 | continue; | |
54 | ||
55 | *name = envline; | |
67e58f39 | 56 | *val = &envline[len + 1]; |
8b9e9c3c | 57 | *prev_envp = prev; |
67e58f39 | 58 | |
41389c40 | 59 | return envp; |
67e58f39 SP |
60 | } |
61 | ||
62 | return NULL; | |
63 | } | |
64 | ||
67e58f39 | 65 | static void |
61117bfa SP |
66 | do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp, |
67 | const tunable_num_t *minp, | |
68 | const tunable_num_t *maxp) | |
67e58f39 | 69 | { |
61117bfa | 70 | tunable_num_t val, min, max; |
ad2f35cb | 71 | |
f94446c3 | 72 | switch (cur->type.type_code) |
61117bfa | 73 | { |
f94446c3 | 74 | case TUNABLE_TYPE_STRING: |
61117bfa SP |
75 | cur->val.strval = valp->strval; |
76 | cur->initialized = true; | |
77 | return; | |
f94446c3 AZ |
78 | case TUNABLE_TYPE_INT_32: |
79 | val = (int32_t) valp->numval; | |
80 | break; | |
81 | case TUNABLE_TYPE_UINT_64: | |
82 | val = (int64_t) valp->numval; | |
83 | break; | |
84 | case TUNABLE_TYPE_SIZE_T: | |
85 | val = (size_t) valp->numval; | |
86 | break; | |
87 | default: | |
88 | __builtin_unreachable (); | |
61117bfa | 89 | } |
ad2f35cb | 90 | |
d1a3dcab SP |
91 | bool unsigned_cmp = unsigned_tunable_type (cur->type.type_code); |
92 | ||
61117bfa SP |
93 | min = minp != NULL ? *minp : cur->type.min; |
94 | max = maxp != NULL ? *maxp : cur->type.max; | |
95 | ||
96 | /* We allow only increasingly restrictive bounds. */ | |
d1a3dcab | 97 | if (tunable_val_lt (min, cur->type.min, unsigned_cmp)) |
61117bfa SP |
98 | min = cur->type.min; |
99 | ||
d1a3dcab | 100 | if (tunable_val_gt (max, cur->type.max, unsigned_cmp)) |
61117bfa SP |
101 | max = cur->type.max; |
102 | ||
103 | /* Skip both bounds if they're inconsistent. */ | |
d1a3dcab | 104 | if (tunable_val_gt (min, max, unsigned_cmp)) |
67e58f39 | 105 | { |
61117bfa SP |
106 | min = cur->type.min; |
107 | max = cur->type.max; | |
108 | } | |
109 | ||
d1a3dcab SP |
110 | /* Bail out if the bounds are not valid. */ |
111 | if (tunable_val_lt (val, min, unsigned_cmp) | |
112 | || tunable_val_lt (max, val, unsigned_cmp)) | |
113 | return; | |
114 | ||
115 | cur->val.numval = val; | |
116 | cur->type.min = min; | |
117 | cur->type.max = max; | |
118 | cur->initialized = true; | |
67e58f39 SP |
119 | } |
120 | ||
44330b6d SP |
121 | /* Validate range of the input value and initialize the tunable CUR if it looks |
122 | good. */ | |
f94446c3 | 123 | static bool |
2a969b53 | 124 | tunable_initialize (tunable_t *cur, const char *strval, size_t len) |
44330b6d | 125 | { |
2a969b53 | 126 | tunable_val_t val = { 0 }; |
44330b6d SP |
127 | |
128 | if (cur->type.type_code != TUNABLE_TYPE_STRING) | |
f94446c3 AZ |
129 | { |
130 | char *endptr = NULL; | |
131 | uint64_t numval = _dl_strtoul (strval, &endptr); | |
132 | if (endptr != strval + len) | |
133 | return false; | |
134 | val.numval = (tunable_num_t) numval; | |
135 | } | |
44330b6d | 136 | else |
2a969b53 | 137 | val.strval = (struct tunable_str_t) { strval, len }; |
61117bfa | 138 | do_tunable_update_val (cur, &val, NULL, NULL); |
f94446c3 AZ |
139 | |
140 | return true; | |
44330b6d SP |
141 | } |
142 | ||
a4c3f5f4 AZ |
143 | bool |
144 | __tunable_is_initialized (tunable_id_t id) | |
145 | { | |
146 | return tunable_list[id].initialized; | |
147 | } | |
148 | rtld_hidden_def (__tunable_is_initialized) | |
149 | ||
44330b6d | 150 | void |
61117bfa SP |
151 | __tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp, |
152 | tunable_num_t *maxp) | |
44330b6d SP |
153 | { |
154 | tunable_t *cur = &tunable_list[id]; | |
155 | ||
dfb8e514 | 156 | do_tunable_update_val (cur, valp, minp, maxp); |
44330b6d SP |
157 | } |
158 | ||
680c597e AZ |
159 | struct tunable_toset_t |
160 | { | |
161 | tunable_t *t; | |
162 | const char *value; | |
2a969b53 | 163 | size_t len; |
680c597e AZ |
164 | }; |
165 | ||
166 | enum { tunables_list_size = array_length (tunable_list) }; | |
167 | ||
168 | /* Parse the tunable string VALSTRING and set TUNABLES with the found tunables | |
2a969b53 AZ |
169 | and their respective values. The VALSTRING is parsed in place, with the |
170 | tunable start and size recorded in TUNABLES. | |
680c597e AZ |
171 | Return the number of tunables found (including 0 if the string is empty) |
172 | or -1 if for an ill-formatted definition. */ | |
173 | static int | |
2a969b53 | 174 | parse_tunables_string (const char *valstring, struct tunable_toset_t *tunables) |
9dd409a5 | 175 | { |
9c96c87d | 176 | if (valstring == NULL || *valstring == '\0') |
680c597e | 177 | return 0; |
9dd409a5 | 178 | |
2a969b53 | 179 | const char *p = valstring; |
9c96c87d | 180 | bool done = false; |
680c597e | 181 | int ntunables = 0; |
9dd409a5 | 182 | |
9c96c87d | 183 | while (!done) |
9dd409a5 | 184 | { |
2a969b53 | 185 | const char *name = p; |
9dd409a5 SP |
186 | |
187 | /* First, find where the name ends. */ | |
9c96c87d AZ |
188 | while (*p != '=' && *p != ':' && *p != '\0') |
189 | p++; | |
9dd409a5 SP |
190 | |
191 | /* If we reach the end of the string before getting a valid name-value | |
192 | pair, bail out. */ | |
9c96c87d | 193 | if (*p == '\0') |
680c597e | 194 | return -1; |
9dd409a5 SP |
195 | |
196 | /* We did not find a valid name-value pair before encountering the | |
197 | colon. */ | |
9c96c87d | 198 | if (*p == ':') |
9dd409a5 | 199 | { |
9c96c87d | 200 | p++; |
9dd409a5 SP |
201 | continue; |
202 | } | |
203 | ||
9c96c87d AZ |
204 | /* Skip the '='. */ |
205 | p++; | |
9dd409a5 | 206 | |
2a969b53 | 207 | const char *value = p; |
9dd409a5 | 208 | |
b4cf6cac | 209 | while (*p != '=' && *p != ':' && *p != '\0') |
9c96c87d AZ |
210 | p++; |
211 | ||
b4cf6cac | 212 | if (*p == '=') |
680c597e | 213 | return -1; |
b4cf6cac | 214 | else if (*p == '\0') |
9c96c87d | 215 | done = true; |
9dd409a5 | 216 | |
9dd409a5 | 217 | /* Add the tunable if it exists. */ |
680c597e | 218 | for (size_t i = 0; i < tunables_list_size; i++) |
9dd409a5 SP |
219 | { |
220 | tunable_t *cur = &tunable_list[i]; | |
221 | ||
28cfa3a4 | 222 | if (tunable_is_name (cur->name, name)) |
9dd409a5 | 223 | { |
2a969b53 AZ |
224 | tunables[ntunables++] = |
225 | (struct tunable_toset_t) { cur, value, p - value }; | |
71648e80 JST |
226 | |
227 | /* Ignore tunables if enable_secure is set */ | |
228 | if (tunable_is_name ("glibc.rtld.enable_secure", name)) | |
229 | { | |
230 | tunable_num_t val = (tunable_num_t) _dl_strtoul (value, NULL); | |
231 | if (val == 1) | |
232 | { | |
233 | __libc_enable_secure = 1; | |
234 | return 0; | |
235 | } | |
236 | } | |
9dd409a5 SP |
237 | break; |
238 | } | |
239 | } | |
9dd409a5 | 240 | } |
680c597e AZ |
241 | |
242 | return ntunables; | |
243 | } | |
244 | ||
245 | static void | |
2a969b53 | 246 | parse_tunables (const char *valstring) |
680c597e AZ |
247 | { |
248 | struct tunable_toset_t tunables[tunables_list_size]; | |
249 | int ntunables = parse_tunables_string (valstring, tunables); | |
eb9291aa AZ |
250 | if (ntunables == -1) |
251 | { | |
252 | _dl_error_printf ( | |
253 | "WARNING: ld.so: invalid GLIBC_TUNABLES `%s': ignored.\n", valstring); | |
254 | return; | |
255 | } | |
680c597e AZ |
256 | |
257 | for (int i = 0; i < ntunables; i++) | |
f94446c3 AZ |
258 | if (!tunable_initialize (tunables[i].t, tunables[i].value, |
259 | tunables[i].len)) | |
260 | _dl_error_printf ("WARNING: ld.so: invalid GLIBC_TUNABLES value `%.*s' " | |
261 | "for option `%s': ignored.\n", | |
262 | (int) tunables[i].len, | |
263 | tunables[i].value, | |
264 | tunables[i].t->name); | |
9dd409a5 SP |
265 | } |
266 | ||
67e58f39 SP |
267 | /* Initialize the tunables list from the environment. For now we only use the |
268 | ENV_ALIAS to find values. Later we will also use the tunable names to find | |
269 | values. */ | |
270 | void | |
271 | __tunables_init (char **envp) | |
272 | { | |
273 | char *envname = NULL; | |
274 | char *envval = NULL; | |
8b9e9c3c | 275 | char **prev_envp = envp; |
67e58f39 | 276 | |
9c96c87d AZ |
277 | /* Ignore tunables for AT_SECURE programs. */ |
278 | if (__libc_enable_secure) | |
279 | return; | |
280 | ||
2a969b53 | 281 | while ((envp = get_next_env (envp, &envname, &envval, &prev_envp)) != NULL) |
67e58f39 | 282 | { |
2a969b53 AZ |
283 | /* The environment variable is allocated on the stack by the kernel, so |
284 | it is safe to keep the references to the suboptions for later parsing | |
285 | of string tunables. */ | |
33237fe8 | 286 | if (tunable_is_name ("GLIBC_TUNABLES", envname)) |
9dd409a5 | 287 | { |
2a969b53 | 288 | parse_tunables (envval); |
9dd409a5 SP |
289 | continue; |
290 | } | |
291 | ||
680c597e | 292 | for (int i = 0; i < tunables_list_size; i++) |
67e58f39 SP |
293 | { |
294 | tunable_t *cur = &tunable_list[i]; | |
295 | ||
296 | /* Skip over tunables that have either been set already or should be | |
297 | skipped. */ | |
bfe04789 | 298 | if (cur->initialized || cur->env_alias[0] == '\0') |
67e58f39 SP |
299 | continue; |
300 | ||
301 | const char *name = cur->env_alias; | |
302 | ||
303 | /* We have a match. Initialize and move on to the next line. */ | |
28cfa3a4 | 304 | if (tunable_is_name (name, envname)) |
67e58f39 | 305 | { |
2a969b53 AZ |
306 | size_t envvallen = 0; |
307 | /* The environment variable is always null-terminated. */ | |
308 | for (const char *p = envval; *p != '\0'; p++, envvallen++); | |
309 | ||
310 | tunable_initialize (cur, envval, envvallen); | |
67e58f39 SP |
311 | break; |
312 | } | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
86f65dff L |
317 | void |
318 | __tunables_print (void) | |
319 | { | |
320 | for (int i = 0; i < array_length (tunable_list); i++) | |
321 | { | |
322 | const tunable_t *cur = &tunable_list[i]; | |
323 | if (cur->type.type_code == TUNABLE_TYPE_STRING | |
2a969b53 | 324 | && cur->val.strval.str == NULL) |
86f65dff L |
325 | _dl_printf ("%s:\n", cur->name); |
326 | else | |
327 | { | |
328 | _dl_printf ("%s: ", cur->name); | |
329 | switch (cur->type.type_code) | |
330 | { | |
331 | case TUNABLE_TYPE_INT_32: | |
332 | _dl_printf ("%d (min: %d, max: %d)\n", | |
333 | (int) cur->val.numval, | |
334 | (int) cur->type.min, | |
335 | (int) cur->type.max); | |
336 | break; | |
337 | case TUNABLE_TYPE_UINT_64: | |
338 | _dl_printf ("0x%lx (min: 0x%lx, max: 0x%lx)\n", | |
339 | (long int) cur->val.numval, | |
340 | (long int) cur->type.min, | |
341 | (long int) cur->type.max); | |
342 | break; | |
343 | case TUNABLE_TYPE_SIZE_T: | |
de477abc | 344 | _dl_printf ("0x%zx (min: 0x%zx, max: 0x%zx)\n", |
86f65dff L |
345 | (size_t) cur->val.numval, |
346 | (size_t) cur->type.min, | |
347 | (size_t) cur->type.max); | |
348 | break; | |
349 | case TUNABLE_TYPE_STRING: | |
2a969b53 AZ |
350 | _dl_printf ("%.*s\n", |
351 | (int) cur->val.strval.len, | |
352 | cur->val.strval.str); | |
86f65dff L |
353 | break; |
354 | default: | |
355 | __builtin_unreachable (); | |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
a4c3f5f4 AZ |
361 | void |
362 | __tunable_get_default (tunable_id_t id, void *valp) | |
363 | { | |
364 | tunable_t *cur = &tunable_list[id]; | |
365 | ||
366 | switch (cur->type.type_code) | |
367 | { | |
368 | case TUNABLE_TYPE_UINT_64: | |
369 | { | |
370 | *((uint64_t *) valp) = (uint64_t) cur->def.numval; | |
371 | break; | |
372 | } | |
373 | case TUNABLE_TYPE_INT_32: | |
374 | { | |
375 | *((int32_t *) valp) = (int32_t) cur->def.numval; | |
376 | break; | |
377 | } | |
378 | case TUNABLE_TYPE_SIZE_T: | |
379 | { | |
380 | *((size_t *) valp) = (size_t) cur->def.numval; | |
381 | break; | |
382 | } | |
383 | case TUNABLE_TYPE_STRING: | |
384 | { | |
2a969b53 | 385 | *((const struct tunable_str_t **)valp) = &cur->def.strval; |
a4c3f5f4 AZ |
386 | break; |
387 | } | |
388 | default: | |
389 | __builtin_unreachable (); | |
390 | } | |
391 | } | |
392 | rtld_hidden_def (__tunable_get_default) | |
393 | ||
67e58f39 SP |
394 | /* Set the tunable value. This is called by the module that the tunable exists |
395 | in. */ | |
396 | void | |
44330b6d | 397 | __tunable_get_val (tunable_id_t id, void *valp, tunable_callback_t callback) |
67e58f39 SP |
398 | { |
399 | tunable_t *cur = &tunable_list[id]; | |
400 | ||
67e58f39 SP |
401 | switch (cur->type.type_code) |
402 | { | |
ad2f35cb SP |
403 | case TUNABLE_TYPE_UINT_64: |
404 | { | |
405 | *((uint64_t *) valp) = (uint64_t) cur->val.numval; | |
406 | break; | |
407 | } | |
67e58f39 SP |
408 | case TUNABLE_TYPE_INT_32: |
409 | { | |
410 | *((int32_t *) valp) = (int32_t) cur->val.numval; | |
411 | break; | |
412 | } | |
413 | case TUNABLE_TYPE_SIZE_T: | |
414 | { | |
415 | *((size_t *) valp) = (size_t) cur->val.numval; | |
416 | break; | |
417 | } | |
418 | case TUNABLE_TYPE_STRING: | |
419 | { | |
2a969b53 | 420 | *((const struct tunable_str_t **) valp) = &cur->val.strval; |
67e58f39 SP |
421 | break; |
422 | } | |
423 | default: | |
424 | __builtin_unreachable (); | |
425 | } | |
426 | ||
44330b6d | 427 | if (cur->initialized && callback != NULL) |
67e58f39 SP |
428 | callback (&cur->val); |
429 | } | |
81efada5 | 430 | |
44330b6d | 431 | rtld_hidden_def (__tunable_get_val) |