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
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2017 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Dispatch function for SMB2_LOCK
*/
#include <smbsrv/smb2_kproto.h>
/*
* [MS-SMB2] 2.2.26 LockSequenceIndex, LockSequenceNumber.
*/
#define SMB2_LSN_SHIFT 4
#define SMB2_LSN_MASK 0xf
typedef struct SMB2_LOCK_ELEMENT {
uint64_t Offset;
uint64_t Length;
uint32_t Flags;
uint32_t reserved;
} lock_elem_t;
static uint32_t smb2_unlock(smb_request_t *);
static uint32_t smb2_locks(smb_request_t *);
static uint32_t smb2_lock_blocking(smb_request_t *);
static boolean_t smb2_lock_chk_lockseq(smb_ofile_t *, uint32_t);
static void smb2_lock_set_lockseq(smb_ofile_t *, uint32_t);
/*
* This is a somewhat arbitrary sanity limit on the length of the
* SMB2_LOCK_ELEMENT array. It usually has length one or two.
*/
int smb2_lock_max_elem = 1024;
smb_sdrc_t
smb2_lock(smb_request_t *sr)
{
lock_elem_t *lvec, *lk;
smb2fid_t smb2fid;
uint32_t LockSequence;
uint32_t status;
uint16_t StructSize;
uint16_t LockCount;
uint16_t i;
int rc;
/*
* Decode SMB2 Lock request
*/
rc = smb_mbc_decodef(
&sr->smb_data, "wwlqq",
&StructSize, /* w */
&LockCount, /* w */
&LockSequence, /* l */
&smb2fid.persistent, /* q */
&smb2fid.temporal); /* q */
if (rc || StructSize != 48)
return (SDRC_ERROR);
/*
* Want FID lookup before the start probe.
*/
status = smb2sr_lookup_fid(sr, &smb2fid);
DTRACE_SMB2_START(op__Lock, smb_request_t *, sr);
if (status)
goto errout; /* Bad FID */
if (sr->fid_ofile->f_node == NULL || LockCount == 0) {
status = NT_STATUS_INVALID_PARAMETER;
goto errout;
}
if (LockCount > smb2_lock_max_elem) {
status = NT_STATUS_INSUFFICIENT_RESOURCES;
goto errout;
}
/*
* Check the LockSequence to determine whether a previous
* lock request succeeded, but the client disconnected
* (retaining a durable or resilient handle). If so, this
* is a lock "replay". We'll find the lock sequence here
* and return success without processing the lock again.
*/
if (sr->session->dialect < SMB_VERS_2_1)
LockSequence = 0;
if ((sr->session->dialect == SMB_VERS_2_1) &&
sr->fid_ofile->dh_vers != SMB2_RESILIENT)
LockSequence = 0;
/* dialect 3.0 or later can always use LockSequence */
if (LockSequence != 0 &&
smb2_lock_chk_lockseq(sr->fid_ofile, LockSequence)) {
status = NT_STATUS_SUCCESS;
goto errout;
}
/*
* Parse the array of SMB2_LOCK_ELEMENT structs.
* This array is free'd in smb_srm_fini.
*/
lvec = smb_srm_zalloc(sr, LockCount * sizeof (*lvec));
for (i = 0; i < LockCount; i++) {
lk = &lvec[i];
rc = smb_mbc_decodef(
&sr->smb_data, "qqll",
&lk->Offset, /* q */
&lk->Length, /* q */
&lk->Flags, /* l */
&lk->reserved); /* l */
if (rc) {
status = NT_STATUS_INVALID_PARAMETER;
goto errout;
}
}
/*
* [MS-SMB2] 3.3.5.14
* If the flags of the [first element of] the Locks array
* [has] SMB2_LOCKFLAG_UNLOCK set, the server MUST process
* the lock array as a series of unlocks. Otherwise, it
* MUST process the lock array as a series of lock requests.
*/
sr->arg.lock.lvec = lvec;
sr->arg.lock.lcnt = LockCount;
sr->arg.lock.lseq = LockSequence;
if (lvec[0].Flags & SMB2_LOCKFLAG_UNLOCK) {
status = smb2_unlock(sr);
} else {
status = smb2_locks(sr);
}
if (sr->fid_ofile->dh_persist) {
smb2_dh_update_locks(sr, sr->fid_ofile);
}
errout:
sr->smb2_status = status;
DTRACE_SMB2_DONE(op__Lock, smb_request_t *, sr);
if (status) {
smb2sr_put_error(sr, status);
return (SDRC_SUCCESS);
}
/*
* Encode SMB2 Lock reply
*/
(void) smb_mbc_encodef(
&sr->reply, "w..",
4); /* StructSize w */
/* reserved .. */
return (SDRC_SUCCESS);
}
/*
* Process what should be an array of unlock requests.
*/
static uint32_t
smb2_unlock(smb_request_t *sr)
{
lock_elem_t *lk;
lock_elem_t *lvec = sr->arg.lock.lvec;
uint32_t LockCount = sr->arg.lock.lcnt;
uint32_t LockSequence = sr->arg.lock.lseq;
uint32_t status = 0;
uint32_t pid = 0; /* SMB2 ignores lock PIDs. */
int i;
for (i = 0; i < LockCount; i++) {
lk = &lvec[i];
if (lk->Flags != SMB2_LOCKFLAG_UNLOCK) {
status = NT_STATUS_INVALID_PARAMETER;
break;
}
status = smb_unlock_range(sr, lk->Offset, lk->Length, pid);
if (status != 0)
break;
}
if (status == 0 && LockSequence != 0) {
smb2_lock_set_lockseq(sr->fid_ofile, LockSequence);
}
return (status);
}
/*
* Process what should be an array of lock requests.
*/
static uint32_t
smb2_locks(smb_request_t *sr)
{
lock_elem_t *lk;
lock_elem_t *lvec = sr->arg.lock.lvec;
uint32_t LockCount = sr->arg.lock.lcnt;
uint32_t LockSequence = sr->arg.lock.lseq;
uint32_t i;
uint32_t ltype;
uint32_t pid = 0; /* SMB2 ignores lock PIDs */
uint32_t timeout = 0;
uint32_t status = 0;
for (i = 0; i < LockCount; i++) {
lk = &lvec[i];
switch (lk->Flags) {
case SMB2_LOCKFLAG_SHARED_LOCK:
case SMB2_LOCKFLAG_EXCLUSIVE_LOCK:
/*
* Blocking locks have special rules:
* Must be exactly one element, else
* invalid parameter.
*/
if (i == 0 && LockCount == 1) {
status = smb2_lock_blocking(sr);
return (status);
}
/* FALLTHROUGH */
case SMB2_LOCKFLAG_UNLOCK:
default:
status = NT_STATUS_INVALID_PARAMETER;
goto end_loop;
/* BEGIN CSTYLED */
case SMB2_LOCKFLAG_SHARED_LOCK |
SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
/* END CSTYLED */
ltype = SMB_LOCK_TYPE_READONLY;
break;
/* BEGIN CSTYLED */
case SMB2_LOCKFLAG_EXCLUSIVE_LOCK |
SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
/* END CSTYLED */
ltype = SMB_LOCK_TYPE_READWRITE;
break;
}
status = smb_lock_range(sr, lk->Offset, lk->Length, pid,
ltype, timeout);
if (status != 0) {
goto end_loop;
}
}
end_loop:
if (status != 0) {
/*
* Oh... we have to rollback.
*/
while (i > 0) {
--i;
lk = &lvec[i];
(void) smb_unlock_range(sr,
lk->Offset, lk->Length, pid);
}
}
if (status == 0 && LockSequence != 0)
smb2_lock_set_lockseq(sr->fid_ofile, LockSequence);
return (status);
}
/*
* Handler for blocking lock requests, which may "go async".
* Always exactly one lock request here.
*/
static uint32_t
smb2_lock_blocking(smb_request_t *sr)
{
lock_elem_t *lk = sr->arg.lock.lvec;
uint32_t LockCount = sr->arg.lock.lcnt;
uint32_t LockSequence = sr->arg.lock.lseq;
uint32_t status;
uint32_t ltype;
uint32_t pid = 0; /* SMB2 ignores lock PIDs */
uint32_t timeout = UINT_MAX;
ASSERT(sr->fid_ofile->f_node != NULL);
ASSERT(LockCount == 1);
switch (lk->Flags) {
case SMB2_LOCKFLAG_SHARED_LOCK:
ltype = SMB_LOCK_TYPE_READONLY;
break;
case SMB2_LOCKFLAG_EXCLUSIVE_LOCK:
ltype = SMB_LOCK_TYPE_READWRITE;
break;
default:
ASSERT(0);
return (NT_STATUS_INTERNAL_ERROR);
}
/*
* Try the lock first with timeout=0 as we can often
* get a lock without going async and avoid an extra
* round trip with the client. Also, only go async
* for status returns that mean we will block.
*/
status = smb_lock_range(sr, lk->Offset, lk->Length, pid, ltype, 0);
if (status == NT_STATUS_LOCK_NOT_GRANTED ||
status == NT_STATUS_FILE_LOCK_CONFLICT) {
status = smb2sr_go_async(sr);
if (status != 0)
return (status);
status = smb_lock_range(sr, lk->Offset, lk->Length,
pid, ltype, timeout);
}
if (status == 0 && LockSequence != 0)
smb2_lock_set_lockseq(sr->fid_ofile, LockSequence);
return (status);
}
/*
* Check whether we've stored a given LockSequence
*
* [MS-SMB2] 3.3.5.14
*
* The server verifies the LockSequence by performing the following steps:
*
* 1. The server MUST use LockSequenceIndex as an index into the
* Open.LockSequenceArray in order to locate the sequence number entry.
* If the index exceeds the maximum extent of the Open.LockSequenceArray,
* or LockSequenceIndex is 0, or if the sequence number entry is empty,
* the server MUST skip step 2 and continue lock/unlock processing.
*
* 2. The server MUST compare LockSequenceNumber to the SequenceNumber of
* the entry located in step 1. If the sequence numbers are equal, the
* server MUST complete the lock/unlock request with success. Otherwise,
* the server MUST reset the entry value to empty and continue lock/unlock
* processing.
*/
boolean_t
smb2_lock_chk_lockseq(smb_ofile_t *ofile, uint32_t lockseq)
{
uint32_t lsi;
uint8_t lsn;
boolean_t rv;
/*
* LockSequenceNumber is the low four bits.
* LockSequenceIndex is the remaining 28 bits.
* valid range is 1..64, which we convert to an
* array index in the range 0..63
*/
lsn = lockseq & SMB2_LSN_MASK;
lsi = (lockseq >> SMB2_LSN_SHIFT);
if (lsi == 0 || lsi > SMB_OFILE_LSEQ_MAX)
return (B_FALSE);
--lsi;
mutex_enter(&ofile->f_mutex);
if (ofile->f_lock_seq[lsi] == lsn) {
rv = B_TRUE;
} else {
ofile->f_lock_seq[lsi] = (uint8_t)-1; /* "Empty" */
rv = B_FALSE;
}
mutex_exit(&ofile->f_mutex);
return (rv);
}
static void
smb2_lock_set_lockseq(smb_ofile_t *ofile, uint32_t lockseq)
{
uint32_t lsi;
uint8_t lsn;
/*
* LockSequenceNumber is the low four bits.
* LockSequenceIndex is the remaining 28 bits.
* valid range is 1..64, which we convert to an
* array index in the range 0..63
*/
lsn = lockseq & SMB2_LSN_MASK;
lsi = (lockseq >> SMB2_LSN_SHIFT);
if (lsi == 0 || lsi > SMB_OFILE_LSEQ_MAX) {
cmn_err(CE_NOTE, "smb2_lock_set_lockseq, index=%u", lsi);
return;
}
--lsi;
mutex_enter(&ofile->f_mutex);
ofile->f_lock_seq[lsi] = lsn;
mutex_exit(&ofile->f_mutex);
}
|