Repository navigation
Conversation
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>
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.
TsnetAcceptreads one 64-byte accept record per connection. A short read is completed by the next read. Two problems in that loop:cbufto nil and replacedoobnwith the next read's0, soParseSocketControlMessagesaw nothing. The accept failed withgot 0 control messages, want 1, and the fd the kernel had installed was never closed, which kept the tailnet connection open.n += nnbefore checkingEINTR.nnis -1 on error, so withn == 0the nextdata[n:]panicked withslice 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.EINTRis retried without changing the byte count.TsnetAcceptstill owns the fd after the helper returns, callscloseOnExec, records the remote address, and hands the fd to C.Tests
tailscale_accept_test.goinjects short reads and EINTR through the helper. Three cases: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
mainhas no helper to call):With this change:
Full package checks on darwin (go1.27.4):
Not verified
Fixes tailscale/tailscale#22057