Skip to content

Commit eb36f3c

Browse files
committed
lanlord: read a refusal even if sending the request failed
A Server may refuse a caller, by uid, before reading its request, and then replies and closes. If that close won the race with the client's write of its request, the write failed with EPIPE, and DialContext returned "sending request: write: broken pipe" without reading the reply that said why. A disallowed user would see that instead of "not allowed", and TestRefusals/uid flaked on ubuntu-latest in CI. Fix it in the Client rather than the Server, to keep the root side as simple as possible. After a failed write, read the reply anyway, since what the server wrote before closing is still readable, and report the write error only if there is no reply. A write that failed because of the context still returns the context's error at once. go test -count=3000 -cpu=1,2,4 -run TestRefusals/uid reproduced the failure before. With this, it passes, including runs where the write got EPIPE. Updates tailscale/corp#51929 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: I17ffa4ec13b068ce572b060f7bac2e01afe29921
1 parent 7c001bc commit eb36f3c

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎lanlord/lanlord.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,22 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con
283283
return nil
284284
}
285285

286-
if _, err := io.WriteString(uc, reqPrefix+ap.String()+"\n"); err != nil {
287-
if cerr := ctxErr(err); cerr != nil {
286+
// The server may refuse before reading the request, replying and
287+
// closing while the request is still being written. The write then
288+
// fails, but the refusal is still there to read, so a failed write
289+
// is reported only if no reply follows.
290+
_, writeErr := io.WriteString(uc, reqPrefix+ap.String()+"\n")
291+
if writeErr != nil {
292+
if cerr := ctxErr(writeErr); cerr != nil {
288293
return nil, cerr
289294
}
290-
return nil, fmt.Errorf("lanlord: sending request: %w", err)
291295
}
292296
buf := make([]byte, maxLine)
293297
oob := make([]byte, unix.CmsgSpace(4))
294298
n, oobn, _, _, err := uc.(*net.UnixConn).ReadMsgUnix(buf, oob)
299+
if writeErr != nil && (err != nil || n == 0) {
300+
return nil, fmt.Errorf("lanlord: sending request: %w", writeErr)
301+
}
295302
if err != nil {
296303
if cerr := ctxErr(err); cerr != nil {
297304
return nil, cerr

0 commit comments

Comments
 (0)