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