Repository navigation
Expand file tree
/
Copy pathconn.go
More file actions
428 lines (396 loc) · 10.6 KB
/
Copy pathconn.go
File metadata and controls
428 lines (396 loc) · 10.6 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package smbserver
import (
"crypto/hmac"
"crypto/sha256"
"errors"
"fmt"
"io"
"net"
"os"
"runtime/debug"
"sync"
"time"
)
// conn is one TCP connection. Its requests are handled one at a time, in
// order, by the goroutine that reads them.
type conn struct {
srv *server
nc net.Conn
// wmu serialises the writes: besides the responses, a connection
// carries the oplock breaks caused by other clients.
wmu sync.Mutex
negotiated bool
smb1Seen bool
dialect uint16
maxIO uint32
// What the client said in NEGOTIATE, for its later validation.
clientGUID [16]byte
clientCaps uint32
clientSecMode uint16
// credits is the number of credits the client currently holds.
credits int
authenticated bool
// mu guards sessions, which another connection reaches when a client
// that reconnected retires its previous session.
mu sync.Mutex
sessions map[uint64]*session
}
// request is one command of a message, with what the connection resolved
// about it before calling the handler.
type request struct {
hdr header
// msg is the command, header included, so that the offsets of the
// protocol index it directly.
msg []byte
sess *session
tree *tree
chain *chain
// sign tells that the response must be signed: the request was, or
// this is the response that concludes a session setup.
sign bool
// fatal makes the connection close once the response is sent.
fatal bool
// silent suppresses the response (CANCEL has none).
silent bool
// note is what a handler wants the trace to say about the request.
note string
}
// chain is the state shared by the commands of a compound request: a related
// command inherits the session, tree and file of its predecessor.
type chain struct {
sessionID uint64
treeID uint32
fileID uint64
hasFile bool
// status is why there is no file, when a CREATE of the chain failed.
status ntStatus
}
var errProtocol = errors.New("protocol violation")
// trace logs every command and its status through Config.Logf. It exists to
// diagnose a client from the outside, in CI for instance.
var trace = os.Getenv("SMBSERVER_TRACE") != ""
func (c *conn) serve() {
defer c.close()
defer func() {
// Last line of defence: a bug in a decoder must cost the
// offending connection, never the process embedding the server.
if p := recover(); p != nil {
c.srv.logf("panic serving %v: %v\n%s", c.nc.RemoteAddr(), p, debug.Stack())
}
}()
for {
if !c.authenticated {
c.nc.SetReadDeadline(time.Now().Add(preAuthTimeout))
}
msg, err := c.readFrame()
if err == nil {
err = c.handleFrame(msg)
}
if err != nil {
if errors.Is(err, errProtocol) {
c.srv.logf("closing %v: %v", c.nc.RemoteAddr(), err)
}
return
}
}
}
func (c *conn) readFrame() ([]byte, error) {
var h [4]byte
if _, err := io.ReadFull(c.nc, h[:]); err != nil {
return nil, err
}
n := int(h[1])<<16 | int(h[2])<<8 | int(h[3])
limit := preAuthFrame
if c.authenticated {
limit = maxIO + 0x10000
}
// The first byte is the NetBIOS session message type, zero on the
// direct TCP transport, which is the only one served.
if h[0] != 0 || n < 4 || n > limit {
return nil, fmt.Errorf("%w: frame type %d of %d bytes", errProtocol, h[0], n)
}
msg := make([]byte, n)
if _, err := io.ReadFull(c.nc, msg); err != nil {
return nil, err
}
return msg, nil
}
func (c *conn) handleFrame(msg []byte) error {
if msg[0] == 0xFF && msg[1] == 'S' && msg[2] == 'M' && msg[3] == 'B' {
return c.smb1Negotiate(msg)
}
return c.handleMessage(msg)
}
// handleMessage processes a message, which is one command or a compound
// chain of them, and sends the responses as one message.
func (c *conn) handleMessage(msg []byte) error {
var (
payload []byte
ch chain
fatal bool
)
for off, n := 0, 0; ; n++ {
hdr, ok := parseHeader(msg[off:])
if !ok || n >= maxCompound {
return errProtocol
}
end := len(msg)
if hdr.next != 0 {
if hdr.next < headerSize || hdr.next%8 != 0 || int(hdr.next) > len(msg)-off-headerSize {
return errProtocol
}
end = off + int(hdr.next)
}
r := &request{hdr: hdr, msg: msg[off:end], chain: &ch}
out, err := c.process(r)
if err != nil {
return err
}
fatal = fatal || r.fatal
if out != nil {
// Each response of a compound is padded to eight bytes,
// linked to the next one, and signed on its own.
if hdr.next != 0 {
out = pad8(out)
le.PutUint32(out[20:], uint32(len(out)))
}
if r.sign && r.sess != nil && r.sess.signingKey != nil {
signWith(r.sess.mac, out)
}
if payload == nil {
payload = out
} else {
payload = append(payload, out...)
}
}
if hdr.next == 0 {
break
}
off = end
}
if payload != nil {
if err := c.send(payload); err != nil {
return err
}
}
if fatal {
return errProtocol
}
return nil
}
// process runs one command and returns its response, header included, or nil
// when the command has none.
func (c *conn) process(r *request) ([]byte, error) {
h := &r.hdr
if h.flags&flagResponse != 0 {
return nil, errProtocol
}
// CANCEL is the one command that consumes no credit.
if h.command != cmdCancel {
charge := max(int(h.creditCharge), 1)
if charge > c.credits {
return nil, fmt.Errorf("%w: credit charge %d exceeds the %d granted", errProtocol, charge, c.credits)
}
c.credits -= charge
}
st, out := c.route(r)
if trace {
c.srv.logf("%v: command %d message %d flags %#x %s: status %#x", c.nc.RemoteAddr(), h.command, h.messageID, h.flags, r.note, uint32(st))
}
if r.silent {
return nil, nil
}
// A response carries its body only on success and on the warnings
// that return data; everything else gets the error body.
if out == nil {
out = errorBody(nil)
}
rh := header{
creditCharge: h.creditCharge,
status: st,
command: h.command,
flags: flagResponse | h.flags&flagRelated,
messageID: h.messageID,
sessionID: h.sessionID,
treeID: h.treeID,
}
if r.sess != nil {
rh.sessionID = r.sess.id
}
if r.tree != nil {
rh.treeID = r.tree.id
}
// Grant what the client asks, within the window, and never leave it
// without a credit to send its next request.
grant := min(max(int(h.credits), 1), maxCredits-c.credits)
c.credits += grant
rh.credits = uint16(grant)
rh.put(out)
r.chain.sessionID = rh.sessionID
r.chain.treeID = rh.treeID
return out, nil
}
// route authenticates the command and dispatches it. Nothing below the
// session lookup runs for a peer that has not logged in.
func (c *conn) route(r *request) (ntStatus, []byte) {
h := &r.hdr
if !c.negotiated {
if h.command != cmdNegotiate {
r.fatal = true
return statusInvalidParameter, nil
}
return c.negotiate(r)
}
switch h.command {
case cmdNegotiate:
// A second NEGOTIATE on a connection is a protocol violation.
r.fatal = true
return statusInvalidParameter, nil
case cmdSessionSetup:
return c.sessionSetup(r)
case cmdEcho:
// Clients send keepalive echoes outside of any session.
if h.sessionID == 0 {
return statusSuccess, emptyBody()
}
}
sid, tid := h.sessionID, h.treeID
if h.flags&flagRelated != 0 {
sid, tid = r.chain.sessionID, r.chain.treeID
}
sess := c.session(sid)
if sess == nil || !sess.authenticated {
return statusUserSessionDeleted, nil
}
// Signing is required: an unsigned or badly signed request is refused
// whatever the client negotiated.
if h.flags&flagSigned == 0 || !verifyWith(sess.mac, r.msg) {
return statusAccessDenied, nil
}
r.sess = sess
r.sign = true
switch h.command {
case cmdLogoff:
sess.close()
return statusSuccess, emptyBody()
case cmdTreeConnect:
return c.treeConnect(r)
case cmdEcho:
return statusSuccess, emptyBody()
case cmdCancel:
// No request ever stays pending, so there is nothing to cancel.
r.silent = true
return statusSuccess, nil
}
tree := sess.tree(tid)
if tree == nil {
return statusNetworkNameDeleted, nil
}
r.tree = tree
switch h.command {
case cmdTreeDisconnect:
sess.removeTree(tree)
return statusSuccess, emptyBody()
case cmdCreate:
st, out := c.create(r)
if st != statusSuccess {
r.chain.hasFile = false
r.chain.status = st
}
return st, out
case cmdClose:
return c.closeFile(r)
case cmdFlush:
return c.flush(r)
case cmdRead:
return c.read(r)
case cmdWrite:
return c.write(r)
case cmdLock:
return c.lock(r)
case cmdIoctl:
return c.ioctl(r)
case cmdQueryDirectory:
return c.queryDirectory(r)
case cmdQueryInfo:
return c.queryInfo(r)
case cmdSetInfo:
return c.setInfo(r)
case cmdOplockBreak:
return c.oplockBreak(r)
}
// CHANGE_NOTIFY lands here: directories are not watched.
return statusNotSupported, nil
}
// errorBody is the SMB2 ERROR response body, with optional error data.
func errorBody(data []byte) []byte {
out := newMsg(8 + max(len(data), 1))
le.PutUint16(out[headerSize:], 9)
le.PutUint32(out[headerSize+4:], uint32(len(data)))
copy(out[headerSize+8:], data)
return out
}
// emptyBody is the four-byte body shared by the responses that carry no
// data (ECHO, LOGOFF, TREE_DISCONNECT, FLUSH, LOCK).
func emptyBody() []byte {
out := newMsg(4)
le.PutUint16(out[headerSize:], 4)
return out
}
func (c *conn) send(payload []byte) error {
var h [4]byte
h[1], h[2], h[3] = byte(len(payload)>>16), byte(len(payload)>>8), byte(len(payload))
c.wmu.Lock()
defer c.wmu.Unlock()
c.nc.SetWriteDeadline(time.Now().Add(writeTimeout))
bufs := net.Buffers{h[:], payload}
_, err := bufs.WriteTo(c.nc)
return err
}
// mac is the SMB 2 signature: the first 16 bytes of the HMAC-SHA256 of the
// message, computed with its signature field zeroed.
func mac(key, msg []byte) []byte {
h := hmac.New(sha256.New, key)
h.Write(msg)
return h.Sum(nil)[:16]
}
// signWith sets the signed flag of a response and fills its signature.
func signWith(mac func([]byte) []byte, msg []byte) {
le.PutUint32(msg[16:], le.Uint32(msg[16:])|flagSigned)
clear(msg[48:64])
copy(msg[48:64], mac(msg))
}
// verifyWith checks the signature of a request. It zeroes the signature
// field of msg, which nothing reads afterwards.
func verifyWith(mac func([]byte) []byte, msg []byte) bool {
var got [16]byte
copy(got[:], msg[48:64])
clear(msg[48:64])
return hmac.Equal(got[:], mac(msg))
}
func sign(key, msg []byte) {
signWith(func(b []byte) []byte { return mac(key, b) }, msg)
}
func verify(key, msg []byte) bool {
return verifyWith(func(b []byte) []byte { return mac(key, b) }, msg)
}
func (c *conn) session(id uint64) *session {
c.mu.Lock()
defer c.mu.Unlock()
return c.sessions[id]
}
func (c *conn) close() {
c.nc.Close()
c.mu.Lock()
sessions := make([]*session, 0, len(c.sessions))
for _, s := range c.sessions {
sessions = append(sessions, s)
}
c.mu.Unlock()
for _, s := range sessions {
s.close()
}
c.srv.mu.Lock()
delete(c.srv.conns, c)
c.srv.mu.Unlock()
}