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 mnt_idmap * idmap,struct smb_sid * psid,uint sidtype,struct smb_fattr * fattr)257 static int sid_to_id(struct mnt_idmap *idmap,
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 = KUIDT_INIT(id);
279 uid = from_vfsuid(idmap, &init_user_ns, VFSUIDT_INIT(uid));
280 if (uid_valid(uid)) {
281 fattr->cf_uid = uid;
282 rc = 0;
283 }
284 } else {
285 kgid_t gid;
286 gid_t id;
287
288 id = le32_to_cpu(psid->sub_auth[psid->num_subauth - 1]);
289 gid = KGIDT_INIT(id);
290 gid = from_vfsgid(idmap, &init_user_ns, VFSGIDT_INIT(gid));
291 if (gid_valid(gid)) {
292 fattr->cf_gid = gid;
293 rc = 0;
294 }
295 }
296
297 return rc;
298 }
299
posix_state_to_acl(struct posix_acl_state * state,struct posix_acl_entry * pace)300 void posix_state_to_acl(struct posix_acl_state *state,
301 struct posix_acl_entry *pace)
302 {
303 int i;
304
305 pace->e_tag = ACL_USER_OBJ;
306 pace->e_perm = state->owner.allow;
307 for (i = 0; i < state->users->n; i++) {
308 pace++;
309 pace->e_tag = ACL_USER;
310 pace->e_uid = state->users->aces[i].uid;
311 pace->e_perm = state->users->aces[i].perms.allow;
312 }
313
314 pace++;
315 pace->e_tag = ACL_GROUP_OBJ;
316 pace->e_perm = state->group.allow;
317
318 for (i = 0; i < state->groups->n; i++) {
319 pace++;
320 pace->e_tag = ACL_GROUP;
321 pace->e_gid = state->groups->aces[i].gid;
322 pace->e_perm = state->groups->aces[i].perms.allow;
323 }
324
325 if (state->users->n || state->groups->n) {
326 pace++;
327 pace->e_tag = ACL_MASK;
328 pace->e_perm = state->mask.allow;
329 }
330
331 pace++;
332 pace->e_tag = ACL_OTHER;
333 pace->e_perm = state->other.allow;
334 }
335
init_acl_state(struct posix_acl_state * state,int cnt)336 int init_acl_state(struct posix_acl_state *state, int cnt)
337 {
338 int alloc;
339
340 memset(state, 0, sizeof(struct posix_acl_state));
341 /*
342 * In the worst case, each individual acl could be for a distinct
343 * named user or group, but we don't know which, so we allocate
344 * enough space for either:
345 */
346 alloc = sizeof(struct posix_ace_state_array)
347 + cnt * sizeof(struct posix_user_ace_state);
348 state->users = kzalloc(alloc, GFP_KERNEL);
349 if (!state->users)
350 return -ENOMEM;
351 state->groups = kzalloc(alloc, GFP_KERNEL);
352 if (!state->groups) {
353 kfree(state->users);
354 return -ENOMEM;
355 }
356 return 0;
357 }
358
free_acl_state(struct posix_acl_state * state)359 void free_acl_state(struct posix_acl_state *state)
360 {
361 kfree(state->users);
362 kfree(state->groups);
363 }
364
parse_dacl(struct mnt_idmap * idmap,struct smb_acl * pdacl,char * end_of_acl,struct smb_sid * pownersid,struct smb_sid * pgrpsid,struct smb_fattr * fattr)365 static void parse_dacl(struct mnt_idmap *idmap,
366 struct smb_acl *pdacl, char *end_of_acl,
367 struct smb_sid *pownersid, struct smb_sid *pgrpsid,
368 struct smb_fattr *fattr)
369 {
370 int i, ret;
371 int num_aces = 0;
372 unsigned int acl_size;
373 char *acl_base;
374 struct smb_ace **ppace;
375 struct posix_acl_entry *cf_pace, *cf_pdace;
376 struct posix_acl_state acl_state, default_acl_state;
377 umode_t mode = 0, acl_mode;
378 bool owner_found = false, group_found = false, others_found = false;
379
380 if (!pdacl)
381 return;
382
383 /* validate that we do not go past end of acl */
384 if (end_of_acl < (char *)pdacl + sizeof(struct smb_acl) ||
385 end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
386 pr_err("ACL too small to parse DACL\n");
387 return;
388 }
389
390 ksmbd_debug(SMB, "DACL revision %d size %d num aces %d\n",
391 le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
392 le32_to_cpu(pdacl->num_aces));
393
394 acl_base = (char *)pdacl;
395 acl_size = sizeof(struct smb_acl);
396
397 num_aces = le32_to_cpu(pdacl->num_aces);
398 if (num_aces <= 0)
399 return;
400
401 if (num_aces > ULONG_MAX / sizeof(struct smb_ace *))
402 return;
403
404 ret = init_acl_state(&acl_state, num_aces);
405 if (ret)
406 return;
407 ret = init_acl_state(&default_acl_state, num_aces);
408 if (ret) {
409 free_acl_state(&acl_state);
410 return;
411 }
412
413 ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *), GFP_KERNEL);
414 if (!ppace) {
415 free_acl_state(&default_acl_state);
416 free_acl_state(&acl_state);
417 return;
418 }
419
420 /*
421 * reset rwx permissions for user/group/other.
422 * Also, if num_aces is 0 i.e. DACL has no ACEs,
423 * user/group/other have no permissions
424 */
425 for (i = 0; i < num_aces; ++i) {
426 if (end_of_acl - acl_base < acl_size)
427 break;
428
429 ppace[i] = (struct smb_ace *)(acl_base + acl_size);
430 acl_base = (char *)ppace[i];
431 acl_size = offsetof(struct smb_ace, sid) +
432 offsetof(struct smb_sid, sub_auth);
433
434 if (end_of_acl - acl_base < acl_size ||
435 ppace[i]->sid.num_subauth > SID_MAX_SUB_AUTHORITIES ||
436 (end_of_acl - acl_base <
437 acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth) ||
438 (le16_to_cpu(ppace[i]->size) <
439 acl_size + sizeof(__le32) * ppace[i]->sid.num_subauth))
440 break;
441
442 acl_size = le16_to_cpu(ppace[i]->size);
443 ppace[i]->access_req =
444 smb_map_generic_desired_access(ppace[i]->access_req);
445
446 if (!(compare_sids(&ppace[i]->sid, &sid_unix_NFS_mode))) {
447 fattr->cf_mode =
448 le32_to_cpu(ppace[i]->sid.sub_auth[2]);
449 break;
450 } else if (!compare_sids(&ppace[i]->sid, pownersid)) {
451 acl_mode = access_flags_to_mode(fattr,
452 ppace[i]->access_req,
453 ppace[i]->type);
454 acl_mode &= 0700;
455
456 if (!owner_found) {
457 mode &= ~(0700);
458 mode |= acl_mode;
459 }
460 owner_found = true;
461 } else if (!compare_sids(&ppace[i]->sid, pgrpsid) ||
462 ppace[i]->sid.sub_auth[ppace[i]->sid.num_subauth - 1] ==
463 DOMAIN_USER_RID_LE) {
464 acl_mode = access_flags_to_mode(fattr,
465 ppace[i]->access_req,
466 ppace[i]->type);
467 acl_mode &= 0070;
468 if (!group_found) {
469 mode &= ~(0070);
470 mode |= acl_mode;
471 }
472 group_found = true;
473 } else if (!compare_sids(&ppace[i]->sid, &sid_everyone)) {
474 acl_mode = access_flags_to_mode(fattr,
475 ppace[i]->access_req,
476 ppace[i]->type);
477 acl_mode &= 0007;
478 if (!others_found) {
479 mode &= ~(0007);
480 mode |= acl_mode;
481 }
482 others_found = true;
483 } else if (!compare_sids(&ppace[i]->sid, &creator_owner)) {
484 continue;
485 } else if (!compare_sids(&ppace[i]->sid, &creator_group)) {
486 continue;
487 } else if (!compare_sids(&ppace[i]->sid, &sid_authusers)) {
488 continue;
489 } else {
490 struct smb_fattr temp_fattr;
491
492 acl_mode = access_flags_to_mode(fattr, ppace[i]->access_req,
493 ppace[i]->type);
494 temp_fattr.cf_uid = INVALID_UID;
495 ret = sid_to_id(idmap, &ppace[i]->sid, SIDOWNER, &temp_fattr);
496 if (ret || uid_eq(temp_fattr.cf_uid, INVALID_UID)) {
497 pr_err("%s: Error %d mapping Owner SID to uid\n",
498 __func__, ret);
499 continue;
500 }
501
502 acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
503 acl_state.users->aces[acl_state.users->n].uid =
504 temp_fattr.cf_uid;
505 acl_state.users->aces[acl_state.users->n++].perms.allow =
506 ((acl_mode & 0700) >> 6) | 0004;
507 default_acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
508 default_acl_state.users->aces[default_acl_state.users->n].uid =
509 temp_fattr.cf_uid;
510 default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
511 ((acl_mode & 0700) >> 6) | 0004;
512 }
513 }
514 kfree(ppace);
515
516 if (owner_found) {
517 /* The owner must be set to at least read-only. */
518 acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
519 acl_state.users->aces[acl_state.users->n].uid = fattr->cf_uid;
520 acl_state.users->aces[acl_state.users->n++].perms.allow =
521 ((mode & 0700) >> 6) | 0004;
522 default_acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
523 default_acl_state.users->aces[default_acl_state.users->n].uid =
524 fattr->cf_uid;
525 default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
526 ((mode & 0700) >> 6) | 0004;
527 }
528
529 if (group_found) {
530 acl_state.group.allow = (mode & 0070) >> 3;
531 acl_state.groups->aces[acl_state.groups->n].gid =
532 fattr->cf_gid;
533 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
534 (mode & 0070) >> 3;
535 default_acl_state.group.allow = (mode & 0070) >> 3;
536 default_acl_state.groups->aces[default_acl_state.groups->n].gid =
537 fattr->cf_gid;
538 default_acl_state.groups->aces[default_acl_state.groups->n++].perms.allow =
539 (mode & 0070) >> 3;
540 }
541
542 if (others_found) {
543 fattr->cf_mode &= ~(0007);
544 fattr->cf_mode |= mode & 0007;
545
546 acl_state.other.allow = mode & 0007;
547 default_acl_state.other.allow = mode & 0007;
548 }
549
550 if (acl_state.users->n || acl_state.groups->n) {
551 acl_state.mask.allow = 0x07;
552
553 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
554 fattr->cf_acls =
555 posix_acl_alloc(acl_state.users->n +
556 acl_state.groups->n + 4, GFP_KERNEL);
557 if (fattr->cf_acls) {
558 cf_pace = fattr->cf_acls->a_entries;
559 posix_state_to_acl(&acl_state, cf_pace);
560 }
561 }
562 }
563
564 if (default_acl_state.users->n || default_acl_state.groups->n) {
565 default_acl_state.mask.allow = 0x07;
566
567 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
568 fattr->cf_dacls =
569 posix_acl_alloc(default_acl_state.users->n +
570 default_acl_state.groups->n + 4, GFP_KERNEL);
571 if (fattr->cf_dacls) {
572 cf_pdace = fattr->cf_dacls->a_entries;
573 posix_state_to_acl(&default_acl_state, cf_pdace);
574 }
575 }
576 }
577 free_acl_state(&acl_state);
578 free_acl_state(&default_acl_state);
579 }
580
set_posix_acl_entries_dacl(struct mnt_idmap * idmap,struct smb_ace * pndace,struct smb_fattr * fattr,u32 * num_aces,u16 * size,u32 nt_aces_num)581 static void set_posix_acl_entries_dacl(struct mnt_idmap *idmap,
582 struct smb_ace *pndace,
583 struct smb_fattr *fattr, u32 *num_aces,
584 u16 *size, u32 nt_aces_num)
585 {
586 struct posix_acl_entry *pace;
587 struct smb_sid *sid;
588 struct smb_ace *ntace;
589 int i, j;
590
591 if (!fattr->cf_acls)
592 goto posix_default_acl;
593
594 pace = fattr->cf_acls->a_entries;
595 for (i = 0; i < fattr->cf_acls->a_count; i++, pace++) {
596 int flags = 0;
597
598 sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
599 if (!sid)
600 break;
601
602 if (pace->e_tag == ACL_USER) {
603 uid_t uid;
604 unsigned int sid_type = SIDOWNER;
605
606 uid = posix_acl_uid_translate(idmap, pace);
607 if (!uid)
608 sid_type = SIDUNIX_USER;
609 id_to_sid(uid, sid_type, sid);
610 } else if (pace->e_tag == ACL_GROUP) {
611 gid_t gid;
612
613 gid = posix_acl_gid_translate(idmap, pace);
614 id_to_sid(gid, SIDUNIX_GROUP, sid);
615 } else if (pace->e_tag == ACL_OTHER && !nt_aces_num) {
616 smb_copy_sid(sid, &sid_everyone);
617 } else {
618 kfree(sid);
619 continue;
620 }
621 ntace = pndace;
622 for (j = 0; j < nt_aces_num; j++) {
623 if (ntace->sid.sub_auth[ntace->sid.num_subauth - 1] ==
624 sid->sub_auth[sid->num_subauth - 1])
625 goto pass_same_sid;
626 ntace = (struct smb_ace *)((char *)ntace +
627 le16_to_cpu(ntace->size));
628 }
629
630 if (S_ISDIR(fattr->cf_mode) && pace->e_tag == ACL_OTHER)
631 flags = 0x03;
632
633 ntace = (struct smb_ace *)((char *)pndace + *size);
634 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, flags,
635 pace->e_perm, 0777);
636 (*num_aces)++;
637 if (pace->e_tag == ACL_USER)
638 ntace->access_req |=
639 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
640
641 if (S_ISDIR(fattr->cf_mode) &&
642 (pace->e_tag == ACL_USER || pace->e_tag == ACL_GROUP)) {
643 ntace = (struct smb_ace *)((char *)pndace + *size);
644 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED,
645 0x03, pace->e_perm, 0777);
646 (*num_aces)++;
647 if (pace->e_tag == ACL_USER)
648 ntace->access_req |=
649 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
650 }
651
652 pass_same_sid:
653 kfree(sid);
654 }
655
656 if (nt_aces_num)
657 return;
658
659 posix_default_acl:
660 if (!fattr->cf_dacls)
661 return;
662
663 pace = fattr->cf_dacls->a_entries;
664 for (i = 0; i < fattr->cf_dacls->a_count; i++, pace++) {
665 sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
666 if (!sid)
667 break;
668
669 if (pace->e_tag == ACL_USER) {
670 uid_t uid;
671
672 uid = posix_acl_uid_translate(idmap, pace);
673 id_to_sid(uid, SIDCREATOR_OWNER, sid);
674 } else if (pace->e_tag == ACL_GROUP) {
675 gid_t gid;
676
677 gid = posix_acl_gid_translate(idmap, pace);
678 id_to_sid(gid, SIDCREATOR_GROUP, sid);
679 } else {
680 kfree(sid);
681 continue;
682 }
683
684 ntace = (struct smb_ace *)((char *)pndace + *size);
685 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, 0x0b,
686 pace->e_perm, 0777);
687 (*num_aces)++;
688 if (pace->e_tag == ACL_USER)
689 ntace->access_req |=
690 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
691 kfree(sid);
692 }
693 }
694
set_ntacl_dacl(struct mnt_idmap * idmap,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)695 static void set_ntacl_dacl(struct mnt_idmap *idmap,
696 struct smb_acl *pndacl,
697 struct smb_acl *nt_dacl,
698 unsigned int aces_size,
699 const struct smb_sid *pownersid,
700 const struct smb_sid *pgrpsid,
701 struct smb_fattr *fattr)
702 {
703 struct smb_ace *ntace, *pndace;
704 int nt_num_aces = le32_to_cpu(nt_dacl->num_aces), num_aces = 0;
705 unsigned short size = 0;
706 int i;
707
708 pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
709 if (nt_num_aces) {
710 ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl));
711 for (i = 0; i < nt_num_aces; i++) {
712 unsigned short nt_ace_size;
713
714 if (offsetof(struct smb_ace, access_req) > aces_size)
715 break;
716
717 nt_ace_size = le16_to_cpu(ntace->size);
718 if (nt_ace_size > aces_size)
719 break;
720
721 memcpy((char *)pndace + size, ntace, nt_ace_size);
722 size += nt_ace_size;
723 aces_size -= nt_ace_size;
724 ntace = (struct smb_ace *)((char *)ntace + nt_ace_size);
725 num_aces++;
726 }
727 }
728
729 set_posix_acl_entries_dacl(idmap, pndace, fattr,
730 &num_aces, &size, nt_num_aces);
731 pndacl->num_aces = cpu_to_le32(num_aces);
732 pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
733 }
734
set_mode_dacl(struct mnt_idmap * idmap,struct smb_acl * pndacl,struct smb_fattr * fattr)735 static void set_mode_dacl(struct mnt_idmap *idmap,
736 struct smb_acl *pndacl, struct smb_fattr *fattr)
737 {
738 struct smb_ace *pace, *pndace;
739 u32 num_aces = 0;
740 u16 size = 0, ace_size = 0;
741 uid_t uid;
742 const struct smb_sid *sid;
743
744 pace = pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
745
746 if (fattr->cf_acls) {
747 set_posix_acl_entries_dacl(idmap, pndace, fattr,
748 &num_aces, &size, num_aces);
749 goto out;
750 }
751
752 /* owner RID */
753 uid = from_kuid(&init_user_ns, fattr->cf_uid);
754 if (uid)
755 sid = &server_conf.domain_sid;
756 else
757 sid = &sid_unix_users;
758 ace_size = fill_ace_for_sid(pace, sid, ACCESS_ALLOWED, 0,
759 fattr->cf_mode, 0700);
760 pace->sid.sub_auth[pace->sid.num_subauth++] = cpu_to_le32(uid);
761 pace->size = cpu_to_le16(ace_size + 4);
762 size += le16_to_cpu(pace->size);
763 pace = (struct smb_ace *)((char *)pndace + size);
764
765 /* Group RID */
766 ace_size = fill_ace_for_sid(pace, &sid_unix_groups,
767 ACCESS_ALLOWED, 0, fattr->cf_mode, 0070);
768 pace->sid.sub_auth[pace->sid.num_subauth++] =
769 cpu_to_le32(from_kgid(&init_user_ns, fattr->cf_gid));
770 pace->size = cpu_to_le16(ace_size + 4);
771 size += le16_to_cpu(pace->size);
772 pace = (struct smb_ace *)((char *)pndace + size);
773 num_aces = 3;
774
775 if (S_ISDIR(fattr->cf_mode)) {
776 pace = (struct smb_ace *)((char *)pndace + size);
777
778 /* creator owner */
779 size += fill_ace_for_sid(pace, &creator_owner, ACCESS_ALLOWED,
780 0x0b, fattr->cf_mode, 0700);
781 pace = (struct smb_ace *)((char *)pndace + size);
782
783 /* creator group */
784 size += fill_ace_for_sid(pace, &creator_group, ACCESS_ALLOWED,
785 0x0b, fattr->cf_mode, 0070);
786 pace = (struct smb_ace *)((char *)pndace + size);
787 num_aces = 5;
788 }
789
790 /* other */
791 size += fill_ace_for_sid(pace, &sid_everyone, ACCESS_ALLOWED, 0,
792 fattr->cf_mode, 0007);
793
794 out:
795 pndacl->num_aces = cpu_to_le32(num_aces);
796 pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
797 }
798
parse_sid(struct smb_sid * psid,char * end_of_acl)799 static int parse_sid(struct smb_sid *psid, char *end_of_acl)
800 {
801 /*
802 * validate that we do not go past end of ACL - sid must be at least 8
803 * bytes long (assuming no sub-auths - e.g. the null SID
804 */
805 if (end_of_acl < (char *)psid + 8) {
806 pr_err("ACL too small to parse SID %p\n", psid);
807 return -EINVAL;
808 }
809
810 if (!psid->num_subauth)
811 return 0;
812
813 if (psid->num_subauth > SID_MAX_SUB_AUTHORITIES ||
814 end_of_acl < (char *)psid + 8 + sizeof(__le32) * psid->num_subauth)
815 return -EINVAL;
816
817 return 0;
818 }
819
820 /* Convert CIFS ACL to POSIX form */
parse_sec_desc(struct mnt_idmap * idmap,struct smb_ntsd * pntsd,int acl_len,struct smb_fattr * fattr)821 int parse_sec_desc(struct mnt_idmap *idmap, struct smb_ntsd *pntsd,
822 int acl_len, struct smb_fattr *fattr)
823 {
824 int rc = 0;
825 struct smb_sid *owner_sid_ptr, *group_sid_ptr;
826 struct smb_acl *dacl_ptr; /* no need for SACL ptr */
827 char *end_of_acl = ((char *)pntsd) + acl_len;
828 __u32 dacloffset;
829 int pntsd_type;
830
831 if (!pntsd)
832 return -EIO;
833
834 if (acl_len < sizeof(struct smb_ntsd))
835 return -EINVAL;
836
837 owner_sid_ptr = (struct smb_sid *)((char *)pntsd +
838 le32_to_cpu(pntsd->osidoffset));
839 group_sid_ptr = (struct smb_sid *)((char *)pntsd +
840 le32_to_cpu(pntsd->gsidoffset));
841 dacloffset = le32_to_cpu(pntsd->dacloffset);
842 dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset);
843 ksmbd_debug(SMB,
844 "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
845 pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
846 le32_to_cpu(pntsd->gsidoffset),
847 le32_to_cpu(pntsd->sacloffset), dacloffset);
848
849 pntsd_type = le16_to_cpu(pntsd->type);
850 if (!(pntsd_type & DACL_PRESENT)) {
851 ksmbd_debug(SMB, "DACL_PRESENT in DACL type is not set\n");
852 return rc;
853 }
854
855 pntsd->type = cpu_to_le16(DACL_PRESENT);
856
857 if (pntsd->osidoffset) {
858 if (le32_to_cpu(pntsd->osidoffset) < sizeof(struct smb_ntsd))
859 return -EINVAL;
860
861 rc = parse_sid(owner_sid_ptr, end_of_acl);
862 if (rc) {
863 pr_err("%s: Error %d parsing Owner SID\n", __func__, rc);
864 return rc;
865 }
866
867 rc = sid_to_id(idmap, owner_sid_ptr, SIDOWNER, fattr);
868 if (rc) {
869 pr_err("%s: Error %d mapping Owner SID to uid\n",
870 __func__, rc);
871 owner_sid_ptr = NULL;
872 }
873 }
874
875 if (pntsd->gsidoffset) {
876 if (le32_to_cpu(pntsd->gsidoffset) < sizeof(struct smb_ntsd))
877 return -EINVAL;
878
879 rc = parse_sid(group_sid_ptr, end_of_acl);
880 if (rc) {
881 pr_err("%s: Error %d mapping Owner SID to gid\n",
882 __func__, rc);
883 return rc;
884 }
885 rc = sid_to_id(idmap, group_sid_ptr, SIDUNIX_GROUP, fattr);
886 if (rc) {
887 pr_err("%s: Error %d mapping Group SID to gid\n",
888 __func__, rc);
889 group_sid_ptr = NULL;
890 }
891 }
892
893 if ((pntsd_type & (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) ==
894 (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ))
895 pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
896 if (pntsd_type & DACL_PROTECTED)
897 pntsd->type |= cpu_to_le16(DACL_PROTECTED);
898
899 if (dacloffset) {
900 if (dacloffset < sizeof(struct smb_ntsd))
901 return -EINVAL;
902
903 parse_dacl(idmap, dacl_ptr, end_of_acl,
904 owner_sid_ptr, group_sid_ptr, fattr);
905 }
906
907 return 0;
908 }
909
910 /* Convert permission bits from mode to equivalent CIFS ACL */
build_sec_desc(struct mnt_idmap * idmap,struct smb_ntsd * pntsd,struct smb_ntsd * ppntsd,int ppntsd_size,int addition_info,__u32 * secdesclen,struct smb_fattr * fattr)911 int build_sec_desc(struct mnt_idmap *idmap,
912 struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd,
913 int ppntsd_size, int addition_info, __u32 *secdesclen,
914 struct smb_fattr *fattr)
915 {
916 int rc = 0;
917 __u32 offset;
918 struct smb_sid *owner_sid_ptr, *group_sid_ptr;
919 struct smb_sid *nowner_sid_ptr, *ngroup_sid_ptr;
920 struct smb_acl *dacl_ptr = NULL; /* no need for SACL ptr */
921 uid_t uid;
922 gid_t gid;
923 unsigned int sid_type = SIDOWNER;
924
925 nowner_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
926 if (!nowner_sid_ptr)
927 return -ENOMEM;
928
929 uid = from_kuid(&init_user_ns, fattr->cf_uid);
930 if (!uid)
931 sid_type = SIDUNIX_USER;
932 id_to_sid(uid, sid_type, nowner_sid_ptr);
933
934 ngroup_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
935 if (!ngroup_sid_ptr) {
936 kfree(nowner_sid_ptr);
937 return -ENOMEM;
938 }
939
940 gid = from_kgid(&init_user_ns, fattr->cf_gid);
941 id_to_sid(gid, SIDUNIX_GROUP, ngroup_sid_ptr);
942
943 offset = sizeof(struct smb_ntsd);
944 pntsd->sacloffset = 0;
945 pntsd->revision = cpu_to_le16(1);
946 pntsd->type = cpu_to_le16(SELF_RELATIVE);
947 if (ppntsd)
948 pntsd->type |= ppntsd->type;
949
950 if (addition_info & OWNER_SECINFO) {
951 pntsd->osidoffset = cpu_to_le32(offset);
952 owner_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
953 smb_copy_sid(owner_sid_ptr, nowner_sid_ptr);
954 offset += 1 + 1 + 6 + (nowner_sid_ptr->num_subauth * 4);
955 }
956
957 if (addition_info & GROUP_SECINFO) {
958 pntsd->gsidoffset = cpu_to_le32(offset);
959 group_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
960 smb_copy_sid(group_sid_ptr, ngroup_sid_ptr);
961 offset += 1 + 1 + 6 + (ngroup_sid_ptr->num_subauth * 4);
962 }
963
964 if (addition_info & DACL_SECINFO) {
965 pntsd->type |= cpu_to_le16(DACL_PRESENT);
966 dacl_ptr = (struct smb_acl *)((char *)pntsd + offset);
967 dacl_ptr->revision = cpu_to_le16(2);
968 dacl_ptr->size = cpu_to_le16(sizeof(struct smb_acl));
969 dacl_ptr->num_aces = 0;
970
971 if (!ppntsd) {
972 set_mode_dacl(idmap, dacl_ptr, fattr);
973 } else {
974 struct smb_acl *ppdacl_ptr;
975 unsigned int dacl_offset = le32_to_cpu(ppntsd->dacloffset);
976 int ppdacl_size, ntacl_size = ppntsd_size - dacl_offset;
977
978 if (!dacl_offset ||
979 (dacl_offset + sizeof(struct smb_acl) > ppntsd_size))
980 goto out;
981
982 ppdacl_ptr = (struct smb_acl *)((char *)ppntsd + dacl_offset);
983 ppdacl_size = le16_to_cpu(ppdacl_ptr->size);
984 if (ppdacl_size > ntacl_size ||
985 ppdacl_size < sizeof(struct smb_acl))
986 goto out;
987
988 set_ntacl_dacl(idmap, dacl_ptr, ppdacl_ptr,
989 ntacl_size - sizeof(struct smb_acl),
990 nowner_sid_ptr, ngroup_sid_ptr,
991 fattr);
992 }
993 pntsd->dacloffset = cpu_to_le32(offset);
994 offset += le16_to_cpu(dacl_ptr->size);
995 }
996
997 out:
998 kfree(nowner_sid_ptr);
999 kfree(ngroup_sid_ptr);
1000 *secdesclen = offset;
1001 return rc;
1002 }
1003
smb_set_ace(struct smb_ace * ace,const struct smb_sid * sid,u8 type,u8 flags,__le32 access_req)1004 static void smb_set_ace(struct smb_ace *ace, const struct smb_sid *sid, u8 type,
1005 u8 flags, __le32 access_req)
1006 {
1007 ace->type = type;
1008 ace->flags = flags;
1009 ace->access_req = access_req;
1010 smb_copy_sid(&ace->sid, sid);
1011 ace->size = cpu_to_le16(1 + 1 + 2 + 4 + 1 + 1 + 6 + (sid->num_subauth * 4));
1012 }
1013
smb_inherit_dacl(struct ksmbd_conn * conn,const struct path * path,unsigned int uid,unsigned int gid)1014 int smb_inherit_dacl(struct ksmbd_conn *conn,
1015 const struct path *path,
1016 unsigned int uid, unsigned int gid)
1017 {
1018 const struct smb_sid *psid, *creator = NULL;
1019 struct smb_ace *parent_aces, *aces;
1020 struct smb_acl *parent_pdacl;
1021 struct smb_ntsd *parent_pntsd = NULL;
1022 struct smb_sid owner_sid, group_sid;
1023 struct dentry *parent = path->dentry->d_parent;
1024 struct mnt_idmap *idmap = mnt_idmap(path->mnt);
1025 int inherited_flags = 0, flags = 0, i, ace_cnt = 0, nt_size = 0, pdacl_size;
1026 int rc = 0, num_aces, dacloffset, pntsd_type, pntsd_size, acl_len, aces_size;
1027 char *aces_base;
1028 bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode);
1029
1030 pntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap,
1031 parent, &parent_pntsd);
1032 if (pntsd_size <= 0)
1033 return -ENOENT;
1034 dacloffset = le32_to_cpu(parent_pntsd->dacloffset);
1035 if (!dacloffset || (dacloffset + sizeof(struct smb_acl) > pntsd_size)) {
1036 rc = -EINVAL;
1037 goto free_parent_pntsd;
1038 }
1039
1040 parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset);
1041 acl_len = pntsd_size - dacloffset;
1042 num_aces = le32_to_cpu(parent_pdacl->num_aces);
1043 pntsd_type = le16_to_cpu(parent_pntsd->type);
1044 pdacl_size = le16_to_cpu(parent_pdacl->size);
1045
1046 if (pdacl_size > acl_len || pdacl_size < sizeof(struct smb_acl)) {
1047 rc = -EINVAL;
1048 goto free_parent_pntsd;
1049 }
1050
1051 aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2, GFP_KERNEL);
1052 if (!aces_base) {
1053 rc = -ENOMEM;
1054 goto free_parent_pntsd;
1055 }
1056
1057 aces = (struct smb_ace *)aces_base;
1058 parent_aces = (struct smb_ace *)((char *)parent_pdacl +
1059 sizeof(struct smb_acl));
1060 aces_size = acl_len - sizeof(struct smb_acl);
1061
1062 if (pntsd_type & DACL_AUTO_INHERITED)
1063 inherited_flags = INHERITED_ACE;
1064
1065 for (i = 0; i < num_aces; i++) {
1066 int pace_size;
1067
1068 if (offsetof(struct smb_ace, access_req) > aces_size)
1069 break;
1070
1071 pace_size = le16_to_cpu(parent_aces->size);
1072 if (pace_size > aces_size)
1073 break;
1074
1075 aces_size -= pace_size;
1076
1077 flags = parent_aces->flags;
1078 if (!smb_inherit_flags(flags, is_dir))
1079 goto pass;
1080 if (is_dir) {
1081 flags &= ~(INHERIT_ONLY_ACE | INHERITED_ACE);
1082 if (!(flags & CONTAINER_INHERIT_ACE))
1083 flags |= INHERIT_ONLY_ACE;
1084 if (flags & NO_PROPAGATE_INHERIT_ACE)
1085 flags = 0;
1086 } else {
1087 flags = 0;
1088 }
1089
1090 if (!compare_sids(&creator_owner, &parent_aces->sid)) {
1091 creator = &creator_owner;
1092 id_to_sid(uid, SIDOWNER, &owner_sid);
1093 psid = &owner_sid;
1094 } else if (!compare_sids(&creator_group, &parent_aces->sid)) {
1095 creator = &creator_group;
1096 id_to_sid(gid, SIDUNIX_GROUP, &group_sid);
1097 psid = &group_sid;
1098 } else {
1099 creator = NULL;
1100 psid = &parent_aces->sid;
1101 }
1102
1103 if (is_dir && creator && flags & CONTAINER_INHERIT_ACE) {
1104 smb_set_ace(aces, psid, parent_aces->type, inherited_flags,
1105 parent_aces->access_req);
1106 nt_size += le16_to_cpu(aces->size);
1107 ace_cnt++;
1108 aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
1109 flags |= INHERIT_ONLY_ACE;
1110 psid = creator;
1111 } else if (is_dir && !(parent_aces->flags & NO_PROPAGATE_INHERIT_ACE)) {
1112 psid = &parent_aces->sid;
1113 }
1114
1115 smb_set_ace(aces, psid, parent_aces->type, flags | inherited_flags,
1116 parent_aces->access_req);
1117 nt_size += le16_to_cpu(aces->size);
1118 aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
1119 ace_cnt++;
1120 pass:
1121 parent_aces = (struct smb_ace *)((char *)parent_aces + pace_size);
1122 }
1123
1124 if (nt_size > 0) {
1125 struct smb_ntsd *pntsd;
1126 struct smb_acl *pdacl;
1127 struct smb_sid *powner_sid = NULL, *pgroup_sid = NULL;
1128 int powner_sid_size = 0, pgroup_sid_size = 0, pntsd_size;
1129 int pntsd_alloc_size;
1130
1131 if (parent_pntsd->osidoffset) {
1132 powner_sid = (struct smb_sid *)((char *)parent_pntsd +
1133 le32_to_cpu(parent_pntsd->osidoffset));
1134 powner_sid_size = 1 + 1 + 6 + (powner_sid->num_subauth * 4);
1135 }
1136 if (parent_pntsd->gsidoffset) {
1137 pgroup_sid = (struct smb_sid *)((char *)parent_pntsd +
1138 le32_to_cpu(parent_pntsd->gsidoffset));
1139 pgroup_sid_size = 1 + 1 + 6 + (pgroup_sid->num_subauth * 4);
1140 }
1141
1142 pntsd_alloc_size = sizeof(struct smb_ntsd) + powner_sid_size +
1143 pgroup_sid_size + sizeof(struct smb_acl) + nt_size;
1144
1145 pntsd = kzalloc(pntsd_alloc_size, GFP_KERNEL);
1146 if (!pntsd) {
1147 rc = -ENOMEM;
1148 goto free_aces_base;
1149 }
1150
1151 pntsd->revision = cpu_to_le16(1);
1152 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PRESENT);
1153 if (le16_to_cpu(parent_pntsd->type) & DACL_AUTO_INHERITED)
1154 pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
1155 pntsd_size = sizeof(struct smb_ntsd);
1156 pntsd->osidoffset = parent_pntsd->osidoffset;
1157 pntsd->gsidoffset = parent_pntsd->gsidoffset;
1158 pntsd->dacloffset = parent_pntsd->dacloffset;
1159
1160 if ((u64)le32_to_cpu(pntsd->osidoffset) + powner_sid_size >
1161 pntsd_alloc_size) {
1162 rc = -EINVAL;
1163 kfree(pntsd);
1164 goto free_aces_base;
1165 }
1166
1167 if ((u64)le32_to_cpu(pntsd->gsidoffset) + pgroup_sid_size >
1168 pntsd_alloc_size) {
1169 rc = -EINVAL;
1170 kfree(pntsd);
1171 goto free_aces_base;
1172 }
1173
1174 if ((u64)le32_to_cpu(pntsd->dacloffset) + sizeof(struct smb_acl) + nt_size >
1175 pntsd_alloc_size) {
1176 rc = -EINVAL;
1177 kfree(pntsd);
1178 goto free_aces_base;
1179 }
1180
1181 if (pntsd->osidoffset) {
1182 struct smb_sid *owner_sid = (struct smb_sid *)((char *)pntsd +
1183 le32_to_cpu(pntsd->osidoffset));
1184 memcpy(owner_sid, powner_sid, powner_sid_size);
1185 pntsd_size += powner_sid_size;
1186 }
1187
1188 if (pntsd->gsidoffset) {
1189 struct smb_sid *group_sid = (struct smb_sid *)((char *)pntsd +
1190 le32_to_cpu(pntsd->gsidoffset));
1191 memcpy(group_sid, pgroup_sid, pgroup_sid_size);
1192 pntsd_size += pgroup_sid_size;
1193 }
1194
1195 if (pntsd->dacloffset) {
1196 struct smb_ace *pace;
1197
1198 pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1199 pdacl->revision = cpu_to_le16(2);
1200 pdacl->size = cpu_to_le16(sizeof(struct smb_acl) + nt_size);
1201 pdacl->num_aces = cpu_to_le32(ace_cnt);
1202 pace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1203 memcpy(pace, aces_base, nt_size);
1204 pntsd_size += sizeof(struct smb_acl) + nt_size;
1205 }
1206
1207 ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, pntsd_size, false);
1208 kfree(pntsd);
1209 }
1210
1211 free_aces_base:
1212 kfree(aces_base);
1213 free_parent_pntsd:
1214 kfree(parent_pntsd);
1215 return rc;
1216 }
1217
smb_inherit_flags(int flags,bool is_dir)1218 bool smb_inherit_flags(int flags, bool is_dir)
1219 {
1220 if (!is_dir)
1221 return (flags & OBJECT_INHERIT_ACE) != 0;
1222
1223 if (flags & OBJECT_INHERIT_ACE && !(flags & NO_PROPAGATE_INHERIT_ACE))
1224 return true;
1225
1226 if (flags & CONTAINER_INHERIT_ACE)
1227 return true;
1228 return false;
1229 }
1230
smb_check_perm_dacl(struct ksmbd_conn * conn,const struct path * path,__le32 * pdaccess,int uid)1231 int smb_check_perm_dacl(struct ksmbd_conn *conn, const struct path *path,
1232 __le32 *pdaccess, int uid)
1233 {
1234 struct mnt_idmap *idmap = mnt_idmap(path->mnt);
1235 struct smb_ntsd *pntsd = NULL;
1236 struct smb_acl *pdacl;
1237 struct posix_acl *posix_acls;
1238 int rc = 0, pntsd_size, acl_size, aces_size, pdacl_size, dacl_offset;
1239 struct smb_sid sid;
1240 int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE);
1241 struct smb_ace *ace;
1242 int i, found = 0;
1243 unsigned int access_bits = 0;
1244 struct smb_ace *others_ace = NULL;
1245 struct posix_acl_entry *pa_entry;
1246 unsigned int sid_type = SIDOWNER;
1247 unsigned short ace_size;
1248
1249 ksmbd_debug(SMB, "check permission using windows acl\n");
1250 pntsd_size = ksmbd_vfs_get_sd_xattr(conn, idmap,
1251 path->dentry, &pntsd);
1252 if (pntsd_size <= 0 || !pntsd)
1253 goto err_out;
1254
1255 dacl_offset = le32_to_cpu(pntsd->dacloffset);
1256 if (!dacl_offset ||
1257 (dacl_offset + sizeof(struct smb_acl) > pntsd_size))
1258 goto err_out;
1259
1260 pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1261 acl_size = pntsd_size - dacl_offset;
1262 pdacl_size = le16_to_cpu(pdacl->size);
1263
1264 if (pdacl_size > acl_size || pdacl_size < sizeof(struct smb_acl))
1265 goto err_out;
1266
1267 if (!pdacl->num_aces) {
1268 if (!(pdacl_size - sizeof(struct smb_acl)) &&
1269 *pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) {
1270 rc = -EACCES;
1271 goto err_out;
1272 }
1273 goto err_out;
1274 }
1275
1276 if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) {
1277 granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
1278 DELETE;
1279
1280 ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1281 aces_size = acl_size - sizeof(struct smb_acl);
1282 for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1283 if (offsetof(struct smb_ace, access_req) > aces_size)
1284 break;
1285 ace_size = le16_to_cpu(ace->size);
1286 if (ace_size > aces_size)
1287 break;
1288 aces_size -= ace_size;
1289 granted |= le32_to_cpu(ace->access_req);
1290 ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1291 }
1292
1293 if (!pdacl->num_aces)
1294 granted = GENERIC_ALL_FLAGS;
1295 }
1296
1297 if (!uid)
1298 sid_type = SIDUNIX_USER;
1299 id_to_sid(uid, sid_type, &sid);
1300
1301 ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1302 aces_size = acl_size - sizeof(struct smb_acl);
1303 for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1304 if (offsetof(struct smb_ace, access_req) > aces_size)
1305 break;
1306 ace_size = le16_to_cpu(ace->size);
1307 if (ace_size > aces_size)
1308 break;
1309 aces_size -= ace_size;
1310
1311 if (!compare_sids(&sid, &ace->sid) ||
1312 !compare_sids(&sid_unix_NFS_mode, &ace->sid)) {
1313 found = 1;
1314 break;
1315 }
1316 if (!compare_sids(&sid_everyone, &ace->sid))
1317 others_ace = ace;
1318
1319 ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1320 }
1321
1322 if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) {
1323 granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
1324 DELETE;
1325
1326 granted |= le32_to_cpu(ace->access_req);
1327
1328 if (!pdacl->num_aces)
1329 granted = GENERIC_ALL_FLAGS;
1330 }
1331
1332 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
1333 posix_acls = get_inode_acl(d_inode(path->dentry), ACL_TYPE_ACCESS);
1334 if (!IS_ERR_OR_NULL(posix_acls) && !found) {
1335 unsigned int id = -1;
1336
1337 pa_entry = posix_acls->a_entries;
1338 for (i = 0; i < posix_acls->a_count; i++, pa_entry++) {
1339 if (pa_entry->e_tag == ACL_USER)
1340 id = posix_acl_uid_translate(idmap, pa_entry);
1341 else if (pa_entry->e_tag == ACL_GROUP)
1342 id = posix_acl_gid_translate(idmap, pa_entry);
1343 else
1344 continue;
1345
1346 if (id == uid) {
1347 mode_to_access_flags(pa_entry->e_perm,
1348 0777,
1349 &access_bits);
1350 if (!access_bits)
1351 access_bits =
1352 SET_MINIMUM_RIGHTS;
1353 posix_acl_release(posix_acls);
1354 goto check_access_bits;
1355 }
1356 }
1357 }
1358 if (!IS_ERR_OR_NULL(posix_acls))
1359 posix_acl_release(posix_acls);
1360 }
1361
1362 if (!found) {
1363 if (others_ace) {
1364 ace = others_ace;
1365 } else {
1366 ksmbd_debug(SMB, "Can't find corresponding sid\n");
1367 rc = -EACCES;
1368 goto err_out;
1369 }
1370 }
1371
1372 switch (ace->type) {
1373 case ACCESS_ALLOWED_ACE_TYPE:
1374 access_bits = le32_to_cpu(ace->access_req);
1375 break;
1376 case ACCESS_DENIED_ACE_TYPE:
1377 case ACCESS_DENIED_CALLBACK_ACE_TYPE:
1378 access_bits = le32_to_cpu(~ace->access_req);
1379 break;
1380 }
1381
1382 check_access_bits:
1383 if (granted &
1384 ~(access_bits | FILE_READ_ATTRIBUTES | READ_CONTROL | WRITE_DAC | DELETE)) {
1385 ksmbd_debug(SMB, "Access denied with winACL, granted : %x, access_req : %x\n",
1386 granted, le32_to_cpu(ace->access_req));
1387 rc = -EACCES;
1388 goto err_out;
1389 }
1390
1391 *pdaccess = cpu_to_le32(granted);
1392 err_out:
1393 kfree(pntsd);
1394 return rc;
1395 }
1396
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)1397 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
1398 const struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
1399 bool type_check, bool get_write)
1400 {
1401 int rc;
1402 struct smb_fattr fattr = {{0}};
1403 struct inode *inode = d_inode(path->dentry);
1404 struct mnt_idmap *idmap = mnt_idmap(path->mnt);
1405 struct iattr newattrs;
1406
1407 fattr.cf_uid = INVALID_UID;
1408 fattr.cf_gid = INVALID_GID;
1409 fattr.cf_mode = inode->i_mode;
1410
1411 rc = parse_sec_desc(idmap, pntsd, ntsd_len, &fattr);
1412 if (rc)
1413 goto out;
1414
1415 newattrs.ia_valid = ATTR_CTIME;
1416 if (!uid_eq(fattr.cf_uid, INVALID_UID)) {
1417 newattrs.ia_valid |= ATTR_UID;
1418 newattrs.ia_uid = fattr.cf_uid;
1419 }
1420 if (!gid_eq(fattr.cf_gid, INVALID_GID)) {
1421 newattrs.ia_valid |= ATTR_GID;
1422 newattrs.ia_gid = fattr.cf_gid;
1423 }
1424 newattrs.ia_valid |= ATTR_MODE;
1425 newattrs.ia_mode = (inode->i_mode & ~0777) | (fattr.cf_mode & 0777);
1426
1427 ksmbd_vfs_remove_acl_xattrs(idmap, path);
1428 /* Update posix acls */
1429 if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && fattr.cf_dacls) {
1430 rc = set_posix_acl(idmap, path->dentry,
1431 ACL_TYPE_ACCESS, fattr.cf_acls);
1432 if (rc < 0)
1433 ksmbd_debug(SMB,
1434 "Set posix acl(ACL_TYPE_ACCESS) failed, rc : %d\n",
1435 rc);
1436 if (S_ISDIR(inode->i_mode) && fattr.cf_dacls) {
1437 rc = set_posix_acl(idmap, path->dentry,
1438 ACL_TYPE_DEFAULT, fattr.cf_dacls);
1439 if (rc)
1440 ksmbd_debug(SMB,
1441 "Set posix acl(ACL_TYPE_DEFAULT) failed, rc : %d\n",
1442 rc);
1443 }
1444 }
1445
1446 inode_lock(inode);
1447 rc = notify_change(idmap, path->dentry, &newattrs, NULL);
1448 inode_unlock(inode);
1449 if (rc)
1450 goto out;
1451
1452 /* Check it only calling from SD BUFFER context */
1453 if (type_check && !(le16_to_cpu(pntsd->type) & DACL_PRESENT))
1454 goto out;
1455
1456 if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
1457 /* Update WinACL in xattr */
1458 ksmbd_vfs_remove_sd_xattrs(idmap, path);
1459 ksmbd_vfs_set_sd_xattr(conn, idmap, path, pntsd, ntsd_len,
1460 get_write);
1461 }
1462
1463 out:
1464 posix_acl_release(fattr.cf_acls);
1465 posix_acl_release(fattr.cf_dacls);
1466 return rc;
1467 }
1468
ksmbd_init_domain(u32 * sub_auth)1469 void ksmbd_init_domain(u32 *sub_auth)
1470 {
1471 int i;
1472
1473 memcpy(&server_conf.domain_sid, &domain, sizeof(struct smb_sid));
1474 for (i = 0; i < 3; ++i)
1475 server_conf.domain_sid.sub_auth[i + 1] = cpu_to_le32(sub_auth[i]);
1476 }
1477