tun: fix unaligned int32 reads in getIFIndex and getMTU - #82
Open
loowr wants to merge 1 commit into
Open
Conversation
getIFIndex and getMTU read the ifr_ifindex/ifr_mtu int32 from a
[ifReqSize]byte stack buffer via:
*(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ]))
The compiler is free to place a byte array at any stack offset, so on
strict-alignment architectures (MIPS32, etc.) this word load can fault
with SIGBUS. The fault is deterministic per build since the frame
layout is fixed at compile time: a mipsel build where the compiler
places ifr at sp+35 makes the load land at sp+51 (3-mod-4), which
MIPS32 cannot handle.
Fix by reading the value byte-wise with binary.LittleEndian, which is
alignment-safe on every architecture and produces identical results.
This is the same class of bug previously fixed for setMTU in 4e883d3,
which converted that path to x/sys/unix Ifreq helpers.
Signed-off-by: loowr <loowr@proton.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getIFIndex and getMTU read the ioctl result (ifr_ifindex, ifr_mtu) from a byte buffer using an unsafe cast: *(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])). A byte array only requires 1-byte alignment, so the compiler is free to place it at any stack offset. On strict-alignment architectures such as MIPS32, the resulting word load can land at a 3-mod-4 address and raise SIGBUS.
The fault is deterministic per build because the frame layout is fixed at compile time. A mipsel build that places ifr at sp+35 makes the load hit sp+51, which MIPS32 cannot execute. This was reproduced with the official tailscale 1.78.1 mipsel binary as well: tailscaled crashes at TUN creation in getIFIndex.
This is the same class of bug previously fixed for setMTU in commit 4e883d3, which converted that path to the x/sys/unix Ifreq helpers.
The fix reads the values byte-wise with binary.LittleEndian, which is alignment-safe on every architecture and produces identical results. Verified by cross-compiling for linux/mipsle (softfloat) and running under qemu-system-mipsel: the fixed binary creates the TUN device and starts the WireGuard engine, while the unpatched build faults at the same instruction.
Assisted by Hermes