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