x86-64: Preserve allocated TLS block in slow path [BZ #34885]

Message ID OS3PR01MB9833D5E90AE7046A0AD6B09DAE612@OS3PR01MB9833.jpnprd01.prod.outlook.com (mailing list archive)
State DCO or assignment missing
Headers
Series x86-64: Preserve allocated TLS block in slow path [BZ #34885] |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed

Commit Message

hhr2020 Oct. 26, 2026, 7:09 a.m. UTC
  When the DTV generation is stale, __tls_get_addr_slow bypasses the DTV
update for module IDs below _dl_tls_initial_modid_limit if a TLS
allocation is active.  It then calls tls_get_addr_tail unconditionally.
If the DTV entry already contains a TLS block, tls_get_addr_tail
allocates a new block, overwrites the DTV entry, leaks the old block,
and loses the thread's TLS values.

This can be reproduced with an audit module whose la_version returns 0.
The rejected auditor makes the loader initialize TLS early, so an
initially loaded module ends up with dynamically allocated TLS.  A
worker thread with a stale DTV then accesses that module's TLS while
another thread is allocating TLS.  The access returns a different
address and the stored value becomes zero; both should remain unchanged.

Return the existing block when dtv[ti_module].pointer.val is not
TLS_DTV_UNALLOCATED, matching the generic __tls_get_addr
implementation.  Call tls_get_addr_tail only for an unallocated entry.
Calling update_get_addr here would re-enter the TLS allocator while
_dl_tls_allocate_active is true.

Add elf/tst-tls-slow to cover this deterministically.  It uses a
rejected audit module, one worker thread with a stale DTV, and an
interposed malloc to pause the allocating thread while the worker
accesses the existing TLS block.

Tested on x86_64-linux-gnu:
- make subdirs=elf check: 567 PASS, 22 UNSUPPORTED, no failures.
- elf/tst-tls-slow fails with the unfixed loader and passes with the
  fix.

Signed-off-by: HHR2020 <hhr20202020@outlook.com>
---
 elf/Makefile             | 18 ++++++++
 elf/tst-tls-slow-audit.c | 27 ++++++++++++
 elf/tst-tls-slow-mod1.c  | 25 +++++++++++
 elf/tst-tls-slow-mod2.c  | 25 +++++++++++
 elf/tst-tls-slow.c       | 92 ++++++++++++++++++++++++++++++++++++++++
 sysdeps/x86_64/dl-tls.c  |  7 ++-
 6 files changed, 192 insertions(+), 2 deletions(-)
 create mode 100644 elf/tst-tls-slow-audit.c
 create mode 100644 elf/tst-tls-slow-mod1.c
 create mode 100644 elf/tst-tls-slow-mod2.c
 create mode 100644 elf/tst-tls-slow.c
  

Comments

Carlos O'Donell Oct. 26, 2026, 1:31 p.m. UTC | #1
On 10/26/26 3:09 AM, HHR2020 wrote:
> When the DTV generation is stale, __tls_get_addr_slow bypasses the DTV
> update for module IDs below _dl_tls_initial_modid_limit if a TLS
> allocation is active.  It then calls tls_get_addr_tail unconditionally.
> If the DTV entry already contains a TLS block, tls_get_addr_tail
> allocates a new block, overwrites the DTV entry, leaks the old block,
> and loses the thread's TLS values.
> 
> This can be reproduced with an audit module whose la_version returns 0.
> The rejected auditor makes the loader initialize TLS early, so an
> initially loaded module ends up with dynamically allocated TLS.  A
> worker thread with a stale DTV then accesses that module's TLS while
> another thread is allocating TLS.  The access returns a different
> address and the stored value becomes zero; both should remain unchanged.
> 
> Return the existing block when dtv[ti_module].pointer.val is not
> TLS_DTV_UNALLOCATED, matching the generic __tls_get_addr
> implementation.  Call tls_get_addr_tail only for an unallocated entry.
> Calling update_get_addr here would re-enter the TLS allocator while
> _dl_tls_allocate_active is true.
> 
> Add elf/tst-tls-slow to cover this deterministically.  It uses a
> rejected audit module, one worker thread with a stale DTV, and an
> interposed malloc to pause the allocating thread while the worker
> accesses the existing TLS block.
> 
> Tested on x86_64-linux-gnu:
> - make subdirs=elf check: 567 PASS, 22 UNSUPPORTED, no failures.
> - elf/tst-tls-slow fails with the unfixed loader and passes with the
>    fix.
> 
> Signed-off-by: HHR2020 <hhr20202020@outlook.com>

Please note that DCO does not permit anonymous contributions, but *does*
permit pseudonymous contributions (like assignment to the FSF would too).

Is this email anonymous or do you have a pseudonym that you normally use?


> ---
>   elf/Makefile             | 18 ++++++++
>   elf/tst-tls-slow-audit.c | 27 ++++++++++++
>   elf/tst-tls-slow-mod1.c  | 25 +++++++++++
>   elf/tst-tls-slow-mod2.c  | 25 +++++++++++
>   elf/tst-tls-slow.c       | 92 ++++++++++++++++++++++++++++++++++++++++
>   sysdeps/x86_64/dl-tls.c  |  7 ++-
>   6 files changed, 192 insertions(+), 2 deletions(-)
>   create mode 100644 elf/tst-tls-slow-audit.c
>   create mode 100644 elf/tst-tls-slow-mod1.c
>   create mode 100644 elf/tst-tls-slow-mod2.c
>   create mode 100644 elf/tst-tls-slow.c
> 
> diff --git a/elf/Makefile b/elf/Makefile
> index f741e78064..f57241e4e4 100644
> --- a/elf/Makefile
> +++ b/elf/Makefile
> @@ -1,4 +1,5 @@
>   # Copyright (C) 1995-2026 Free Software Foundation, Inc.
> +# Copyright The GNU Toolchain Authors.
>   # This file is part of the GNU C Library.
>   
>   # The GNU C Library is free software; you can redistribute it and/or
> @@ -514,6 +515,7 @@ tests += \
>     tst-tls-ie \
>     tst-tls-ie-dlmopen \
>     tst-tls-manydynamic \
> +  tst-tls-slow \
>     tst-tls4 \
>     tst-tls5 \
>     tst-tls10 \
> @@ -1085,6 +1087,9 @@ modules-names += \
>     tst-tls-ie-mod4 \
>     tst-tls-ie-mod5 \
>     tst-tls-ie-mod6 \
> +  tst-tls-slow-audit \
> +  tst-tls-slow-mod1 \
> +  tst-tls-slow-mod2 \
>     tst-tls19mod1 \
>     tst-tls19mod2 \
>     tst-tls19mod3 \
> @@ -3694,6 +3699,19 @@ tst-tls22-mod1-gnu2.so-no-z-defs = yes
>   tst-tls22-mod2.so-no-z-defs = yes
>   tst-tls22-mod2-gnu2.so-no-z-defs = yes
>   
> +$(objpfx)tst-tls-slow: $(objpfx)tst-tls-slow-mod1.so \
> +  $(shared-thread-library)
> +$(objpfx)tst-tls-slow.out: $(objpfx)tst-tls-slow-mod2.so \
> +  $(objpfx)tst-tls-slow-audit.so
> +tst-tls-slow-ENV = LD_AUDIT=$(objpfx)tst-tls-slow-audit.so
> +# The rejected audit module must load libc in its namespace.
> +LDFLAGS-tst-tls-slow-audit.so = -Wl,--no-as-needed
> +# Exercise __tls_get_addr rather than the TLS descriptor resolver.
> +ifneq (no,$(have-test-mtls-traditional))
> +CFLAGS-tst-tls-slow-mod1.c += -mtls-dialect=$(have-test-mtls-traditional)
> +CFLAGS-tst-tls-slow-mod2.c += -mtls-dialect=$(have-test-mtls-traditional)
> +endif
> +
>   $(objpfx)tst-tls23: $(shared-thread-library)
>   $(objpfx)tst-tls23.out: $(objpfx)tst-tls23-mod.so
>   
> diff --git a/elf/tst-tls-slow-audit.c b/elf/tst-tls-slow-audit.c
> new file mode 100644
> index 0000000000..9d861db678
> --- /dev/null
> +++ b/elf/tst-tls-slow-audit.c
> @@ -0,0 +1,27 @@
> +/* Rejected audit module for tst-tls-slow.
> +   Copyright The GNU Toolchain Authors.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <link.h>
> +
> +unsigned int
> +la_version (unsigned int version)
> +{
> +  /* Loading and then rejecting this module and its libc dependency
> +     leaves the initially loaded module with dynamically allocated TLS.  */
> +  return 0;
> +}
> diff --git a/elf/tst-tls-slow-mod1.c b/elf/tst-tls-slow-mod1.c
> new file mode 100644
> index 0000000000..361be1a9c1
> --- /dev/null
> +++ b/elf/tst-tls-slow-mod1.c
> @@ -0,0 +1,25 @@
> +/* Initially loaded TLS module for tst-tls-slow.
> +   Copyright The GNU Toolchain Authors.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +static __thread void *slot;
> +
> +void **
> +tls_slow_address (void)
> +{
> +  return &slot;
> +}
> diff --git a/elf/tst-tls-slow-mod2.c b/elf/tst-tls-slow-mod2.c
> new file mode 100644
> index 0000000000..a72c4049f0
> --- /dev/null
> +++ b/elf/tst-tls-slow-mod2.c
> @@ -0,0 +1,25 @@
> +/* Dynamically loaded TLS module for tst-tls-slow.
> +   Copyright The GNU Toolchain Authors.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +static __thread int slot;
> +
> +int *
> +tls_slow_dynamic_address (void)
> +{
> +  return &slot;
> +}
> diff --git a/elf/tst-tls-slow.c b/elf/tst-tls-slow.c
> new file mode 100644
> index 0000000000..40d2678bc8
> --- /dev/null
> +++ b/elf/tst-tls-slow.c
> @@ -0,0 +1,92 @@
> +/* Check that the TLS slow path preserves an allocated TLS block.
> +   Copyright The GNU Toolchain Authors.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <stdbool.h>
> +#include <stdatomic.h>
> +#include <stdlib.h>
> +#include <support/check.h>
> +#include <support/xdlfcn.h>
> +#include <support/xthread.h>
> +
> +void **tls_slow_address (void);
> +extern void *__libc_malloc (size_t);
> +
> +static pthread_barrier_t initialized;
> +static pthread_barrier_t allocating;
> +static pthread_barrier_t checked;
> +static atomic_bool intercept_malloc;
> +
> +void *
> +malloc (size_t size)
> +{
> +  if (atomic_exchange (&intercept_malloc, false))
> +    {
> +      /* The main thread is allocating the dynamically loaded module's
> +         TLS block.  Keep _dl_tls_allocate_active () true while the worker
> +         accesses its already allocated TLS block.  The flag is cleared
> +         first so that a second allocation in the worker does not block.  */
> +      xpthread_barrier_wait (&allocating);
> +      xpthread_barrier_wait (&checked);
> +    }
> +  return __libc_malloc (size);
> +}
> +
> +static void *
> +worker (void *closure)
> +{
> +  int token;
> +  void **initial = tls_slow_address ();
> +  *initial = &token;
> +  xpthread_barrier_wait (&initialized);
> +
> +  /* Wait until dlopen has advanced the global TLS generation and the
> +     main thread is inside the TLS allocator.  This thread's DTV still
> +     has the old generation.  */
> +  xpthread_barrier_wait (&allocating);
> +  void **current = tls_slow_address ();
> +  TEST_VERIFY (current == initial);
> +  TEST_VERIFY (*current == &token);
> +  xpthread_barrier_wait (&checked);
> +  return NULL;
> +}
> +
> +static int
> +do_test (void)
> +{
> +  xpthread_barrier_init (&initialized, NULL, 2);
> +  xpthread_barrier_init (&allocating, NULL, 2);
> +  xpthread_barrier_init (&checked, NULL, 2);
> +  pthread_t thread = xpthread_create (NULL, worker, NULL);
> +  xpthread_barrier_wait (&initialized);
> +
> +  void *handle = xdlopen ("tst-tls-slow-mod2.so", RTLD_NOW | RTLD_LOCAL);
> +  int *(*address) (void) = xdlsym (handle, "tls_slow_dynamic_address");
> +  atomic_store (&intercept_malloc, true);
> +  TEST_COMPARE (*address (), 0);
> +  /* Verify that the malloc wrapper performed the synchronization.  */
> +  TEST_VERIFY_EXIT (!atomic_load (&intercept_malloc));
> +
> +  xpthread_join (thread);
> +  xdlclose (handle);
> +  xpthread_barrier_destroy (&initialized);
> +  xpthread_barrier_destroy (&allocating);
> +  xpthread_barrier_destroy (&checked);
> +  return 0;
> +}
> +
> +#include <support/test-driver.c>
> diff --git a/sysdeps/x86_64/dl-tls.c b/sysdeps/x86_64/dl-tls.c
> index 6aee0972bb..3ab52a133d 100644
> --- a/sysdeps/x86_64/dl-tls.c
> +++ b/sysdeps/x86_64/dl-tls.c
> @@ -1,5 +1,6 @@
>   /* Thread-local storage handling in the ELF dynamic linker.  x86-64 version.
>      Copyright (C) 2017-2026 Free Software Foundation, Inc.
> +   Copyright The GNU Toolchain Authors.
>      This file is part of the GNU C Library.
>   
>      The GNU C Library is free software; you can redistribute it and/or
> @@ -64,15 +65,17 @@ __tls_get_addr_slow (tls_index *ti)
>   	}
>       }
>   
> -  if (__glibc_unlikely (dtv[ti->ti_module].pointer.val == TLS_DTV_UNALLOCATED))
> +  void *p = dtv[ti->ti_module].pointer.val;
> +  if (__glibc_unlikely (p == TLS_DTV_UNALLOCATED))
>       {
>         if (__glibc_unlikely (GLRO (dl_debug_mask) & DL_DEBUG_TLS))
>   	_dl_debug_printf ("tls: modid %lu lazy allocation; TCB=0x%lx\n",
>   			  (unsigned long int) ti->ti_module,
>   			  (unsigned long int) THREAD_SELF);
> +      return tls_get_addr_tail (ti, dtv, NULL);
>       }
>   
> -  return tls_get_addr_tail (ti, dtv, NULL);
> +  return tls_get_addr_adjust (p, ti);
>   }
>   #else
>
  
hhr2020 Oct. 27, 2026, 12:51 a.m. UTC | #2
在 2026/10/26 21:31, Carlos O'Donell 写道:

> On 10/26/26 3:09 AM, HHR2020 wrote:
>> When the DTV generation is stale, __tls_get_addr_slow bypasses the DTV
>> update for module IDs below _dl_tls_initial_modid_limit if a TLS
>> allocation is active.  It then calls tls_get_addr_tail unconditionally.
>> If the DTV entry already contains a TLS block, tls_get_addr_tail
>> allocates a new block, overwrites the DTV entry, leaks the old block,
>> and loses the thread's TLS values.
>>
>> This can be reproduced with an audit module whose la_version returns 0.
>> The rejected auditor makes the loader initialize TLS early, so an
>> initially loaded module ends up with dynamically allocated TLS. A
>> worker thread with a stale DTV then accesses that module's TLS while
>> another thread is allocating TLS.  The access returns a different
>> address and the stored value becomes zero; both should remain unchanged.
>>
>> Return the existing block when dtv[ti_module].pointer.val is not
>> TLS_DTV_UNALLOCATED, matching the generic __tls_get_addr
>> implementation.  Call tls_get_addr_tail only for an unallocated entry.
>> Calling update_get_addr here would re-enter the TLS allocator while
>> _dl_tls_allocate_active is true.
>>
>> Add elf/tst-tls-slow to cover this deterministically.  It uses a
>> rejected audit module, one worker thread with a stale DTV, and an
>> interposed malloc to pause the allocating thread while the worker
>> accesses the existing TLS block.
>>
>> Tested on x86_64-linux-gnu:
>> - make subdirs=elf check: 567 PASS, 22 UNSUPPORTED, no failures.
>> - elf/tst-tls-slow fails with the unfixed loader and passes with the
>>    fix.
>>
>> Signed-off-by: HHR2020 <hhr20202020@outlook.com>
>
> Please note that DCO does not permit anonymous contributions, but *does*
> permit pseudonymous contributions (like assignment to the FSF would too).
>
> Is this email anonymous or do you have a pseudonym that you normally use?
>
Hi Carlos,

It is a pseudonym I normally use, not an anonymous contribution.

"HHR2020" is my usual online handle, and hhr20202020@outlook.com is a
real mailbox that I monitor and can be reached at.  I use the same
identity for my Bugzilla account and for this contribution under the
DCO.

>
>> ---
>>   elf/Makefile             | 18 ++++++++
>>   elf/tst-tls-slow-audit.c | 27 ++++++++++++
>>   elf/tst-tls-slow-mod1.c  | 25 +++++++++++
>>   elf/tst-tls-slow-mod2.c  | 25 +++++++++++
>>   elf/tst-tls-slow.c       | 92 ++++++++++++++++++++++++++++++++++++++++
>>   sysdeps/x86_64/dl-tls.c  |  7 ++-
>>   6 files changed, 192 insertions(+), 2 deletions(-)
>>   create mode 100644 elf/tst-tls-slow-audit.c
>>   create mode 100644 elf/tst-tls-slow-mod1.c
>>   create mode 100644 elf/tst-tls-slow-mod2.c
>>   create mode 100644 elf/tst-tls-slow.c
>>
>> diff --git a/elf/Makefile b/elf/Makefile
>> index f741e78064..f57241e4e4 100644
>> --- a/elf/Makefile
>> +++ b/elf/Makefile
>> @@ -1,4 +1,5 @@
>>   # Copyright (C) 1995-2026 Free Software Foundation, Inc.
>> +# Copyright The GNU Toolchain Authors.
>>   # This file is part of the GNU C Library.
>>     # The GNU C Library is free software; you can redistribute it and/or
>> @@ -514,6 +515,7 @@ tests += \
>>     tst-tls-ie \
>>     tst-tls-ie-dlmopen \
>>     tst-tls-manydynamic \
>> +  tst-tls-slow \
>>     tst-tls4 \
>>     tst-tls5 \
>>     tst-tls10 \
>> @@ -1085,6 +1087,9 @@ modules-names += \
>>     tst-tls-ie-mod4 \
>>     tst-tls-ie-mod5 \
>>     tst-tls-ie-mod6 \
>> +  tst-tls-slow-audit \
>> +  tst-tls-slow-mod1 \
>> +  tst-tls-slow-mod2 \
>>     tst-tls19mod1 \
>>     tst-tls19mod2 \
>>     tst-tls19mod3 \
>> @@ -3694,6 +3699,19 @@ tst-tls22-mod1-gnu2.so-no-z-defs = yes
>>   tst-tls22-mod2.so-no-z-defs = yes
>>   tst-tls22-mod2-gnu2.so-no-z-defs = yes
>>   +$(objpfx)tst-tls-slow: $(objpfx)tst-tls-slow-mod1.so \
>> +  $(shared-thread-library)
>> +$(objpfx)tst-tls-slow.out: $(objpfx)tst-tls-slow-mod2.so \
>> +  $(objpfx)tst-tls-slow-audit.so
>> +tst-tls-slow-ENV = LD_AUDIT=$(objpfx)tst-tls-slow-audit.so
>> +# The rejected audit module must load libc in its namespace.
>> +LDFLAGS-tst-tls-slow-audit.so = -Wl,--no-as-needed
>> +# Exercise __tls_get_addr rather than the TLS descriptor resolver.
>> +ifneq (no,$(have-test-mtls-traditional))
>> +CFLAGS-tst-tls-slow-mod1.c += 
>> -mtls-dialect=$(have-test-mtls-traditional)
>> +CFLAGS-tst-tls-slow-mod2.c += 
>> -mtls-dialect=$(have-test-mtls-traditional)
>> +endif
>> +
>>   $(objpfx)tst-tls23: $(shared-thread-library)
>>   $(objpfx)tst-tls23.out: $(objpfx)tst-tls23-mod.so
>>   diff --git a/elf/tst-tls-slow-audit.c b/elf/tst-tls-slow-audit.c
>> new file mode 100644
>> index 0000000000..9d861db678
>> --- /dev/null
>> +++ b/elf/tst-tls-slow-audit.c
>> @@ -0,0 +1,27 @@
>> +/* Rejected audit module for tst-tls-slow.
>> +   Copyright The GNU Toolchain Authors.
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#include <link.h>
>> +
>> +unsigned int
>> +la_version (unsigned int version)
>> +{
>> +  /* Loading and then rejecting this module and its libc dependency
>> +     leaves the initially loaded module with dynamically allocated 
>> TLS.  */
>> +  return 0;
>> +}
>> diff --git a/elf/tst-tls-slow-mod1.c b/elf/tst-tls-slow-mod1.c
>> new file mode 100644
>> index 0000000000..361be1a9c1
>> --- /dev/null
>> +++ b/elf/tst-tls-slow-mod1.c
>> @@ -0,0 +1,25 @@
>> +/* Initially loaded TLS module for tst-tls-slow.
>> +   Copyright The GNU Toolchain Authors.
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +static __thread void *slot;
>> +
>> +void **
>> +tls_slow_address (void)
>> +{
>> +  return &slot;
>> +}
>> diff --git a/elf/tst-tls-slow-mod2.c b/elf/tst-tls-slow-mod2.c
>> new file mode 100644
>> index 0000000000..a72c4049f0
>> --- /dev/null
>> +++ b/elf/tst-tls-slow-mod2.c
>> @@ -0,0 +1,25 @@
>> +/* Dynamically loaded TLS module for tst-tls-slow.
>> +   Copyright The GNU Toolchain Authors.
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +static __thread int slot;
>> +
>> +int *
>> +tls_slow_dynamic_address (void)
>> +{
>> +  return &slot;
>> +}
>> diff --git a/elf/tst-tls-slow.c b/elf/tst-tls-slow.c
>> new file mode 100644
>> index 0000000000..40d2678bc8
>> --- /dev/null
>> +++ b/elf/tst-tls-slow.c
>> @@ -0,0 +1,92 @@
>> +/* Check that the TLS slow path preserves an allocated TLS block.
>> +   Copyright The GNU Toolchain Authors.
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#include <stdbool.h>
>> +#include <stdatomic.h>
>> +#include <stdlib.h>
>> +#include <support/check.h>
>> +#include <support/xdlfcn.h>
>> +#include <support/xthread.h>
>> +
>> +void **tls_slow_address (void);
>> +extern void *__libc_malloc (size_t);
>> +
>> +static pthread_barrier_t initialized;
>> +static pthread_barrier_t allocating;
>> +static pthread_barrier_t checked;
>> +static atomic_bool intercept_malloc;
>> +
>> +void *
>> +malloc (size_t size)
>> +{
>> +  if (atomic_exchange (&intercept_malloc, false))
>> +    {
>> +      /* The main thread is allocating the dynamically loaded module's
>> +         TLS block.  Keep _dl_tls_allocate_active () true while the 
>> worker
>> +         accesses its already allocated TLS block.  The flag is cleared
>> +         first so that a second allocation in the worker does not 
>> block.  */
>> +      xpthread_barrier_wait (&allocating);
>> +      xpthread_barrier_wait (&checked);
>> +    }
>> +  return __libc_malloc (size);
>> +}
>> +
>> +static void *
>> +worker (void *closure)
>> +{
>> +  int token;
>> +  void **initial = tls_slow_address ();
>> +  *initial = &token;
>> +  xpthread_barrier_wait (&initialized);
>> +
>> +  /* Wait until dlopen has advanced the global TLS generation and the
>> +     main thread is inside the TLS allocator.  This thread's DTV still
>> +     has the old generation.  */
>> +  xpthread_barrier_wait (&allocating);
>> +  void **current = tls_slow_address ();
>> +  TEST_VERIFY (current == initial);
>> +  TEST_VERIFY (*current == &token);
>> +  xpthread_barrier_wait (&checked);
>> +  return NULL;
>> +}
>> +
>> +static int
>> +do_test (void)
>> +{
>> +  xpthread_barrier_init (&initialized, NULL, 2);
>> +  xpthread_barrier_init (&allocating, NULL, 2);
>> +  xpthread_barrier_init (&checked, NULL, 2);
>> +  pthread_t thread = xpthread_create (NULL, worker, NULL);
>> +  xpthread_barrier_wait (&initialized);
>> +
>> +  void *handle = xdlopen ("tst-tls-slow-mod2.so", RTLD_NOW | 
>> RTLD_LOCAL);
>> +  int *(*address) (void) = xdlsym (handle, "tls_slow_dynamic_address");
>> +  atomic_store (&intercept_malloc, true);
>> +  TEST_COMPARE (*address (), 0);
>> +  /* Verify that the malloc wrapper performed the synchronization.  */
>> +  TEST_VERIFY_EXIT (!atomic_load (&intercept_malloc));
>> +
>> +  xpthread_join (thread);
>> +  xdlclose (handle);
>> +  xpthread_barrier_destroy (&initialized);
>> +  xpthread_barrier_destroy (&allocating);
>> +  xpthread_barrier_destroy (&checked);
>> +  return 0;
>> +}
>> +
>> +#include <support/test-driver.c>
>> diff --git a/sysdeps/x86_64/dl-tls.c b/sysdeps/x86_64/dl-tls.c
>> index 6aee0972bb..3ab52a133d 100644
>> --- a/sysdeps/x86_64/dl-tls.c
>> +++ b/sysdeps/x86_64/dl-tls.c
>> @@ -1,5 +1,6 @@
>>   /* Thread-local storage handling in the ELF dynamic linker. x86-64 
>> version.
>>      Copyright (C) 2017-2026 Free Software Foundation, Inc.
>> +   Copyright The GNU Toolchain Authors.
>>      This file is part of the GNU C Library.
>>        The GNU C Library is free software; you can redistribute it 
>> and/or
>> @@ -64,15 +65,17 @@ __tls_get_addr_slow (tls_index *ti)
>>       }
>>       }
>>   -  if (__glibc_unlikely (dtv[ti->ti_module].pointer.val == 
>> TLS_DTV_UNALLOCATED))
>> +  void *p = dtv[ti->ti_module].pointer.val;
>> +  if (__glibc_unlikely (p == TLS_DTV_UNALLOCATED))
>>       {
>>         if (__glibc_unlikely (GLRO (dl_debug_mask) & DL_DEBUG_TLS))
>>       _dl_debug_printf ("tls: modid %lu lazy allocation; TCB=0x%lx\n",
>>                 (unsigned long int) ti->ti_module,
>>                 (unsigned long int) THREAD_SELF);
>> +      return tls_get_addr_tail (ti, dtv, NULL);
>>       }
>>   -  return tls_get_addr_tail (ti, dtv, NULL);
>> +  return tls_get_addr_adjust (p, ti);
>>   }
>>   #else
>
>
  

Patch

diff --git a/elf/Makefile b/elf/Makefile
index f741e78064..f57241e4e4 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -1,4 +1,5 @@ 
 # Copyright (C) 1995-2026 Free Software Foundation, Inc.
+# Copyright The GNU Toolchain Authors.
 # This file is part of the GNU C Library.
 
 # The GNU C Library is free software; you can redistribute it and/or
@@ -514,6 +515,7 @@  tests += \
   tst-tls-ie \
   tst-tls-ie-dlmopen \
   tst-tls-manydynamic \
+  tst-tls-slow \
   tst-tls4 \
   tst-tls5 \
   tst-tls10 \
@@ -1085,6 +1087,9 @@  modules-names += \
   tst-tls-ie-mod4 \
   tst-tls-ie-mod5 \
   tst-tls-ie-mod6 \
+  tst-tls-slow-audit \
+  tst-tls-slow-mod1 \
+  tst-tls-slow-mod2 \
   tst-tls19mod1 \
   tst-tls19mod2 \
   tst-tls19mod3 \
@@ -3694,6 +3699,19 @@  tst-tls22-mod1-gnu2.so-no-z-defs = yes
 tst-tls22-mod2.so-no-z-defs = yes
 tst-tls22-mod2-gnu2.so-no-z-defs = yes
 
+$(objpfx)tst-tls-slow: $(objpfx)tst-tls-slow-mod1.so \
+  $(shared-thread-library)
+$(objpfx)tst-tls-slow.out: $(objpfx)tst-tls-slow-mod2.so \
+  $(objpfx)tst-tls-slow-audit.so
+tst-tls-slow-ENV = LD_AUDIT=$(objpfx)tst-tls-slow-audit.so
+# The rejected audit module must load libc in its namespace.
+LDFLAGS-tst-tls-slow-audit.so = -Wl,--no-as-needed
+# Exercise __tls_get_addr rather than the TLS descriptor resolver.
+ifneq (no,$(have-test-mtls-traditional))
+CFLAGS-tst-tls-slow-mod1.c += -mtls-dialect=$(have-test-mtls-traditional)
+CFLAGS-tst-tls-slow-mod2.c += -mtls-dialect=$(have-test-mtls-traditional)
+endif
+
 $(objpfx)tst-tls23: $(shared-thread-library)
 $(objpfx)tst-tls23.out: $(objpfx)tst-tls23-mod.so
 
diff --git a/elf/tst-tls-slow-audit.c b/elf/tst-tls-slow-audit.c
new file mode 100644
index 0000000000..9d861db678
--- /dev/null
+++ b/elf/tst-tls-slow-audit.c
@@ -0,0 +1,27 @@ 
+/* Rejected audit module for tst-tls-slow.
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <link.h>
+
+unsigned int
+la_version (unsigned int version)
+{
+  /* Loading and then rejecting this module and its libc dependency
+     leaves the initially loaded module with dynamically allocated TLS.  */
+  return 0;
+}
diff --git a/elf/tst-tls-slow-mod1.c b/elf/tst-tls-slow-mod1.c
new file mode 100644
index 0000000000..361be1a9c1
--- /dev/null
+++ b/elf/tst-tls-slow-mod1.c
@@ -0,0 +1,25 @@ 
+/* Initially loaded TLS module for tst-tls-slow.
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+static __thread void *slot;
+
+void **
+tls_slow_address (void)
+{
+  return &slot;
+}
diff --git a/elf/tst-tls-slow-mod2.c b/elf/tst-tls-slow-mod2.c
new file mode 100644
index 0000000000..a72c4049f0
--- /dev/null
+++ b/elf/tst-tls-slow-mod2.c
@@ -0,0 +1,25 @@ 
+/* Dynamically loaded TLS module for tst-tls-slow.
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+static __thread int slot;
+
+int *
+tls_slow_dynamic_address (void)
+{
+  return &slot;
+}
diff --git a/elf/tst-tls-slow.c b/elf/tst-tls-slow.c
new file mode 100644
index 0000000000..40d2678bc8
--- /dev/null
+++ b/elf/tst-tls-slow.c
@@ -0,0 +1,92 @@ 
+/* Check that the TLS slow path preserves an allocated TLS block.
+   Copyright The GNU Toolchain Authors.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdbool.h>
+#include <stdatomic.h>
+#include <stdlib.h>
+#include <support/check.h>
+#include <support/xdlfcn.h>
+#include <support/xthread.h>
+
+void **tls_slow_address (void);
+extern void *__libc_malloc (size_t);
+
+static pthread_barrier_t initialized;
+static pthread_barrier_t allocating;
+static pthread_barrier_t checked;
+static atomic_bool intercept_malloc;
+
+void *
+malloc (size_t size)
+{
+  if (atomic_exchange (&intercept_malloc, false))
+    {
+      /* The main thread is allocating the dynamically loaded module's
+         TLS block.  Keep _dl_tls_allocate_active () true while the worker
+         accesses its already allocated TLS block.  The flag is cleared
+         first so that a second allocation in the worker does not block.  */
+      xpthread_barrier_wait (&allocating);
+      xpthread_barrier_wait (&checked);
+    }
+  return __libc_malloc (size);
+}
+
+static void *
+worker (void *closure)
+{
+  int token;
+  void **initial = tls_slow_address ();
+  *initial = &token;
+  xpthread_barrier_wait (&initialized);
+
+  /* Wait until dlopen has advanced the global TLS generation and the
+     main thread is inside the TLS allocator.  This thread's DTV still
+     has the old generation.  */
+  xpthread_barrier_wait (&allocating);
+  void **current = tls_slow_address ();
+  TEST_VERIFY (current == initial);
+  TEST_VERIFY (*current == &token);
+  xpthread_barrier_wait (&checked);
+  return NULL;
+}
+
+static int
+do_test (void)
+{
+  xpthread_barrier_init (&initialized, NULL, 2);
+  xpthread_barrier_init (&allocating, NULL, 2);
+  xpthread_barrier_init (&checked, NULL, 2);
+  pthread_t thread = xpthread_create (NULL, worker, NULL);
+  xpthread_barrier_wait (&initialized);
+
+  void *handle = xdlopen ("tst-tls-slow-mod2.so", RTLD_NOW | RTLD_LOCAL);
+  int *(*address) (void) = xdlsym (handle, "tls_slow_dynamic_address");
+  atomic_store (&intercept_malloc, true);
+  TEST_COMPARE (*address (), 0);
+  /* Verify that the malloc wrapper performed the synchronization.  */
+  TEST_VERIFY_EXIT (!atomic_load (&intercept_malloc));
+
+  xpthread_join (thread);
+  xdlclose (handle);
+  xpthread_barrier_destroy (&initialized);
+  xpthread_barrier_destroy (&allocating);
+  xpthread_barrier_destroy (&checked);
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/sysdeps/x86_64/dl-tls.c b/sysdeps/x86_64/dl-tls.c
index 6aee0972bb..3ab52a133d 100644
--- a/sysdeps/x86_64/dl-tls.c
+++ b/sysdeps/x86_64/dl-tls.c
@@ -1,5 +1,6 @@ 
 /* Thread-local storage handling in the ELF dynamic linker.  x86-64 version.
    Copyright (C) 2017-2026 Free Software Foundation, Inc.
+   Copyright The GNU Toolchain Authors.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -64,15 +65,17 @@  __tls_get_addr_slow (tls_index *ti)
 	}
     }
 
-  if (__glibc_unlikely (dtv[ti->ti_module].pointer.val == TLS_DTV_UNALLOCATED))
+  void *p = dtv[ti->ti_module].pointer.val;
+  if (__glibc_unlikely (p == TLS_DTV_UNALLOCATED))
     {
       if (__glibc_unlikely (GLRO (dl_debug_mask) & DL_DEBUG_TLS))
 	_dl_debug_printf ("tls: modid %lu lazy allocation; TCB=0x%lx\n",
 			  (unsigned long int) ti->ti_module,
 			  (unsigned long int) THREAD_SELF);
+      return tls_get_addr_tail (ti, dtv, NULL);
     }
 
-  return tls_get_addr_tail (ti, dtv, NULL);
+  return tls_get_addr_adjust (p, ti);
 }
 #else