diff mbox series

arm: Avoid UB in elf_machine_rel()

Message ID 20220623214932.4752-1-palmer@rivosinc.com
State New
Headers show
Series arm: Avoid UB in elf_machine_rel() | expand

Commit Message

Palmer Dabbelt June 23, 2022, 9:49 p.m. UTC
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>

--

I haven't build this one, as I'm just doing it locally and don't have
everything setup here.
---
 sysdeps/arm/dl-machine.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

Fangrui Song June 24, 2022, 5:46 a.m. UTC | #1
On 2022-06-23, Palmer Dabbelt wrote:
>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>

LGTM

Reviewed-by: Fangrui Song <maskray@google.com>

>--
>
>I haven't build this one, as I'm just doing it locally and don't have
>everything setup here.
>---
> sysdeps/arm/dl-machine.h | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)
>
>diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h
>index 68dcb96d9d..6bb3917340 100644
>--- a/sysdeps/arm/dl-machine.h
>+++ b/sysdeps/arm/dl-machine.h
>@@ -402,10 +402,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
>@@ -425,7 +422,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:
>-- 
>2.34.1
>
diff mbox series

Patch

diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h
index 68dcb96d9d..6bb3917340 100644
--- a/sysdeps/arm/dl-machine.h
+++ b/sysdeps/arm/dl-machine.h
@@ -402,10 +402,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
@@ -425,7 +422,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: