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