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