1 /* The tunable framework. See the README.tunables to know how to use the
2 tunable in a glibc module.
4 Copyright (C) 2016-2017 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 <http://www.gnu.org/licenses/>. */
30 #define TUNABLES_INTERNAL 1
31 #include "dl-tunables.h"
33 #include <not-errno.h>
35 #if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
36 # define GLIBC_TUNABLES "GLIBC_TUNABLES"
39 #if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
41 tunables_strdup (const char *in
)
45 while (in
[i
++] != '\0');
46 char *out
= __sbrk (i
);
48 /* FIXME: In reality if the allocation fails, __sbrk will crash attempting to
49 set the thread-local errno since the TCB has not yet been set up. This
50 needs to be fixed with an __sbrk implementation that does not set
52 if (out
== (void *)-1)
65 get_next_env (char **envp
, char **name
, size_t *namelen
, char **val
,
68 while (envp
!= NULL
&& *envp
!= NULL
)
71 char *envline
= *envp
++;
74 while (envline
[len
] != '\0' && envline
[len
] != '=')
77 /* Just the name and no value, go to the next one. */
78 if (envline
[len
] == '\0')
83 *val
= &envline
[len
+ 1];
92 #define TUNABLE_SET_VAL_IF_VALID_RANGE(__cur, __val, __type, __default_min, \
95 __type min = (__cur)->type.min; \
96 __type max = (__cur)->type.max; \
100 min = __default_min; \
101 max = __default_max; \
104 if ((__type) (__val) >= min && (__type) (val) <= max) \
106 (__cur)->val.numval = val; \
107 (__cur)->initialized = true; \
112 do_tunable_update_val (tunable_t
*cur
, const void *valp
)
116 if (cur
->type
.type_code
!= TUNABLE_TYPE_STRING
)
117 val
= *((int64_t *) valp
);
119 switch (cur
->type
.type_code
)
121 case TUNABLE_TYPE_INT_32
:
123 TUNABLE_SET_VAL_IF_VALID_RANGE (cur
, val
, int64_t, INT32_MIN
, INT32_MAX
);
126 case TUNABLE_TYPE_UINT_64
:
128 TUNABLE_SET_VAL_IF_VALID_RANGE (cur
, val
, uint64_t, 0, UINT64_MAX
);
131 case TUNABLE_TYPE_SIZE_T
:
133 TUNABLE_SET_VAL_IF_VALID_RANGE (cur
, val
, uint64_t, 0, SIZE_MAX
);
136 case TUNABLE_TYPE_STRING
:
138 cur
->val
.strval
= valp
;
142 __builtin_unreachable ();
146 /* Validate range of the input value and initialize the tunable CUR if it looks
149 tunable_initialize (tunable_t
*cur
, const char *strval
)
154 if (cur
->type
.type_code
!= TUNABLE_TYPE_STRING
)
156 val
= _dl_strtoul (strval
, NULL
);
161 cur
->initialized
= true;
164 do_tunable_update_val (cur
, valp
);
168 __tunable_set_val (tunable_id_t id
, void *valp
)
170 tunable_t
*cur
= &tunable_list
[id
];
172 do_tunable_update_val (cur
, valp
);
175 #if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
176 /* Parse the tunable string TUNESTR and adjust it to drop any tunables that may
177 be unsafe for AT_SECURE processes so that it can be used as the new
178 environment variable value for GLIBC_TUNABLES. VALSTRING is the original
179 environment variable string which we use to make NULL terminated values so
180 that we don't have to allocate memory again for it. */
182 parse_tunables (char *tunestr
, char *valstring
)
184 if (tunestr
== NULL
|| *tunestr
== '\0')
194 /* First, find where the name ends. */
195 while (p
[len
] != '=' && p
[len
] != ':' && p
[len
] != '\0')
198 /* If we reach the end of the string before getting a valid name-value
203 /* We did not find a valid name-value pair before encountering the
213 /* Take the value from the valstring since we need to NULL terminate it. */
214 char *value
= &valstring
[p
- tunestr
];
217 while (p
[len
] != ':' && p
[len
] != '\0')
220 /* Add the tunable if it exists. */
221 for (size_t i
= 0; i
< sizeof (tunable_list
) / sizeof (tunable_t
); i
++)
223 tunable_t
*cur
= &tunable_list
[i
];
225 if (tunable_is_name (cur
->name
, name
))
227 /* If we are in a secure context (AT_SECURE) then ignore the tunable
228 unless it is explicitly marked as secure. Tunable values take
229 precendence over their envvar aliases. */
230 if (__libc_enable_secure
)
232 if (cur
->security_level
== TUNABLE_SECLEVEL_SXID_ERASE
)
236 /* Last tunable in the valstring. Null-terminate and
243 /* Remove the current tunable from the string. We do
244 this by overwriting the string starting from NAME
245 (which is where the current tunable begins) with
246 the remainder of the string. We then have P point
247 to NAME so that we continue in the correct
248 position in the valstring. */
249 char *q
= &p
[len
+ 1];
258 if (cur
->security_level
!= TUNABLE_SECLEVEL_NONE
)
263 tunable_initialize (cur
, value
);
276 /* Enable the glibc.malloc.check tunable in SETUID/SETGID programs only when
277 the system administrator has created the /etc/suid-debug file. This is a
278 special case where we want to conditionally enable/disable a tunable even
279 for setuid binaries. We use the special version of access() to avoid
280 setting ERRNO, which is a TLS variable since TLS has not yet been set
284 maybe_enable_malloc_check (void)
286 tunable_id_t id
= TUNABLE_ENUM_NAME (glibc
, malloc
, check
);
287 if (__libc_enable_secure
&& __access_noerrno ("/etc/suid-debug", F_OK
) == 0)
288 tunable_list
[id
].security_level
= TUNABLE_SECLEVEL_NONE
;
291 /* Initialize the tunables list from the environment. For now we only use the
292 ENV_ALIAS to find values. Later we will also use the tunable names to find
295 __tunables_init (char **envp
)
297 char *envname
= NULL
;
300 char **prev_envp
= envp
;
302 maybe_enable_malloc_check ();
304 while ((envp
= get_next_env (envp
, &envname
, &len
, &envval
,
305 &prev_envp
)) != NULL
)
307 #if TUNABLES_FRONTEND == TUNABLES_FRONTEND_valstring
308 if (tunable_is_name (GLIBC_TUNABLES
, envname
))
310 char *new_env
= tunables_strdup (envname
);
312 parse_tunables (new_env
+ len
+ 1, envval
);
313 /* Put in the updated envval. */
314 *prev_envp
= new_env
;
319 for (int i
= 0; i
< sizeof (tunable_list
) / sizeof (tunable_t
); i
++)
321 tunable_t
*cur
= &tunable_list
[i
];
323 /* Skip over tunables that have either been set already or should be
325 if (cur
->initialized
|| cur
->env_alias
== NULL
)
328 const char *name
= cur
->env_alias
;
330 /* We have a match. Initialize and move on to the next line. */
331 if (tunable_is_name (name
, envname
))
333 /* For AT_SECURE binaries, we need to check the security settings of
334 the tunable and decide whether we read the value and also whether
335 we erase the value so that child processes don't inherit them in
337 if (__libc_enable_secure
)
339 if (cur
->security_level
== TUNABLE_SECLEVEL_SXID_ERASE
)
341 /* Erase the environment variable. */
342 char **ep
= prev_envp
;
346 if (tunable_is_name (name
, *ep
))
357 /* Reset the iterator so that we read the environment again
358 from the point we erased. */
362 if (cur
->security_level
!= TUNABLE_SECLEVEL_NONE
)
366 tunable_initialize (cur
, envval
);
373 /* Set the tunable value. This is called by the module that the tunable exists
376 __tunable_get_val (tunable_id_t id
, void *valp
, tunable_callback_t callback
)
378 tunable_t
*cur
= &tunable_list
[id
];
380 switch (cur
->type
.type_code
)
382 case TUNABLE_TYPE_UINT_64
:
384 *((uint64_t *) valp
) = (uint64_t) cur
->val
.numval
;
387 case TUNABLE_TYPE_INT_32
:
389 *((int32_t *) valp
) = (int32_t) cur
->val
.numval
;
392 case TUNABLE_TYPE_SIZE_T
:
394 *((size_t *) valp
) = (size_t) cur
->val
.numval
;
397 case TUNABLE_TYPE_STRING
:
399 *((const char **)valp
) = cur
->val
.strval
;
403 __builtin_unreachable ();
406 if (cur
->initialized
&& callback
!= NULL
)
407 callback (&cur
->val
);
410 rtld_hidden_def (__tunable_get_val
)