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