Compare commits

..

2 Commits

Author SHA1 Message Date
JackDoan 334dd7a85d NAPI broken on old kernels and also not helpful 2026-04-24 17:36:20 -05:00
JackDoan c9d5a6e35a be safer 2026-04-24 16:48:52 -05:00
3 changed files with 6 additions and 37 deletions
+4 -23
View File
@@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"runtime"
"sync/atomic"
"syscall"
"unsafe"
@@ -264,24 +263,12 @@ func (r *Offload) writeWithScratch(buf []byte, iovs *[2]unix.Iovec) (int, error)
// to validVnetHdr during Offload construction so we don't rebuild it here.
iovs[1].Base = &buf[0]
iovs[1].SetLen(len(buf))
iovPtr := unsafe.Pointer(&iovs[0])
// Pin the caller's buffer AND the iovec array through the syscall.
return r.rawWrite(iovPtr, 2, buf, iovs)
return r.rawWrite(unsafe.Slice(&iovs[0], len(iovs)))
}
func (r *Offload) rawWrite(iovs unsafe.Pointer, iovcnt int, keepAlive ...interface{}) (int, error) {
func (r *Offload) rawWrite(iovs []unix.Iovec) (int, error) {
for {
n, _, errno := syscall.Syscall(unix.SYS_WRITEV, uintptr(r.fd), uintptr(iovs), uintptr(iovcnt))
// Anchor the iovec array + every user-supplied payload slice
// through the syscall return. Without these, Go's GC may move or
// collect the underlying backing arrays while the kernel is still
// reading them via DMA (we pass the iovec as uintptr, so the
// compiler does not keep it live). Observed in practice as a
// kernel refcount underflow on tun_chr_write_iter / sock_wfree.
runtime.KeepAlive(iovs)
for _, ka := range keepAlive {
runtime.KeepAlive(ka)
}
n, _, errno := syscall.Syscall(unix.SYS_WRITEV, uintptr(r.fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)))
if errno == 0 {
if int(n) < virtioNetHdrLen {
return 0, io.ErrShortWrite
@@ -364,13 +351,7 @@ func (r *Offload) WriteGSO(hdr []byte, pays [][]byte, gsoSize uint16, isV6 bool,
r.gsoIovs[2+i].SetLen(len(p))
}
iovPtr := unsafe.Pointer(&r.gsoIovs[0])
iovCnt := len(r.gsoIovs)
// Pin EVERYTHING the kernel might still read via DMA: the backing iovec
// slice, the IP/TCP header buffer, and every individual payload
// fragment. Skipping any of these risks a use-after-free in
// tun_chr_write_iter if GC runs mid-syscall.
_, err := r.rawWrite(iovPtr, iovCnt, r.gsoIovs, hdr, pays)
_, err := r.rawWrite(r.gsoIovs)
return err
}
-12
View File
@@ -3,7 +3,6 @@ package tio
import (
"fmt"
"os"
"runtime"
"sync/atomic"
"syscall"
"unsafe"
@@ -121,13 +120,6 @@ func (t *Poll) readOne(to []byte) (int, error) {
}
for {
n, _, errno := syscall.Syscall(syscall.SYS_READV, uintptr(t.fd), uintptr(unsafe.Pointer(&iovecs[0])), 2)
// Pin the iovec + destination buffer backing array across the syscall.
// Without these the Go runtime may move/GC them while the kernel is
// still writing via DMA (we pass the iovec as uintptr, which hides it
// from escape analysis). Same class of bug as rawWrite in the Offload
// path.
runtime.KeepAlive(iovecs)
runtime.KeepAlive(to)
if errno == 0 {
bytesRead := int(n)
if bytesRead < 4 {
@@ -174,10 +166,6 @@ func (t *Poll) Write(from []byte) (int, error) {
}
for {
n, _, errno := syscall.Syscall(syscall.SYS_WRITEV, uintptr(t.fd), uintptr(unsafe.Pointer(&iovecs[0])), 2)
// Pin the iovec + source buffer backing array across the syscall.
// See readOne's KeepAlive comment for rationale.
runtime.KeepAlive(iovecs)
runtime.KeepAlive(from)
if errno == 0 {
return int(n) - 4, nil
}
+2 -2
View File
@@ -139,7 +139,7 @@ func newTun(c *config.C, l *logrus.Logger, vpnNetworks []netip.Prefix, multiqueu
return nil, err
}
vnetHdr := true
name, err := tunSetIff(fd, nameStr, baseFlags|unix.IFF_VNET_HDR|unix.IFF_NAPI)
name, err := tunSetIff(fd, nameStr, baseFlags|unix.IFF_VNET_HDR)
if err != nil {
_ = unix.Close(fd)
vnetHdr = false
@@ -307,7 +307,7 @@ func (t *tun) NewMultiQueueReader() error {
flags := uint16(unix.IFF_TUN | unix.IFF_NO_PI | unix.IFF_MULTI_QUEUE)
if t.vnetHdr {
flags |= unix.IFF_VNET_HDR | unix.IFF_NAPI
flags |= unix.IFF_VNET_HDR
}
if _, err = tunSetIff(fd, t.Device, flags); err != nil {
_ = unix.Close(fd)