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