mirror of
https://github.com/slackhq/nebula.git
synced 2026-08-15 10:26:59 +02:00
44dd2e9ca4
Multi-disciplinary correctness review of the batched tun / GSO-GRO / sendmmsg rework. Each fix has a regression test; the merged tree builds on linux/darwin/openbsd/windows/freebsd/netbsd, vets clean, passes the unit and e2e suites, and is -race clean. Critical: - C1 zero-length inner UDP datagram no longer panics the process (remote DoS): the UDP coalescer routes payLen==0 to passthrough instead of seeding a GSO slot, and WriteGSO skips empty payload iovecs as defense in depth. - C2 segmenter no longer corrupts inner headers when gsoSize < headerLen: the L3+L4 header is snapshotted once and each segment stamped from the copy, replacing the destructive overlapping in-place slide (SegmentTCP + SegmentUDP). High: - H1 applyOuterECN updates the IPv4 header checksum (RFC 1624 incremental) when folding outer CE into the inner ToS, so passthrough packets are no longer dropped by the peer stack. - H2 the GRO reject path caps the borrowed RX segment ([:n:n]) so a reject can no longer overrun into the next coalesced segment's Nebula header. Note: oversized ICMPv6 rejects that need >16B beyond the segment are now refused rather than sent under GRO (safe; see TOFIX.md for the scratch-buffer follow-up). - H3 WriteBatch falls back to per-packet WriteTo for a chunk when writeSockaddr fails, so one bad-family destination costs only its own packet, not the batch. - H4 UserDevice.Readers returns N distinct queue wrappers with private buffers (sharing the pipes) so concurrent readers no longer race/overwrite borrowed packet bytes. - H5 Poll.Close / Offload.Close no longer null t.fd (matching master's tunFile.Close), removing the data race with a concurrent readOne load. Medium/Low: - M1 the UDP GSO 127-segment gate moved from kernel >=5.5 to >=6.9 (the real UDP_MAX_SEGMENTS 64->128 threshold), avoiding EINVAL + per-packet fallback on 5.5-6.8 kernels. - M2 NewMultiQueueReader replays the offload mask newTun actually negotiated instead of the TSO-only mask, so adding a queue no longer disables USO device-wide; the advertised USO capability derives from the same mask. - M3 the shutdown eventfd is closed in pollQueueSet.Close / offloadQueueSet.Close (double-close guarded), fixing the per-lifecycle fd leak. - M4 dual-stack ECN selects the cmsg by address family, not socket family: RX parseRecvCmsg reads both IP_TOS and IPV6_TCLASS; TX writeEntryCmsg stamps IP_TOS for v4/v4-mapped dests and IPV6_TCLASS for v6 (on-host verified). - L1 newPoll no longer closes the fd on failure (matching newOffload), removing the double-close on QueueSet.Add error.
158 lines
4.7 KiB
Go
158 lines
4.7 KiB
Go
//go:build linux && !android && !e2e_testing
|
|
// +build linux,!android,!e2e_testing
|
|
|
|
package tio
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// newReadPipe returns a read fd. The matching write fd is registered for cleanup.
|
|
// The caller takes ownership of the read fd (pass it into a QueueSet).
|
|
func newReadPipe(t *testing.T) int {
|
|
t.Helper()
|
|
var fds [2]int
|
|
if err := unix.Pipe2(fds[:], unix.O_CLOEXEC); err != nil {
|
|
t.Fatalf("pipe2: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = unix.Close(fds[1]) })
|
|
return fds[0]
|
|
}
|
|
|
|
func TestPoll_WakeForShutdown_WakesFriends(t *testing.T) {
|
|
pipe1 := newReadPipe(t)
|
|
pipe2 := newReadPipe(t)
|
|
parent, err := NewPollQueueSet()
|
|
require.NoError(t, err)
|
|
require.NoError(t, parent.Add(pipe1))
|
|
require.NoError(t, parent.Add(pipe2))
|
|
t.Cleanup(func() {
|
|
_ = unix.Close(pipe1)
|
|
_ = unix.Close(pipe2)
|
|
})
|
|
|
|
readers := parent.Queues()
|
|
errs := make([]error, len(readers))
|
|
var wg sync.WaitGroup
|
|
for i, r := range readers {
|
|
wg.Add(1)
|
|
go func(i int, r Queue) {
|
|
defer wg.Done()
|
|
_, errs[i] = r.Read()
|
|
}(i, r)
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
if err := parent.Close(); err != nil {
|
|
t.Fatalf("Close: %v", err)
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() { wg.Wait(); close(done) }()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("readers did not wake")
|
|
}
|
|
|
|
for i, err := range errs {
|
|
if !errors.Is(err, os.ErrClosed) {
|
|
t.Errorf("reader %d: expected os.ErrClosed, got %v", i, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestPoll_NewPoll_DoesNotCloseFdOnFailure pins the ownership rule: when
|
|
// newPoll fails, it must leave fd open so the caller (pollQueueSet.Add's
|
|
// callers in tun_linux.go) is the sole closer. If newPoll also closed fd,
|
|
// the poll path would double-close on Add error. We force the failure with
|
|
// an O_PATH descriptor: fcntl(F_SETFL) — which SetNonblock performs — is not
|
|
// permitted on O_PATH fds and fails with EBADF, while the fd itself stays
|
|
// open so we can observe that newPoll left it alone.
|
|
func TestPoll_NewPoll_DoesNotCloseFdOnFailure(t *testing.T) {
|
|
fd, err := unix.Open("/", unix.O_PATH|unix.O_CLOEXEC, 0)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = unix.Close(fd) })
|
|
|
|
p, err := newPoll(fd, 1)
|
|
require.Error(t, err, "SetNonblock on an O_PATH fd should fail")
|
|
require.Nil(t, p)
|
|
|
|
// If newPoll had closed fd, F_GETFD would report it closed. It staying
|
|
// open proves newPoll left the fd for the caller to close exactly once.
|
|
require.True(t, fdOpen(t, fd), "newPoll must not close fd on failure; caller is the sole closer")
|
|
}
|
|
|
|
func TestPoll_Close_Idempotent(t *testing.T) {
|
|
tf, err := newPoll(newReadPipe(t), 1)
|
|
require.NoError(t, err)
|
|
if err := tf.Close(); err != nil {
|
|
t.Fatalf("first Close: %v", err)
|
|
}
|
|
if err := tf.Close(); err != nil {
|
|
t.Fatalf("second Close should be a no-op, got %v", err)
|
|
}
|
|
}
|
|
|
|
// fdOpen reports whether fd currently refers to an open file description.
|
|
// A closed (or never-allocated) fd makes F_GETFD fail with EBADF.
|
|
func fdOpen(t *testing.T, fd int) bool {
|
|
t.Helper()
|
|
_, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0)
|
|
if err == nil {
|
|
return true
|
|
}
|
|
if errors.Is(err, unix.EBADF) {
|
|
return false
|
|
}
|
|
t.Fatalf("unexpected fcntl(F_GETFD) error on fd %d: %v", fd, err)
|
|
return false
|
|
}
|
|
|
|
// TestPollQueueSet_Close_ClosesShutdownFd is the regression test for the
|
|
// leaked shutdown eventfd: the container that owns shutdownFd must close it in
|
|
// Close, and a second Close must be a safe no-op.
|
|
func TestPollQueueSet_Close_ClosesShutdownFd(t *testing.T) {
|
|
qs, err := NewPollQueueSet()
|
|
require.NoError(t, err)
|
|
c, ok := qs.(*pollQueueSet)
|
|
require.True(t, ok)
|
|
require.NoError(t, qs.Add(newReadPipe(t)))
|
|
|
|
shutdownFd := c.shutdownFd
|
|
require.True(t, fdOpen(t, shutdownFd), "shutdown eventfd should be open before Close")
|
|
|
|
require.NoError(t, qs.Close())
|
|
require.False(t, fdOpen(t, shutdownFd), "shutdown eventfd should be closed after Close")
|
|
|
|
// Second Close must not touch fds (shutdownFd is now -1) and must return nil.
|
|
require.NoError(t, qs.Close())
|
|
}
|
|
|
|
// TestOffloadQueueSet_Close_ClosesShutdownFd mirrors the poll regression test
|
|
// for the GSO/offload queueset.
|
|
func TestOffloadQueueSet_Close_ClosesShutdownFd(t *testing.T) {
|
|
qs, err := NewOffloadQueueSet(false)
|
|
require.NoError(t, err)
|
|
c, ok := qs.(*offloadQueueSet)
|
|
require.True(t, ok)
|
|
require.NoError(t, qs.Add(newReadPipe(t)))
|
|
|
|
shutdownFd := c.shutdownFd
|
|
require.True(t, fdOpen(t, shutdownFd), "shutdown eventfd should be open before Close")
|
|
|
|
require.NoError(t, qs.Close())
|
|
require.False(t, fdOpen(t, shutdownFd), "shutdown eventfd should be closed after Close")
|
|
|
|
// Second Close must not touch fds (shutdownFd is now -1) and must return nil.
|
|
require.NoError(t, qs.Close())
|
|
}
|