]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
arm: Avoid UB in elf_machine_rel()
authorPalmer Dabbelt <palmer@rivosinc.com>
Thu, 23 Jun 2022 21:49:32 +0000 (14:49 -0700)
committerAndreas K. Hüttel <dilfridge@gentoo.org>
Wed, 26 Jun 2024 10:45:43 +0000 (12:45 +0200)
This recently came up during a cleanup to remove misaligned accesses
from the RISC-V port.

Link: https://sourceware.org/pipermail/libc-alpha/2022-June/139961.html
Suggested-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Reviewed-by: Fangrui Song <maskray@google.com>
sysdeps/arm/dl-machine.h

index dd1a0f6b6e7a5ea8bc8e476471d7089b85ff403c..9186831be3a62661bee2e5af92001d34a7ea70b0 100644 (file)
@@ -349,10 +349,7 @@ elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[],
          break;
        case R_ARM_ABS32:
          {
-           struct unaligned
-             {
-               Elf32_Addr x;
-             } __attribute__ ((packed, may_alias));
+           ElfW(Addr) tmp;
 # ifndef RTLD_BOOTSTRAP
           /* This is defined in rtld.c, but nowhere in the static
              libc.a; make the reference weak so static programs can
@@ -372,7 +369,9 @@ elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[],
              value -= SYMBOL_ADDRESS (map, refsym, true);
 # endif
            /* Support relocations on mis-aligned offsets.  */
-           ((struct unaligned *) reloc_addr)->x += value;
+           memcpy (&tmp, reloc_addr, sizeof tmp);
+           tmp += value;
+           memcpy (reloc_addr, &tmp, sizeof tmp);
            break;
          }
        case R_ARM_TLS_DESC: