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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2016 Nexenta Systems, Inc. All rights reserved.
*/
#include <smbsrv/smb_kproto.h>
static void smb_encode_sacl(mbuf_chain_t *, smb_acl_t *);
static void smb_encode_dacl(mbuf_chain_t *, smb_acl_t *);
static smb_acl_t *smb_decode_acl(mbuf_chain_t *, uint32_t);
/*
* smb_nt_transact_query_security_info
*
* This command allows the client to retrieve the security descriptor
* on a file. The result of the call is returned to the client in the
* Data part of the transaction response.
*
* Some clients specify a non-zero maximum data return size (mdrcnt)
* for the SD and some specify zero. In either case, if the mdrcnt is
* too small we need to return NT_STATUS_BUFFER_TOO_SMALL and a buffer
* size hint. The client should then retry with the appropriate buffer
* size.
*
* Client Parameter Block Description
* ================================== =================================
*
* USHORT Fid; FID of target
* USHORT Reserved; MBZ
* ULONG secinfo; Fields of descriptor to set
*
* Data Block Encoding Description
* ================================== ==================================
*
* Data[TotalDataCount] Security Descriptor information
*/
smb_sdrc_t
smb_nt_transact_query_security_info(struct smb_request *sr, struct smb_xa *xa)
{
smb_sd_t sd;
uint32_t secinfo;
uint32_t sdlen;
uint32_t status;
smb_error_t err;
if (smb_mbc_decodef(&xa->req_param_mb, "w2.l",
&sr->smb_fid, &secinfo) != 0) {
smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
return (SDRC_ERROR);
}
smbsr_lookup_file(sr);
if (sr->fid_ofile == NULL) {
smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
return (SDRC_ERROR);
}
if ((sr->fid_ofile->f_node == NULL) ||
(sr->fid_ofile->f_ftype != SMB_FTYPE_DISK)) {
smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
ERRDOS, ERROR_ACCESS_DENIED);
return (SDRC_ERROR);
}
sr->user_cr = smb_ofile_getcred(sr->fid_ofile);
if (sr->tid_tree->t_acltype != ACE_T) {
/*
* If target filesystem doesn't support ACE_T acls then
* don't process SACL
*/
secinfo &= ~SMB_SACL_SECINFO;
}
status = smb_sd_read(sr, &sd, secinfo);
if (status != NT_STATUS_SUCCESS) {
smbsr_error(sr, status, 0, 0);
return (SDRC_ERROR);
}
sdlen = smb_sd_len(&sd, secinfo);
if (sdlen == 0) {
smb_sd_term(&sd);
smbsr_error(sr, NT_STATUS_INVALID_SECURITY_DESCR, 0, 0);
return (SDRC_ERROR);
}
if (sdlen > xa->smb_mdrcnt) {
/*
* The maximum data return count specified by the
* client is not big enough to hold the security
* descriptor. We have to return an error but we
* should provide a buffer size hint for the client.
*/
(void) smb_mbc_encodef(&xa->rep_param_mb, "l", sdlen);
err.status = NT_STATUS_BUFFER_TOO_SMALL;
err.errcls = ERRDOS;
err.errcode = ERROR_INSUFFICIENT_BUFFER;
smbsr_set_error(sr, &err);
smb_sd_term(&sd);
return (SDRC_SUCCESS);
}
smb_encode_sd(&xa->rep_data_mb, &sd, secinfo);
(void) smb_mbc_encodef(&xa->rep_param_mb, "l", sdlen);
smb_sd_term(&sd);
return (SDRC_SUCCESS);
}
/*
* smb_nt_transact_set_security_info
*
* This command allows the client to change the security descriptor on a
* file. All we do here is decode the parameters and the data. The data
* is passed directly to smb_nt_set_security_object, with the security
* information describing the information to set. There are no response
* parameters or data.
*
* Client Parameter Block Encoding Description
* ================================== ==================================
* USHORT Fid; FID of target
* USHORT Reserved; MBZ
* ULONG SecurityInformation; Fields of SD that to set
*
* Data Block Encoding Description
* ================================== ==================================
* Data[TotalDataCount] Security Descriptor information
*/
smb_sdrc_t
smb_nt_transact_set_security_info(struct smb_request *sr, struct smb_xa *xa)
{
smb_sd_t sd;
uint32_t secinfo;
uint32_t status;
if (smb_mbc_decodef(&xa->req_param_mb, "w2.l",
&sr->smb_fid, &secinfo) != 0) {
smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
return (SDRC_ERROR);
}
smbsr_lookup_file(sr);
if (sr->fid_ofile == NULL) {
smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
return (SDRC_ERROR);
}
if ((sr->fid_ofile->f_node == NULL) ||
(sr->fid_ofile->f_ftype != SMB_FTYPE_DISK)) {
smbsr_error(sr, NT_STATUS_ACCESS_DENIED, 0, 0);
return (SDRC_ERROR);
}
sr->user_cr = smb_ofile_getcred(sr->fid_ofile);
if (SMB_TREE_IS_READONLY(sr)) {
smbsr_error(sr, NT_STATUS_MEDIA_WRITE_PROTECTED, 0, 0);
return (SDRC_ERROR);
}
if (sr->tid_tree->t_acltype != ACE_T) {
/*
* If target filesystem doesn't support ACE_T acls then
* don't process SACL
*/
secinfo &= ~SMB_SACL_SECINFO;
}
if ((secinfo & SMB_ALL_SECINFO) == 0) {
return (NT_STATUS_SUCCESS);
}
status = smb_decode_sd(&xa->req_data_mb, &sd);
if (status != NT_STATUS_SUCCESS) {
smbsr_error(sr, status, 0, 0);
return (SDRC_ERROR);
}
if (((secinfo & SMB_OWNER_SECINFO) && (sd.sd_owner == NULL)) ||
((secinfo & SMB_GROUP_SECINFO) && (sd.sd_group == NULL))) {
smbsr_error(sr, NT_STATUS_INVALID_PARAMETER, 0, 0);
return (SDRC_ERROR);
}
if (!smb_node_is_system(sr->fid_ofile->f_node))
status = smb_sd_write(sr, &sd, secinfo);
smb_sd_term(&sd);
if (status != NT_STATUS_SUCCESS) {
smbsr_error(sr, status, 0, 0);
return (SDRC_ERROR);
}
return (SDRC_SUCCESS);
}
/*
* smb_encode_sd
*
* Encodes given security descriptor in the reply buffer.
*/
void
smb_encode_sd(mbuf_chain_t *mbc, smb_sd_t *sd, uint32_t secinfo)
{
uint32_t offset = SMB_SD_HDRSIZE;
/* encode header */
(void) smb_mbc_encodef(mbc, "b.w",
sd->sd_revision, sd->sd_control | SE_SELF_RELATIVE);
/* owner offset */
if (secinfo & SMB_OWNER_SECINFO) {
ASSERT(sd->sd_owner);
(void) smb_mbc_encodef(mbc, "l", offset);
offset += smb_sid_len(sd->sd_owner);
} else {
(void) smb_mbc_encodef(mbc, "l", 0);
}
/* group offset */
if (secinfo & SMB_GROUP_SECINFO) {
ASSERT(sd->sd_group);
(void) smb_mbc_encodef(mbc, "l", offset);
offset += smb_sid_len(sd->sd_group);
} else {
(void) smb_mbc_encodef(mbc, "l", 0);
}
/* SACL offset */
if ((secinfo & SMB_SACL_SECINFO) && (sd->sd_sacl)) {
(void) smb_mbc_encodef(mbc, "l", offset);
offset += smb_acl_len(sd->sd_sacl);
} else {
(void) smb_mbc_encodef(mbc, "l", 0);
}
/* DACL offset */
if ((secinfo & SMB_DACL_SECINFO) && (sd->sd_dacl))
(void) smb_mbc_encodef(mbc, "l", offset);
else
(void) smb_mbc_encodef(mbc, "l", 0);
if (secinfo & SMB_OWNER_SECINFO)
smb_encode_sid(mbc, sd->sd_owner);
if (secinfo & SMB_GROUP_SECINFO)
smb_encode_sid(mbc, sd->sd_group);
if (secinfo & SMB_SACL_SECINFO)
smb_encode_sacl(mbc, sd->sd_sacl);
if (secinfo & SMB_DACL_SECINFO)
smb_encode_dacl(mbc, sd->sd_dacl);
}
/*
* smb_encode_sid
*
* Encodes given SID in the reply buffer.
*/
void
smb_encode_sid(mbuf_chain_t *mbc, smb_sid_t *sid)
{
int i;
(void) smb_mbc_encodef(mbc, "bb",
sid->sid_revision, sid->sid_subauthcnt);
for (i = 0; i < NT_SID_AUTH_MAX; i++) {
(void) smb_mbc_encodef(mbc, "b",
sid->sid_authority[i]);
}
for (i = 0; i < sid->sid_subauthcnt; i++) {
(void) smb_mbc_encodef(mbc, "l",
sid->sid_subauth[i]);
}
}
/*
* smb_encode_sacl
*
* Encodes given SACL in the reply buffer.
*/
static void
smb_encode_sacl(mbuf_chain_t *mbc, smb_acl_t *acl)
{
smb_ace_t *ace;
int i;
if (acl == NULL)
return;
/* encode header */
(void) smb_mbc_encodef(mbc, "b.ww2.", acl->sl_revision,
acl->sl_bsize, acl->sl_acecnt);
for (i = 0, ace = acl->sl_aces; i < acl->sl_acecnt; i++, ace++) {
(void) smb_mbc_encodef(mbc, "bbwl",
ace->se_hdr.se_type, ace->se_hdr.se_flags,
ace->se_hdr.se_bsize, ace->se_mask);
smb_encode_sid(mbc, ace->se_sid);
}
}
/*
* smb_encode_dacl
*
* Encodes given DACL in the reply buffer.
*/
static void
smb_encode_dacl(mbuf_chain_t *mbc, smb_acl_t *acl)
{
smb_ace_t *ace;
if (acl == NULL)
return;
/* encode header */
(void) smb_mbc_encodef(mbc, "b.ww2.", acl->sl_revision,
acl->sl_bsize, acl->sl_acecnt);
ace = list_head(&acl->sl_sorted);
while (ace) {
(void) smb_mbc_encodef(mbc, "bbwl",
ace->se_hdr.se_type, ace->se_hdr.se_flags,
ace->se_hdr.se_bsize, ace->se_mask);
smb_encode_sid(mbc, ace->se_sid);
ace = list_next(&acl->sl_sorted, ace);
}
}
/*
* smb_decode_sd
*
* Decodes the security descriptor in the request buffer
* and set the fields of 'sd' appropraitely. Upon successful
* return, caller must free allocated memories by calling
* smb_sd_term().
*/
uint32_t
smb_decode_sd(mbuf_chain_t *mbc, smb_sd_t *sd)
{
struct mbuf_chain sdbuf;
uint32_t owner_offs;
uint32_t group_offs;
uint32_t sacl_offs;
uint32_t dacl_offs;
int rc;
smb_sd_init(sd, SECURITY_DESCRIPTOR_REVISION);
(void) MBC_SHADOW_CHAIN(&sdbuf, mbc,
mbc->chain_offset,
mbc->max_bytes - mbc->chain_offset);
rc = smb_mbc_decodef(&sdbuf, "b.wllll",
&sd->sd_revision, &sd->sd_control,
&owner_offs, &group_offs, &sacl_offs, &dacl_offs);
/* Prevent disallowed flags in smb_sd_term. */
sd->sd_control &= ~SE_SELF_RELATIVE;
if (rc != 0)
goto decode_error;
if (owner_offs != 0) {
if (owner_offs < SMB_SD_HDRSIZE)
goto decode_error;
sd->sd_owner = smb_decode_sid(mbc, owner_offs);
if (sd->sd_owner == NULL)
goto decode_error;
}
if (group_offs != 0) {
if (group_offs < SMB_SD_HDRSIZE)
goto decode_error;
sd->sd_group = smb_decode_sid(mbc, group_offs);
if (sd->sd_group == NULL)
goto decode_error;
}
if (sacl_offs != 0) {
if ((sd->sd_control & SE_SACL_PRESENT) == 0)
goto decode_error;
if (sacl_offs < SMB_SD_HDRSIZE)
goto decode_error;
sd->sd_sacl = smb_decode_acl(mbc, sacl_offs);
if (sd->sd_sacl == NULL)
goto decode_error;
}
if (dacl_offs != 0) {
if ((sd->sd_control & SE_DACL_PRESENT) == 0)
goto decode_error;
if (dacl_offs < SMB_SD_HDRSIZE)
goto decode_error;
sd->sd_dacl = smb_decode_acl(mbc, dacl_offs);
if (sd->sd_dacl == NULL)
goto decode_error;
}
return (NT_STATUS_SUCCESS);
decode_error:
smb_sd_term(sd);
return (NT_STATUS_INVALID_SECURITY_DESCR);
}
/*
* smb_decode_sid
*
* Allocates memory and decodes the SID in the request buffer
* Upon successful return, caller must free the allocated memory
* by calling smb_sid_free()
*/
smb_sid_t *
smb_decode_sid(mbuf_chain_t *mbc, uint32_t offset)
{
uint8_t revision;
uint8_t subauth_cnt;
struct mbuf_chain sidbuf;
smb_sid_t *sid;
int sidlen;
int bytes_left;
int i;
offset += mbc->chain_offset;
bytes_left = mbc->max_bytes - offset;
if (bytes_left < (int)sizeof (smb_sid_t))
return (NULL);
if (MBC_SHADOW_CHAIN(&sidbuf, mbc, offset, bytes_left) != 0)
return (NULL);
if (smb_mbc_decodef(&sidbuf, "bb", &revision, &subauth_cnt))
return (NULL);
sidlen = sizeof (smb_sid_t) - sizeof (uint32_t) +
(subauth_cnt * sizeof (uint32_t));
sid = kmem_alloc(sidlen, KM_SLEEP);
sid->sid_revision = revision;
sid->sid_subauthcnt = subauth_cnt;
for (i = 0; i < NT_SID_AUTH_MAX; i++) {
if (smb_mbc_decodef(&sidbuf, "b", &sid->sid_authority[i]))
goto decode_err;
}
for (i = 0; i < sid->sid_subauthcnt; i++) {
if (smb_mbc_decodef(&sidbuf, "l", &sid->sid_subauth[i]))
goto decode_err;
}
return (sid);
decode_err:
kmem_free(sid, sidlen);
return (NULL);
}
/*
* smb_decode_acl
*
* Allocates memory and decodes the ACL in the request buffer
* Upon successful return, caller must free the allocated memory
* by calling smb_acl_free().
*/
static smb_acl_t *
smb_decode_acl(mbuf_chain_t *mbc, uint32_t offset)
{
struct mbuf_chain aclbuf;
smb_acl_t *acl;
smb_ace_t *ace;
uint8_t revision;
uint16_t size;
uint16_t acecnt;
int bytes_left;
uint32_t sid_offs = offset;
int sidlen;
int i;
offset += mbc->chain_offset;
bytes_left = mbc->max_bytes - offset;
if (bytes_left < SMB_ACL_HDRSIZE)
return (NULL);
if (MBC_SHADOW_CHAIN(&aclbuf, mbc, offset, bytes_left) != 0)
return (NULL);
if (smb_mbc_decodef(&aclbuf, "b.ww2.", &revision, &size, &acecnt))
return (NULL);
if (size == 0)
return (NULL);
acl = smb_acl_alloc(revision, size, acecnt);
sid_offs += SMB_ACL_HDRSIZE;
for (i = 0, ace = acl->sl_aces; i < acl->sl_acecnt; i++, ace++) {
if (smb_mbc_decodef(&aclbuf, "bbwl",
&ace->se_hdr.se_type, &ace->se_hdr.se_flags,
&ace->se_hdr.se_bsize, &ace->se_mask))
goto decode_error;
sid_offs += SMB_ACE_HDRSIZE + sizeof (ace->se_mask);
ace->se_sid = smb_decode_sid(mbc, sid_offs);
if (ace->se_sid == NULL)
goto decode_error;
/* This is SID length plus any paddings between ACEs */
sidlen = ace->se_hdr.se_bsize -
(SMB_ACE_HDRSIZE + sizeof (ace->se_mask));
aclbuf.chain_offset += sidlen;
sid_offs += sidlen;
}
return (acl);
decode_error:
smb_acl_free(acl);
return (NULL);
}
|