Skip to content

tun: fix unaligned int32 reads in getIFIndex and getMTU - #82

Open
loowr wants to merge 1 commit into
tailscale:tailscalefrom
loowr:fix/mips32-alignment-getifindex-getmtu
Open

tun: fix unaligned int32 reads in getIFIndex and getMTU#82
loowr wants to merge 1 commit into
tailscale:tailscalefrom
loowr:fix/mips32-alignment-getifindex-getmtu

Conversation

@loowr

@loowr loowr commented Jul 31, 2026

Copy link
Copy Markdown

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

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>
@loowr
loowr changed the base branch from master to tailscale July 31, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant