Merge tag 'gpio-updates-for-v5.15' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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                 if (id > 0) {
278                         uid = make_kuid(user_ns, id);
279                         if (uid_valid(uid) && kuid_has_mapping(user_ns, uid)) {
280                                 fattr->cf_uid = uid;
281                                 rc = 0;
282                         }
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                 if (id > 0) {
290                         gid = make_kgid(user_ns, id);
291                         if (gid_valid(gid) && kgid_has_mapping(user_ns, gid)) {
292                                 fattr->cf_gid = gid;
293                                 rc = 0;
294                         }
295                 }
296         }
297
298         return rc;
299 }
300
301 void posix_state_to_acl(struct posix_acl_state *state,
302                         struct posix_acl_entry *pace)
303 {
304         int i;
305
306         pace->e_tag = ACL_USER_OBJ;
307         pace->e_perm = state->owner.allow;
308         for (i = 0; i < state->users->n; i++) {
309                 pace++;
310                 pace->e_tag = ACL_USER;
311                 pace->e_uid = state->users->aces[i].uid;
312                 pace->e_perm = state->users->aces[i].perms.allow;
313         }
314
315         pace++;
316         pace->e_tag = ACL_GROUP_OBJ;
317         pace->e_perm = state->group.allow;
318
319         for (i = 0; i < state->groups->n; i++) {
320                 pace++;
321                 pace->e_tag = ACL_GROUP;
322                 pace->e_gid = state->groups->aces[i].gid;
323                 pace->e_perm = state->groups->aces[i].perms.allow;
324         }
325
326         if (state->users->n || state->groups->n) {
327                 pace++;
328                 pace->e_tag = ACL_MASK;
329                 pace->e_perm = state->mask.allow;
330         }
331
332         pace++;
333         pace->e_tag = ACL_OTHER;
334         pace->e_perm = state->other.allow;
335 }
336
337 int init_acl_state(struct posix_acl_state *state, int cnt)
338 {
339         int alloc;
340
341         memset(state, 0, sizeof(struct posix_acl_state));
342         /*
343          * In the worst case, each individual acl could be for a distinct
344          * named user or group, but we don't know which, so we allocate
345          * enough space for either:
346          */
347         alloc = sizeof(struct posix_ace_state_array)
348                 + cnt * sizeof(struct posix_user_ace_state);
349         state->users = kzalloc(alloc, GFP_KERNEL);
350         if (!state->users)
351                 return -ENOMEM;
352         state->groups = kzalloc(alloc, GFP_KERNEL);
353         if (!state->groups) {
354                 kfree(state->users);
355                 return -ENOMEM;
356         }
357         return 0;
358 }
359
360 void free_acl_state(struct posix_acl_state *state)
361 {
362         kfree(state->users);
363         kfree(state->groups);
364 }
365
366 static void parse_dacl(struct user_namespace *user_ns,
367                        struct smb_acl *pdacl, char *end_of_acl,
368                        struct smb_sid *pownersid, struct smb_sid *pgrpsid,
369                        struct smb_fattr *fattr)
370 {
371         int i, ret;
372         int num_aces = 0;
373         int acl_size;
374         char *acl_base;
375         struct smb_ace **ppace;
376         struct posix_acl_entry *cf_pace, *cf_pdace;
377         struct posix_acl_state acl_state, default_acl_state;
378         umode_t mode = 0, acl_mode;
379         bool owner_found = false, group_found = false, others_found = false;
380
381         if (!pdacl)
382                 return;
383
384         /* validate that we do not go past end of acl */
385         if (end_of_acl <= (char *)pdacl ||
386             end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
387                 pr_err("ACL too small to parse DACL\n");
388                 return;
389         }
390
391         ksmbd_debug(SMB, "DACL revision %d size %d num aces %d\n",
392                     le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
393                     le32_to_cpu(pdacl->num_aces));
394
395         acl_base = (char *)pdacl;
396         acl_size = sizeof(struct smb_acl);
397
398         num_aces = le32_to_cpu(pdacl->num_aces);
399         if (num_aces <= 0)
400                 return;
401
402         if (num_aces > ULONG_MAX / sizeof(struct smb_ace *))
403                 return;
404
405         ppace = kmalloc_array(num_aces, sizeof(struct smb_ace *), GFP_KERNEL);
406         if (!ppace)
407                 return;
408
409         ret = init_acl_state(&acl_state, num_aces);
410         if (ret)
411                 return;
412         ret = init_acl_state(&default_acl_state, num_aces);
413         if (ret) {
414                 free_acl_state(&acl_state);
415                 return;
416         }
417
418         /*
419          * reset rwx permissions for user/group/other.
420          * Also, if num_aces is 0 i.e. DACL has no ACEs,
421          * user/group/other have no permissions
422          */
423         for (i = 0; i < num_aces; ++i) {
424                 ppace[i] = (struct smb_ace *)(acl_base + acl_size);
425                 acl_base = (char *)ppace[i];
426                 acl_size = le16_to_cpu(ppace[i]->size);
427                 ppace[i]->access_req =
428                         smb_map_generic_desired_access(ppace[i]->access_req);
429
430                 if (!(compare_sids(&ppace[i]->sid, &sid_unix_NFS_mode))) {
431                         fattr->cf_mode =
432                                 le32_to_cpu(ppace[i]->sid.sub_auth[2]);
433                         break;
434                 } else if (!compare_sids(&ppace[i]->sid, pownersid)) {
435                         acl_mode = access_flags_to_mode(fattr,
436                                                         ppace[i]->access_req,
437                                                         ppace[i]->type);
438                         acl_mode &= 0700;
439
440                         if (!owner_found) {
441                                 mode &= ~(0700);
442                                 mode |= acl_mode;
443                         }
444                         owner_found = true;
445                 } else if (!compare_sids(&ppace[i]->sid, pgrpsid) ||
446                            ppace[i]->sid.sub_auth[ppace[i]->sid.num_subauth - 1] ==
447                             DOMAIN_USER_RID_LE) {
448                         acl_mode = access_flags_to_mode(fattr,
449                                                         ppace[i]->access_req,
450                                                         ppace[i]->type);
451                         acl_mode &= 0070;
452                         if (!group_found) {
453                                 mode &= ~(0070);
454                                 mode |= acl_mode;
455                         }
456                         group_found = true;
457                 } else if (!compare_sids(&ppace[i]->sid, &sid_everyone)) {
458                         acl_mode = access_flags_to_mode(fattr,
459                                                         ppace[i]->access_req,
460                                                         ppace[i]->type);
461                         acl_mode &= 0007;
462                         if (!others_found) {
463                                 mode &= ~(0007);
464                                 mode |= acl_mode;
465                         }
466                         others_found = true;
467                 } else if (!compare_sids(&ppace[i]->sid, &creator_owner)) {
468                         continue;
469                 } else if (!compare_sids(&ppace[i]->sid, &creator_group)) {
470                         continue;
471                 } else if (!compare_sids(&ppace[i]->sid, &sid_authusers)) {
472                         continue;
473                 } else {
474                         struct smb_fattr temp_fattr;
475
476                         acl_mode = access_flags_to_mode(fattr, ppace[i]->access_req,
477                                                         ppace[i]->type);
478                         temp_fattr.cf_uid = INVALID_UID;
479                         ret = sid_to_id(user_ns, &ppace[i]->sid, SIDOWNER, &temp_fattr);
480                         if (ret || uid_eq(temp_fattr.cf_uid, INVALID_UID)) {
481                                 pr_err("%s: Error %d mapping Owner SID to uid\n",
482                                        __func__, ret);
483                                 continue;
484                         }
485
486                         acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
487                         acl_state.users->aces[acl_state.users->n].uid =
488                                 temp_fattr.cf_uid;
489                         acl_state.users->aces[acl_state.users->n++].perms.allow =
490                                 ((acl_mode & 0700) >> 6) | 0004;
491                         default_acl_state.owner.allow = ((acl_mode & 0700) >> 6) | 0004;
492                         default_acl_state.users->aces[default_acl_state.users->n].uid =
493                                 temp_fattr.cf_uid;
494                         default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
495                                 ((acl_mode & 0700) >> 6) | 0004;
496                 }
497         }
498         kfree(ppace);
499
500         if (owner_found) {
501                 /* The owner must be set to at least read-only. */
502                 acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
503                 acl_state.users->aces[acl_state.users->n].uid = fattr->cf_uid;
504                 acl_state.users->aces[acl_state.users->n++].perms.allow =
505                         ((mode & 0700) >> 6) | 0004;
506                 default_acl_state.owner.allow = ((mode & 0700) >> 6) | 0004;
507                 default_acl_state.users->aces[default_acl_state.users->n].uid =
508                         fattr->cf_uid;
509                 default_acl_state.users->aces[default_acl_state.users->n++].perms.allow =
510                         ((mode & 0700) >> 6) | 0004;
511         }
512
513         if (group_found) {
514                 acl_state.group.allow = (mode & 0070) >> 3;
515                 acl_state.groups->aces[acl_state.groups->n].gid =
516                         fattr->cf_gid;
517                 acl_state.groups->aces[acl_state.groups->n++].perms.allow =
518                         (mode & 0070) >> 3;
519                 default_acl_state.group.allow = (mode & 0070) >> 3;
520                 default_acl_state.groups->aces[default_acl_state.groups->n].gid =
521                         fattr->cf_gid;
522                 default_acl_state.groups->aces[default_acl_state.groups->n++].perms.allow =
523                         (mode & 0070) >> 3;
524         }
525
526         if (others_found) {
527                 fattr->cf_mode &= ~(0007);
528                 fattr->cf_mode |= mode & 0007;
529
530                 acl_state.other.allow = mode & 0007;
531                 default_acl_state.other.allow = mode & 0007;
532         }
533
534         if (acl_state.users->n || acl_state.groups->n) {
535                 acl_state.mask.allow = 0x07;
536
537                 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
538                         fattr->cf_acls =
539                                 posix_acl_alloc(acl_state.users->n +
540                                         acl_state.groups->n + 4, GFP_KERNEL);
541                         if (fattr->cf_acls) {
542                                 cf_pace = fattr->cf_acls->a_entries;
543                                 posix_state_to_acl(&acl_state, cf_pace);
544                         }
545                 }
546         }
547
548         if (default_acl_state.users->n || default_acl_state.groups->n) {
549                 default_acl_state.mask.allow = 0x07;
550
551                 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
552                         fattr->cf_dacls =
553                                 posix_acl_alloc(default_acl_state.users->n +
554                                 default_acl_state.groups->n + 4, GFP_KERNEL);
555                         if (fattr->cf_dacls) {
556                                 cf_pdace = fattr->cf_dacls->a_entries;
557                                 posix_state_to_acl(&default_acl_state, cf_pdace);
558                         }
559                 }
560         }
561         free_acl_state(&acl_state);
562         free_acl_state(&default_acl_state);
563 }
564
565 static void set_posix_acl_entries_dacl(struct user_namespace *user_ns,
566                                        struct smb_ace *pndace,
567                                        struct smb_fattr *fattr, u32 *num_aces,
568                                        u16 *size, u32 nt_aces_num)
569 {
570         struct posix_acl_entry *pace;
571         struct smb_sid *sid;
572         struct smb_ace *ntace;
573         int i, j;
574
575         if (!fattr->cf_acls)
576                 goto posix_default_acl;
577
578         pace = fattr->cf_acls->a_entries;
579         for (i = 0; i < fattr->cf_acls->a_count; i++, pace++) {
580                 int flags = 0;
581
582                 sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
583                 if (!sid)
584                         break;
585
586                 if (pace->e_tag == ACL_USER) {
587                         uid_t uid;
588                         unsigned int sid_type = SIDOWNER;
589
590                         uid = from_kuid(user_ns, pace->e_uid);
591                         if (!uid)
592                                 sid_type = SIDUNIX_USER;
593                         id_to_sid(uid, sid_type, sid);
594                 } else if (pace->e_tag == ACL_GROUP) {
595                         gid_t gid;
596
597                         gid = from_kgid(user_ns, pace->e_gid);
598                         id_to_sid(gid, SIDUNIX_GROUP, sid);
599                 } else if (pace->e_tag == ACL_OTHER && !nt_aces_num) {
600                         smb_copy_sid(sid, &sid_everyone);
601                 } else {
602                         kfree(sid);
603                         continue;
604                 }
605                 ntace = pndace;
606                 for (j = 0; j < nt_aces_num; j++) {
607                         if (ntace->sid.sub_auth[ntace->sid.num_subauth - 1] ==
608                                         sid->sub_auth[sid->num_subauth - 1])
609                                 goto pass_same_sid;
610                         ntace = (struct smb_ace *)((char *)ntace +
611                                         le16_to_cpu(ntace->size));
612                 }
613
614                 if (S_ISDIR(fattr->cf_mode) && pace->e_tag == ACL_OTHER)
615                         flags = 0x03;
616
617                 ntace = (struct smb_ace *)((char *)pndace + *size);
618                 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, flags,
619                                 pace->e_perm, 0777);
620                 (*num_aces)++;
621                 if (pace->e_tag == ACL_USER)
622                         ntace->access_req |=
623                                 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
624
625                 if (S_ISDIR(fattr->cf_mode) &&
626                     (pace->e_tag == ACL_USER || pace->e_tag == ACL_GROUP)) {
627                         ntace = (struct smb_ace *)((char *)pndace + *size);
628                         *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED,
629                                         0x03, 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
636 pass_same_sid:
637                 kfree(sid);
638         }
639
640         if (nt_aces_num)
641                 return;
642
643 posix_default_acl:
644         if (!fattr->cf_dacls)
645                 return;
646
647         pace = fattr->cf_dacls->a_entries;
648         for (i = 0; i < fattr->cf_dacls->a_count; i++, pace++) {
649                 sid = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
650                 if (!sid)
651                         break;
652
653                 if (pace->e_tag == ACL_USER) {
654                         uid_t uid;
655
656                         uid = from_kuid(user_ns, pace->e_uid);
657                         id_to_sid(uid, SIDCREATOR_OWNER, sid);
658                 } else if (pace->e_tag == ACL_GROUP) {
659                         gid_t gid;
660
661                         gid = from_kgid(user_ns, pace->e_gid);
662                         id_to_sid(gid, SIDCREATOR_GROUP, sid);
663                 } else {
664                         kfree(sid);
665                         continue;
666                 }
667
668                 ntace = (struct smb_ace *)((char *)pndace + *size);
669                 *size += fill_ace_for_sid(ntace, sid, ACCESS_ALLOWED, 0x0b,
670                                 pace->e_perm, 0777);
671                 (*num_aces)++;
672                 if (pace->e_tag == ACL_USER)
673                         ntace->access_req |=
674                                 FILE_DELETE_LE | FILE_DELETE_CHILD_LE;
675                 kfree(sid);
676         }
677 }
678
679 static void set_ntacl_dacl(struct user_namespace *user_ns,
680                            struct smb_acl *pndacl,
681                            struct smb_acl *nt_dacl,
682                            const struct smb_sid *pownersid,
683                            const struct smb_sid *pgrpsid,
684                            struct smb_fattr *fattr)
685 {
686         struct smb_ace *ntace, *pndace;
687         int nt_num_aces = le32_to_cpu(nt_dacl->num_aces), num_aces = 0;
688         unsigned short size = 0;
689         int i;
690
691         pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
692         if (nt_num_aces) {
693                 ntace = (struct smb_ace *)((char *)nt_dacl + sizeof(struct smb_acl));
694                 for (i = 0; i < nt_num_aces; i++) {
695                         memcpy((char *)pndace + size, ntace, le16_to_cpu(ntace->size));
696                         size += le16_to_cpu(ntace->size);
697                         ntace = (struct smb_ace *)((char *)ntace + le16_to_cpu(ntace->size));
698                         num_aces++;
699                 }
700         }
701
702         set_posix_acl_entries_dacl(user_ns, pndace, fattr,
703                                    &num_aces, &size, nt_num_aces);
704         pndacl->num_aces = cpu_to_le32(num_aces);
705         pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
706 }
707
708 static void set_mode_dacl(struct user_namespace *user_ns,
709                           struct smb_acl *pndacl, struct smb_fattr *fattr)
710 {
711         struct smb_ace *pace, *pndace;
712         u32 num_aces = 0;
713         u16 size = 0, ace_size = 0;
714         uid_t uid;
715         const struct smb_sid *sid;
716
717         pace = pndace = (struct smb_ace *)((char *)pndacl + sizeof(struct smb_acl));
718
719         if (fattr->cf_acls) {
720                 set_posix_acl_entries_dacl(user_ns, pndace, fattr,
721                                            &num_aces, &size, num_aces);
722                 goto out;
723         }
724
725         /* owner RID */
726         uid = from_kuid(user_ns, fattr->cf_uid);
727         if (uid)
728                 sid = &server_conf.domain_sid;
729         else
730                 sid = &sid_unix_users;
731         ace_size = fill_ace_for_sid(pace, sid, ACCESS_ALLOWED, 0,
732                                     fattr->cf_mode, 0700);
733         pace->sid.sub_auth[pace->sid.num_subauth++] = cpu_to_le32(uid);
734         pace->size = cpu_to_le16(ace_size + 4);
735         size += le16_to_cpu(pace->size);
736         pace = (struct smb_ace *)((char *)pndace + size);
737
738         /* Group RID */
739         ace_size = fill_ace_for_sid(pace, &sid_unix_groups,
740                                     ACCESS_ALLOWED, 0, fattr->cf_mode, 0070);
741         pace->sid.sub_auth[pace->sid.num_subauth++] =
742                 cpu_to_le32(from_kgid(user_ns, fattr->cf_gid));
743         pace->size = cpu_to_le16(ace_size + 4);
744         size += le16_to_cpu(pace->size);
745         pace = (struct smb_ace *)((char *)pndace + size);
746         num_aces = 3;
747
748         if (S_ISDIR(fattr->cf_mode)) {
749                 pace = (struct smb_ace *)((char *)pndace + size);
750
751                 /* creator owner */
752                 size += fill_ace_for_sid(pace, &creator_owner, ACCESS_ALLOWED,
753                                          0x0b, fattr->cf_mode, 0700);
754                 pace = (struct smb_ace *)((char *)pndace + size);
755
756                 /* creator group */
757                 size += fill_ace_for_sid(pace, &creator_group, ACCESS_ALLOWED,
758                                          0x0b, fattr->cf_mode, 0070);
759                 pace = (struct smb_ace *)((char *)pndace + size);
760                 num_aces = 5;
761         }
762
763         /* other */
764         size += fill_ace_for_sid(pace, &sid_everyone, ACCESS_ALLOWED, 0,
765                                  fattr->cf_mode, 0007);
766
767 out:
768         pndacl->num_aces = cpu_to_le32(num_aces);
769         pndacl->size = cpu_to_le16(le16_to_cpu(pndacl->size) + size);
770 }
771
772 static int parse_sid(struct smb_sid *psid, char *end_of_acl)
773 {
774         /*
775          * validate that we do not go past end of ACL - sid must be at least 8
776          * bytes long (assuming no sub-auths - e.g. the null SID
777          */
778         if (end_of_acl < (char *)psid + 8) {
779                 pr_err("ACL too small to parse SID %p\n", psid);
780                 return -EINVAL;
781         }
782
783         return 0;
784 }
785
786 /* Convert CIFS ACL to POSIX form */
787 int parse_sec_desc(struct user_namespace *user_ns, struct smb_ntsd *pntsd,
788                    int acl_len, struct smb_fattr *fattr)
789 {
790         int rc = 0;
791         struct smb_sid *owner_sid_ptr, *group_sid_ptr;
792         struct smb_acl *dacl_ptr; /* no need for SACL ptr */
793         char *end_of_acl = ((char *)pntsd) + acl_len;
794         __u32 dacloffset;
795         int pntsd_type;
796
797         if (!pntsd)
798                 return -EIO;
799
800         owner_sid_ptr = (struct smb_sid *)((char *)pntsd +
801                         le32_to_cpu(pntsd->osidoffset));
802         group_sid_ptr = (struct smb_sid *)((char *)pntsd +
803                         le32_to_cpu(pntsd->gsidoffset));
804         dacloffset = le32_to_cpu(pntsd->dacloffset);
805         dacl_ptr = (struct smb_acl *)((char *)pntsd + dacloffset);
806         ksmbd_debug(SMB,
807                     "revision %d type 0x%x ooffset 0x%x goffset 0x%x sacloffset 0x%x dacloffset 0x%x\n",
808                     pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
809                     le32_to_cpu(pntsd->gsidoffset),
810                     le32_to_cpu(pntsd->sacloffset), dacloffset);
811
812         pntsd_type = le16_to_cpu(pntsd->type);
813         if (!(pntsd_type & DACL_PRESENT)) {
814                 ksmbd_debug(SMB, "DACL_PRESENT in DACL type is not set\n");
815                 return rc;
816         }
817
818         pntsd->type = cpu_to_le16(DACL_PRESENT);
819
820         if (pntsd->osidoffset) {
821                 rc = parse_sid(owner_sid_ptr, end_of_acl);
822                 if (rc) {
823                         pr_err("%s: Error %d parsing Owner SID\n", __func__, rc);
824                         return rc;
825                 }
826
827                 rc = sid_to_id(user_ns, owner_sid_ptr, SIDOWNER, fattr);
828                 if (rc) {
829                         pr_err("%s: Error %d mapping Owner SID to uid\n",
830                                __func__, rc);
831                         owner_sid_ptr = NULL;
832                 }
833         }
834
835         if (pntsd->gsidoffset) {
836                 rc = parse_sid(group_sid_ptr, end_of_acl);
837                 if (rc) {
838                         pr_err("%s: Error %d mapping Owner SID to gid\n",
839                                __func__, rc);
840                         return rc;
841                 }
842                 rc = sid_to_id(user_ns, group_sid_ptr, SIDUNIX_GROUP, fattr);
843                 if (rc) {
844                         pr_err("%s: Error %d mapping Group SID to gid\n",
845                                __func__, rc);
846                         group_sid_ptr = NULL;
847                 }
848         }
849
850         if ((pntsd_type & (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ)) ==
851             (DACL_AUTO_INHERITED | DACL_AUTO_INHERIT_REQ))
852                 pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
853         if (pntsd_type & DACL_PROTECTED)
854                 pntsd->type |= cpu_to_le16(DACL_PROTECTED);
855
856         if (dacloffset) {
857                 parse_dacl(user_ns, dacl_ptr, end_of_acl,
858                            owner_sid_ptr, group_sid_ptr, fattr);
859         }
860
861         return 0;
862 }
863
864 /* Convert permission bits from mode to equivalent CIFS ACL */
865 int build_sec_desc(struct user_namespace *user_ns,
866                    struct smb_ntsd *pntsd, struct smb_ntsd *ppntsd,
867                    int addition_info, __u32 *secdesclen,
868                    struct smb_fattr *fattr)
869 {
870         int rc = 0;
871         __u32 offset;
872         struct smb_sid *owner_sid_ptr, *group_sid_ptr;
873         struct smb_sid *nowner_sid_ptr, *ngroup_sid_ptr;
874         struct smb_acl *dacl_ptr = NULL; /* no need for SACL ptr */
875         uid_t uid;
876         gid_t gid;
877         unsigned int sid_type = SIDOWNER;
878
879         nowner_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
880         if (!nowner_sid_ptr)
881                 return -ENOMEM;
882
883         uid = from_kuid(user_ns, fattr->cf_uid);
884         if (!uid)
885                 sid_type = SIDUNIX_USER;
886         id_to_sid(uid, sid_type, nowner_sid_ptr);
887
888         ngroup_sid_ptr = kmalloc(sizeof(struct smb_sid), GFP_KERNEL);
889         if (!ngroup_sid_ptr) {
890                 kfree(nowner_sid_ptr);
891                 return -ENOMEM;
892         }
893
894         gid = from_kgid(user_ns, fattr->cf_gid);
895         id_to_sid(gid, SIDUNIX_GROUP, ngroup_sid_ptr);
896
897         offset = sizeof(struct smb_ntsd);
898         pntsd->sacloffset = 0;
899         pntsd->revision = cpu_to_le16(1);
900         pntsd->type = cpu_to_le16(SELF_RELATIVE);
901         if (ppntsd)
902                 pntsd->type |= ppntsd->type;
903
904         if (addition_info & OWNER_SECINFO) {
905                 pntsd->osidoffset = cpu_to_le32(offset);
906                 owner_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
907                 smb_copy_sid(owner_sid_ptr, nowner_sid_ptr);
908                 offset += 1 + 1 + 6 + (nowner_sid_ptr->num_subauth * 4);
909         }
910
911         if (addition_info & GROUP_SECINFO) {
912                 pntsd->gsidoffset = cpu_to_le32(offset);
913                 group_sid_ptr = (struct smb_sid *)((char *)pntsd + offset);
914                 smb_copy_sid(group_sid_ptr, ngroup_sid_ptr);
915                 offset += 1 + 1 + 6 + (ngroup_sid_ptr->num_subauth * 4);
916         }
917
918         if (addition_info & DACL_SECINFO) {
919                 pntsd->type |= cpu_to_le16(DACL_PRESENT);
920                 dacl_ptr = (struct smb_acl *)((char *)pntsd + offset);
921                 dacl_ptr->revision = cpu_to_le16(2);
922                 dacl_ptr->size = cpu_to_le16(sizeof(struct smb_acl));
923                 dacl_ptr->num_aces = 0;
924
925                 if (!ppntsd) {
926                         set_mode_dacl(user_ns, dacl_ptr, fattr);
927                 } else if (!ppntsd->dacloffset) {
928                         goto out;
929                 } else {
930                         struct smb_acl *ppdacl_ptr;
931
932                         ppdacl_ptr = (struct smb_acl *)((char *)ppntsd +
933                                                 le32_to_cpu(ppntsd->dacloffset));
934                         set_ntacl_dacl(user_ns, dacl_ptr, ppdacl_ptr,
935                                        nowner_sid_ptr, ngroup_sid_ptr, fattr);
936                 }
937                 pntsd->dacloffset = cpu_to_le32(offset);
938                 offset += le16_to_cpu(dacl_ptr->size);
939         }
940
941 out:
942         kfree(nowner_sid_ptr);
943         kfree(ngroup_sid_ptr);
944         *secdesclen = offset;
945         return rc;
946 }
947
948 static void smb_set_ace(struct smb_ace *ace, const struct smb_sid *sid, u8 type,
949                         u8 flags, __le32 access_req)
950 {
951         ace->type = type;
952         ace->flags = flags;
953         ace->access_req = access_req;
954         smb_copy_sid(&ace->sid, sid);
955         ace->size = cpu_to_le16(1 + 1 + 2 + 4 + 1 + 1 + 6 + (sid->num_subauth * 4));
956 }
957
958 int smb_inherit_dacl(struct ksmbd_conn *conn,
959                      struct path *path,
960                      unsigned int uid, unsigned int gid)
961 {
962         const struct smb_sid *psid, *creator = NULL;
963         struct smb_ace *parent_aces, *aces;
964         struct smb_acl *parent_pdacl;
965         struct smb_ntsd *parent_pntsd = NULL;
966         struct smb_sid owner_sid, group_sid;
967         struct dentry *parent = path->dentry->d_parent;
968         struct user_namespace *user_ns = mnt_user_ns(path->mnt);
969         int inherited_flags = 0, flags = 0, i, ace_cnt = 0, nt_size = 0;
970         int rc = 0, num_aces, dacloffset, pntsd_type, acl_len;
971         char *aces_base;
972         bool is_dir = S_ISDIR(d_inode(path->dentry)->i_mode);
973
974         acl_len = ksmbd_vfs_get_sd_xattr(conn, user_ns,
975                                          parent, &parent_pntsd);
976         if (acl_len <= 0)
977                 return -ENOENT;
978         dacloffset = le32_to_cpu(parent_pntsd->dacloffset);
979         if (!dacloffset) {
980                 rc = -EINVAL;
981                 goto free_parent_pntsd;
982         }
983
984         parent_pdacl = (struct smb_acl *)((char *)parent_pntsd + dacloffset);
985         num_aces = le32_to_cpu(parent_pdacl->num_aces);
986         pntsd_type = le16_to_cpu(parent_pntsd->type);
987
988         aces_base = kmalloc(sizeof(struct smb_ace) * num_aces * 2, GFP_KERNEL);
989         if (!aces_base) {
990                 rc = -ENOMEM;
991                 goto free_parent_pntsd;
992         }
993
994         aces = (struct smb_ace *)aces_base;
995         parent_aces = (struct smb_ace *)((char *)parent_pdacl +
996                         sizeof(struct smb_acl));
997
998         if (pntsd_type & DACL_AUTO_INHERITED)
999                 inherited_flags = INHERITED_ACE;
1000
1001         for (i = 0; i < num_aces; i++) {
1002                 flags = parent_aces->flags;
1003                 if (!smb_inherit_flags(flags, is_dir))
1004                         goto pass;
1005                 if (is_dir) {
1006                         flags &= ~(INHERIT_ONLY_ACE | INHERITED_ACE);
1007                         if (!(flags & CONTAINER_INHERIT_ACE))
1008                                 flags |= INHERIT_ONLY_ACE;
1009                         if (flags & NO_PROPAGATE_INHERIT_ACE)
1010                                 flags = 0;
1011                 } else {
1012                         flags = 0;
1013                 }
1014
1015                 if (!compare_sids(&creator_owner, &parent_aces->sid)) {
1016                         creator = &creator_owner;
1017                         id_to_sid(uid, SIDOWNER, &owner_sid);
1018                         psid = &owner_sid;
1019                 } else if (!compare_sids(&creator_group, &parent_aces->sid)) {
1020                         creator = &creator_group;
1021                         id_to_sid(gid, SIDUNIX_GROUP, &group_sid);
1022                         psid = &group_sid;
1023                 } else {
1024                         creator = NULL;
1025                         psid = &parent_aces->sid;
1026                 }
1027
1028                 if (is_dir && creator && flags & CONTAINER_INHERIT_ACE) {
1029                         smb_set_ace(aces, psid, parent_aces->type, inherited_flags,
1030                                     parent_aces->access_req);
1031                         nt_size += le16_to_cpu(aces->size);
1032                         ace_cnt++;
1033                         aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
1034                         flags |= INHERIT_ONLY_ACE;
1035                         psid = creator;
1036                 } else if (is_dir && !(parent_aces->flags & NO_PROPAGATE_INHERIT_ACE)) {
1037                         psid = &parent_aces->sid;
1038                 }
1039
1040                 smb_set_ace(aces, psid, parent_aces->type, flags | inherited_flags,
1041                             parent_aces->access_req);
1042                 nt_size += le16_to_cpu(aces->size);
1043                 aces = (struct smb_ace *)((char *)aces + le16_to_cpu(aces->size));
1044                 ace_cnt++;
1045 pass:
1046                 parent_aces =
1047                         (struct smb_ace *)((char *)parent_aces + le16_to_cpu(parent_aces->size));
1048         }
1049
1050         if (nt_size > 0) {
1051                 struct smb_ntsd *pntsd;
1052                 struct smb_acl *pdacl;
1053                 struct smb_sid *powner_sid = NULL, *pgroup_sid = NULL;
1054                 int powner_sid_size = 0, pgroup_sid_size = 0, pntsd_size;
1055
1056                 if (parent_pntsd->osidoffset) {
1057                         powner_sid = (struct smb_sid *)((char *)parent_pntsd +
1058                                         le32_to_cpu(parent_pntsd->osidoffset));
1059                         powner_sid_size = 1 + 1 + 6 + (powner_sid->num_subauth * 4);
1060                 }
1061                 if (parent_pntsd->gsidoffset) {
1062                         pgroup_sid = (struct smb_sid *)((char *)parent_pntsd +
1063                                         le32_to_cpu(parent_pntsd->gsidoffset));
1064                         pgroup_sid_size = 1 + 1 + 6 + (pgroup_sid->num_subauth * 4);
1065                 }
1066
1067                 pntsd = kzalloc(sizeof(struct smb_ntsd) + powner_sid_size +
1068                                 pgroup_sid_size + sizeof(struct smb_acl) +
1069                                 nt_size, GFP_KERNEL);
1070                 if (!pntsd) {
1071                         rc = -ENOMEM;
1072                         goto free_aces_base;
1073                 }
1074
1075                 pntsd->revision = cpu_to_le16(1);
1076                 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PRESENT);
1077                 if (le16_to_cpu(parent_pntsd->type) & DACL_AUTO_INHERITED)
1078                         pntsd->type |= cpu_to_le16(DACL_AUTO_INHERITED);
1079                 pntsd_size = sizeof(struct smb_ntsd);
1080                 pntsd->osidoffset = parent_pntsd->osidoffset;
1081                 pntsd->gsidoffset = parent_pntsd->gsidoffset;
1082                 pntsd->dacloffset = parent_pntsd->dacloffset;
1083
1084                 if (pntsd->osidoffset) {
1085                         struct smb_sid *owner_sid = (struct smb_sid *)((char *)pntsd +
1086                                         le32_to_cpu(pntsd->osidoffset));
1087                         memcpy(owner_sid, powner_sid, powner_sid_size);
1088                         pntsd_size += powner_sid_size;
1089                 }
1090
1091                 if (pntsd->gsidoffset) {
1092                         struct smb_sid *group_sid = (struct smb_sid *)((char *)pntsd +
1093                                         le32_to_cpu(pntsd->gsidoffset));
1094                         memcpy(group_sid, pgroup_sid, pgroup_sid_size);
1095                         pntsd_size += pgroup_sid_size;
1096                 }
1097
1098                 if (pntsd->dacloffset) {
1099                         struct smb_ace *pace;
1100
1101                         pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1102                         pdacl->revision = cpu_to_le16(2);
1103                         pdacl->size = cpu_to_le16(sizeof(struct smb_acl) + nt_size);
1104                         pdacl->num_aces = cpu_to_le32(ace_cnt);
1105                         pace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1106                         memcpy(pace, aces_base, nt_size);
1107                         pntsd_size += sizeof(struct smb_acl) + nt_size;
1108                 }
1109
1110                 ksmbd_vfs_set_sd_xattr(conn, user_ns,
1111                                        path->dentry, pntsd, pntsd_size);
1112                 kfree(pntsd);
1113         }
1114
1115 free_aces_base:
1116         kfree(aces_base);
1117 free_parent_pntsd:
1118         kfree(parent_pntsd);
1119         return rc;
1120 }
1121
1122 bool smb_inherit_flags(int flags, bool is_dir)
1123 {
1124         if (!is_dir)
1125                 return (flags & OBJECT_INHERIT_ACE) != 0;
1126
1127         if (flags & OBJECT_INHERIT_ACE && !(flags & NO_PROPAGATE_INHERIT_ACE))
1128                 return true;
1129
1130         if (flags & CONTAINER_INHERIT_ACE)
1131                 return true;
1132         return false;
1133 }
1134
1135 int smb_check_perm_dacl(struct ksmbd_conn *conn, struct path *path,
1136                         __le32 *pdaccess, int uid)
1137 {
1138         struct user_namespace *user_ns = mnt_user_ns(path->mnt);
1139         struct smb_ntsd *pntsd = NULL;
1140         struct smb_acl *pdacl;
1141         struct posix_acl *posix_acls;
1142         int rc = 0, acl_size;
1143         struct smb_sid sid;
1144         int granted = le32_to_cpu(*pdaccess & ~FILE_MAXIMAL_ACCESS_LE);
1145         struct smb_ace *ace;
1146         int i, found = 0;
1147         unsigned int access_bits = 0;
1148         struct smb_ace *others_ace = NULL;
1149         struct posix_acl_entry *pa_entry;
1150         unsigned int sid_type = SIDOWNER;
1151         char *end_of_acl;
1152
1153         ksmbd_debug(SMB, "check permission using windows acl\n");
1154         acl_size = ksmbd_vfs_get_sd_xattr(conn, user_ns,
1155                                           path->dentry, &pntsd);
1156         if (acl_size <= 0 || !pntsd || !pntsd->dacloffset) {
1157                 kfree(pntsd);
1158                 return 0;
1159         }
1160
1161         pdacl = (struct smb_acl *)((char *)pntsd + le32_to_cpu(pntsd->dacloffset));
1162         end_of_acl = ((char *)pntsd) + acl_size;
1163         if (end_of_acl <= (char *)pdacl) {
1164                 kfree(pntsd);
1165                 return 0;
1166         }
1167
1168         if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size) ||
1169             le16_to_cpu(pdacl->size) < sizeof(struct smb_acl)) {
1170                 kfree(pntsd);
1171                 return 0;
1172         }
1173
1174         if (!pdacl->num_aces) {
1175                 if (!(le16_to_cpu(pdacl->size) - sizeof(struct smb_acl)) &&
1176                     *pdaccess & ~(FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE)) {
1177                         rc = -EACCES;
1178                         goto err_out;
1179                 }
1180                 kfree(pntsd);
1181                 return 0;
1182         }
1183
1184         if (*pdaccess & FILE_MAXIMAL_ACCESS_LE) {
1185                 granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
1186                         DELETE;
1187
1188                 ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1189                 for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1190                         granted |= le32_to_cpu(ace->access_req);
1191                         ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1192                         if (end_of_acl < (char *)ace)
1193                                 goto err_out;
1194                 }
1195
1196                 if (!pdacl->num_aces)
1197                         granted = GENERIC_ALL_FLAGS;
1198         }
1199
1200         if (!uid)
1201                 sid_type = SIDUNIX_USER;
1202         id_to_sid(uid, sid_type, &sid);
1203
1204         ace = (struct smb_ace *)((char *)pdacl + sizeof(struct smb_acl));
1205         for (i = 0; i < le32_to_cpu(pdacl->num_aces); i++) {
1206                 if (!compare_sids(&sid, &ace->sid) ||
1207                     !compare_sids(&sid_unix_NFS_mode, &ace->sid)) {
1208                         found = 1;
1209                         break;
1210                 }
1211                 if (!compare_sids(&sid_everyone, &ace->sid))
1212                         others_ace = ace;
1213
1214                 ace = (struct smb_ace *)((char *)ace + le16_to_cpu(ace->size));
1215                 if (end_of_acl < (char *)ace)
1216                         goto err_out;
1217         }
1218
1219         if (*pdaccess & FILE_MAXIMAL_ACCESS_LE && found) {
1220                 granted = READ_CONTROL | WRITE_DAC | FILE_READ_ATTRIBUTES |
1221                         DELETE;
1222
1223                 granted |= le32_to_cpu(ace->access_req);
1224
1225                 if (!pdacl->num_aces)
1226                         granted = GENERIC_ALL_FLAGS;
1227         }
1228
1229         if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
1230                 posix_acls = get_acl(d_inode(path->dentry), ACL_TYPE_ACCESS);
1231                 if (posix_acls && !found) {
1232                         unsigned int id = -1;
1233
1234                         pa_entry = posix_acls->a_entries;
1235                         for (i = 0; i < posix_acls->a_count; i++, pa_entry++) {
1236                                 if (pa_entry->e_tag == ACL_USER)
1237                                         id = from_kuid(user_ns,
1238                                                        pa_entry->e_uid);
1239                                 else if (pa_entry->e_tag == ACL_GROUP)
1240                                         id = from_kgid(user_ns,
1241                                                        pa_entry->e_gid);
1242                                 else
1243                                         continue;
1244
1245                                 if (id == uid) {
1246                                         mode_to_access_flags(pa_entry->e_perm,
1247                                                              0777,
1248                                                              &access_bits);
1249                                         if (!access_bits)
1250                                                 access_bits =
1251                                                         SET_MINIMUM_RIGHTS;
1252                                         goto check_access_bits;
1253                                 }
1254                         }
1255                 }
1256                 if (posix_acls)
1257                         posix_acl_release(posix_acls);
1258         }
1259
1260         if (!found) {
1261                 if (others_ace) {
1262                         ace = others_ace;
1263                 } else {
1264                         ksmbd_debug(SMB, "Can't find corresponding sid\n");
1265                         rc = -EACCES;
1266                         goto err_out;
1267                 }
1268         }
1269
1270         switch (ace->type) {
1271         case ACCESS_ALLOWED_ACE_TYPE:
1272                 access_bits = le32_to_cpu(ace->access_req);
1273                 break;
1274         case ACCESS_DENIED_ACE_TYPE:
1275         case ACCESS_DENIED_CALLBACK_ACE_TYPE:
1276                 access_bits = le32_to_cpu(~ace->access_req);
1277                 break;
1278         }
1279
1280 check_access_bits:
1281         if (granted &
1282             ~(access_bits | FILE_READ_ATTRIBUTES | READ_CONTROL | WRITE_DAC | DELETE)) {
1283                 ksmbd_debug(SMB, "Access denied with winACL, granted : %x, access_req : %x\n",
1284                             granted, le32_to_cpu(ace->access_req));
1285                 rc = -EACCES;
1286                 goto err_out;
1287         }
1288
1289         *pdaccess = cpu_to_le32(granted);
1290 err_out:
1291         kfree(pntsd);
1292         return rc;
1293 }
1294
1295 int set_info_sec(struct ksmbd_conn *conn, struct ksmbd_tree_connect *tcon,
1296                  struct path *path, struct smb_ntsd *pntsd, int ntsd_len,
1297                  bool type_check)
1298 {
1299         int rc;
1300         struct smb_fattr fattr = {{0}};
1301         struct inode *inode = d_inode(path->dentry);
1302         struct user_namespace *user_ns = mnt_user_ns(path->mnt);
1303         struct iattr newattrs;
1304
1305         fattr.cf_uid = INVALID_UID;
1306         fattr.cf_gid = INVALID_GID;
1307         fattr.cf_mode = inode->i_mode;
1308
1309         rc = parse_sec_desc(user_ns, pntsd, ntsd_len, &fattr);
1310         if (rc)
1311                 goto out;
1312
1313         newattrs.ia_valid = ATTR_CTIME;
1314         if (!uid_eq(fattr.cf_uid, INVALID_UID)) {
1315                 newattrs.ia_valid |= ATTR_UID;
1316                 newattrs.ia_uid = fattr.cf_uid;
1317         }
1318         if (!gid_eq(fattr.cf_gid, INVALID_GID)) {
1319                 newattrs.ia_valid |= ATTR_GID;
1320                 newattrs.ia_gid = fattr.cf_gid;
1321         }
1322         newattrs.ia_valid |= ATTR_MODE;
1323         newattrs.ia_mode = (inode->i_mode & ~0777) | (fattr.cf_mode & 0777);
1324
1325         inode_lock(inode);
1326         rc = notify_change(user_ns, path->dentry, &newattrs, NULL);
1327         inode_unlock(inode);
1328         if (rc)
1329                 goto out;
1330
1331         ksmbd_vfs_remove_acl_xattrs(user_ns, path->dentry);
1332         /* Update posix acls */
1333         if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && fattr.cf_dacls) {
1334                 rc = set_posix_acl(user_ns, inode,
1335                                    ACL_TYPE_ACCESS, fattr.cf_acls);
1336                 if (S_ISDIR(inode->i_mode) && fattr.cf_dacls)
1337                         rc = set_posix_acl(user_ns, inode,
1338                                            ACL_TYPE_DEFAULT, fattr.cf_dacls);
1339         }
1340
1341         /* Check it only calling from SD BUFFER context */
1342         if (type_check && !(le16_to_cpu(pntsd->type) & DACL_PRESENT))
1343                 goto out;
1344
1345         if (test_share_config_flag(tcon->share_conf, KSMBD_SHARE_FLAG_ACL_XATTR)) {
1346                 /* Update WinACL in xattr */
1347                 ksmbd_vfs_remove_sd_xattrs(user_ns, path->dentry);
1348                 ksmbd_vfs_set_sd_xattr(conn, user_ns,
1349                                        path->dentry, pntsd, ntsd_len);
1350         }
1351
1352 out:
1353         posix_acl_release(fattr.cf_acls);
1354         posix_acl_release(fattr.cf_dacls);
1355         mark_inode_dirty(inode);
1356         return rc;
1357 }
1358
1359 void ksmbd_init_domain(u32 *sub_auth)
1360 {
1361         int i;
1362
1363         memcpy(&server_conf.domain_sid, &domain, sizeof(struct smb_sid));
1364         for (i = 0; i < 3; ++i)
1365                 server_conf.domain_sid.sub_auth[i + 1] = cpu_to_le32(sub_auth[i]);
1366 }