Skip to content

libtailscale: keep the accepted fd when a record is read in parts - #76

Open
Juupeee wants to merge 1 commit into
tailscale:mainfrom
Juupeee:fix/libtailscale-accept-record-fd
Open

Juupeee wants to merge 1 commit into
tailscale:mainfrom
Juupeee:fix/libtailscale-accept-record-fd

Conversation

@Juupeee

@Juupeee Juupeee commented Dec 3, 2026

Copy link
Copy Markdown

TsnetAccept reads one 64-byte accept record per connection. A short read is completed by the next read. Two problems in that loop:

  • The SCM_RIGHTS fd arrives with the first read. The loop then set cbuf to nil and replaced oobn with the next read's 0, so ParseSocketControlMessage saw nothing. The accept failed with got 0 control messages, want 1, and the fd the kernel had installed was never closed, which kept the tailnet connection open.
  • The loop did n += nn before checking EINTR. nn is -1 on error, so with n == 0 the next data[n:] panicked with slice bounds out of range [-1:], and with a partial record it moved the offset back one byte.

This moves the record read into recvAcceptRecord, which takes the recvmsg function as a parameter. The control buffer stays allocated for every read, each read's SCM_RIGHTS fds are collected, and a deferred close drops all collected fds unless the accept succeeds. EINTR is retried without changing the byte count.

TsnetAccept still owns the fd after the helper returns, calls closeOnExec, records the remote address, and hands the fd to C.

Tests

tailscale_accept_test.go injects short reads and EINTR through the helper. Three cases:

  • A record in two parts with the fd on the first part must return the fd and the assembled address.
  • An EINTR before any bytes must not move the offset.
  • An fd received before an EOF in the middle of a record must be closed.

These call the helper with a fake recvmsg, so they do not depend on the kernel stopping a stream read at an fd-carrying send. That behavior is Linux-specific and the test machine is darwin.

The tests fail against the original loop moved verbatim into the helper (upstream main has no helper to call):

--- FAIL: TestRecvAcceptRecordSplitRead
    recvAcceptRecord: libtailscale: got 0 control messages, want 1
--- FAIL: TestRecvAcceptRecordEINTR
panic: runtime error: slice bounds out of range [-1:] [recovered, repanicked]
--- FAIL: TestRecvAcceptRecordClosesFdsOnError
    fd 7 was not closed: fcntl error <nil>

With this change:

$ go test -run TestRecvAcceptRecord -v .
--- PASS: TestRecvAcceptRecordSplitRead (0.00s)
--- PASS: TestRecvAcceptRecordEINTR (0.00s)
--- PASS: TestRecvAcceptRecordClosesFdsOnError (0.00s)

Full package checks on darwin (go1.27.4):

$ gofmt -l tailscale.go tailscale_accept_test.go
(no output)
$ go vet .
(no output, exit 0)
$ go test -count=1 .
ok  	github.com/tailscale/libtailscale	4.200s

Not verified

  • No test sends a real split record over a socketpair with SCM_RIGHTS on Linux. The partial-read path is exercised through the injected recvmsg function only. On darwin a stream read coalesces both sends, so the kernel-level split cannot be produced here.
  • CI has not run yet.

Fixes tailscale/tailscale#22057

TsnetAccept reads each 64-byte accept record in a loop so a short read
can be completed by the next one. The SCM_RIGHTS fd arrives with the
first read, but the loop set cbuf to nil and replaced oobn with the
next read's zero before parsing. The accept then failed with "got 0
control messages, want 1", and the fd the kernel had installed was
never closed, so the tailnet connection behind it stayed open.

The loop also added nn to n before checking for EINTR. nn is -1 on
error, so with no bytes read the next data[n:] slice panicked, and
after a partial record the offset moved back one byte.

Move the record read into recvAcceptRecord. Keep one control buffer
for every read, parse SCM_RIGHTS when it arrives, and collect the fds.
A deferred close drops all of them unless the accept succeeds. Retry
EINTR without changing the byte count.

Tests inject short reads and EINTR through the helper's recvmsg
parameter. A split record must return its fd, an interrupted read must
not move the offset, and an fd received before a truncated record must
be closed.

Fixes tailscale/tailscale#22057

Signed-off-by: Juupeee <mjupriamin@gmail.com>
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.

libtailscale: TsnetAccept loses the connection fd when an accept record is read in parts

1 participant