hyperboria / bugs

Peer-to-peer IPv6 networking, secure and near-zero-conf.
153 stars 17 forks source link

Unaligned memory access, illegal in ARM/MIPS #39

Closed viric closed 9 years ago

viric commented 9 years ago

I had troubles with iptunnel not working. I was getting:

1431420004 DEBUG IpTunnel.c:687 Got message with wrong address for connection

The check for addresses in IpTunnel.c is using a bad pointer cast that results in illegal memory access (unaligned). This has undefined behaviour in ARM, so the result of the prefix comparison is actually undefined.

https://github.com/hyperboria/cjdns/blob/master/tunnel/IpTunnel.c#L516-L543

Something else has to be used. Stack uint32_t variables, etc.

Sysadmins can set /proc/cpu/alignment to 'fixup' to allow the kernel handle these unaligned memory access by catching the cpu exception and fixing it to allow running broken software, but it is very slow. The default kernel alignment setting is 'ignore', so: undefined behaviour.

(@cpages)

viric commented 9 years ago

As an extra, I think that the endianness is wrong in case of prefix matching. In my case, for example, addressA contains the sequence of bytes (192, 168, 34, 1). So the right-shift in little-endian uint32 would be wrong.

viric commented 9 years ago

@cjdelisle addressed this in https://github.com/cjdelisle/cjdns/commit/f964d14fafbc1701697403a71a5ddbe3a41586e1 and the next commit. The fix seems correct. Tested.