[v3,1/1] rtld: add tunable for audit-specific surplus TLS
Checks
Commit Message
Add glibc.rtld.audit_static_tls which specifies additional
surplus TLS just for audit modules, as when audit modules are
used the loader can't compute TLS space before the auditors
run. This surplus exists until the loader can recompute the
TLS needs, and is reduced for future threads.
---
Changes in v3:
- rebased
- fixed typo in dl-tls.c
Changes in v2:
- rebased
- fix tst-rtld-list-tunables
elf/dl-tls.c | 43 ++++++++++++++++++++++++++++++++--
elf/dl-tunables.list | 5 ++++
elf/rtld.c | 2 ++
elf/tst-rtld-list-tunables.exp | 1 +
manual/tunables.texi | 22 +++++++++++++++++
sysdeps/generic/ldsodefs.h | 5 ++++
6 files changed, 76 insertions(+), 2 deletions(-)
Comments
On 10/29/26 5:44 PM, DJ Delorie wrote:
>
> Add glibc.rtld.audit_static_tls which specifies additional
> surplus TLS just for audit modules, as when audit modules are
> used the loader can't compute TLS space before the auditors
> run. This surplus exists until the loader can recompute the
> TLS needs, and is reduced for future threads.
> ---
> Changes in v3:
> - rebased
> - fixed typo in dl-tls.c
> Changes in v2:
> - rebased
> - fix tst-rtld-list-tunables
>
> elf/dl-tls.c | 43 ++++++++++++++++++++++++++++++++--
> elf/dl-tunables.list | 5 ++++
> elf/rtld.c | 2 ++
> elf/tst-rtld-list-tunables.exp | 1 +
> manual/tunables.texi | 22 +++++++++++++++++
> sysdeps/generic/ldsodefs.h | 5 ++++
> 6 files changed, 76 insertions(+), 2 deletions(-)
>
> diff --git a/elf/dl-tls.c b/elf/dl-tls.c
> index 31b26fe8d3..f7308d8ce5 100644
> --- a/elf/dl-tls.c
> +++ b/elf/dl-tls.c
> @@ -130,6 +130,10 @@ tls_static_surplus (int nns, int opt_tls)
> tunable, and sets an additional minimum alignment on the TLS
> data. */
> static size_t optional_static_tls_alignment = 0;
> +static size_t dl_audit_tls = 0;
> +static size_t dl_original_optional = 0;
> +static size_t dl_original_surplus = 0;
> +static size_t dl_original_align = 1;
>
> /* Calculate the size of the static TLS surplus, when the given
> number of audit modules are loaded. Must be called after the
> @@ -145,6 +149,8 @@ _dl_tls_static_surplus_init (size_t naudit)
> /* Ignore invalid requests. */
> if (! powerof2 (optional_static_tls_alignment))
> optional_static_tls_alignment = 0;
> + if (naudit)
> + dl_audit_tls = TUNABLE_GET (audit_static_tls, size_t, NULL);
> if (nns > DL_NNS)
> nns = DL_NNS;
> if (DL_NNS - nns < naudit)
> @@ -155,6 +161,33 @@ _dl_tls_static_surplus_init (size_t naudit)
> GL(dl_tls_static_optional) = opt_tls;
> assert (LEGACY_TLS >= 0);
> GLRO(dl_tls_static_surplus) = tls_static_surplus (nns, opt_tls) + LEGACY_TLS;
> +
> + if (dl_audit_tls > 0)
> + {
> + if (dl_audit_tls >= SIZE_MAX / 2)
> + _dl_fatal_printf ("glibc.rtld.audit_static_tls exceeds SIZE_MAX/2\n");
> + dl_original_optional = GL(dl_tls_static_optional);
> + dl_original_surplus = GLRO(dl_tls_static_surplus);
> + }
> +}
> +
> +/* Undo as much of the audit-specific extra TLS space accounted for
> + above, as possible. After this function, the unused amount of TLS
> + must be at least the dl_tls_static_surplus amount. */
> +void
> +_dl_tls_prune_audit_storage (void)
> +{
> + if (dl_audit_tls > 0)
> + {
> + GL(dl_tls_static_optional) = dl_original_optional;
> + GLRO(dl_tls_static_surplus) = dl_original_surplus;
> + GLRO(dl_tls_static_size) = roundup (GL(dl_tls_static_used)
> + + dl_original_surplus,
> + dl_original_align);
> +#ifdef TLS_TCB_AT_TP
Won't this always be true? I don't actually see any case where it can be
undefined.
> + GLRO(dl_tls_static_size) += TLS_TCB_SIZE;
> +#endif
> + }
> }
>
> /* Out-of-memory handler. */
> @@ -380,9 +413,12 @@ _dl_determine_tlsoffset (void)
> _dl_extra_tls_set_offset (-offset);
>
> GL(dl_tls_static_used) = offset;
> - GLRO (dl_tls_static_size) = (roundup (offset + GLRO(dl_tls_static_surplus),
> + GLRO (dl_tls_static_size) = (roundup (offset
> + + GLRO(dl_tls_static_surplus)
> + + dl_audit_tls,
> max_align)
> + TLS_TCB_SIZE);
> + dl_original_align = max_align;
> #elif TLS_DTV_AT_TP
> /* The TLS blocks start right after the TCB. */
> size_t offset = TLS_TCB_SIZE;
> @@ -467,8 +503,11 @@ _dl_determine_tlsoffset (void)
> offset += extra_tls_size;
>
> GL(dl_tls_static_used) = offset;
> - GLRO (dl_tls_static_size) = roundup (offset + GLRO(dl_tls_static_surplus),
> + GLRO (dl_tls_static_size) = roundup (offset
> + + GLRO(dl_tls_static_surplus)
> + + dl_audit_tls,
> TCB_ALIGNMENT);
> + dl_original_align = TCB_ALIGNMENT;
> #else
> # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
> #endif
> diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
> index 4c78444542..d8daf30ab0 100644
> --- a/elf/dl-tunables.list
> +++ b/elf/dl-tunables.list
> @@ -106,6 +106,11 @@ glibc {
> minval: 0
> default: 0
> }
> + audit_static_tls {
> + type: SIZE_T
> + minval: 0
> + default: 0
> + }
> enable_secure {
> type: INT_32
> minval: 0
> diff --git a/elf/rtld.c b/elf/rtld.c
> index 9233c50256..5799b1d007 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -2353,6 +2353,8 @@ dl_main (const ElfW(Phdr) *phdr,
> _dl_debug_post_relocate (main_map);
> }
>
> + _dl_tls_prune_audit_storage ();
> +
> /* All ld.so initialization is complete. Apply RELRO. */
> _dl_protect_relro (&_dl_rtld_map);
>
> diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
> index 17520d6b31..d208804723 100644
> --- a/elf/tst-rtld-list-tunables.exp
> +++ b/elf/tst-rtld-list-tunables.exp
> @@ -10,6 +10,7 @@ glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
> glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
> glibc.malloc.top_pad: 0x20000 (min: 0x0, max: 0x[f]+)
> glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.rtld.audit_static_tls: 0x0 (min: 0x0, max: 0x[f]+)
> glibc.rtld.dynamic_sort: 2 (min: 1, max: 2)
> glibc.rtld.enable_secure: 0 (min: 0, max: 1)
> glibc.rtld.execstack: 1 (min: 0, max: 2)
> diff --git a/manual/tunables.texi b/manual/tunables.texi
> index 2cb516b1dc..12565c6ae8 100644
> --- a/manual/tunables.texi
> +++ b/manual/tunables.texi
> @@ -429,6 +429,28 @@ for (for example) audit modules that have a higher-than-default
> alignment requirement.
> @end deftp
>
> +@deftp Tunable glibc.rtld.audit_static_tls
> +Sets an additional amount of memory to be allocated in the initial
> +thread's TLS when one or more auditing modules are to be loaded.
> +Since auditing modules must run before the loader is done loading
> +dynamic objects, the loader cannot know how much TLS space is needed
> +before it must create the first TLS area. If no auditing modules are
> +loaded, the initial TLS size can be computed after the dynamic objects
> +are loaded. When this tunable is used, the initial TLS size can be
> +very large, and the size computation is adjusted after loading is
> +complete, so that any additional threads created will have a more
> +suitable TLS size (including any optional space as specified by
> +@code{glibc.rtld.optional_static_tls}). The default is to not
> +allocate any additional TLS for audit modules.
> +
> +As an example, if a program required 1MB of TLS space and specified an
> +optional 1MB surplus, normally every thread would have 2MB of TLS
> +space. If an auditing module requires 3MB of TLS space, and 5MB of
> +audit TLS was specified by this tunable, the main thread would have
> +7MB of TLS space (2MB plus the specified 5MB) and every other thread
> +would have 5MB (2MB plus the actual 3MB).
> +@end deftp
> +
> @deftp Tunable glibc.rtld.dynamic_sort
> Sets the algorithm to use for DSO sorting, valid values are @samp{1} and
> @samp{2}. For value of @samp{1}, an older O(n^3) algorithm is used, which is
> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
> index 305ca6e0df..54535452b3 100644
> --- a/sysdeps/generic/ldsodefs.h
> +++ b/sysdeps/generic/ldsodefs.h
> @@ -1184,6 +1184,11 @@ extern void _dl_determine_tlsoffset (void) attribute_hidden;
> number of audit modules are loaded. */
> void _dl_tls_static_surplus_init (size_t naudit) attribute_hidden;
>
> +/* After startup is complete but before user entry, the size of the
> + audit modules is more knowable so we can reduce the TLS size
> + accordingly. */
> +void _dl_tls_prune_audit_storage (void) attribute_hidden;
> +
> /* This function is called very early from dl_main to set up TLS and
> other thread-related data structures. */
> void __tls_pre_init_tp (void) attribute_hidden;
On 29/10/26 13:44, DJ Delorie wrote:
>
> Add glibc.rtld.audit_static_tls which specifies additional
> surplus TLS just for audit modules, as when audit modules are
> used the loader can't compute TLS space before the auditors
> run. This surplus exists until the loader can recompute the
> TLS needs, and is reduced for future threads.
The reserved space is not used only by audit modules, since init_tls
runs before the main program's dependencies are loaded. So *every*
object loaded at startup that needs static TLS gets it from the
surplus, including the program's own dependencies and the base
namespace libc.so. I think it is worth to mention this on commit
message.
This patch also needs a NEWS entry for the new tunable and tests.
> ---
> Changes in v3:
> - rebased
> - fixed typo in dl-tls.c
> Changes in v2:
> - rebased
> - fix tst-rtld-list-tunables
>
> elf/dl-tls.c | 43 ++++++++++++++++++++++++++++++++--
> elf/dl-tunables.list | 5 ++++
> elf/rtld.c | 2 ++
> elf/tst-rtld-list-tunables.exp | 1 +
> manual/tunables.texi | 22 +++++++++++++++++
> sysdeps/generic/ldsodefs.h | 5 ++++
> 6 files changed, 76 insertions(+), 2 deletions(-)
>
> diff --git a/elf/dl-tls.c b/elf/dl-tls.c
> index 31b26fe8d3..f7308d8ce5 100644
> --- a/elf/dl-tls.c
> +++ b/elf/dl-tls.c
> @@ -130,6 +130,10 @@ tls_static_surplus (int nns, int opt_tls)
> tunable, and sets an additional minimum alignment on the TLS
> data. */
> static size_t optional_static_tls_alignment = 0;
> +static size_t dl_audit_tls = 0;
> +static size_t dl_original_optional = 0;
> +static size_t dl_original_surplus = 0;
The 'dl_original_surplus' is not really required, nothing access it after
_dl_tls_static_surplus_init.
> +static size_t dl_original_align = 1;
>
> /* Calculate the size of the static TLS surplus, when the given
> number of audit modules are loaded. Must be called after the
> @@ -145,6 +149,8 @@ _dl_tls_static_surplus_init (size_t naudit)
> /* Ignore invalid requests. */
> if (! powerof2 (optional_static_tls_alignment))
> optional_static_tls_alignment = 0;
> + if (naudit)
No implicit checks: 'if (naudit > 0)'.
> + dl_audit_tls = TUNABLE_GET (audit_static_tls, size_t, NULL);
> if (nns > DL_NNS)
> nns = DL_NNS;
> if (DL_NNS - nns < naudit)
> @@ -155,6 +161,33 @@ _dl_tls_static_surplus_init (size_t naudit)
> GL(dl_tls_static_optional) = opt_tls;
> assert (LEGACY_TLS >= 0);
> GLRO(dl_tls_static_surplus) = tls_static_surplus (nns, opt_tls) + LEGACY_TLS;
> +
> + if (dl_audit_tls > 0)
> + {
> + if (dl_audit_tls >= SIZE_MAX / 2)
> + _dl_fatal_printf ("glibc.rtld.audit_static_tls exceeds SIZE_MAX/2\n");
The usual way to do this is a maxval in dl-tunables.list, which makes the loader
ignore out-of-range values. I think it would be better to just use very large
max value (0x40000000 / 1 GiB) or maybe improve the tunables definition to
accept symbolic value (like SSIZE_MAX_DIV_2):
cat << EOF > auditmod.c
#include <link.h>
#include <stdio.h>
extern void _dl_get_tls_static_info (size_t *, size_t *);
__thread char aud_buf[56 * 1024] __attribute__ ((tls_model ("initial-exec")));
unsigned int
la_version (unsigned int version)
{
size_t size, align;
_dl_get_tls_static_info (&size, &align);
fprintf (stderr, "auditor: static TLS size %zu (initial thread block)\n",
size);
aud_buf[0] = 1;
return version;
}
EOF
cat <<EOF > libie.c
__thread char ie_buf[56 * 1024] __attribute__ ((tls_model ("initial-exec")));
char *
ie_addr (void)
{
return ie_buf;
}
EOF
cat << EOF > main.c
#include <dlfcn.h>
#include <stdio.h>
extern void _dl_get_tls_static_info (size_t *, size_t *);
int
main (int argc, char **argv)
{
size_t size, align;
_dl_get_tls_static_info (&size, &align);
printf ("main: static TLS size %zu\n", size);
fflush (stdout);
void *h = dlopen (argv[1], RTLD_NOW);
if (h == NULL)
{
printf ("main: dlopen failed: %s\n", dlerror ());
return 1;
}
puts ("main: dlopen succeeded");
return 0;
}
EOF
$ gcc -O2 -o main main.c
$ gcc -O2 -fPIC -shared -o libie.so libie.c
$ gcc -O2 -fPIC -shared -o auditmod.so auditmod.c
$ GLIBC_TUNABLES=glibc.rtld.optional_static_tls=0x10000 \
elf/ld.so --library-path . --audit auditmod.so main /tmp/review/libie.so
auditor: static TLS size 69440 (initial thread block)
main: static TLS size 69440
main: dlopen failed: libie.so: cannot allocate memory in static TLS block: ie_buf: requested 000000000000e940, available 0000000000002e00
$ GLIBC_TUNABLES=glibc.rtld.optional_static_tls=0x10000:glibc.rtld.audit_static_tls=16 \
elf/ld.so --library-path . --audit auditmod.so main libie.so
auditor: static TLS size 69440 (initial thread block)
main: static TLS size 127040
Segmentation fault
The size should only ever shrink. I think we want something like:
static size_t dl_audit_tls;
static size_t dl_original_optional;
static size_t
tls_static_size (size_t used, size_t surplus, size_t align)
{
#if TLS_TCB_AT_TP
return roundup (used + surplus, align) + TLS_TCB_SIZE;
#elif TLS_DTV_AT_TP
return roundup (used + surplus, TCB_ALIGNMENT);
#else
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
#endif
}
void
_dl_tls_static_surplus_init (size_t naudit)
{
[...]
dl_original_optional = opt_tls;
[...]
}
void
_dl_tls_prune_audit_storage (void)
{
if (dl_audit_tls == 0)
return;
size_t size = tls_static_size (GL(dl_tls_static_used),
GLRO(dl_tls_static_surplus),
GLRO(dl_tls_static_align));
if (size < GLRO(dl_tls_static_size))
{
GLRO(dl_tls_static_size) = size;
GL(dl_tls_static_optional) = dl_original_optional;
}
}
And adjust it on _dl_determine_tlsoffset:
GLRO (dl_tls_static_size) = tls_static_size (offset,
GLRO(dl_tls_static_surplus)
+ dl_audit_tls,
max_align);
With this, the above example should safely fail to load the library.
> + dl_original_optional = GL(dl_tls_static_optional);
> + dl_original_surplus = GLRO(dl_tls_static_surplus);
> + }
> +}
> +
> +/* Undo as much of the audit-specific extra TLS space accounted for
> + above, as possible. After this function, the unused amount of TLS
> + must be at least the dl_tls_static_surplus amount. */
> +void
> +_dl_tls_prune_audit_storage (void)
> +{
> + if (dl_audit_tls > 0)
> + {
> + GL(dl_tls_static_optional) = dl_original_optional;
> + GLRO(dl_tls_static_surplus) = dl_original_surplus;
> + GLRO(dl_tls_static_size) = roundup (GL(dl_tls_static_used)
> + + dl_original_surplus,
> + dl_original_align);
I think this can set GLRO(dl_tls_static_size) larger than the initial thread's
static TLS block, and the block is already allocated at this point (and cannot
grow).
The old size based on 'offset + surplus + dl_audit_tls' and the new on
'use + surplus'. So whatever the objects loaded at startup use more than
dl_audit_tls (used - offset > dl_audit_tls) the size grow.
> +#ifdef TLS_TCB_AT_TP
As Gabriel noted, this is always true (every port defines both TLS_TCB_AT_TP and
TLS_DTV_AT_TP).
> + GLRO(dl_tls_static_size) += TLS_TCB_SIZE;
> +#endif
> + }
> }
>
> /* Out-of-memory handler. */
> @@ -380,9 +413,12 @@ _dl_determine_tlsoffset (void)
> _dl_extra_tls_set_offset (-offset);
>
> GL(dl_tls_static_used) = offset;
> - GLRO (dl_tls_static_size) = (roundup (offset + GLRO(dl_tls_static_surplus),
> + GLRO (dl_tls_static_size) = (roundup (offset
> + + GLRO(dl_tls_static_surplus)
> + + dl_audit_tls,
> max_align)
> + TLS_TCB_SIZE);
> + dl_original_align = max_align;
> #elif TLS_DTV_AT_TP
> /* The TLS blocks start right after the TCB. */
> size_t offset = TLS_TCB_SIZE;
> @@ -467,8 +503,11 @@ _dl_determine_tlsoffset (void)
> offset += extra_tls_size;
>
> GL(dl_tls_static_used) = offset;
> - GLRO (dl_tls_static_size) = roundup (offset + GLRO(dl_tls_static_surplus),
> + GLRO (dl_tls_static_size) = roundup (offset
> + + GLRO(dl_tls_static_surplus)
> + + dl_audit_tls,
> TCB_ALIGNMENT);
> + dl_original_align = TCB_ALIGNMENT;
> #else
> # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
> #endif
> diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
> index 4c78444542..d8daf30ab0 100644
> --- a/elf/dl-tunables.list
> +++ b/elf/dl-tunables.list
> @@ -106,6 +106,11 @@ glibc {
> minval: 0
> default: 0
> }
> + audit_static_tls {
> + type: SIZE_T
> + minval: 0
> + default: 0
> + }
> enable_secure {
> type: INT_32
> minval: 0
> diff --git a/elf/rtld.c b/elf/rtld.c
> index 9233c50256..5799b1d007 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -2353,6 +2353,8 @@ dl_main (const ElfW(Phdr) *phdr,
> _dl_debug_post_relocate (main_map);
> }
>
> + _dl_tls_prune_audit_storage ();
> +
> /* All ld.so initialization is complete. Apply RELRO. */
> _dl_protect_relro (&_dl_rtld_map);
>
> diff --git a/elf/tst-rtld-list-tunables.exp b/elf/tst-rtld-list-tunables.exp
> index 17520d6b31..d208804723 100644
> --- a/elf/tst-rtld-list-tunables.exp
> +++ b/elf/tst-rtld-list-tunables.exp
> @@ -10,6 +10,7 @@ glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
> glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
> glibc.malloc.top_pad: 0x20000 (min: 0x0, max: 0x[f]+)
> glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
> +glibc.rtld.audit_static_tls: 0x0 (min: 0x0, max: 0x[f]+)
> glibc.rtld.dynamic_sort: 2 (min: 1, max: 2)
> glibc.rtld.enable_secure: 0 (min: 0, max: 1)
> glibc.rtld.execstack: 1 (min: 0, max: 2)
> diff --git a/manual/tunables.texi b/manual/tunables.texi
> index 2cb516b1dc..12565c6ae8 100644
> --- a/manual/tunables.texi
> +++ b/manual/tunables.texi
> @@ -429,6 +429,28 @@ for (for example) audit modules that have a higher-than-default
> alignment requirement.
> @end deftp
>
> +@deftp Tunable glibc.rtld.audit_static_tls
> +Sets an additional amount of memory to be allocated in the initial
> +thread's TLS when one or more auditing modules are to be loaded.
I think it would be worth adding that the value is in bytes and that it is
for static TLS only. The initial-exec TLS, TLS blocks accessed through an
initial-exec access, and the space used by the TLSDESC and powerpc
optimizations. Dynamic TLS in an audit module does not use this space.
> +Since auditing modules must run before the loader is done loading
> +dynamic objects, the loader cannot know how much TLS space is needed
> +before it must create the first TLS area. If no auditing modules are
> +loaded, the initial TLS size can be computed after the dynamic objects
> +are loaded. When this tunable is used, the initial TLS size can be
> +very large, and the size computation is adjusted after loading is
> +complete, so that any additional threads created will have a more
> +suitable TLS size (including any optional space as specified by
> +@code{glibc.rtld.optional_static_tls}). The default is to not
> +allocate any additional TLS for audit modules.
> +
> +As an example, if a program required 1MB of TLS space and specified an
> +optional 1MB surplus, normally every thread would have 2MB of TLS
> +space. If an auditing module requires 3MB of TLS space, and 5MB of
> +audit TLS was specified by this tunable, the main thread would have
> +7MB of TLS space (2MB plus the specified 5MB) and every other thread
> +would have 5MB (2MB plus the actual 3MB).
> +@end deftp
> +
> @deftp Tunable glibc.rtld.dynamic_sort
> Sets the algorithm to use for DSO sorting, valid values are @samp{1} and
> @samp{2}. For value of @samp{1}, an older O(n^3) algorithm is used, which is
> diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
> index 305ca6e0df..54535452b3 100644
> --- a/sysdeps/generic/ldsodefs.h
> +++ b/sysdeps/generic/ldsodefs.h
> @@ -1184,6 +1184,11 @@ extern void _dl_determine_tlsoffset (void) attribute_hidden;
> number of audit modules are loaded. */
> void _dl_tls_static_surplus_init (size_t naudit) attribute_hidden;
>
> +/* After startup is complete but before user entry, the size of the
> + audit modules is more knowable so we can reduce the TLS size
> + accordingly. */
> +void _dl_tls_prune_audit_storage (void) attribute_hidden;
> +
> /* This function is called very early from dl_main to set up TLS and
> other thread-related data structures. */
> void __tls_pre_init_tp (void) attribute_hidden;
@@ -130,6 +130,10 @@ tls_static_surplus (int nns, int opt_tls)
tunable, and sets an additional minimum alignment on the TLS
data. */
static size_t optional_static_tls_alignment = 0;
+static size_t dl_audit_tls = 0;
+static size_t dl_original_optional = 0;
+static size_t dl_original_surplus = 0;
+static size_t dl_original_align = 1;
/* Calculate the size of the static TLS surplus, when the given
number of audit modules are loaded. Must be called after the
@@ -145,6 +149,8 @@ _dl_tls_static_surplus_init (size_t naudit)
/* Ignore invalid requests. */
if (! powerof2 (optional_static_tls_alignment))
optional_static_tls_alignment = 0;
+ if (naudit)
+ dl_audit_tls = TUNABLE_GET (audit_static_tls, size_t, NULL);
if (nns > DL_NNS)
nns = DL_NNS;
if (DL_NNS - nns < naudit)
@@ -155,6 +161,33 @@ _dl_tls_static_surplus_init (size_t naudit)
GL(dl_tls_static_optional) = opt_tls;
assert (LEGACY_TLS >= 0);
GLRO(dl_tls_static_surplus) = tls_static_surplus (nns, opt_tls) + LEGACY_TLS;
+
+ if (dl_audit_tls > 0)
+ {
+ if (dl_audit_tls >= SIZE_MAX / 2)
+ _dl_fatal_printf ("glibc.rtld.audit_static_tls exceeds SIZE_MAX/2\n");
+ dl_original_optional = GL(dl_tls_static_optional);
+ dl_original_surplus = GLRO(dl_tls_static_surplus);
+ }
+}
+
+/* Undo as much of the audit-specific extra TLS space accounted for
+ above, as possible. After this function, the unused amount of TLS
+ must be at least the dl_tls_static_surplus amount. */
+void
+_dl_tls_prune_audit_storage (void)
+{
+ if (dl_audit_tls > 0)
+ {
+ GL(dl_tls_static_optional) = dl_original_optional;
+ GLRO(dl_tls_static_surplus) = dl_original_surplus;
+ GLRO(dl_tls_static_size) = roundup (GL(dl_tls_static_used)
+ + dl_original_surplus,
+ dl_original_align);
+#ifdef TLS_TCB_AT_TP
+ GLRO(dl_tls_static_size) += TLS_TCB_SIZE;
+#endif
+ }
}
/* Out-of-memory handler. */
@@ -380,9 +413,12 @@ _dl_determine_tlsoffset (void)
_dl_extra_tls_set_offset (-offset);
GL(dl_tls_static_used) = offset;
- GLRO (dl_tls_static_size) = (roundup (offset + GLRO(dl_tls_static_surplus),
+ GLRO (dl_tls_static_size) = (roundup (offset
+ + GLRO(dl_tls_static_surplus)
+ + dl_audit_tls,
max_align)
+ TLS_TCB_SIZE);
+ dl_original_align = max_align;
#elif TLS_DTV_AT_TP
/* The TLS blocks start right after the TCB. */
size_t offset = TLS_TCB_SIZE;
@@ -467,8 +503,11 @@ _dl_determine_tlsoffset (void)
offset += extra_tls_size;
GL(dl_tls_static_used) = offset;
- GLRO (dl_tls_static_size) = roundup (offset + GLRO(dl_tls_static_surplus),
+ GLRO (dl_tls_static_size) = roundup (offset
+ + GLRO(dl_tls_static_surplus)
+ + dl_audit_tls,
TCB_ALIGNMENT);
+ dl_original_align = TCB_ALIGNMENT;
#else
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
#endif
@@ -106,6 +106,11 @@ glibc {
minval: 0
default: 0
}
+ audit_static_tls {
+ type: SIZE_T
+ minval: 0
+ default: 0
+ }
enable_secure {
type: INT_32
minval: 0
@@ -2353,6 +2353,8 @@ dl_main (const ElfW(Phdr) *phdr,
_dl_debug_post_relocate (main_map);
}
+ _dl_tls_prune_audit_storage ();
+
/* All ld.so initialization is complete. Apply RELRO. */
_dl_protect_relro (&_dl_rtld_map);
@@ -10,6 +10,7 @@ glibc.malloc.tcache_count: 0x0 (min: 0x0, max: 0x[f]+)
glibc.malloc.tcache_max: 0x0 (min: 0x0, max: 0x[f]+)
glibc.malloc.top_pad: 0x20000 (min: 0x0, max: 0x[f]+)
glibc.malloc.trim_threshold: 0x0 (min: 0x0, max: 0x[f]+)
+glibc.rtld.audit_static_tls: 0x0 (min: 0x0, max: 0x[f]+)
glibc.rtld.dynamic_sort: 2 (min: 1, max: 2)
glibc.rtld.enable_secure: 0 (min: 0, max: 1)
glibc.rtld.execstack: 1 (min: 0, max: 2)
@@ -429,6 +429,28 @@ for (for example) audit modules that have a higher-than-default
alignment requirement.
@end deftp
+@deftp Tunable glibc.rtld.audit_static_tls
+Sets an additional amount of memory to be allocated in the initial
+thread's TLS when one or more auditing modules are to be loaded.
+Since auditing modules must run before the loader is done loading
+dynamic objects, the loader cannot know how much TLS space is needed
+before it must create the first TLS area. If no auditing modules are
+loaded, the initial TLS size can be computed after the dynamic objects
+are loaded. When this tunable is used, the initial TLS size can be
+very large, and the size computation is adjusted after loading is
+complete, so that any additional threads created will have a more
+suitable TLS size (including any optional space as specified by
+@code{glibc.rtld.optional_static_tls}). The default is to not
+allocate any additional TLS for audit modules.
+
+As an example, if a program required 1MB of TLS space and specified an
+optional 1MB surplus, normally every thread would have 2MB of TLS
+space. If an auditing module requires 3MB of TLS space, and 5MB of
+audit TLS was specified by this tunable, the main thread would have
+7MB of TLS space (2MB plus the specified 5MB) and every other thread
+would have 5MB (2MB plus the actual 3MB).
+@end deftp
+
@deftp Tunable glibc.rtld.dynamic_sort
Sets the algorithm to use for DSO sorting, valid values are @samp{1} and
@samp{2}. For value of @samp{1}, an older O(n^3) algorithm is used, which is
@@ -1184,6 +1184,11 @@ extern void _dl_determine_tlsoffset (void) attribute_hidden;
number of audit modules are loaded. */
void _dl_tls_static_surplus_init (size_t naudit) attribute_hidden;
+/* After startup is complete but before user entry, the size of the
+ audit modules is more knowable so we can reduce the TLS size
+ accordingly. */
+void _dl_tls_prune_audit_storage (void) attribute_hidden;
+
/* This function is called very early from dl_main to set up TLS and
other thread-related data structures. */
void __tls_pre_init_tp (void) attribute_hidden;