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