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