179894
commit ddcacd91cc10ff92d6201eda87047d029c14158d
179894
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
179894
Date:   Thu Feb 11 11:40:11 2021 +0000
179894
179894
    i386: Avoid lazy relocation of tlsdesc [BZ #27137]
179894
    
179894
    Lazy tlsdesc relocation is racy because the static tls optimization and
179894
    tlsdesc management operations are done without holding the dlopen lock.
179894
    
179894
    This similar to the commit b7cf203b5c17dd6d9878537d41e0c7cc3d270a67
179894
    for aarch64, but it fixes a different race: bug 27137.
179894
    
179894
    On i386 the code is a bit more complicated than on x86_64 because both
179894
    rel and rela relocs are supported.
179894
179894
diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h
179894
index e5776ef7bc8ad749..3a30671591284d79 100644
179894
--- a/sysdeps/i386/dl-machine.h
179894
+++ b/sysdeps/i386/dl-machine.h
179894
@@ -679,50 +679,32 @@ elf_machine_lazy_rel (struct link_map *map,
179894
     }
179894
   else if (__glibc_likely (r_type == R_386_TLS_DESC))
179894
     {
179894
-      struct tlsdesc volatile * __attribute__((__unused__)) td =
179894
-	(struct tlsdesc volatile *)reloc_addr;
179894
-
179894
-      /* Handle relocations that reference the local *ABS* in a simple
179894
-	 way, so as to preserve a potential addend.  */
179894
-      if (ELF32_R_SYM (reloc->r_info) == 0)
179894
-	td->entry = _dl_tlsdesc_resolve_abs_plus_addend;
179894
-      /* Given a known-zero addend, we can store a pointer to the
179894
-	 reloc in the arg position.  */
179894
-      else if (td->arg == 0)
179894
-	{
179894
-	  td->arg = (void*)reloc;
179894
-	  td->entry = _dl_tlsdesc_resolve_rel;
179894
-	}
179894
-      else
179894
-	{
179894
-	  /* We could handle non-*ABS* relocations with non-zero addends
179894
-	     by allocating dynamically an arg to hold a pointer to the
179894
-	     reloc, but that sounds pointless.  */
179894
-	  const Elf32_Rel *const r = reloc;
179894
-	  /* The code below was borrowed from elf_dynamic_do_rel().  */
179894
-	  const ElfW(Sym) *const symtab =
179894
-	    (const void *) D_PTR (map, l_info[DT_SYMTAB]);
179894
+      const Elf32_Rel *const r = reloc;
179894
+      /* The code below was borrowed from elf_dynamic_do_rel().  */
179894
+      const ElfW(Sym) *const symtab =
179894
+	(const void *) D_PTR (map, l_info[DT_SYMTAB]);
179894
 
179894
+      /* Always initialize TLS descriptors completely at load time, in
179894
+	 case static TLS is allocated for it that requires locking.  */
179894
 # ifdef RTLD_BOOTSTRAP
179894
-	  /* The dynamic linker always uses versioning.  */
179894
-	  assert (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL);
179894
+      /* The dynamic linker always uses versioning.  */
179894
+      assert (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL);
179894
 # else
179894
-	  if (map->l_info[VERSYMIDX (DT_VERSYM)])
179894
+      if (map->l_info[VERSYMIDX (DT_VERSYM)])
179894
 # endif
179894
-	    {
179894
-	      const ElfW(Half) *const version =
179894
-		(const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
179894
-	      ElfW(Half) ndx = version[ELFW(R_SYM) (r->r_info)] & 0x7fff;
179894
-	      elf_machine_rel (map, r, &symtab[ELFW(R_SYM) (r->r_info)],
179894
-			       &map->l_versions[ndx],
179894
-			       (void *) (l_addr + r->r_offset), skip_ifunc);
179894
-	    }
179894
+	{
179894
+	  const ElfW(Half) *const version =
179894
+	    (const void *) D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
179894
+	  ElfW(Half) ndx = version[ELFW(R_SYM) (r->r_info)] & 0x7fff;
179894
+	  elf_machine_rel (map, r, &symtab[ELFW(R_SYM) (r->r_info)],
179894
+			   &map->l_versions[ndx],
179894
+			   (void *) (l_addr + r->r_offset), skip_ifunc);
179894
+	}
179894
 # ifndef RTLD_BOOTSTRAP
179894
-	  else
179894
-	    elf_machine_rel (map, r, &symtab[ELFW(R_SYM) (r->r_info)], NULL,
179894
-			     (void *) (l_addr + r->r_offset), skip_ifunc);
179894
+      else
179894
+	elf_machine_rel (map, r, &symtab[ELFW(R_SYM) (r->r_info)], NULL,
179894
+			 (void *) (l_addr + r->r_offset), skip_ifunc);
179894
 # endif
179894
-	}
179894
     }
179894
   else if (__glibc_unlikely (r_type == R_386_IRELATIVE))
179894
     {
179894
@@ -749,11 +731,21 @@ elf_machine_lazy_rela (struct link_map *map,
179894
     ;
179894
   else if (__glibc_likely (r_type == R_386_TLS_DESC))
179894
     {
179894
-      struct tlsdesc volatile * __attribute__((__unused__)) td =
179894
-	(struct tlsdesc volatile *)reloc_addr;
179894
+      const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
179894
+      const ElfW (Sym) *symtab = (const void *)D_PTR (map, l_info[DT_SYMTAB]);
179894
+      const ElfW (Sym) *sym = &symtab[symndx];
179894
+      const struct r_found_version *version = NULL;
179894
+
179894
+      if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
179894
+	{
179894
+	  const ElfW (Half) *vernum =
179894
+	    (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
179894
+	  version = &map->l_versions[vernum[symndx] & 0x7fff];
179894
+	}
179894
 
179894
-      td->arg = (void*)reloc;
179894
-      td->entry = _dl_tlsdesc_resolve_rela;
179894
+      /* Always initialize TLS descriptors completely at load time, in
179894
+	 case static TLS is allocated for it that requires locking.  */
179894
+      elf_machine_rela (map, reloc, sym, version, reloc_addr, skip_ifunc);
179894
     }
179894
   else if (__glibc_unlikely (r_type == R_386_IRELATIVE))
179894
     {