1 // SPDX-License-Identifier: LGPL-2.1+
2 /*
3 * Copyright (C) International Business Machines Corp., 2007,2008
4 * Author(s): Steve French (sfrench@us.ibm.com)
5 * Copyright (C) 2020 Samsung Electronics Co., Ltd.
6 * Author(s): Namjae Jeon <linkinjeon@kernel.org>
7 */
8
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/mnt_idmapping.h>
13
14 #include "smbacl.h"
15 #include "smb_common.h"
16 #include "server.h"
17 #include "misc.h"
18 #include "mgmt/share_config.h"
19
20 static const struct smb_sid domain = {1, 4, {0, 0, 0, 0, 0, 5},
21 {cpu_to_le32(21), cpu_to_le32(1), cpu_to_le32(2), cpu_to_le32(3),
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
23
24 /* security id for everyone/world system group */
25 static const struct smb_sid creator_owner = {
26 1, 1, {0, 0, 0, 0, 0, 3}, {0} };
27 /* security id for everyone/world system group */
28 static const struct smb_sid creator_group = {
29 1, 1, {0, 0, 0, 0, 0, 3}, {cpu_to_le32(1)} };
30
31 /* security id for everyone/world system group */
32 static const struct smb_sid sid_everyone = {
33 1, 1, {0, 0, 0, 0, 0, 1}, {0} };
34 /* security id for Authenticated Users system group */
35 static const struct smb_sid sid_authusers = {
36 1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(11)} };
37
38 /* S-1-22-1 Unmapped Unix users */
39 static const struct smb_sid sid_unix_users = {1, 1, {0, 0, 0, 0, 0, 22},
40 {cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
41
42 /* S-1-22-2 Unmapped Unix groups */
43 static const struct smb_sid sid_unix_groups = { 1, 1, {0, 0, 0, 0, 0, 22},
44 {cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
45
46 /*
47 * See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
48 */
49
50 /* S-1-5-88 MS NFS and Apple style UID/GID/mode */
51
52 /* S-1-5-88-1 Unix uid */
53 static const struct smb_sid sid_unix_NFS_users = { 1, 2, {0, 0, 0, 0, 0, 5},
54 {cpu_to_le32(88),
55 cpu_to_le32(1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
56
57 /* S-1-5-88-2 Unix gid */
58 static const struct smb_sid sid_unix_NFS_groups = { 1, 2, {0, 0, 0, 0, 0, 5},
59 {cpu_to_le32(88),
60 cpu_to_le32(2), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
61
62 /* S-1-5-88-3 Unix mode */
63 static const struct smb_sid sid_unix_NFS_mode = { 1, 2, {0, 0, 0, 0, 0, 5},
64 {cpu_to_le32(88),
65 cpu_to_le32(3), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
66
67 /*
68 * if the two SIDs (roughly equivalent to a UUID for a user or group) are
69 * the same returns zero, if they do not match returns non-zero.
70 */
compare_sids(const struct smb_sid * ctsid,const struct smb_sid * cwsid)71 int compare_sids(const struct smb_sid *ctsid, const struct smb_sid *cwsid)
72 {
73 int i;
74 int num_subauth, num_sat, num_saw;
75
76 if (!ctsid || !cwsid)
77 return 1;
78
79 /* compare the revision */
80 if (ctsid->revision != cwsid->revision) {
81 if (ctsid->revision > cwsid->revision)
82 return 1;
83 else
84 return -1;
85 }
86
87 /* compare all of the six auth values */
88 for (i = 0; i < NUM_AUTHS; ++i) {
89 if (ctsid->authority[i] != cwsid->authority[i]) {
90 if (ctsid->authority[i] > cwsid->authority[i])
91 return 1;
92 else
93 return -1;
94 }
95 }
96
97 /* compare all of the subauth values if any */
98 num_sat = ctsid->num_subauth;
99 num_saw = cwsid->num_subauth;
100 num_subauth = min(num_sat, num_saw);
101 if (num_subauth) {
102 for (i = 0; i < num_subauth; ++i) {
103 if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) {
104 if (le32_to_cpu(ctsid->sub_auth[i]) >
105 le32_to_cpu(cwsid->sub_auth[i]))
106 return 1;
107 else
108 return -1;
109 }
110 }
111 }
112
113 return 0; /* sids compare/match */
114 }
115
smb_copy_sid(struct smb_sid * dst,const struct smb_sid * src)116 static void smb_copy_sid(struct smb_sid *dst, const struct smb_sid *src)
117 {
118 int i;
119
120 dst->revision = src->revision;
121 dst->num_subauth = min_t(u8, src->num_subauth, SID_MAX_SUB_AUTHORITIES);
122 for (i = 0; i < NUM_AUTHS; ++i)
123 dst->authority[i] = src->authority[i];
124 for (i = 0; i < dst->num_subauth; ++i)
125 dst->sub_auth[i] = src->sub_auth[i];
126 }
127
128 /*
129 * change posix mode to reflect permissions
130 * pmode is the existing mode (we only want to overwrite part of this
131 * bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007
132 */
access_flags_to_mode(struct smb_fattr * fattr,__le32 ace_flags,int type)133 static umode_t access_flags_to_mode(struct smb_fattr *fattr, __le32 ace_flags,
134 int type)
135 {
136 __u32 flags = le32_to_cpu(ace_flags);
137 umode_t mode = 0;
138
139 if (flags & GENERIC_ALL) {
140 mode = 0777;
141 ksmbd_debug(SMB, "all perms\n");
142 return mode;
143 }
144
145 if ((flags & GENERIC_READ) || (flags & FILE_READ_RIGHTS))
146 mode = 0444;
147 if ((flags & GENERIC_WRITE) || (flags & FILE_WRITE_RIGHTS)) {
148 mode |= 0222;
149 if (S_ISDIR(fattr->cf_mode))
150 mode |= 0111;
151 }
152 if ((flags & GENERIC_EXECUTE) || (flags & FILE_EXEC_RIGHTS))
153 mode |= 0111;
154
155 if (type == ACCESS_DENIED_ACE_TYPE || type == ACCESS_DENIED_OBJECT_ACE_TYPE)
156 mode = ~mode;
157
158 ksmbd_debug(SMB, "access flags 0x%x mode now %04o\n", flags, mode);
159
160 return mode;
161 }
162
163 /*
164 * Generate access flags to reflect permissions mode is the existing mode.
165 * This function is called for every ACE in the DACL whose SID matches
166 * with either owner or group or everyone.
167 */
mode_to_access_flags(umode_t mode,umode_t bits_to_use,__u32 * pace_flags)168 static void mode_to_access_flags(umode_t mode, umode_t bits_to_use,
169 __u32 *pace_flags)
170 {
171 /* reset access mask */
172 *pace_flags = 0x0;
173
174 /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */
175 mode &= bits_to_use;
176
177 /*
178 * check for R/W/X UGO since we do not know whose flags
179 * is this but we have cleared all the bits sans RWX for
180 * either user or group or other as per bits_to_use
181 */
182 if (mode & 0444)
183 *pace_flags |= SET_FILE_READ_RIGHTS;
184 if (mode & 0222)
185 *pace_flags |= FILE_WRITE_RIGHTS;
186 if (mode & 0111)
187 *pace_flags |= SET_FILE_EXEC_RIGHTS;
188
189 ksmbd_debug(SMB, "mode: %o, access flags now 0x%x\n",
190 mode, *pace_flags);
191 }
192
fill_ace_for_sid(struct smb_ace * pntace,const struct smb_sid * psid,int type,int flags,umode_t mode,umode_t bits)193 static __u16 fill_ace_for_sid(struct smb_ace *pntace,
194 const struct smb_sid *psid, int type, int flags,
195 umode_t mode, umode_t bits)
196 {
197 int i;
198 __u16 size = 0;
199 __u32 access_req = 0;
200
201 pntace->type = type;
202 pntace->flags = flags;
203 mode_to_access_flags(mode, bits, &access_req);
204 if (!access_req)
205 access_req = SET_MINIMUM_RIGHTS;
206 pntace->access_req = cpu_to_le32(access_req);
207
208 pntace->sid.revision = psid->revision;
209 pntace->sid.num_subauth = psid->num_subauth;
210 for (i = 0; i < NUM_AUTHS; i++)
211 pntace->sid.authority[i] = psid->authority[i];
212 for (i = 0; i < psid->num_subauth; i++)
213 pntace->sid.sub_auth[i] = psid->sub_auth[i];
214
215 size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4);
216 pntace->size = cpu_to_le16(size);
217
218 return size;
219 }
220
id_to_sid(unsigned int cid,uint sidtype,struct smb_sid * ssid)221 void id_to_sid(unsigned int cid, uint sidtype, struct smb_sid *ssid)
222 {
223 switch (sidtype) {
224 case SIDOWNER:
225 smb_copy_sid(ssid, &server_conf.domain_sid);
226 break;
227 case SIDUNIX_USER:
228 smb_copy_sid(ssid, &sid_unix_users);
229 break;
230 case SIDUNIX_GROUP:
231 smb_copy_sid(ssid, &sid_unix_groups);
232 break;
233 case SIDCREATOR_OWNER:
234 smb_copy_sid(ssid, &creator_owner);
235 return;
236 case SIDCREATOR_GROUP:
237 smb_copy_sid(ssid, &creator_group);
238 return;
239 case SIDNFS_USER:
240 smb_copy_sid(ssid, &sid_unix_NFS_users);
241 break;
242 case SIDNFS_GROUP:
243 smb_copy_sid(ssid, &sid_unix_NFS_groups);
244 break;
245 case SIDNFS_MODE:
246 smb_copy_sid(ssid, &sid_unix_NFS_mode);
247 break;
248 default:
249 return;
250 }
251
252 /* RID */
253 ssid->sub_auth[ssid->num_subauth] = cpu_to_le32(cid);
254 ssid->num_subauth++;
255 }
256
sid_to_id(struct user_namespace * user_ns,struct smb_sid * psid,uint sidtype,struct smb_fattr * fattr)257 static int sid_to_id(struct user_namespace *user_ns,
258 struct smb_sid *psid, uint sidtype,
259 struct smb_fattr *fattr)
260 {
261 int rc = -EINVAL;
262
263 /*
264 * If we have too many subauthorities, then something is really wrong.
265 * Just return an error.
266 */
267 if (unlikely(psid->num_subauth > SID_MAX_SUB_AUTHORITIES)) {
268 pr_err("%s: %u subauthorities is too many!\n",
269 __func__, psid->num_subauth);
270 return -EIO;
271 }
272
273 if (sidtype == SIDOWNER) {
274 kuid_t uid;
275 uid_t id;
276
277 id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]);
278 uid = mapped_kuid_user(user_ns, &init_user_ns, KUIDT_INIT(id));
279 if (uid_valid(uid)) {
280 fattr->cf_uid = uid;
281 rc = 0;
282 }
283 } else {
284 kgid_t gid;
285 gid_t id;
286
287 id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]);
288 gid = mapped_kgid_user(user_ns, &init_user_ns, KGIDT_INIT(id));
289 if (gid_valid(gid)) {
290 fattr->cf_gid = gid;
291 rc = 0;
292 }
293 }
294
295 return rc;
296 }
297
posix_state_to_acl(struct posix_acl_state * state,struct posix_acl_entry * pace)298 void posix_state_to_acl(struct posix_acl_state *state,
299 struct posix_acl_entry *pace)
300 {
301 int i;
302
303 pace->e_tag = ACL_USER_OBJ;
304 pace->e_perm = state->owner.allow;
305 for (i = 0; i < state->users->n; i++) {
306 pace++;
307 pace->e_tag = ACL_USER;
308 pace->e_uid = state->users->aces[i].uid;
309 pace->e_perm = state->users->aces[i].perms.allow;
310 }
311
312 pace++;
313 pace->e_tag = ACL_GROUP_OBJ;
314 pace->e_perm = state->group.allow;
315
316 for (i = 0; i < state->groups->n; i++) {
317 pace++;
318 pace->e_tag = ACL_GROUP;
319 pace->e_gid = state->groups->aces[i].gid;
320 pace->e_perm = state->groups->aces[i].perms.allow;
321 }
322
323 if (state->users->n || state->groups->n) {
324 pace++;
325 pace->e_tag = ACL_MASK;
326 pace->e_perm = state->mask.allow;
327 }
328
329 pace++;
330 pace->e_tag = ACL_OTHER;
331 pace->e_perm = state->other.allow;
332 }
333
init_acl_state(struct posix_acl_state * state,int cnt)334 int init_acl_state(struct posix_acl_state *state, int cnt)
335 {
336 int alloc;
337
338 memset(state, 0, sizeof(struct posix_acl_state));
339 /*
340 * In the worst case, each individual acl could be for a distinct
341 * named user or group, but we don't know which, so we allocate
342 * enough space for either:
343 */
344 alloc = sizeof(struct posix_ace_state_array)
345 + cnt * sizeof(struct posix_user_ace_state);
346 state->users = kzalloc(alloc, GFP_KERNEL);
347 if (!state->users)
348 return -ENOMEM;
349 state->groups = kzalloc(alloc, GFP_KERNEL);
350 if (!state->groups) {
351 kfree(state->users);
352 return -ENOMEM;
353 }
354 return 0;
355 }
356
free_acl_state(struct posix_acl_state * state)357 void free_acl_state(struct posix_acl_state *state)
358 {
359 kfree(state->users);
360 kfree(state->groups);
361 }
362
parse_dacl(struct user_namespace * user_ns,struct smb_acl * pdacl,char * end_of_acl,struct smb_sid * pownersid,struct smb_sid * pgrpsid,struct smb_fattr * fattr)363 static void parse_dacl(struct user_namespace *user_ns,
364 struct smb_acl *pdacl, char *end_of_acl,
365 struct smb_sid *pownersid, struct smb_sid *pgrpsid,
366 struct smb_fattr *fattr)
367 {
368 int i, ret;
369 int num_aces = 0;
370 unsigned int acl_size;
371 char *acl_base;
372 struct smb_ace **ppace;
373 struct posix_acl_entry *cf_pace, *cf_pdace;
374 struct posix_acl_state acl_state, default_acl_state;
375 umode_t mode = 0, acl_mode;
376 bool owner_found = false, group_found = false, others_found = false;
377
378 if (!pdacl)
379 return;
380
381 /* validate that we do not go past end of acl */
382 if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) ||
383 end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
384 pr_err("ACL too small to parse DACL\n");
385 return;
386 }
387
388 ksmbd_debug(SMB, "DACL revision %d size %d num aces %d\n",
389 le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
390 le32_to_cpu(pdacl->num_aces));
391
392 acl_base = (char *)pdacl;
393 acl_size = sizeof(struct smb_acl);
394
395 num_aces = le32_to_cpu(pdacl->num_aces);
396 if (num_aces <= 0)
397 return;
398
399 if (num_aces > ULONG_MAX / sizeof(struct smb_ace *))
400 return;
401
402 ret = init_acl_state(&acl_state, num_aces);
403 if (ret)
404 return;
405 ret = init_acl_state(&default_acl_state, num_aces);
406 if (ret) {
407 free_acl_state(&acl_state);
408 return;
409 }
410
411 ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *), GFP_KERNEL);
412 if (!ppace) {
413 free_acl_state(&default_acl_state);
414 free_acl_state(&acl_state);
415 return;
416 }
417
418 /*
419 * reset rwx permissions for user/group/other.
420 * Also, if num_aces is 0 i.e. DACL has no ACEs,
421 * user/group/other have no permissions
422 */
423 for (i = 0; i < num_aces; ++i) {
424 if (end_of_acl - acl_base < acl_size)
425 break;
426
427 ppace[i] = (struct smb_ace *)(acl_base + acl_size);
428 acl_base = (char *)ppace[i];
429 acl_size = offsetof(struct smb_ace, sid) +
430 offsetof(struct smb_sid, sub_auth);
431
432 if (end_of_acl - acl_base < acl_size ||
433 ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES ||
434 (end_of_acl - acl_base <
435 acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) ||
436 (le16_to_cpu(ppace[i]->size) <
437 acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth))
438 break;
439
440 acl_size = le16_to_cpu(ppace[i]->size);
441 ppace[i]->access_req =
442 smb_map_generic_desired_access(ppace[i]->access_req);
443
444 if (!(compare_sids(&ppace[i]->sid, &sid_unix_NFS_mode))) {
445 fattr->cf_mode =
446 le32_to_cpu(ppace[i]->sid.sub_auth[2]);
447 break;
448 } else if (!compare_sids(&ppace[i]->sid, pownersid)) {
449 acl_mode = access_flags_to_mode(fattr,
450 ppace[i]->access_req,
451 ppace[i]->type);
452 acl_mode &= 0700;
453
454 if (!owner_found) {
455 mode &= ~(0700);
456 mode |= acl_mode;
457 }
458 owner_found = true;
459 } else if (!compare_sids(&ppace[i]->sid, pgrpsid) ||
460 ppace[i]->sid.sub_auth[ppace[i]->sid.num_subauth - 1] ==
461 DOMAIN_USER_RID_LE) {
462 acl_mode = access_flags_to_mode(fattr,
463 ppace[i]->access_req,
464 ppace[i]->type);
465 acl_mode &= 0070;
466 if (!group_found) {
467 mode &= ~(0070);
468 mode |= acl_mode;
469 }
470 group_found = true;
471 } else if (!compare_sids(&ppace[i]->sid, &sid_everyone)) {
472 acl_mode = access_flags_to_mode(fattr,
473 ppace[i]->access_req,
474 ppace[i]->type);
475 acl_mode &= 0007;
476 if (!others_found) {
477 mode &= ~(0007);
478 mode |= acl_mode;
479 }
480 others_found = true;
481 } else if (!compare_sids(&ppace[i]->sid, &creator_owner)) {
482 continue;
483 } else if (!compare_sids(&ppace[i]->sid, &creator_group)) {
484 continue;
485 } else if (!compare_sids(&ppace[i]->sid, &sid_authusers)) {
486 continue;
487 } else {
488 struct smb_fattr temp_fattr;
489
490 acl_mode = access_flags_to_mode(fattr, ppace[i]->access_req,
491 ppace[i]->type);
492 temp_fattr.cf_uid = INVALID_UID;
493 ret = sid_to_id(user_ns, &ppace[i]->sid, SIDOWNER, &temp_fattr);
494 if (ret || uid_eq(temp_fattr.cf_uid, INVALID_UID)) {
495 pr_err("%s: Error %d mapping Owner SID to uid\n",
496 __func__, ret);
497 continue;
498 }
499
500 acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
501 acl_state.users->aces[acl_state.users->n].uid =
502 temp_fattr.cf_uid;
503 acl_state.users->aces[acl_state.users->n++].perms.allow =
504 ((acl_mode & 0700) >> 6) | 0004;
505 default_acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
506 default_acl_state.users->aces[default_acl_state.users->n].uid =
507 temp_fattr.cf_uid;
508 default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
509 ((acl_mode & 0700) >> 6) | 0004;
510 }
511 }
512 kfree(ppace);
513
514 if (owner_found) {
515 /* The owner must be set to at least read-only. */
516 acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
517 acl_state.users->aces[acl_state.users->n].uid = fattr->cf_uid;
518 acl_state.users->aces[acl_state.users->n++].perms.allow =
519 ((mode & 0700) >> 6) | 0004;
520 default_acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
521 default_acl_state.users->aces[default_acl_state.users->n].uid =
522 fattr->cf_uid;
523 default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
524 ((mode & 0700) >> 6) | 0004;
525 }
526
527 if (group_found) {
528 acl_state.group.allow = (mode & 0070) >> 3;
529 acl_state.groups->aces[acl_state.groups->n].gid =
530 fattr->cf_gid;
531 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
532 (mode & 0070) >> 3;
533 default_acl_state.group.allow = (mode & 0070) >> 3;
534 default_acl_state.groups->aces[default_acl_state.groups->n].gid =
535 fattr->cf_gid;
536 default_acl_state.groups->aces[default_acl_state.groups->n++].perms.allow =
537 (mode & 0070) >> 3;
538 }
539
540 if (others_found) {
541 fattr->cf_mode &= ~(0007);
542 fattr->cf_mode |= mode & 0007;
543
544 acl_state.other.allow = mode & 0007;
545 default_acl_state.other.allow = mode & 0007;
546 }
547
548 if (acl_state.users->n || acl_state.groups->n) {
549 acl_state.mask.allow = 0x07;
550
551 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
552 fattr->cf_acls =
553 posix_acl_alloc(acl_state.users->n +
554 acl_state.groups->n + 4, GFP_KERNEL);
555 if (fattr->cf_acls) {
556 cf_pace = fattr->cf_acls->a_entries;
557 posix_state_to_acl(&acl_state, cf_pace);
558 }
559 }
560 }
561
562 if (default_acl_state.users->n || default_acl_state.groups->n) {
563 default_acl_state.mask.allow = 0x07;
564
565 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
566 fattr->cf_dacls =
567 posix_acl_alloc(default_acl_state.users->n +
568 default_acl_state.groups->n + 4, GFP_KERNEL);
569 if (fattr->cf_dacls) {
570 cf_pdace = fattr->cf_dacls->a_entries;
571 posix_state_to_acl(&default_acl_state, cf_pdace);
572 }
573 }
574 }
575 free_acl_state(&acl_state);
576 free_acl_state(&default_acl_state);
577 }
578
set_posix_acl_entries_dacl(struct user_namespace * user_ns,struct smb_ace * pndace,struct smb_fattr * fattr,u32 * num_aces,u16 * size,u32 nt_aces_num)579 static void set_posix_acl_entries_dacl(struct user_namespace *user_ns,
580 struct smb_ace *pndace,
581 struct smb_fattr *fattr, u32 *num_aces,
582 u16 *size, u32 nt_aces_num)
583 {
584 struct posix_acl_entry *pace;
585 struct smb_sid *sid;
586 struct smb_ace *ntace;
587 int i, j;
588
589 if (!fattr->cf_acls)
590 goto posix_default_acl;
591
592 pace = fattr->cf_acls->a_entries;
593 for (i = 0; i < fattr->cf_acls->a_count; i++, pace++) {
594 int flags = 0;
595
596 sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
597 if (!sid)
598 break;
599
600 if (pace->e_tag == ACL_USER) {
601 uid_t uid;
602 unsigned int sid_type = SIDOWNER;
603
604 uid = posix_acl_uid_translate(user_ns, pace);
605 if (!uid)
606 sid_type = SIDUNIX_USER;
607 id_to_sid(uid, sid_type, sid);
608 } else if (pace->e_tag == ACL_GROUP) {
609 gid_t gid;
610
611 gid = posix_acl_gid_translate(user_ns, pace);
612 id_to_sid(gid, SIDUNIX_GROUP, sid);
613 } else if (pace->e_tag == ACL_OTHER && !nt_aces_num) {
614 smb_copy_sid(sid, &sid_everyone);
615 } else {
616 kfree(sid);
617 continue;
618 }
619 ntace = pndace;
620 for (j = 0; j < nt_aces_num; j++) {
621 if (ntace->sid.sub_auth[ntace->sid.num_subauth - 1] ==
622 sid->sub_auth[sid->num_subauth - 1])
623 goto pass_same_sid;
624 ntace = (struct smb_ace *)((char *)ntace +
625 le16_to_cpu(ntace->size));
626 }
627
628 if (S_ISDIR(fattr->cf_mode) && pace->e_tag == ACL_OTHER)
629 flags = 0x03;
630
631 ntace = (struct smb_ace *)((char *)pndace + *size);
632 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, flags,
633 pace->e_perm, 0777);
634 (*num_aces)++;
635 if (pace->e_tag == ACL_USER)
636 ntace->access_req |=
637 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
638
639 if (S_ISDIR(fattr->cf_mode) &&
640 (pace->e_tag == ACL_USER || pace->e_tag == ACL_GROUP)) {
641 ntace = (struct smb_ace *)((char *)pndace + *size);
642 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED,
643 0x03, pace->e_perm, 0777);
644 (*num_aces)++;
645 if (pace->e_tag == ACL_USER)
646 ntace->access_req |=
647 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
648 }
649
650 pass_same_sid:
651 kfree(sid);
652 }
653
654 if (nt_aces_num)
655 return;
656
657 posix_default_acl:
658 if (!fattr->cf_dacls)
659 return;
660
661 pace = fattr->cf_dacls->a_entries;
662 for (i = 0; i < fattr->cf_dacls->a_count; i++, pace++) {
663 sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
664 if (!sid)
665 break;
666
667 if (pace->e_tag == ACL_USER) {
668 uid_t uid;
669
670 uid = posix_acl_uid_translate(user_ns, pace);
671 id_to_sid(uid, SIDCREATOR_OWNER, sid);
672 } else if (pace->e_tag == ACL_GROUP) {
673 gid_t gid;
674
675 gid = posix_acl_gid_translate(user_ns, pace);
676 id_to_sid(gid, SIDCREATOR_GROUP, sid);
677 } else {
678 kfree(sid);
679 continue;
680 }
681
682 ntace = (struct smb_ace *)((char *)pndace + *size);
683 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, 0x0b,
684 pace->e_perm, 0777);
685 (*num_aces)++;
686 if (pace->e_tag == ACL_USER)
687 ntace->access_req |=
688 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
689 kfree(sid);
690 }
691 }
692
set_ntacl_dacl(struct user_namespace * user_ns,struct smb_acl * pndacl,struct smb_acl * nt_dacl,unsigned int aces_size,const struct smb_sid * pownersid,const struct smb_sid * pgrpsid,struct smb_fattr * fattr)693 static void set_ntacl_dacl(struct user_namespace *user_ns,
694 struct smb_acl *pndacl,
695 struct smb_acl *nt_dacl,
696 unsigned int aces_size,
697 const struct smb_sid *pownersid,
698 const struct smb_sid *pgrpsid,
699 struct smb_fattr *fattr)
700 {
701 struct smb_ace *ntace, *pndace;
702 int nt_num_aces = le32_to_cpu(nt_dacl->num_aces), num_aces = 0;
703 unsigned short size = 0;
704 int i;
705
706 pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
707 if (nt_num_aces) {
708 ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl));
709 for (i = 0; i < nt_num_aces; i++) {
710 unsigned short nt_ace_size;
711
712 if (offsetof(struct smb_ace, access_req) > aces_size)
713 break;
714
715 nt_ace_size = le16_to_cpu(ntace->size);
716 if (nt_ace_size > aces_size)
717 break;
718
719 memcpy((char *)pndace + size, ntace, nt_ace_size);
720 size += nt_ace_size;
721 aces_size -= nt_ace_size;
722 ntace = (struct smb_ace *)((char *)ntace + nt_ace_size);
723 num_aces++;
724 }
725 }
726
727 set_posix_acl_entries_dacl(user_ns, pndace, fattr,
728 &num_aces, &size, nt_num_aces);
729 pndacl->num_aces = cpu_to_le32(num_aces);
730 pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
731 }
732
set_mode_dacl(struct user_namespace * user_ns,struct smb_acl * pndacl,struct smb_fattr * fattr)733 static void set_mode_dacl(struct user_namespace *user_ns,
734 struct smb_acl *pndacl, struct smb_fattr *fattr)
735 {
736 struct smb_ace *pace, *pndace;
737 u32 num_aces = 0;
738 u16 size = 0, ace_size = 0;
739 uid_t uid;
740 const struct smb_sid *sid;
741
742 pace = pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
743
744 if (fattr->cf_acls) {
745 set_posix_acl_entries_dacl(user_ns, pndace, fattr,
746 &num_aces, &size, num_aces);
747 goto out;
748 }
749
750 /* owner RID */
751 uid = from_kuid(&init_user_ns, fattr->cf_uid);
752 if (uid)
753 sid = &server_conf.domain_sid;
754 else
755 sid = &sid_unix_users;
756 ace_size = fill_ace_for_sid(pace, sid, ACCESS_ALLOWED, 0,
757 fattr->cf_mode, 0700);
758 pace->sid.sub_auth[pace->sid.num_subauth++] = cpu_to_le32(uid);
759 pace->size = cpu_to_le16(ace_size + 4);
760 size += le16_to_cpu(pace->size);
761 pace = (struct smb_ace *)((char *)pndace + size);
762
763 /* Group RID */
764 ace_size = fill_ace_for_sid(pace, &sid_unix_groups,
765 ACCESS_ALLOWED, 0, fattr->cf_mode, 0070);
766 pace->sid.sub_auth[pace->sid.num_subauth++] =
767 cpu_to_le32(from_kgid(&init_user_ns, fattr->cf_gid));
768 pace->size = cpu_to_le16(ace_size + 4);
769 size += le16_to_cpu(pace->size);
770 pace = (struct smb_ace *)((char *)pndace + size);
771 num_aces = 3;
772
773 if (S_ISDIR(fattr->cf_mode)) {
774 pace = (struct smb_ace *)((char *)pndace + size);
775
776 /* creator owner */
777 size += fill_ace_for_sid(pace, &creator_owner, ACCESS_ALLOWED,
778 0x0b, fattr->cf_mode, 0700);
779 pace = (struct smb_ace *)((char *)pndace + size);
780
781 /* creator group */
782 size += fill_ace_for_sid(pace, &creator_group, ACCESS_ALLOWED,
783 0x0b, fattr->cf_mode, 0070);
784 pace = (struct smb_ace *)((char *)pndace + size);
785 num_aces = 5;
786 }
787
788 /* other */
789 size += fill_ace_for_sid(pace, &sid_everyone, ACCESS_ALLOWED, 0,
790 fattr->cf_mode, 0007);
791
792 out:
793 pndacl->num_aces = cpu_to_le32(num_aces);
794 pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
795 }
796
parse_sid(struct smb_sid * psid,char * end_of_acl)797 static int parse_sid(struct smb_sid *psid, char *end_of_acl)
798 {
799 /*
800 * validate that we do not go past end of ACL - sid must be at least 8
801 * bytes long (assuming no sub-auths - e.g. the null SID
802 */
803 if (end_of_acl < (char *)psid + 8) {
804 pr_err("ACL too small to parse SID %p\n", psid);
805 return -EINVAL;
806 }
807
808 return 0;
809 }
810
811 /* Convert CIFS ACL to POSIX form */
parse_sec_desc(struct user_namespace * user_ns,struct smb_ntsd * pntsd,int acl_len,struct smb_fattr * fattr)812 int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
813 int acl_len, struct smb_fattr *fattr)
814 {
815 int rc = 0;
816 struct smb_sid *owner_sid_ptr, *group_sid_ptr;
817 struct smb_acl *dacl_ptr; /* no need for SACL ptr */
818 char *end_of_acl = ((char *)pntsd) + acl_len;
819 __u32 dacloffset;
820 int pntsd_type;
821
822 if (!pntsd)
823 return -EIO;
824
825 if (acl_len < sizeof(struct smb_ntsd))
826 return -EINVAL;
827
828 owner_sid_ptr = (struct smb_sid *)((char *)pntsd +
829 le32_to_cpu(pntsd->osidoffset));
830 group_sid_ptr = (struct smb_sid *)((char *)pntsd +
831 le32_to_cpu(pntsd->gsidoffset));
832 dacloffset = le32_to_cpu(pntsd->dacloffset);
833 dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset);
834 ksmbd_debug(SMB,
835 "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
836 pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
837 le32_to_cpu(pntsd->gsidoffset),
838 le32_to_cpu(pntsd->sacloffset), dacloffset);
839
840 pntsd_type = le16_to_cpu(pntsd->type);
841 if (!(pntsd_type & DACL_PRESENT)) {
842 ksmbd_debug(SMB, "DACL_PRESENT in DACL type is not set\n");
843 return rc;
844 }
845
846 pntsd->type = cpu_to_le16(DACL_PRESENT);
847
848 if (pntsd->osidoffset) {
849 rc = parse_sid(owner_sid_ptr, end_of_acl);
850 if (rc) {
851 pr_err("%s: Error %d parsing Owner SID\n", __func__, rc);
852 return rc;
853 }
854
855 rc = sid_to_id(user_ns, owner_sid_ptr, SIDOWNER, fattr);
856 if (rc) {
857 pr_err("%s: Error %d mapping Owner SID to uid\n",
858 __func__, rc);
859 owner_sid_ptr = NULL;
860 }
861 }
862
863 if (pntsd->gsidoffset) {
864 rc = parse_sid(group_sid_ptr, end_of_acl);
865 if (rc) {
866 pr_err("%s: Error %d mapping Owner SID to gid\n",
867 __func__, rc);
868 return rc;
869 }
870 rc = sid_to_id(user_ns, group_sid_ptr, SIDUNIX_GROUP, fattr);
871 if (rc) {
872 pr_err("%s: Error %d mapping Group SID to gid\n",
873 __func__, rc);
874 group_sid_ptr = NULL;
875 }
876 }
877
878 if ((pntsd_type & (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) ==
879 (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ))
880 pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
881 if (pntsd_type & DACL_PROTECTED)
882 pntsd->type |= cpu_to_le16(DACL_PROTECTED);
883
884 if (dacloffset) {
885 parse_dacl(user_ns, dacl_ptr, end_of_acl,
886 owner_sid_ptr, group_sid_ptr, fattr);
887 }
888
889 return 0;
890 }
891
892 /* Convert permission bits from mode to equivalent CIFS ACL */
build_sec_desc(struct user_namespace * user_ns,struct smb_ntsd * pntsd,struct smb_ntsd * ppntsd,int ppntsd_size,int addition_info,__u32 * secdesclen,struct smb_fattr * fattr)893 int build_sec_desc(struct user_namespace *user_ns,
894 struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd,
895 int ppntsd_size, int addition_info, __u32 *secdesclen,
896 struct smb_fattr *fattr)
897 {
898 int rc = 0;
899 __u32 offset;
900 struct smb_sid *owner_sid_ptr, *group_sid_ptr;
901 struct smb_sid *nowner_sid_ptr, *ngroup_sid_ptr;
902 struct smb_acl *dacl_ptr = NULL; /* no need for SACL ptr */
903 uid_t uid;
904 gid_t gid;
905 unsigned int sid_type = SIDOWNER;
906
907 nowner_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
908 if (!nowner_sid_ptr)
909 return -ENOMEM;
910
911 uid = from_kuid(&init_user_ns, fattr->cf_uid);
912 if (!uid)
913 sid_type = SIDUNIX_USER;
914 id_to_sid(uid, sid_type, nowner_sid_ptr);
915
916 ngroup_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
917 if (!ngroup_sid_ptr) {
918 kfree(nowner_sid_ptr);
919 return -ENOMEM;
920 }
921
922 gid = from_kgid(&init_user_ns, fattr->cf_gid);
923 id_to_sid(gid, SIDUNIX_GROUP, ngroup_sid_ptr);
924
925 offset = sizeof(struct smb_ntsd);
926 pntsd->sacloffset = 0;
927 pntsd->revision = cpu_to_le16(1);
928 pntsd->type = cpu_to_le16(SELF_RELATIVE);
929 if (ppntsd)
930 pntsd->type |= ppntsd->type;
931
932 if (addition_info & OWNER_SECINFO) {
933 pntsd->osidoffset = cpu_to_le32(offset);
934 owner_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
935 smb_copy_sid(owner_sid_ptr, nowner_sid_ptr);
936 offset += 1 + 1 + 6 + (nowner_sid_ptr->num_subauth * 4);
937 }
938
939 if (addition_info & GROUP_SECINFO) {
940 pntsd->gsidoffset = cpu_to_le32(offset);
941 group_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
942 smb_copy_sid(group_sid_ptr, ngroup_sid_ptr);
943 offset += 1 + 1 + 6 + (ngroup_sid_ptr->num_subauth * 4);
944 }
945
946 if (addition_info & DACL_SECINFO) {
947 pntsd->type |= cpu_to_le16(DACL_PRESENT);
948 dacl_ptr = (struct smb_acl *)((char *)pntsd + offset);
949 dacl_ptr->revision = cpu_to_le16(2);
950 dacl_ptr->size = cpu_to_le16(sizeof(struct smb_acl));
951 dacl_ptr->num_aces = 0;
952
953 if (!ppntsd) {
954 set_mode_dacl(user_ns, dacl_ptr, fattr);
955 } else {
956 struct smb_acl *ppdacl_ptr;
957 unsigned int dacl_offset = le32_to_cpu(ppntsd->dacloffset);
958 int ppdacl_size, ntacl_size = ppntsd_size - dacl_offset;
959
960 if (!dacl_offset ||
961 (dacl_offset + sizeof(struct smb_acl) > ppntsd_size))
962 goto out;
963
964 ppdacl_ptr = (struct smb_acl *)((char *)ppntsd + dacl_offset);
965 ppdacl_size = le16_to_cpu(ppdacl_ptr->size);
966 if (ppdacl_size > ntacl_size ||
967 ppdacl_size < sizeof(struct smb_acl))
968 goto out;
969
970 set_ntacl_dacl(user_ns, dacl_ptr, ppdacl_ptr,
971 ntacl_size - sizeof(struct smb_acl),
972 nowner_sid_ptr, ngroup_sid_ptr,
973 fattr);
974 }
975 pntsd->dacloffset = cpu_to_le32(offset);
976 offset += le16_to_cpu(dacl_ptr->size);
977 }
978
979 out:
980 kfree(nowner_sid_ptr);
981 kfree(ngroup_sid_ptr);
982 *secdesclen = offset;
983 return rc;
984 }
985
smb_set_ace(struct smb_ace * ace,const struct smb_sid * sid,u8 type,u8 flags,__le32 access_req)986 static void smb_set_ace(struct smb_ace *ace, const struct smb_sid *sid, u8 type,
987 u8 flags, __le32 access_req)
988 {
989 ace->type = type;
990 ace->flags = flags;
991 ace->access_req = access_req;
992 smb_copy_sid(&ace->sid, sid);
993 ace->size = cpu_to_le16(1 + 1 + 2 + 4 + 1 + 1 + 6 + (sid->num_subauth * 4));
994 }
995
smb_inherit_dacl(struct ksmbd_conn * conn,const struct path * path,unsigned int uid,unsigned int gid)996 int smb_inherit_dacl(struct ksmbd_conn *conn,
997 const struct path *path,
998 unsigned int uid, unsigned int gid)
999 {
1000 const struct smb_sid *psid, *creator = NULL;
1001 struct smb_ace *parent_aces, *aces;
1002 struct smb_acl *parent_pdacl;
1003 struct smb_ntsd *parent_pntsd = NULL;
1004 struct smb_sid owner_sid, group_sid;
1005 struct dentry *parent = path->dentry->d_parent;
1006 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
1007 int inherited_flags = 0, flags = 0, i, ace_cnt = 0, nt_size = 0, pdacl_size;
1008 int rc = 0, num_aces, dacloffset, pntsd_type, pntsd_size, acl_len, aces_size;
1009 char *aces_base;
1010 bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode);
1011
1012 pntsd_size = ksmbd_vfs_get_sd_xattr(conn, user_ns,
1013 parent, &parent_pntsd);
1014 if (pntsd_size <= 0)
1015 return -ENOENT;
1016 dacloffset = le32_to_cpu(parent_pntsd->dacloffset);
1017 if (!dacloffset || (dacloffset + sizeof(struct smb_acl) > pntsd_size)) {
1018 rc = -EINVAL;
1019 goto free_parent_pntsd;
1020 }
1021
1022 parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset);
1023 acl_len = pntsd_size - dacloffset;
1024 num_aces = le32_to_cpu(parent_pdacl->num_aces);
1025 pntsd_type = le16_to_cpu(parent_pntsd->type);
1026 pdacl_size = le16_to_cpu(parent_pdacl->size);
1027
1028 if (pdacl_size > acl_len || pdacl_size < sizeof(struct smb_acl)) {
1029 rc = -EINVAL;
1030 goto free_parent_pntsd;
1031 }
1032
1033 aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2, GFP_KERNEL);
1034 if (!aces_base) {
1035 rc = -ENOMEM;
1036 goto free_parent_pntsd;
1037 }
1038
1039 aces = (struct smb_ace *)aces_base;
1040 parent_aces = (struct smb_ace *)((char *)parent_pdacl +
1041 sizeof(struct smb_acl));
1042 aces_size = acl_len - sizeof(struct smb_acl);
1043
1044 if (pntsd_type & DACL_AUTO_INHERITED)
1045 inherited_flags = INHERITED_ACE;
1046
1047 for (i = 0; i < num_aces; i++) {
1048 int pace_size;
1049
1050 if (offsetof(struct smb_ace, access_req) > aces_size)
1051 break;
1052
1053 pace_size = le16_to_cpu(parent_aces->size);
1054 if (pace_size > aces_size)
1055 break;
1056
1057 aces_size -= pace_size;
1058
1059 flags = parent_aces->flags;
1060 if (!smb_inherit_flags(flags, is_dir))
1061 goto pass;
1062 if (is_dir) {
1063 flags &= ~(INHERIT_ONLY_ACE | INHERITED_ACE);
1064 if (!(flags & CONTAINER_INHERIT_ACE))
1065 flags |= INHERIT_ONLY_ACE;
1066 if (flags & NO_PROPAGATE_INHERIT_ACE)
1067 flags = 0;
1068 } else {
1069 flags = 0;
1070 }
1071
1072 if (!compare_sids(&creator_owner, &parent_aces->sid)) {
1073 creator = &creator_owner;
1074 id_to_sid(uid, SIDOWNER, &owner_sid);
1075 psid = &owner_sid;
1076 } else if (!compare_sids(&creator_group, &parent_aces->sid)) {
1077 creator = &creator_group;
1078 id_to_sid(gid, SIDUNIX_GROUP, &group_sid);
1079 psid = &group_sid;
1080 } else {
1081 creator = NULL;
1082 psid = &parent_aces->sid;
1083 }
1084
1085 if (is_dir && creator && flags & CONTAINER_INHERIT_ACE) {
1086 smb_set_ace(aces, psid, parent_aces->type, inherited_flags,
1087 parent_aces->access_req);
1088 nt_size += le16_to_cpu(aces->size);
1089 ace_cnt++;
1090 aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
1091 flags |= INHERIT_ONLY_ACE;
1092 psid = creator;
1093 } else if (is_dir && !(parent_aces->flags & NO_PROPAGATE_INHERIT_ACE)) {
1094 psid = &parent_aces->sid;
1095 }
1096
1097 smb_set_ace(aces, psid, parent_aces->type, flags | inherited_flags,
1098 parent_aces->access_req);
1099 nt_size += le16_to_cpu(aces->size);
1100 aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
1101 ace_cnt++;
1102 pass:
1103 parent_aces = (struct smb_ace *)((char *)parent_aces + pace_size);
1104 }
1105
1106 if (nt_size > 0) {
1107 struct smb_ntsd *pntsd;
1108 struct smb_acl *pdacl;
1109 struct smb_sid *powner_sid = NULL, *pgroup_sid = NULL;
1110 int powner_sid_size = 0, pgroup_sid_size = 0, pntsd_size;
1111 int pntsd_alloc_size;
1112
1113 if (parent_pntsd->osidoffset) {
1114 powner_sid = (struct smb_sid *)((char *)parent_pntsd +
1115 le32_to_cpu(parent_pntsd->osidoffset));
1116 powner_sid_size = 1 + 1 + 6 + (powner_sid->num_subauth * 4);
1117 }
1118 if (parent_pntsd->gsidoffset) {
1119 pgroup_sid = (struct smb_sid *)((char *)parent_pntsd +
1120 le32_to_cpu(parent_pntsd->gsidoffset));
1121 pgroup_sid_size = 1 + 1 + 6 + (pgroup_sid->num_subauth * 4);
1122 }
1123
1124 pntsd_alloc_size = sizeof(struct smb_ntsd) + powner_sid_size +
1125 pgroup_sid_size + sizeof(struct smb_acl) + nt_size;
1126
1127 pntsd = kzalloc(pntsd_alloc_size, GFP_KERNEL);
1128 if (!pntsd) {
1129 rc = -ENOMEM;
1130 goto free_aces_base;
1131 }
1132
1133 pntsd->revision = cpu_to_le16(1);
1134 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PRESENT);
1135 if (le16_to_cpu(parent_pntsd->type) & DACL_AUTO_INHERITED)
1136 pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
1137 pntsd_size = sizeof(struct smb_ntsd);
1138 pntsd->osidoffset = parent_pntsd->osidoffset;
1139 pntsd->gsidoffset = parent_pntsd->gsidoffset;
1140 pntsd->dacloffset = parent_pntsd->dacloffset;
1141
1142 if ((u64)le32_to_cpu(pntsd->osidoffset) + powner_sid_size >
1143 pntsd_alloc_size) {
1144 rc = -EINVAL;
1145 kfree(pntsd);
1146 goto free_aces_base;
1147 }
1148
1149 if ((u64)le32_to_cpu(pntsd->gsidoffset) + pgroup_sid_size >
1150 pntsd_alloc_size) {
1151 rc = -EINVAL;
1152 kfree(pntsd);
1153 goto free_aces_base;
1154 }
1155
1156 if ((u64)le32_to_cpu(pntsd->dacloffset) + sizeof(struct smb_acl) + nt_size >
1157 pntsd_alloc_size) {
1158 rc = -EINVAL;
1159 kfree(pntsd);
1160 goto free_aces_base;
1161 }
1162
1163 if (pntsd->osidoffset) {
1164 struct smb_sid *owner_sid = (struct smb_sid *)((char *)pntsd +
1165 le32_to_cpu(pntsd->osidoffset));
1166 memcpy(owner_sid, powner_sid, powner_sid_size);
1167 pntsd_size += powner_sid_size;
1168 }
1169
1170 if (pntsd->gsidoffset) {
1171 struct smb_sid *group_sid = (struct smb_sid *)((char *)pntsd +
1172 le32_to_cpu(pntsd->gsidoffset));
1173 memcpy(group_sid, pgroup_sid, pgroup_sid_size);
1174 pntsd_size += pgroup_sid_size;
1175 }
1176
1177 if (pntsd->dacloffset) {
1178 struct smb_ace *pace;
1179
1180 pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1181 pdacl->revision = cpu_to_le16(2);
1182 pdacl->size = cpu_to_le16(sizeof(struct smb_acl) + nt_size);
1183 pdacl->num_aces = cpu_to_le32(ace_cnt);
1184 pace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1185 memcpy(pace, aces_base, nt_size);
1186 pntsd_size += sizeof(struct smb_acl) + nt_size;
1187 }
1188
1189 ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, pntsd_size, false);
1190 kfree(pntsd);
1191 }
1192
1193 free_aces_base:
1194 kfree(aces_base);
1195 free_parent_pntsd:
1196 kfree(parent_pntsd);
1197 return rc;
1198 }
1199
smb_inherit_flags(int flags,bool is_dir)1200 bool smb_inherit_flags(int flags, bool is_dir)
1201 {
1202 if (!is_dir)
1203 return (flags & OBJECT_INHERIT_ACE) != 0;
1204
1205 if (flags & OBJECT_INHERIT_ACE && !(flags & NO_PROPAGATE_INHERIT_ACE))
1206 return true;
1207
1208 if (flags & CONTAINER_INHERIT_ACE)
1209 return true;
1210 return false;
1211 }
1212
smb_check_perm_dacl(struct ksmbd_conn * conn,const struct path * path,__le32 * pdaccess,int uid)1213 int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
1214 __le32 *pdaccess, int uid)
1215 {
1216 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
1217 struct smb_ntsd *pntsd = NULL;
1218 struct smb_acl *pdacl;
1219 struct posix_acl *posix_acls;
1220 int rc = 0, pntsd_size, acl_size, aces_size, pdacl_size, dacl_offset;
1221 struct smb_sid sid;
1222 int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE);
1223 struct smb_ace *ace;
1224 int i, found = 0;
1225 unsigned int access_bits = 0;
1226 struct smb_ace *others_ace = NULL;
1227 struct posix_acl_entry *pa_entry;
1228 unsigned int sid_type = SIDOWNER;
1229 unsigned short ace_size;
1230
1231 ksmbd_debug(SMB, "check permission using windows acl\n");
1232 pntsd_size = ksmbd_vfs_get_sd_xattr(conn, user_ns,
1233 path->dentry, &pntsd);
1234 if (pntsd_size <= 0 || !pntsd)
1235 goto err_out;
1236
1237 dacl_offset = le32_to_cpu(pntsd->dacloffset);
1238 if (!dacl_offset ||
1239 (dacl_offset + sizeof(struct smb_acl) > pntsd_size))
1240 goto err_out;
1241
1242 pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1243 acl_size = pntsd_size - dacl_offset;
1244 pdacl_size = le16_to_cpu(pdacl->size);
1245
1246 if (pdacl_size > acl_size || pdacl_size < sizeof(struct smb_acl))
1247 goto err_out;
1248
1249 if (!pdacl->num_aces) {
1250 if (!(pdacl_size - sizeof(struct smb_acl)) &&
1251 *pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) {
1252 rc = -EACCES;
1253 goto err_out;
1254 }
1255 goto err_out;
1256 }
1257
1258 if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) {
1259 granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
1260 DELETE;
1261
1262 ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1263 aces_size = acl_size - sizeof(struct smb_acl);
1264 for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1265 if (offsetof(struct smb_ace, access_req) > aces_size)
1266 break;
1267 ace_size = le16_to_cpu(ace->size);
1268 if (ace_size > aces_size)
1269 break;
1270 aces_size -= ace_size;
1271 granted |= le32_to_cpu(ace->access_req);
1272 ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1273 }
1274
1275 if (!pdacl->num_aces)
1276 granted = GENERIC_ALL_FLAGS;
1277 }
1278
1279 if (!uid)
1280 sid_type = SIDUNIX_USER;
1281 id_to_sid(uid, sid_type, &sid);
1282
1283 ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1284 aces_size = acl_size - sizeof(struct smb_acl);
1285 for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1286 if (offsetof(struct smb_ace, access_req) > aces_size)
1287 break;
1288 ace_size = le16_to_cpu(ace->size);
1289 if (ace_size > aces_size)
1290 break;
1291 aces_size -= ace_size;
1292
1293 if (!compare_sids(&sid, &ace->sid) ||
1294 !compare_sids(&sid_unix_NFS_mode, &ace->sid)) {
1295 found = 1;
1296 break;
1297 }
1298 if (!compare_sids(&sid_everyone, &ace->sid))
1299 others_ace = ace;
1300
1301 ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1302 }
1303
1304 if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) {
1305 granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
1306 DELETE;
1307
1308 granted |= le32_to_cpu(ace->access_req);
1309
1310 if (!pdacl->num_aces)
1311 granted = GENERIC_ALL_FLAGS;
1312 }
1313
1314 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
1315 posix_acls = get_acl(d_inode(path->dentry), ACL_TYPE_ACCESS);
1316 if (!IS_ERR_OR_NULL(posix_acls) && !found) {
1317 unsigned int id = -1;
1318
1319 pa_entry = posix_acls->a_entries;
1320 for (i = 0; i < posix_acls->a_count; i++, pa_entry++) {
1321 if (pa_entry->e_tag == ACL_USER)
1322 id = posix_acl_uid_translate(user_ns, pa_entry);
1323 else if (pa_entry->e_tag == ACL_GROUP)
1324 id = posix_acl_gid_translate(user_ns, pa_entry);
1325 else
1326 continue;
1327
1328 if (id == uid) {
1329 mode_to_access_flags(pa_entry->e_perm,
1330 0777,
1331 &access_bits);
1332 if (!access_bits)
1333 access_bits =
1334 SET_MINIMUM_RIGHTS;
1335 posix_acl_release(posix_acls);
1336 goto check_access_bits;
1337 }
1338 }
1339 }
1340 if (!IS_ERR_OR_NULL(posix_acls))
1341 posix_acl_release(posix_acls);
1342 }
1343
1344 if (!found) {
1345 if (others_ace) {
1346 ace = others_ace;
1347 } else {
1348 ksmbd_debug(SMB, "Can't find corresponding sid\n");
1349 rc = -EACCES;
1350 goto err_out;
1351 }
1352 }
1353
1354 switch (ace->type) {
1355 case ACCESS_ALLOWED_ACE_TYPE:
1356 access_bits = le32_to_cpu(ace->access_req);
1357 break;
1358 case ACCESS_DENIED_ACE_TYPE:
1359 case ACCESS_DENIED_CALLBACK_ACE_TYPE:
1360 access_bits = le32_to_cpu(~ace->access_req);
1361 break;
1362 }
1363
1364 check_access_bits:
1365 if (granted &
1366 ~(access_bits | FILE_READ_ATTRIBUTES | READ_CONTROL | WRITE_DAC | DELETE)) {
1367 ksmbd_debug(SMB, "Access denied with winACL, granted : %x, access_req : %x\n",
1368 granted, le32_to_cpu(ace->access_req));
1369 rc = -EACCES;
1370 goto err_out;
1371 }
1372
1373 *pdaccess = cpu_to_le32(granted);
1374 err_out:
1375 kfree(pntsd);
1376 return rc;
1377 }
1378
set_info_sec(struct ksmbd_conn * conn,struct ksmbd_tree_connect * tcon,const struct path * path,struct smb_ntsd * pntsd,int ntsd_len,bool type_check,bool get_write)1379 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
1380 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
1381 bool type_check, bool get_write)
1382 {
1383 int rc;
1384 struct smb_fattr fattr = {{0}};
1385 struct inode *inode = d_inode(path->dentry);
1386 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
1387 struct iattr newattrs;
1388
1389 fattr.cf_uid = INVALID_UID;
1390 fattr.cf_gid = INVALID_GID;
1391 fattr.cf_mode = inode->i_mode;
1392
1393 rc = parse_sec_desc(user_ns, pntsd, ntsd_len, &fattr);
1394 if (rc)
1395 goto out;
1396
1397 newattrs.ia_valid = ATTR_CTIME;
1398 if (!uid_eq(fattr.cf_uid, INVALID_UID)) {
1399 newattrs.ia_valid |= ATTR_UID;
1400 newattrs.ia_uid = fattr.cf_uid;
1401 }
1402 if (!gid_eq(fattr.cf_gid, INVALID_GID)) {
1403 newattrs.ia_valid |= ATTR_GID;
1404 newattrs.ia_gid = fattr.cf_gid;
1405 }
1406 newattrs.ia_valid |= ATTR_MODE;
1407 newattrs.ia_mode = (inode->i_mode & ~0777) | (fattr.cf_mode & 0777);
1408
1409 ksmbd_vfs_remove_acl_xattrs(user_ns, path);
1410 /* Update posix acls */
1411 if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && fattr.cf_dacls) {
1412 rc = set_posix_acl(user_ns, inode,
1413 ACL_TYPE_ACCESS, fattr.cf_acls);
1414 if (rc < 0)
1415 ksmbd_debug(SMB,
1416 "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1417 rc);
1418 if (S_ISDIR(inode->i_mode) && fattr.cf_dacls) {
1419 rc = set_posix_acl(user_ns, inode,
1420 ACL_TYPE_DEFAULT, fattr.cf_dacls);
1421 if (rc)
1422 ksmbd_debug(SMB,
1423 "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1424 rc);
1425 }
1426 }
1427
1428 inode_lock(inode);
1429 rc = notify_change(user_ns, path->dentry, &newattrs, NULL);
1430 inode_unlock(inode);
1431 if (rc)
1432 goto out;
1433
1434 /* Check it only calling from SD BUFFER context */
1435 if (type_check && !(le16_to_cpu(pntsd->type) & DACL_PRESENT))
1436 goto out;
1437
1438 if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
1439 /* Update WinACL in xattr */
1440 ksmbd_vfs_remove_sd_xattrs(user_ns, path);
1441 ksmbd_vfs_set_sd_xattr(conn, user_ns, path, pntsd, ntsd_len,
1442 get_write);
1443 }
1444
1445 out:
1446 posix_acl_release(fattr.cf_acls);
1447 posix_acl_release(fattr.cf_dacls);
1448 return rc;
1449 }
1450
ksmbd_init_domain(u32 * sub_auth)1451 void ksmbd_init_domain(u32 *sub_auth)
1452 {
1453 int i;
1454
1455 memcpy(&server_conf.domain_sid, &domain, sizeof(struct smb_sid));
1456 for (i = 0; i < 3; ++i)
1457 server_conf.domain_sid.sub_auth[i + 1] = cpu_to_le32(sub_auth[i]);
1458 }
1459