Skip to contentSkip to conversation

wireguard: allowedips: Prevent unaligned memory accesses

PATCH In review Helge Deller 2 participants 2 messages Jul 3, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wireguard: allowedips: Prevent unaligned memory accesses

On the parisc platform the Linux kernel issues kernel warnings because
swap_endian() tries to load a 128-bit IPv6 address from an unaligned
memory location:
Kernel: unaligned access to 0x55f4688c in wg_allowedips_insert_v6+0x2c/0x80 [wireguard] (iir 0xf3010df)
Kernel: unaligned access to 0x55f46884 in wg_allowedips_insert_v6+0x38/0x80 [wireguard] (iir 0xf2010dc)

Avoid such unaligned memory accesses by instead using the get_unaligned_be64() helper macro.

drivers/net/wireguard/allowedips.c +2 -2
index 0ba714ca5185..daf967130b72 100644
@@ -15,8 +15,8 @@ static void swap_endian(u8 *dst, const u8 *src, u8 bits)
 	if (bits == 32) {
 		*(u32 *)dst = be32_to_cpu(*(const __be32 *)src);
 	} else if (bits == 128) {
-		((u64 *)dst)[0] = be64_to_cpu(((const __be64 *)src)[0]);
-		((u64 *)dst)[1] = be64_to_cpu(((const __be64 *)src)[1]);
+		((u64 *)dst)[0] = get_unaligned_be64(src);
+		((u64 *)dst)[1] = get_unaligned_be64(src[8]);
 	}
 }
 
#1 lore raw

Hi Helge,

Quoted 16 lines · Helge Deller

On the parisc platform the Linux kernel issues kernel warnings because
swap_endian() tries to load a 128-bit IPv6 address from an unaligned
memory location:
Kernel: unaligned access to 0x55f4688c in wg_allowedips_insert_v6+0x2c/0x80 [wireguard] (iir 0xf3010df)
Kernel: unaligned access to 0x55f46884 in wg_allowedips_insert_v6+0x38/0x80 [wireguard] (iir 0xf2010dc)

Avoid such unaligned memory accesses by instead using the get_unaligned_be64() helper macro.

drivers/net/wireguard/allowedips.c +0 -0
index 0ba714ca5185..daf967130b72 100644

drivers/net/wireguard/allowedips.c static void swap_endian(u8 *dst, const u8 *src, u8 bits) · line 18

Quoted 6 lines
drivers/net/wireguard/allowedips.c +1 -2
@@ -15,8 +15,8 @@ static void swap_endian(u8 *dst, const u8 *src, u8 bits)
 	if (bits == 32) {
 		*(u32 *)dst = be32_to_cpu(*(const __be32 *)src);
 	} else if (bits == 128) {
-		((u64 *)dst)[0] = be64_to_cpu(((const __be64 *)src)[0]);
-		((u64 *)dst)[1] = be64_to_cpu(((const __be64 *)src)[1]);
+		((u64 *)dst)[0] = get_unaligned_be64(src);

Ahh, right, in6_addr only has a u32 member, not a u64 member, so that's potentially unaligned.

drivers/net/wireguard/allowedips.c static void swap_endian(u8 *dst, const u8 *src, u8 bits) · line 19

Quoted 1 line
drivers/net/wireguard/allowedips.c +1 -0
+		((u64 *)dst)[1] = get_unaligned_be64(src[8]);

src[8] is not correct, however. (This crashes; did you test it?) I've fixed it up and put it in the wireguard tree:

https://git.kernel.org/pub/scm/linux/kernel/git/zx2c4/wireguard-linux.git/commit/?id=ad3d9eef8e1b304b243e63124581f97c88ce7ff9

I'll push this up to net.git soon. Thanks for the patch.

Jason