Skip to contentSkip to conversation

Alignement crash on sparc64

Openvpn-devel Jeremie Courreges-Anglas 3 participants 13 messages May 26, 2017 – Jun 11, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

I am the maintainer of openvpn in the OpenBSD ports tree. Here's a report from Stefan Sperling (Cc'ed).

Quoted 7 lines

An openvpn server running on OpenBSD/sparc64 6.1 crashes when a client connects and starts doing traffic. Is anyone else seeing this?

(gdb) run
Starting program: /usr/local/sbin/openvpn /etc/openvpn/server.conf (no debugging symbols found)

Quoted 7 lines

Program received signal SIGBUS, Bus error. 0x000000a99df82664 in write_tun_header () from /usr/local/sbin/openvpn (gdb) bt

#0  0x000000a99df82664 in write_tun_header () from /usr/local/sbin/openvpn
#1  0x000000a99df136d4 in process_outgoing_tun () from /usr/local/sbin/openvpn
#2  0x000000a99df31094 in tunnel_server_udp () from /usr/local/sbin/openvpn
#3  0x000000a99df3a6ac in main () from /usr/local/sbin/openvpn

To work around this, the patch below (also written by Stefan) was committed in our ports tree. TARGET_FREEBSD and TARGET_DRAGONFLY seem to be affected too.

Thoughts?

src/openvpn/tun.c +3 -3
index 3504fbf4..51a7f82d 100644
@@ -1654,11 +1654,11 @@ write_tun_header(struct tuntap *tt, uint8_t *buf, int len)
     {
         u_int32_t type;
         struct iovec iv[2];
-        struct ip *iph;
+        struct ip iph;
 
-        iph = (struct ip *) buf;
+        memcpy(&iph, buf, sizeof(iph));
 
-        if (iph->ip_v == 6)
+        if (iph.ip_v == 6)
         {
             type = htonl(AF_INET6);
         }
Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#1 lore raw

Hi,

Quoted 5 lines · Jeremie Courreges-Anglas

I am the maintainer of openvpn in the OpenBSD ports tree. Here's a report from Stefan Sperling (Cc'ed).

Quoted 2 lines

An openvpn server running on OpenBSD/sparc64 6.1 crashes when a client connects and starts doing traffic. Is anyone else seeing this?

Thanks for the report. I'll look into it.

Copying around the IP header doesn't feel like the proper solution, though - it's basically replacing an unaligned access somewhere with a larger (unaligned!) mem copy, where most likely changing a word access to 2x byte access (etc) might achieve the same thing without copying.

Quoted 3 lines

To work around this, the patch below (also written by Stefan) was committed in our ports tree. TARGET_FREEBSD and TARGET_DRAGONFLY seem to be affected too.

FreeBSD/Sparc64 traps unaligned memory accesses and handles them kernel-side. So you'll see a slowdown, but no signal.

Linux/Sparc64 has the same restrictions (unaligned access causing a signal), but nobody has reported that yet, so maybe the underlying memory alignment of the allocated buffer happens to be good enough there.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#2 lore raw
Quoted 21 lines · Gert Doering

Hi,

Quoted 5 lines · Jeremie Courreges-Anglas

I am the maintainer of openvpn in the OpenBSD ports tree. Here's a report from Stefan Sperling (Cc'ed).

Quoted 2 lines

An openvpn server running on OpenBSD/sparc64 6.1 crashes when a client connects and starts doing traffic. Is anyone else seeing this?

Thanks for the report. I'll look into it.

Copying around the IP header doesn't feel like the proper solution, though - it's basically replacing an unaligned access somewhere with a larger (unaligned!) mem copy, where most likely changing a word access to 2x byte access (etc) might achieve the same thing without copying.

Quoted 3 lines

To work around this, the patch below (also written by Stefan) was committed in our ports tree. TARGET_FREEBSD and TARGET_DRAGONFLY seem to be affected too.

Quoted 7 lines

FreeBSD/Sparc64 traps unaligned memory accesses and handles them kernel-side. So you'll see a slowdown, but no signal.

Linux/Sparc64 has the same restrictions (unaligned access causing a signal), but nobody has reported that yet, so maybe the underlying memory alignment of the allocated buffer happens to be good enough there.

Using mostly with linux sparc64, but I have openbsd sparc64 installed and I can't reproduce it here....

we are using openvpn (2.4.0-6) on sparc64 linux (debian sid/unstable) as clients to x86_64 server (openvpn-2.3.14-137.4.x86_64) - works without errors.

I made a few tests yesterday with openvpn static key:

1. server openvpn (2.4.0-6) on sparc64 linux, client is linux openvpn-2.3.14-1.fc25.x86_64 - works

2. server openvpn (2.4.0-6) on sparc64 linux, client openvpn 2.3.11 openbsd 6.1 sparc64 - works

3. server 2.3.11 (old 6.0 version) openbsd 6.1 sparc64, client openvpn (2.4.0-6) on sparc64 linux - works

4. server 2.4.1 (6.1 version) openbsd 6.1 sparc64, client openvpn (2.4.0-6) on sparc64 linux - works

checked all 4 with copying back and forth 100Mbyte test file over vpn connection...

bash-4.3# openvpn l1.conf
Tue Jun 6 13:12:13 2017 disabling NCP mode (--ncp-disable) because not in P2MP client or server mode
Tue Jun 6 13:12:13 2017 OpenVPN 2.4.1 sparc64-unknown-openbsd6.1 [SSL (OpenSSL)] [LZO] [LZ4] [MH/RECVDA] [AEAD] built on May 19 2017 Tue Jun 6 13:12:13 2017 library versions: LibreSSL 2.6.0, LZO 2.09 Tue Jun 6 13:12:13 2017 WARNING: INSECURE cipher with block size less than 128 bit (64 bit). This allows attacks like SWEET32. Mitigate by using a --cipher with a larger block size (e.g. AES-256-CBC). Tue Jun 6 13:12:13 2017 WARNING: INSECURE cipher with block size less than 128 bit (64 bit). This allows attacks like SWEET32. Mitigate by using a --cipher with a larger block size (e.g. AES-256-CBC).

Tue Jun  6 13:12:13 2017 TUN/TAP device /dev/tun0 opened
Tue Jun  6 13:12:13 2017 do_ifconfig, tt->did_ifconfig_ipv6_setup=0
Tue Jun  6 13:12:13 2017 /sbin/ifconfig tun0 10.9.0.2 10.9.0.1 mtu
1500 netmask 255.255.255.255 up
Tue Jun  6 13:12:13 2017 Could not determine IPv4/IPv6 protocol. Using AF_INET
Tue Jun  6 13:12:13 2017 UDPv4 link local (bound): [AF_INET][undef]:1194
Tue Jun  6 13:12:13 2017 UDPv4 link remote: [AF_UNSPEC]
Tue Jun  6 13:12:14 2017 Peer Connection Initiated with

[AF_INET]192.168.158.92:1194
Tue Jun 6 13:12:14 2017 Initialization Sequence Completed

...
-bash-4.3$ ls -lh /tmp/test-file.bin
-rw-r--r-- 1 mator wheel 100M Jun 6 13:03 /tmp/test-file.bin
-bash-4.3$ scp /tmp/test-file.bin 10.9.0.1:/tmp/1.bin [email protected]...'s password:
test-file.bin

                                           100%  100MB   6.4MB/s

00:15

-bash-4.3$ cat l1.conf
dev tun
ifconfig 10.9.0.2 10.9.0.1
comp-lzo
mute-replay-warnings
<secret>
...
</secret>

PS: if someone needs a sparc64 shell for more tests or build (CI) machine, just email me. Thanks.

#11 lore raw
In reply to Gert Doering
Quoted 9 lines · Gert Doering

Hi,

Quoted 5 lines · Jeremie Courreges-Anglas

I am the maintainer of openvpn in the OpenBSD ports tree. Here's a report from Stefan Sperling (Cc'ed).

Quoted 2 lines

An openvpn server running on OpenBSD/sparc64 6.1 crashes when a client connects and starts doing traffic. Is anyone else seeing this?

Quoted 7 lines

Thanks for the report. I'll look into it.

Copying around the IP header doesn't feel like the proper solution, though - it's basically replacing an unaligned access somewhere with a larger (unaligned!) mem copy, where most likely changing a word access to 2x byte access (etc) might achieve the same thing without copying.

Agreed, there's probably a better way to handle this. The best workaround I can think of would be using openvpn_iphdr / OPENVPN_IPH_GET_VER, like the NetBSD code does. It accesses only the first byte of the buffer and thus shouldn't be affected by alignement constraints.

This patch has been successfully tested (v4 and v6 traffic) by Stefan Sperling on OpenBSD/sparc64, on top of the openvpn-2.4.2 release.

From 3f5cf7fa574c3bb7495bfb43d09e96c368702445 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= <[email protected]...>
Date: Sat, 10 Jun 2017 16:48:44 +0200
Subject: [PATCH] Fix an unaligned access on OpenBSD/sparc64

The pointer to the packet content doesn't seem to be word-aligned, resulting in a SIGBUS when accessing it as a pointer to struct ip that contains bit fields.

Replace with struct openvpn_iphdr and OPENVPN_IPH_GET_VER, which only does a one byte access and thus isn't affected by alignement.

---
 src/openvpn/tun.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/openvpn/tun.c +3 -3
index 3504fbf4..0ca0c19e 100644
@@ -1654,11 +1654,11 @@ write_tun_header(struct tuntap *tt, uint8_t *buf, int len)
     {
         u_int32_t type;
         struct iovec iv[2];
-        struct ip *iph;
+        struct openvpn_iphdr *iph;
 
-        iph = (struct ip *) buf;
+        iph = (struct openvpn_iphdr *) buf;
 
-        if (iph->ip_v == 6)
+        if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
         {
             type = htonl(AF_INET6);
         }
Signature · 1 line
2.13.0
Quoted 7 lines
Quoted 4 lines

To work around this, the patch below (also written by Stefan) was committed in our ports tree. TARGET_FREEBSD and TARGET_DRAGONFLY seem to be affected too.

FreeBSD/Sparc64 traps unaligned memory accesses and handles them kernel-side. So you'll see a slowdown, but no signal.

Sounds bad. The proposed patch affects only OpenBSD and Mac OSX (untested there, but looks safe); but FreeBSD and DragonflyBSD would probably benefit from the same change.

Quoted 4 lines

Linux/Sparc64 has the same restrictions (unaligned access causing a signal), but nobody has reported that yet, so maybe the underlying memory alignment of the allocated buffer happens to be good enough there.

I don't know much about the memory allocator used in openvpn, but I'd expect it to provide at least word-aligned pointers.

The code we're discussing is specific to BSD systems, another possible reason why Linux looks unaffected.

Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#3 lore raw
In reply to Anatoly Pugachev
Quoted 42 lines · Anatoly Pugachev
Quoted 21 lines · Gert Doering

Hi,

Quoted 5 lines · Jeremie Courreges-Anglas

I am the maintainer of openvpn in the OpenBSD ports tree. Here's a report from Stefan Sperling (Cc'ed).

Quoted 2 lines

An openvpn server running on OpenBSD/sparc64 6.1 crashes when a client connects and starts doing traffic. Is anyone else seeing this?

Thanks for the report. I'll look into it.

Copying around the IP header doesn't feel like the proper solution, though - it's basically replacing an unaligned access somewhere with a larger (unaligned!) mem copy, where most likely changing a word access to 2x byte access (etc) might achieve the same thing without copying.

Quoted 3 lines

To work around this, the patch below (also written by Stefan) was committed in our ports tree. TARGET_FREEBSD and TARGET_DRAGONFLY seem to be affected too.

Quoted 7 lines

FreeBSD/Sparc64 traps unaligned memory accesses and handles them kernel-side. So you'll see a slowdown, but no signal.

Linux/Sparc64 has the same restrictions (unaligned access causing a signal), but nobody has reported that yet, so maybe the underlying memory alignment of the allocated buffer happens to be good enough there.

Using mostly with linux sparc64, but I have openbsd sparc64 installed and I can't reproduce it here....

we are using openvpn (2.4.0-6) on sparc64 linux (debian sid/unstable) as clients to x86_64 server (openvpn-2.3.14-137.4.x86_64) - works without errors.

I made a few tests yesterday with openvpn static key:

1. server openvpn (2.4.0-6) on sparc64 linux, client is linux openvpn-2.3.14-1.fc25.x86_64 - works

Quoted 8 lines

2. server openvpn (2.4.0-6) on sparc64 linux, client openvpn 2.3.11 openbsd 6.1 sparc64 - works

3. server 2.3.11 (old 6.0 version) openbsd 6.1 sparc64, client openvpn (2.4.0-6) on sparc64 linux - works

4. server 2.4.1 (6.1 version) openbsd 6.1 sparc64, client openvpn (2.4.0-6) on sparc64 linux - works

That's a lot of tests, thank you. Dunno why you can't reproduce it with 2.4.1 (and 2.3.11 seems to contain the same code in write_tun_header).

[...]

Quoted 8 lines

-bash-4.3$ cat l1.conf
dev tun
ifconfig 10.9.0.2 10.9.0.1
comp-lzo
mute-replay-warnings
<secret>
...
</secret>

OpenBSD 6.1 on sparc64, vanilla openvpn-2.4.2 freshly built. Using client mode with your configuration snippet and an IPv4 remote, I can reliably trigger a SIGBUS.

xxx ~/src/openvpn-2.4.2$ gdb src/openvpn/openvpn openvpn.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "sparc64-unknown-openbsd6.1"...
Core was generated by `openvpn'.
Program terminated with signal 10, Bus error.
Loaded symbols for /home/jca/src/openvpn-2.4.2/src/openvpn/openvpn Reading symbols from /usr/lib/libssl.so.43.2...done.
Loaded symbols for /usr/lib/libssl.so.43.2
Reading symbols from /usr/lib/libcrypto.so.41.1...done.
Loaded symbols for /usr/lib/libcrypto.so.41.1
Reading symbols from /usr/lib/libc.so.89.3...done.
Loaded symbols for /usr/lib/libc.so.89.3
Reading symbols from /usr/libexec/ld.so...done.
Loaded symbols for /usr/libexec/ld.so
#0 write_tun_header (tt=0x1ddd4e1900, buf=0x1d336921c5 "E", len=84) at tun.c:1661
1661 if (iph->ip_v == 6)
(gdb)

buf points to an odd address.

Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#4 lore raw

Hi,

Quoted 6 lines · Jeremie Courreges-Anglas

Loaded symbols for /usr/libexec/ld.so
#0 write_tun_header (tt=0x1ddd4e1900, buf=0x1d336921c5 "E", len=84) at tun.c:1661
1661 if (iph->ip_v == 6)
(gdb)

buf points to an odd address.

That's a single-byte access...? Nothing to be aligned here.

On FreeBSD, ip_v is defined as such:

* Structure of an internet header, naked of options. */
struct ip {
#if BYTE_ORDER == LITTLE_ENDIAN
u_char ip_hl:4, /* header length */
ip_v:4; /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
u_char ip_v:4, /* version */
ip_hl:4; /* header length */
#endif
...

... so whatever it is, it's part of a single uchar at the very beginning of that header... strange.

OTOH, the compiler might assume 16bit-alignment here and use a 16bit- access to get the first word, then bit-shift (someone who understands sparc64 assembly around to check?) - in this case, the following change should help:

old:

    if (iph->ip_v == 6)

new:

    if (OPENVPN_IPH_GET_VER(*buf) == 6)

this is an existing macro which takes an int argument, shifting and masking the right bits, and since *buf is "uint8_t *buf", this should both be correct and alignment-ignorant.

It might be less efficient than accessing the bit field - but then, it's the same work to do, so hopefully the compiler is smart enough to compile it to the same instructions.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#5 lore raw
Quoted 27 lines · Gert Doering

Hi,

Quoted 6 lines · Jeremie Courreges-Anglas

Loaded symbols for /usr/libexec/ld.so
#0 write_tun_header (tt=0x1ddd4e1900, buf=0x1d336921c5 "E", len=84) at tun.c:1661
1661 if (iph->ip_v == 6)
(gdb)

buf points to an odd address.

That's a single-byte access...? Nothing to be aligned here.

On FreeBSD, ip_v is defined as such:

* Structure of an internet header, naked of options. */
struct ip {
#if BYTE_ORDER == LITTLE_ENDIAN
u_char ip_hl:4, /* header length */
ip_v:4; /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
u_char ip_v:4, /* version */
ip_hl:4; /* header length */
#endif
...

Quoted 2 lines

... so whatever it is, it's part of a single uchar at the very beginning of that header... strange.

OpenBSD:

struct ip {

#if _BYTE_ORDER == _LITTLE_ENDIAN
	u_int     ip_hl:4,		/* header length */
		  ip_v:4;		/* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
	u_int     ip_v:4,		/* version */
		  ip_hl:4;		/* header length */

#endif
...

u_char -> u_int. Dunno if it matters to the compiler, I can take a look later.

Quoted 7 lines

OTOH, the compiler might assume 16bit-alignment here and use a 16bit- access to get the first word, then bit-shift (someone who understands sparc64 assembly around to check?) - in this case, the following change should help:

old:

Quoted 8 lines
    if (iph->ip_v == 6)

new:

    if (OPENVPN_IPH_GET_VER(*buf) == 6)

this is an existing macro which takes an int argument, shifting and masking the right bits, and since *buf is "uint8_t *buf", this should both be correct and alignment-ignorant.

See my proposed patch, also in this thread. I merely copy/pasted the NetBSD code.

Quoted 3 lines

It might be less efficient than accessing the bit field - but then, it's the same work to do, so hopefully the compiler is smart enough to compile it to the same instructions.

master (32 bits access):

0x0000000000081844 <write_tun_header+100>:      ld  [ %i1 ], %g1
0x0000000000081848 <write_tun_header+104>:      sethi  %hi(0xf0000000), %g2
0x000000000008184c <write_tun_header+108>:      sethi  %hi(0x60000000), %g3
0x0000000000081850 <write_tun_header+112>:      and  %g1, %g2, %g1
0x0000000000081854 <write_tun_header+116>:      cmp  %g1, %g3

patched:

0x0000000000081844 <write_tun_header+100>:      ldub  [ %i1 ], %g1
0x0000000000081848 <write_tun_header+104>:      srl  %g1, 4, %g1
0x000000000008184c <write_tun_header+108>:      cmp  %g1, 6
Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#6 lore raw

Hi,

Quoted 10 lines · Jeremie Courreges-Anglas

OpenBSD:

struct ip {

#if _BYTE_ORDER == _LITTLE_ENDIAN
	u_int     ip_hl:4,		/* header length */
		  ip_v:4;		/* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
	u_int     ip_v:4,		/* version */
		  ip_hl:4;		/* header length */
Quoted 5 lines

#endif
...

u_char -> u_int. Dunno if it matters to the compiler, I can take a look later.

I'm fairly sure it does matter. An int is likely to be at least 16 bit, maybe 32 bit, so the compiler can assume "word aligned" - and *boom*.

(Wonder why it doesn't crash for Mator, as our buffer alignment in memory should be fairly consistent - it might depend on crypto and compression options, though)

Quoted 11 lines
Quoted 9 lines

old:

    if (iph->ip_v == 6)

new:

    if (OPENVPN_IPH_GET_VER(*buf) == 6)

this is an existing macro which takes an int argument, shifting and masking the right bits, and since *buf is "uint8_t *buf", this should both be correct and alignment-ignorant.

See my proposed patch, also in this thread.

"Copying around 20 bytes to ensure a single-byte access is really a single-byte access" is definitely *not* the right way here. So NAK on that patch.

It will stop the crashes, of course, but since no other parts of struct ip are ever needed, it's just waste of CPU cycles.

Quoted 1 line

I merely copy/pasted the NetBSD code.

Uh? Is this from the NetBSD ports tree? Because our tun.c does not have a single memcpy() today... :-)

Actually, my tun.c's TARGET_NETBSD version of write_tun() has something similar to what I suggested:

        struct openvpn_iphdr *iph;

        iph = (struct openvpn_iphdr *) buf;

        if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)

... slightly more speaking, and since "openvpn_iphdr" is a locally defined structure with "uchar version_len" at the very beginning, this amounts to the same thing.

Quoted 11 lines
Quoted 3 lines

It might be less efficient than accessing the bit field - but then, it's the same work to do, so hopefully the compiler is smart enough to compile it to the same instructions.

master (32 bits access):

0x0000000000081844 <write_tun_header+100>:      ld  [ %i1 ], %g1
0x0000000000081848 <write_tun_header+104>:      sethi  %hi(0xf0000000), %g2
0x000000000008184c <write_tun_header+108>:      sethi  %hi(0x60000000), %g3
0x0000000000081850 <write_tun_header+112>:      and  %g1, %g2, %g1
0x0000000000081854 <write_tun_header+116>:      cmp  %g1, %g3
Quoted 4 lines

patched:

0x0000000000081844 <write_tun_header+100>:      ldub  [ %i1 ], %g1
0x0000000000081848 <write_tun_header+104>:      srl  %g1, 4, %g1
0x000000000008184c <write_tun_header+108>:      cmp  %g1, 6

Sounds like an improvement, at least in code size :-) (but I have no idea how the performance of these operations compares)

... and I can see that the "master" version is going to fail on unaligned packets... so, needs fixing.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#7 lore raw
Quoted 61 lines · Gert Doering

Hi,

Quoted 10 lines · Jeremie Courreges-Anglas

OpenBSD:

struct ip {

#if _BYTE_ORDER == _LITTLE_ENDIAN
	u_int     ip_hl:4,		/* header length */
		  ip_v:4;		/* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
	u_int     ip_v:4,		/* version */
		  ip_hl:4;		/* header length */
Quoted 5 lines

#endif
...

u_char -> u_int. Dunno if it matters to the compiler, I can take a look later.

I'm fairly sure it does matter. An int is likely to be at least 16 bit, maybe 32 bit, so the compiler can assume "word aligned" - and *boom*.

(Wonder why it doesn't crash for Mator, as our buffer alignment in memory should be fairly consistent - it might depend on crypto and compression options, though)

Quoted 11 lines
Quoted 9 lines

old:

    if (iph->ip_v == 6)

new:

    if (OPENVPN_IPH_GET_VER(*buf) == 6)

this is an existing macro which takes an int argument, shifting and masking the right bits, and since *buf is "uint8_t *buf", this should both be correct and alignment-ignorant.

See my proposed patch, also in this thread.

"Copying around 20 bytes to ensure a single-byte access is really a single-byte access" is definitely *not* the right way here. So NAK on that patch.

It will stop the crashes, of course, but since no other parts of struct ip are ever needed, it's just waste of CPU cycles.

Quoted 1 line

I merely copy/pasted the NetBSD code.

Uh? Is this from the NetBSD ports tree? Because our tun.c does not have a single memcpy() today... :-)

Actually, my tun.c's TARGET_NETBSD version of write_tun() has something similar to what I suggested:

        struct openvpn_iphdr *iph;

        iph = (struct openvpn_iphdr *) buf;

        if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
Quoted 3 lines

... slightly more speaking, and since "openvpn_iphdr" is a locally defined structure with "uchar version_len" at the very beginning, this amounts to the same thing.

This is exactly the NetBSD code I was mentioning. Here's the mail:

https://sourceforge.net/p/openvpn/mailman/message/35887533/

(weird, the mail content appears to be truncated on the web interface, but at least you can get the patch in "Attachments").

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

It might be less efficient than accessing the bit field - but then, it's the same work to do, so hopefully the compiler is smart enough to compile it to the same instructions.

master (32 bits access):

0x0000000000081844 <write_tun_header+100>:      ld  [ %i1 ], %g1
0x0000000000081848 <write_tun_header+104>:      sethi  %hi(0xf0000000), %g2
0x000000000008184c <write_tun_header+108>:      sethi  %hi(0x60000000), %g3
0x0000000000081850 <write_tun_header+112>:      and  %g1, %g2, %g1
0x0000000000081854 <write_tun_header+116>:      cmp  %g1, %g3
Quoted 7 lines
Quoted 4 lines

patched:

0x0000000000081844 <write_tun_header+100>:      ldub  [ %i1 ], %g1
0x0000000000081848 <write_tun_header+104>:      srl  %g1, 4, %g1
0x000000000008184c <write_tun_header+108>:      cmp  %g1, 6

Sounds like an improvement, at least in code size :-) (but I have no idea how the performance of these operations compares)

Neither do I, I'm no sparcv9 hacker.

Quoted 2 lines

... and I can see that the "master" version is going to fail on unaligned packets... so, needs fixing.

Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#8 lore raw
In reply to Gert Doering

Hi,

Quoted 14 lines · Gert Doering
Quoted 1 line

I merely copy/pasted the NetBSD code.

Uh? Is this from the NetBSD ports tree? Because our tun.c does not have a single memcpy() today... :-)

Actually, my tun.c's TARGET_NETBSD version of write_tun() has something similar to what I suggested:

        struct openvpn_iphdr *iph;

        iph = (struct openvpn_iphdr *) buf;

        if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
Quoted 3 lines

... slightly more speaking, and since "openvpn_iphdr" is a locally defined structure with "uchar version_len" at the very beginning, this amounts to the same thing.

Aaaah. I have been enlightened - there was a second patch, which does exactly this :-) - and I had not seen the mail yet.

Thanks, will give it a closer look.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#9 lore raw
In reply to Gert Doering
Quoted 16 lines · Gert Doering

Hi,

Quoted 13 lines · Jeremie Courreges-Anglas

OpenBSD:

struct ip {

#if _BYTE_ORDER == _LITTLE_ENDIAN
	u_int     ip_hl:4,		/* header length */
		  ip_v:4;		/* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
	u_int     ip_v:4,		/* version */
		  ip_hl:4;		/* header length */

#endif
...

Quoted 5 lines
Quoted 2 lines

u_char -> u_int. Dunno if it matters to the compiler, I can take a look later.

I'm fairly sure it does matter. An int is likely to be at least 16 bit, maybe 32 bit, so the compiler can assume "word aligned" - and *boom*.

It doesn't seem to matter that much. Which aligns well with the "bit fields are nasty" experience people seem to have. From a quick glance at the c99 standard, I couldn't find language about this.

"A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type."

Here's a snippet:

--8<--
#include <sys/types.h>

#include <netinet/in.h>

#include <endian.h>
#include <stdlib.h>

struct ip {
#if _BYTE_ORDER == _LITTLE_ENDIAN
u_int ip_hl:4, /* header length */
ip_v:4; /* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
u_int ip_v:4, /* version */
ip_hl:4; /* header length */
#endif

        u_int8_t  ip_tos;               /* type of service */
        u_int16_t ip_len;               /* total length */
        u_int16_t ip_id;                /* identification */
        u_int16_t ip_off;               /* fragment offset field */
#define IP_RF 0x8000                    /* reserved fragment flag */
#define IP_DF 0x4000                    /* dont fragment flag */
#define IP_MF 0x2000                    /* more fragments flag */
#define IP_MF 0x2000                    /* more fragments flag */
#define IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
        u_int8_t  ip_ttl;               /* time to live */
        u_int8_t  ip_p;                 /* protocol */
        u_int16_t ip_sum;               /* checksum */

struct in_addr ip_src, ip_dst; /* source and dest address */
};

struct ip_freebsd {
#if _BYTE_ORDER == _LITTLE_ENDIAN
u_char ip_hl:4, /* header length */
ip_v:4; /* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
u_char ip_v:4, /* version */
ip_hl:4; /* header length */
#endif

        u_int8_t  ip_tos;               /* type of service */
        u_int16_t ip_len;               /* total length */
        u_int16_t ip_id;                /* identification */
        u_int16_t ip_off;               /* fragment offset field */
#define IP_RF 0x8000                    /* reserved fragment flag */
#define IP_DF 0x4000                    /* dont fragment flag */
#define IP_MF 0x2000                    /* more fragments flag */
#define IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
        u_int8_t  ip_ttl;               /* time to live */
        u_int8_t  ip_p;                 /* protocol */
        u_int16_t ip_sum;               /* checksum */

struct in_addr ip_src, ip_dst; /* source and dest address */
};

void
f(void *p)

{
        struct ip *iph;

        iph = (struct ip *)p;

        if (iph->ip_v == 6)
                abort();
}

void
g(void *p)

{
        struct ip_freebsd *iph;

        iph = (struct ip_freebsd *)p;

        if (iph->ip_v == 6)
                abort();
}

--8<--

gcc-4.2.1 (system compiler on OpenBSD)

(gdb) disas f
Dump of assembler code for function f:

0x0000000000000040 <f+0>:       save  %sp, -208, %sp
0x0000000000000044 <f+4>:       ld  [ %i0 ], %g1
0x0000000000000048 <f+8>:       sethi  %hi(0xf0000000), %g2
0x000000000000004c <f+12>:      sethi  %hi(0x60000000), %g3
0x0000000000000050 <f+16>:      and  %g1, %g2, %g1
0x0000000000000054 <f+20>:      cmp  %g1, %g3
0x0000000000000058 <f+24>:      be,pn   %icc, 0x68 <f+40>
0x000000000000005c <f+28>:      nop
0x0000000000000060 <f+32>:      rett  %i7 + 8
0x0000000000000064 <f+36>:      nop
0x0000000000000068 <f+40>:      call  0x68 <f+40>
0x000000000000006c <f+44>:      nop
0x0000000000000070 <f+48>:      nop
End of assembler dump.
(gdb) disas g
Dump of assembler code for function g:
0x0000000000000000 <g+0>:       save  %sp, -208, %sp
0x0000000000000004 <g+4>:       ld  [ %i0 ], %g1
0x0000000000000008 <g+8>:       sethi  %hi(0xf0000000), %g2
0x000000000000000c <g+12>:      sethi  %hi(0x60000000), %g3
0x0000000000000010 <g+16>:      and  %g1, %g2, %g1
0x0000000000000014 <g+20>:      cmp  %g1, %g3
0x0000000000000018 <g+24>:      be,pn   %icc, 0x28 <g+40>
0x000000000000001c <g+28>:      nop
0x0000000000000020 <g+32>:      rett  %i7 + 8
0x0000000000000024 <g+36>:      nop
0x0000000000000028 <g+40>:      call  0x28 <g+40>
0x000000000000002c <g+44>:      nop
0x0000000000000030 <g+48>:      nop

End of assembler dump.

gcc 4.9.4:

(gdb) disas f
Dump of assembler code for function f:

0x0000000000000000 <f+0>:       save  %sp, -176, %sp
0x0000000000000004 <f+4>:       ld  [ %i0 ], %g2
0x0000000000000008 <f+8>:       sethi  %hi(0xf0000000), %g1
0x000000000000000c <f+12>:      and  %g2, %g1, %g1
0x0000000000000010 <f+16>:      sethi  %hi(0x60000000), %g2
0x0000000000000014 <f+20>:      cmp  %g1, %g2
0x0000000000000018 <f+24>:      be,pn   %icc, 0x28 <f+40>
0x000000000000001c <f+28>:      nop
0x0000000000000020 <f+32>:      rett  %i7 + 8
0x0000000000000024 <f+36>:      nop
0x0000000000000028 <f+40>:      call  0x28 <f+40>
0x000000000000002c <f+44>:      nop
0x0000000000000030 <f+48>:      nop
End of assembler dump.
(gdb) disas g
Dump of assembler code for function g:
0x0000000000000040 <g+0>:       save  %sp, -176, %sp
0x0000000000000044 <g+4>:       ld  [ %i0 ], %g2
0x0000000000000048 <g+8>:       sethi  %hi(0xf0000000), %g1
0x000000000000004c <g+12>:      and  %g2, %g1, %g1
0x0000000000000050 <g+16>:      sethi  %hi(0x60000000), %g2
0x0000000000000054 <g+20>:      cmp  %g1, %g2
0x0000000000000058 <g+24>:      be,pn   %icc, 0x68 <g+40>
0x000000000000005c <g+28>:      nop
0x0000000000000060 <g+32>:      rett  %i7 + 8
0x0000000000000064 <g+36>:      nop
0x0000000000000068 <g+40>:      call  0x68 <g+40>
0x000000000000006c <g+44>:      nop
0x0000000000000070 <g+48>:      nop

End of assembler dump.

So, no difference in the generated code between the OpenBSD and FreeBSD structures.

Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#10 lore raw
Quoted 21 lines · Jeremie Courreges-Anglas
Quoted 19 lines · Gert Doering

Hi,

Quoted 15 lines · Jeremie Courreges-Anglas

OpenBSD:

struct ip {

#if _BYTE_ORDER == _LITTLE_ENDIAN
	u_int     ip_hl:4,		/* header length */
		  ip_v:4;		/* version */
#endif
#if _BYTE_ORDER == _BIG_ENDIAN
	u_int     ip_v:4,		/* version */
		  ip_hl:4;		/* header length */

#endif
...

u_char -> u_int. Dunno if it matters to the compiler, I can take a look later.

Quoted 6 lines
Quoted 2 lines

I'm fairly sure it does matter. An int is likely to be at least 16 bit, maybe 32 bit, so the compiler can assume "word aligned" - and *boom*.

It doesn't seem to matter that much. Which aligns well with the "bit fields are nasty" experience people seem to have. From a quick glance at the c99 standard, I couldn't find language about this.

Appendix J.1 mentions this: "The alignment of the addressable storage unit allocated to hold a bit-field (6.7.2.1)." as unspecified behavior.

Signature · 1 line
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE
#12 lore raw

[Openvpn-devel] [PATCH applied] Re: Fix an unaligned access on OpenBSD/sparc64

In reply to Jeremie Courreges-Anglas

ACK, thanks. As discussed in great detail :-)

Smoke-tested on OpenBSD 6.0/amd64 (to ensure v4/v6 still work).

Note to self: clean up the different per-OS ways of doing the same thing here.

Your patch has been applied to the master, release/2.4 and release/2.3 branch.

commit 3e4e300d6c5ea9c320e62def79e5b70f8e255248 (master) commit 29ba477801998aff4e0ae7c1edc50b4ac9d145d9 (release/2.4) commit 02845e8dc6bd5ee0be1b4a5616f9da90d6e38f4b (release/2.3)

Author: Jérémie Courrèges-Anglas
Date:   Sat Jun 10 16:48:44 2017 +0200
     Fix an unaligned access on OpenBSD/sparc64

     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg14769.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering