/
fdsrv.go
220 lines (201 loc) · 5.23 KB
/
fdsrv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2022 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Serves a file descriptor over an AF_UNIX socket when presented with a nonce.
//
// You must pass the socket path and nonce to the client via some out-of-band
// mechanism, such as gRPC or a bash script.
//
// Notes:
// - Uses the unix domain socket abstract namespace
// - Picks its own path in the abstract namespace for the socket.
// - Shared FDs are essentially duped, and they point to the same struct file:
// they share offsets and whatnot.
//
// Options:
// - WithServeOnce: serve once and shuts down (default is forever)
// - WithTimeout: cancel itself after a timeout (default none)
//
// Usage Server:
//
// fds, err := NewServer(fd_to_share, "some_nonce", WithServeOnce())
// var s path = fds.UDSPath()
//
// // Pass path and some_nonce to the client via an out of band mechanism
//
// fds.Serve(); // Blocks until the server is done
// fds.Close()
//
// Usage Client:
//
// sfd, err := GetSharedFD("uds_path", "some_nonce")
package fdsrv
import (
"errors"
"io"
"net"
"syscall"
"time"
)
var (
ErrTruncatedWrite = errors.New("truncated write")
ErrEmptyNonce = errors.New("nonce must not be empty")
ErrMissingSCM = errors.New("missing socket control message")
ErrNotOneUnixRights = errors.New("expected exactly one unix rights")
)
type Server struct {
dupedFD int
nonce string
listener *net.UnixListener
timeout time.Duration
serveOnce bool
}
// Serves the fd, returns true if successful, err for a server error.
// "false, nil" means the client was wrong, not the server.
func (fds *Server) handleConnection(uc *net.UnixConn) (bool, error) {
defer uc.Close()
buf := make([]byte, 4096)
n, err := uc.Read(buf)
if err != nil {
return false, err
}
query := string(buf[:n])
if query != fds.nonce {
io.WriteString(uc, "BAD NONCE")
return false, nil
}
oob := syscall.UnixRights(fds.dupedFD)
good := []byte("GOOD NONCE")
goodn, oobn, err := uc.WriteMsgUnix(good, oob, nil)
if err != nil {
return false, err
}
if goodn != len(good) || oobn != len(oob) {
return false, ErrTruncatedWrite
}
return true, nil
}
// NewServer creates a server. Close() it when you're done.
func NewServer(fd int, nonce string, options ...func(*Server) error) (*Server, error) {
var err error
fds := &Server{}
if len(nonce) == 0 {
return nil, ErrEmptyNonce
}
fds.nonce = nonce
for _, op := range options {
if err := op(fds); err != nil {
return nil, err
}
}
// An empty addr tells Linux to "autobind" to an available path in the
// abstract unix domain socket namespace
ua, err := net.ResolveUnixAddr("unix", "")
if err != nil {
return nil, err
}
fds.listener, err = net.ListenUnix("unix", ua)
if err != nil {
return nil, err
}
// Caller could close the file while we are running. Keep our own copy.
fds.dupedFD, err = syscall.Dup(int(fd))
if err != nil {
fds.listener.Close()
return nil, err
}
return fds, nil
}
// WithTimeOut adds a timeout option to NewServer
func WithTimeout(timeout time.Duration) func(*Server) error {
return func(fds *Server) error {
fds.timeout = timeout
return nil
}
}
// WithServeOnce sets the "serve once and exit" option to NewServer
func WithServeOnce() func(*Server) error {
return func(fds *Server) error {
fds.serveOnce = true
return nil
}
}
// UDSPath returns the Unix Domain Socket path the server is listening on
func (fds *Server) UDSPath() string {
return fds.listener.Addr().String()
}
// Close closes the server
func (fds *Server) Close() {
fds.listener.Close()
syscall.Close(fds.dupedFD)
}
// Serve serves the FD
func (fds *Server) Serve() error {
var deadline time.Time
if fds.timeout != 0 {
deadline = time.Now().Add(fds.timeout)
}
fds.listener.SetDeadline(deadline)
for {
conn, err := fds.listener.AcceptUnix()
if err != nil {
return err
}
conn.SetDeadline(deadline)
succeeded, err := fds.handleConnection(conn)
if err != nil {
return err
}
if succeeded && fds.serveOnce {
break
}
}
return nil
}
// GetSharedFD gets an FD served at udsPath with nonce
func GetSharedFD(udsPath, nonce string) (int, error) {
ua, err := net.ResolveUnixAddr("unix", udsPath)
if err != nil {
return 0, err
}
uc, err := net.DialUnix("unix", nil, ua)
if err != nil {
return 0, err
}
var nonceb = []byte(nonce)
n, err := uc.Write(nonceb)
if err != nil {
return 0, err
}
// If you don't send at least a byte, the server won't recvmsg. This
// is a Linux UDS SOCK_STREAM thing. It's possible that nonce is "",
// and len(nonceb) == 0. We could check earlier for an empty string,
// but test code uses this method to make sure the server can handle an
// empty nonce (BadEmptyNonce).
if n == 0 {
return 0, ErrEmptyNonce
}
if n != len(nonceb) {
return 0, ErrTruncatedWrite
}
oob := make([]byte, 1024)
_, oobn, _, _, err := uc.ReadMsgUnix(nil, oob)
if err != nil {
return 0, err
}
scm, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return 0, err
}
if len(scm) != 1 {
return 0, ErrMissingSCM
}
urs, err := syscall.ParseUnixRights(&scm[0])
if err != nil {
return 0, err
}
if len(urs) != 1 {
return 0, ErrNotOneUnixRights
}
return urs[0], nil
}