[PATCH v5 3/4] Add system-wide tunables: Apply tunables part
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Mon Mar 2 13:43:30 GMT 2026
On 21/02/24 20:49, DJ Delorie wrote:
>
> Load ld.so.cache and fetch the tunables extension. Apply
> those tunables to the current program. We do not yet apply
> security policies.
> ---
> elf/Makefile | 1 +
> elf/dl-cache.c | 25 +++++++++++++++++++
> elf/dl-tunables.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++
> elf/tunconf.h | 3 +++
> 4 files changed, 92 insertions(+)
>
> diff --git a/elf/Makefile b/elf/Makefile
> index 42da891f5a..282af88e71 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -91,6 +91,7 @@ dl-routines = \
>
> ifeq (yes,$(use-ldconfig))
> dl-routines += dl-cache
> +CFLAGS-dl-tunables.c += -DUSE_LDCONFIG
> endif
>
> ifeq (yesyes,$(build-shared)$(run-built-tests))
> diff --git a/elf/dl-cache.c b/elf/dl-cache.c
> index 4612dea835..a6b019e46c 100644
> --- a/elf/dl-cache.c
> +++ b/elf/dl-cache.c
> @@ -29,6 +29,7 @@
> #include <sys/types.h>
> #include <fcntl.h>
> #include <sys/stat.h>
> +#include "tunconf.h"
>
> /* This is the starting address and the size of the mmap()ed file. */
> static struct cache_file *cache;
> @@ -601,3 +602,27 @@ _dl_unload_cache (void)
> now. */
> }
> #endif
> +
> +const struct tunable_header_cached *
> +_dl_load_cache_tunables (const char **data)
> +{
> + struct cache_extension_all_loaded ext;
> +
> + /* This loads the cache (temporary). */
> + if (_dl_check_ldsocache_needs_loading ())
> + _dl_maybe_load_ldsocache ();
> +
> + if (cache_new && cache_new != (void *) -1)
> + *data = (const char *) cache_new;
> + else
> + return NULL;
> +
> + if (!cache_extension_load (cache_new, cache, cachesize, &ext))
> + return NULL;
> +
> + /* Validate length/contents here. */
Minor nit: double space after period.
> + if (ext.sections[cache_extension_tag_tunables].size
> + < sizeof(struct tunable_header_cached))
> + return NULL;
> + return ext.sections[cache_extension_tag_tunables].base;
> +}
> diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c
> index 37ade374c6..83808f75e7 100644
> --- a/elf/dl-tunables.c
> +++ b/elf/dl-tunables.c
> @@ -36,6 +36,7 @@
>
> #define TUNABLES_INTERNAL 1
> #include "dl-tunables.h"
> +#include "tunconf.h"
>
> /* The function might be called before the process is self-relocated. */
> static size_t
> @@ -306,6 +307,68 @@ __tunables_init (char **envp)
> char *envval = NULL;
> char **prev_envp = envp;
>
> +#if defined(SHARED) && defined (USE_LDCONFIG)
I think for tunables we will need to also enable the ld.so.cache loading and
parsing for static file as well.
And this would add an inconsistency on Hurd, where is does not enable
USE_LDCONFIG but does support tunables. But I think Hurd developers will need
to define if and how they would want to enable it.
> + const struct tunable_header_cached *thc;
> + const char *td;
> +
> + thc = _dl_load_cache_tunables (&td);
> + if (thc != NULL)
> + {
> + for (int t = 0; t < thc->num_tunables; ++ t)
> + {
> + const struct tunable_entry_cached *tec = &( thc->tunables[t] );
> + int tid = tec->tunable_id;
> + const char *name = td + tec->name_offset;
> + const char *value = td + tec->value_offset;
> +
> + /* Check that we have the correct tunable, and search by
> + name if needed. We rely on order of operations here to
> + avoid mis-indexing tunables[]. */
> + if (tid < 0 || tid >= tunables_list_size
> + || strcmp (name, tunable_list[tid].name) != 0)
> + {
> + /* It does not, search by name instead. */
> + tid = -1;
> + for (int i = 0; i < tunables_list_size; i++)
> + {
> + if (strcmp (name, tunable_list[i].name) == 0)
> + {
> + tid = i;
> + break;
> + }
> + }
> + if (tid == -1)
> + continue;
> + }
> + /* At this point, TID is valid for the tunable we want. See
> + if the parsed type matches the desired type. */
> +
> + if (tunable_list[tid].type.type_code == TUNABLE_TYPE_STRING)
> + {
> + /* This is a memory leak but there's no easy way around
> + it, as the mapping will go away. */
> + tunable_list[tid].val.strval.str = __strdup (value);
> + tunable_list[tid].val.strval.len = strlen (value);
> + }
> + else
> + {
> + tunable_val_t tval;
> + if (tec->flags & TUNCONF_FLAG_PARSED)
> + {
> + tval.numval = tec->parsed_value;
> + do_tunable_update_val (& tunable_list[tid],
> + &tval, NULL, NULL);
> + }
> + else
> + {
> + tunable_initialize (& tunable_list[tid],
> + value, strlen (value));
> + }
> + }
> + }
> + }
> +#endif
> +
> /* Ignore tunables for AT_SECURE programs. */
> if (__libc_enable_secure)
> return;
> diff --git a/elf/tunconf.h b/elf/tunconf.h
> index a6c5f0dd9a..623fb7546d 100644
> --- a/elf/tunconf.h
> +++ b/elf/tunconf.h
> @@ -38,3 +38,6 @@ void parse_tunconf (const char *filename, int do_chroot, char *opt_chroot);
> struct tunable_header_cached * get_tunconf_ext (uint32_t str_offset);
> #define TUNCONF_SIZE(thc_p) (sizeof(struct tunable_header_cached) \
> + thc_p->num_tunables * sizeof (struct tunable_entry_cached))
> +
> +extern const struct tunable_header_cached *
> +_dl_load_cache_tunables (const char **data);
More information about the Libc-alpha
mailing list