fs/ntfs3: Fix a memory leak on object opts
[platform/kernel/linux-starfive.git] / fs / ntfs3 / xattr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7
8 #include <linux/fs.h>
9 #include <linux/posix_acl.h>
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/xattr.h>
12
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16
17 // clang-format off
18 #define SYSTEM_DOS_ATTRIB    "system.dos_attrib"
19 #define SYSTEM_NTFS_ATTRIB   "system.ntfs_attrib"
20 #define SYSTEM_NTFS_SECURITY "system.ntfs_security"
21 // clang-format on
22
23 static inline size_t unpacked_ea_size(const struct EA_FULL *ea)
24 {
25         return ea->size ? le32_to_cpu(ea->size)
26                         : ALIGN(struct_size(ea, name,
27                                             1 + ea->name_len +
28                                                     le16_to_cpu(ea->elength)),
29                                 4);
30 }
31
32 static inline size_t packed_ea_size(const struct EA_FULL *ea)
33 {
34         return struct_size(ea, name,
35                            1 + ea->name_len + le16_to_cpu(ea->elength)) -
36                offsetof(struct EA_FULL, flags);
37 }
38
39 /*
40  * find_ea
41  *
42  * Assume there is at least one xattr in the list.
43  */
44 static inline bool find_ea(const struct EA_FULL *ea_all, u32 bytes,
45                            const char *name, u8 name_len, u32 *off)
46 {
47         *off = 0;
48
49         if (!ea_all || !bytes)
50                 return false;
51
52         for (;;) {
53                 const struct EA_FULL *ea = Add2Ptr(ea_all, *off);
54                 u32 next_off = *off + unpacked_ea_size(ea);
55
56                 if (next_off > bytes)
57                         return false;
58
59                 if (ea->name_len == name_len &&
60                     !memcmp(ea->name, name, name_len))
61                         return true;
62
63                 *off = next_off;
64                 if (next_off >= bytes)
65                         return false;
66         }
67 }
68
69 /*
70  * ntfs_read_ea - Read all extended attributes.
71  * @ea:         New allocated memory.
72  * @info:       Pointer into resident data.
73  */
74 static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
75                         size_t add_bytes, const struct EA_INFO **info)
76 {
77         int err;
78         struct ATTR_LIST_ENTRY *le = NULL;
79         struct ATTRIB *attr_info, *attr_ea;
80         void *ea_p;
81         u32 size;
82
83         static_assert(le32_to_cpu(ATTR_EA_INFO) < le32_to_cpu(ATTR_EA));
84
85         *ea = NULL;
86         *info = NULL;
87
88         attr_info =
89                 ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, NULL);
90         attr_ea =
91                 ni_find_attr(ni, attr_info, &le, ATTR_EA, NULL, 0, NULL, NULL);
92
93         if (!attr_ea || !attr_info)
94                 return 0;
95
96         *info = resident_data_ex(attr_info, sizeof(struct EA_INFO));
97         if (!*info)
98                 return -EINVAL;
99
100         /* Check Ea limit. */
101         size = le32_to_cpu((*info)->size);
102         if (size > ni->mi.sbi->ea_max_size)
103                 return -EFBIG;
104
105         if (attr_size(attr_ea) > ni->mi.sbi->ea_max_size)
106                 return -EFBIG;
107
108         /* Allocate memory for packed Ea. */
109         ea_p = kmalloc(size + add_bytes, GFP_NOFS);
110         if (!ea_p)
111                 return -ENOMEM;
112
113         if (attr_ea->non_res) {
114                 struct runs_tree run;
115
116                 run_init(&run);
117
118                 err = attr_load_runs(attr_ea, ni, &run, NULL);
119                 if (!err)
120                         err = ntfs_read_run_nb(ni->mi.sbi, &run, 0, ea_p, size,
121                                                NULL);
122                 run_close(&run);
123
124                 if (err)
125                         goto out;
126         } else {
127                 void *p = resident_data_ex(attr_ea, size);
128
129                 if (!p) {
130                         err = -EINVAL;
131                         goto out;
132                 }
133                 memcpy(ea_p, p, size);
134         }
135
136         memset(Add2Ptr(ea_p, size), 0, add_bytes);
137         *ea = ea_p;
138         return 0;
139
140 out:
141         kfree(ea_p);
142         *ea = NULL;
143         return err;
144 }
145
146 /*
147  * ntfs_list_ea
148  *
149  * Copy a list of xattrs names into the buffer
150  * provided, or compute the buffer size required.
151  *
152  * Return:
153  * * Number of bytes used / required on
154  * * -ERRNO - on failure
155  */
156 static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer,
157                             size_t bytes_per_buffer)
158 {
159         const struct EA_INFO *info;
160         struct EA_FULL *ea_all = NULL;
161         const struct EA_FULL *ea;
162         u32 off, size;
163         int err;
164         size_t ret;
165
166         err = ntfs_read_ea(ni, &ea_all, 0, &info);
167         if (err)
168                 return err;
169
170         if (!info || !ea_all)
171                 return 0;
172
173         size = le32_to_cpu(info->size);
174
175         /* Enumerate all xattrs. */
176         for (ret = 0, off = 0; off < size; off += unpacked_ea_size(ea)) {
177                 ea = Add2Ptr(ea_all, off);
178
179                 if (buffer) {
180                         if (ret + ea->name_len + 1 > bytes_per_buffer) {
181                                 err = -ERANGE;
182                                 goto out;
183                         }
184
185                         memcpy(buffer + ret, ea->name, ea->name_len);
186                         buffer[ret + ea->name_len] = 0;
187                 }
188
189                 ret += ea->name_len + 1;
190         }
191
192 out:
193         kfree(ea_all);
194         return err ? err : ret;
195 }
196
197 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
198                        void *buffer, size_t size, size_t *required)
199 {
200         struct ntfs_inode *ni = ntfs_i(inode);
201         const struct EA_INFO *info;
202         struct EA_FULL *ea_all = NULL;
203         const struct EA_FULL *ea;
204         u32 off, len;
205         int err;
206
207         if (!(ni->ni_flags & NI_FLAG_EA))
208                 return -ENODATA;
209
210         if (!required)
211                 ni_lock(ni);
212
213         len = 0;
214
215         if (name_len > 255) {
216                 err = -ENAMETOOLONG;
217                 goto out;
218         }
219
220         err = ntfs_read_ea(ni, &ea_all, 0, &info);
221         if (err)
222                 goto out;
223
224         if (!info)
225                 goto out;
226
227         /* Enumerate all xattrs. */
228         if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off)) {
229                 err = -ENODATA;
230                 goto out;
231         }
232         ea = Add2Ptr(ea_all, off);
233
234         len = le16_to_cpu(ea->elength);
235         if (!buffer) {
236                 err = 0;
237                 goto out;
238         }
239
240         if (len > size) {
241                 err = -ERANGE;
242                 if (required)
243                         *required = len;
244                 goto out;
245         }
246
247         memcpy(buffer, ea->name + ea->name_len + 1, len);
248         err = 0;
249
250 out:
251         kfree(ea_all);
252         if (!required)
253                 ni_unlock(ni);
254
255         return err ? err : len;
256 }
257
258 static noinline int ntfs_set_ea(struct inode *inode, const char *name,
259                                 size_t name_len, const void *value,
260                                 size_t val_size, int flags, int locked)
261 {
262         struct ntfs_inode *ni = ntfs_i(inode);
263         struct ntfs_sb_info *sbi = ni->mi.sbi;
264         int err;
265         struct EA_INFO ea_info;
266         const struct EA_INFO *info;
267         struct EA_FULL *new_ea;
268         struct EA_FULL *ea_all = NULL;
269         size_t add, new_pack;
270         u32 off, size;
271         __le16 size_pack;
272         struct ATTRIB *attr;
273         struct ATTR_LIST_ENTRY *le;
274         struct mft_inode *mi;
275         struct runs_tree ea_run;
276         u64 new_sz;
277         void *p;
278
279         if (!locked)
280                 ni_lock(ni);
281
282         run_init(&ea_run);
283
284         if (name_len > 255) {
285                 err = -ENAMETOOLONG;
286                 goto out;
287         }
288
289         add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
290
291         err = ntfs_read_ea(ni, &ea_all, add, &info);
292         if (err)
293                 goto out;
294
295         if (!info) {
296                 memset(&ea_info, 0, sizeof(ea_info));
297                 size = 0;
298                 size_pack = 0;
299         } else {
300                 memcpy(&ea_info, info, sizeof(ea_info));
301                 size = le32_to_cpu(ea_info.size);
302                 size_pack = ea_info.size_pack;
303         }
304
305         if (info && find_ea(ea_all, size, name, name_len, &off)) {
306                 struct EA_FULL *ea;
307                 size_t ea_sz;
308
309                 if (flags & XATTR_CREATE) {
310                         err = -EEXIST;
311                         goto out;
312                 }
313
314                 ea = Add2Ptr(ea_all, off);
315
316                 /*
317                  * Check simple case when we try to insert xattr with the same value
318                  * e.g. ntfs_save_wsl_perm
319                  */
320                 if (val_size && le16_to_cpu(ea->elength) == val_size &&
321                     !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
322                         /* xattr already contains the required value. */
323                         goto out;
324                 }
325
326                 /* Remove current xattr. */
327                 if (ea->flags & FILE_NEED_EA)
328                         le16_add_cpu(&ea_info.count, -1);
329
330                 ea_sz = unpacked_ea_size(ea);
331
332                 le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
333
334                 memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
335
336                 size -= ea_sz;
337                 memset(Add2Ptr(ea_all, size), 0, ea_sz);
338
339                 ea_info.size = cpu_to_le32(size);
340
341                 if ((flags & XATTR_REPLACE) && !val_size) {
342                         /* Remove xattr. */
343                         goto update_ea;
344                 }
345         } else {
346                 if (flags & XATTR_REPLACE) {
347                         err = -ENODATA;
348                         goto out;
349                 }
350
351                 if (!ea_all) {
352                         ea_all = kzalloc(add, GFP_NOFS);
353                         if (!ea_all) {
354                                 err = -ENOMEM;
355                                 goto out;
356                         }
357                 }
358         }
359
360         /* Append new xattr. */
361         new_ea = Add2Ptr(ea_all, size);
362         new_ea->size = cpu_to_le32(add);
363         new_ea->flags = 0;
364         new_ea->name_len = name_len;
365         new_ea->elength = cpu_to_le16(val_size);
366         memcpy(new_ea->name, name, name_len);
367         new_ea->name[name_len] = 0;
368         memcpy(new_ea->name + name_len + 1, value, val_size);
369         new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
370
371         /* Should fit into 16 bits. */
372         if (new_pack > 0xffff) {
373                 err = -EFBIG; // -EINVAL?
374                 goto out;
375         }
376         ea_info.size_pack = cpu_to_le16(new_pack);
377
378         /* New size of ATTR_EA. */
379         size += add;
380         if (size > sbi->ea_max_size) {
381                 err = -EFBIG; // -EINVAL?
382                 goto out;
383         }
384         ea_info.size = cpu_to_le32(size);
385
386 update_ea:
387
388         if (!info) {
389                 /* Create xattr. */
390                 if (!size) {
391                         err = 0;
392                         goto out;
393                 }
394
395                 err = ni_insert_resident(ni, sizeof(struct EA_INFO),
396                                          ATTR_EA_INFO, NULL, 0, NULL, NULL,
397                                          NULL);
398                 if (err)
399                         goto out;
400
401                 err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
402                                          NULL);
403                 if (err)
404                         goto out;
405         }
406
407         new_sz = size;
408         err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
409                             false, NULL);
410         if (err)
411                 goto out;
412
413         le = NULL;
414         attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
415         if (!attr) {
416                 err = -EINVAL;
417                 goto out;
418         }
419
420         if (!size) {
421                 /* Delete xattr, ATTR_EA_INFO */
422                 ni_remove_attr_le(ni, attr, mi, le);
423         } else {
424                 p = resident_data_ex(attr, sizeof(struct EA_INFO));
425                 if (!p) {
426                         err = -EINVAL;
427                         goto out;
428                 }
429                 memcpy(p, &ea_info, sizeof(struct EA_INFO));
430                 mi->dirty = true;
431         }
432
433         le = NULL;
434         attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
435         if (!attr) {
436                 err = -EINVAL;
437                 goto out;
438         }
439
440         if (!size) {
441                 /* Delete xattr, ATTR_EA */
442                 ni_remove_attr_le(ni, attr, mi, le);
443         } else if (attr->non_res) {
444                 err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size);
445                 if (err)
446                         goto out;
447         } else {
448                 p = resident_data_ex(attr, size);
449                 if (!p) {
450                         err = -EINVAL;
451                         goto out;
452                 }
453                 memcpy(p, ea_all, size);
454                 mi->dirty = true;
455         }
456
457         /* Check if we delete the last xattr. */
458         if (size)
459                 ni->ni_flags |= NI_FLAG_EA;
460         else
461                 ni->ni_flags &= ~NI_FLAG_EA;
462
463         if (ea_info.size_pack != size_pack)
464                 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
465         mark_inode_dirty(&ni->vfs_inode);
466
467 out:
468         if (!locked)
469                 ni_unlock(ni);
470
471         run_close(&ea_run);
472         kfree(ea_all);
473
474         return err;
475 }
476
477 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
478 static inline void ntfs_posix_acl_release(struct posix_acl *acl)
479 {
480         if (acl && refcount_dec_and_test(&acl->a_refcount))
481                 kfree(acl);
482 }
483
484 static struct posix_acl *ntfs_get_acl_ex(struct user_namespace *mnt_userns,
485                                          struct inode *inode, int type,
486                                          int locked)
487 {
488         struct ntfs_inode *ni = ntfs_i(inode);
489         const char *name;
490         size_t name_len;
491         struct posix_acl *acl;
492         size_t req;
493         int err;
494         void *buf;
495
496         /* Allocate PATH_MAX bytes. */
497         buf = __getname();
498         if (!buf)
499                 return ERR_PTR(-ENOMEM);
500
501         /* Possible values of 'type' was already checked above. */
502         if (type == ACL_TYPE_ACCESS) {
503                 name = XATTR_NAME_POSIX_ACL_ACCESS;
504                 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
505         } else {
506                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
507                 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
508         }
509
510         if (!locked)
511                 ni_lock(ni);
512
513         err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
514
515         if (!locked)
516                 ni_unlock(ni);
517
518         /* Translate extended attribute to acl. */
519         if (err >= 0) {
520                 acl = posix_acl_from_xattr(mnt_userns, buf, err);
521                 if (!IS_ERR(acl))
522                         set_cached_acl(inode, type, acl);
523         } else {
524                 acl = err == -ENODATA ? NULL : ERR_PTR(err);
525         }
526
527         __putname(buf);
528
529         return acl;
530 }
531
532 /*
533  * ntfs_get_acl - inode_operations::get_acl
534  */
535 struct posix_acl *ntfs_get_acl(struct inode *inode, int type)
536 {
537         /* TODO: init_user_ns? */
538         return ntfs_get_acl_ex(&init_user_ns, inode, type, 0);
539 }
540
541 static noinline int ntfs_set_acl_ex(struct user_namespace *mnt_userns,
542                                     struct inode *inode, struct posix_acl *acl,
543                                     int type, int locked)
544 {
545         const char *name;
546         size_t size, name_len;
547         void *value = NULL;
548         int err = 0;
549
550         if (S_ISLNK(inode->i_mode))
551                 return -EOPNOTSUPP;
552
553         switch (type) {
554         case ACL_TYPE_ACCESS:
555                 if (acl) {
556                         umode_t mode = inode->i_mode;
557
558                         err = posix_acl_equiv_mode(acl, &mode);
559                         if (err < 0)
560                                 return err;
561
562                         if (inode->i_mode != mode) {
563                                 inode->i_mode = mode;
564                                 mark_inode_dirty(inode);
565                         }
566
567                         if (!err) {
568                                 /*
569                                  * ACL can be exactly represented in the
570                                  * traditional file mode permission bits.
571                                  */
572                                 acl = NULL;
573                         }
574                 }
575                 name = XATTR_NAME_POSIX_ACL_ACCESS;
576                 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
577                 break;
578
579         case ACL_TYPE_DEFAULT:
580                 if (!S_ISDIR(inode->i_mode))
581                         return acl ? -EACCES : 0;
582                 name = XATTR_NAME_POSIX_ACL_DEFAULT;
583                 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
584                 break;
585
586         default:
587                 return -EINVAL;
588         }
589
590         if (!acl) {
591                 size = 0;
592                 value = NULL;
593         } else {
594                 size = posix_acl_xattr_size(acl->a_count);
595                 value = kmalloc(size, GFP_NOFS);
596                 if (!value)
597                         return -ENOMEM;
598
599                 err = posix_acl_to_xattr(mnt_userns, acl, value, size);
600                 if (err < 0)
601                         goto out;
602         }
603
604         err = ntfs_set_ea(inode, name, name_len, value, size, 0, locked);
605         if (!err)
606                 set_cached_acl(inode, type, acl);
607
608 out:
609         kfree(value);
610
611         return err;
612 }
613
614 /*
615  * ntfs_set_acl - inode_operations::set_acl
616  */
617 int ntfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
618                  struct posix_acl *acl, int type)
619 {
620         return ntfs_set_acl_ex(mnt_userns, inode, acl, type, 0);
621 }
622
623 static int ntfs_xattr_get_acl(struct user_namespace *mnt_userns,
624                               struct inode *inode, int type, void *buffer,
625                               size_t size)
626 {
627         struct posix_acl *acl;
628         int err;
629
630         if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
631                 ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
632                 return -EOPNOTSUPP;
633         }
634
635         acl = ntfs_get_acl(inode, type);
636         if (IS_ERR(acl))
637                 return PTR_ERR(acl);
638
639         if (!acl)
640                 return -ENODATA;
641
642         err = posix_acl_to_xattr(mnt_userns, acl, buffer, size);
643         ntfs_posix_acl_release(acl);
644
645         return err;
646 }
647
648 static int ntfs_xattr_set_acl(struct user_namespace *mnt_userns,
649                               struct inode *inode, int type, const void *value,
650                               size_t size)
651 {
652         struct posix_acl *acl;
653         int err;
654
655         if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
656                 ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
657                 return -EOPNOTSUPP;
658         }
659
660         if (!inode_owner_or_capable(mnt_userns, inode))
661                 return -EPERM;
662
663         if (!value) {
664                 acl = NULL;
665         } else {
666                 acl = posix_acl_from_xattr(mnt_userns, value, size);
667                 if (IS_ERR(acl))
668                         return PTR_ERR(acl);
669
670                 if (acl) {
671                         err = posix_acl_valid(mnt_userns, acl);
672                         if (err)
673                                 goto release_and_out;
674                 }
675         }
676
677         err = ntfs_set_acl(mnt_userns, inode, acl, type);
678
679 release_and_out:
680         ntfs_posix_acl_release(acl);
681         return err;
682 }
683
684 /*
685  * ntfs_init_acl - Initialize the ACLs of a new inode.
686  *
687  * Called from ntfs_create_inode().
688  */
689 int ntfs_init_acl(struct user_namespace *mnt_userns, struct inode *inode,
690                   struct inode *dir)
691 {
692         struct posix_acl *default_acl, *acl;
693         int err;
694
695         /*
696          * TODO: Refactoring lock.
697          * ni_lock(dir) ... -> posix_acl_create(dir,...) -> ntfs_get_acl -> ni_lock(dir)
698          */
699         inode->i_default_acl = NULL;
700
701         default_acl = ntfs_get_acl_ex(mnt_userns, dir, ACL_TYPE_DEFAULT, 1);
702
703         if (!default_acl || default_acl == ERR_PTR(-EOPNOTSUPP)) {
704                 inode->i_mode &= ~current_umask();
705                 err = 0;
706                 goto out;
707         }
708
709         if (IS_ERR(default_acl)) {
710                 err = PTR_ERR(default_acl);
711                 goto out;
712         }
713
714         acl = default_acl;
715         err = __posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
716         if (err < 0)
717                 goto out1;
718         if (!err) {
719                 posix_acl_release(acl);
720                 acl = NULL;
721         }
722
723         if (!S_ISDIR(inode->i_mode)) {
724                 posix_acl_release(default_acl);
725                 default_acl = NULL;
726         }
727
728         if (default_acl)
729                 err = ntfs_set_acl_ex(mnt_userns, inode, default_acl,
730                                       ACL_TYPE_DEFAULT, 1);
731
732         if (!acl)
733                 inode->i_acl = NULL;
734         else if (!err)
735                 err = ntfs_set_acl_ex(mnt_userns, inode, acl, ACL_TYPE_ACCESS,
736                                       1);
737
738         posix_acl_release(acl);
739 out1:
740         posix_acl_release(default_acl);
741
742 out:
743         return err;
744 }
745 #endif
746
747 /*
748  * ntfs_acl_chmod - Helper for ntfs3_setattr().
749  */
750 int ntfs_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode)
751 {
752         struct super_block *sb = inode->i_sb;
753
754         if (!(sb->s_flags & SB_POSIXACL))
755                 return 0;
756
757         if (S_ISLNK(inode->i_mode))
758                 return -EOPNOTSUPP;
759
760         return posix_acl_chmod(mnt_userns, inode, inode->i_mode);
761 }
762
763 /*
764  * ntfs_permission - inode_operations::permission
765  */
766 int ntfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
767                     int mask)
768 {
769         if (ntfs_sb(inode->i_sb)->options->noacsrules) {
770                 /* "No access rules" mode - Allow all changes. */
771                 return 0;
772         }
773
774         return generic_permission(mnt_userns, inode, mask);
775 }
776
777 /*
778  * ntfs_listxattr - inode_operations::listxattr
779  */
780 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
781 {
782         struct inode *inode = d_inode(dentry);
783         struct ntfs_inode *ni = ntfs_i(inode);
784         ssize_t ret;
785
786         if (!(ni->ni_flags & NI_FLAG_EA)) {
787                 /* no xattr in file */
788                 return 0;
789         }
790
791         ni_lock(ni);
792
793         ret = ntfs_list_ea(ni, buffer, size);
794
795         ni_unlock(ni);
796
797         return ret;
798 }
799
800 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
801                          struct inode *inode, const char *name, void *buffer,
802                          size_t size)
803 {
804         int err;
805         struct ntfs_inode *ni = ntfs_i(inode);
806         size_t name_len = strlen(name);
807
808         /* Dispatch request. */
809         if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
810             !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
811                 /* system.dos_attrib */
812                 if (!buffer) {
813                         err = sizeof(u8);
814                 } else if (size < sizeof(u8)) {
815                         err = -ENODATA;
816                 } else {
817                         err = sizeof(u8);
818                         *(u8 *)buffer = le32_to_cpu(ni->std_fa);
819                 }
820                 goto out;
821         }
822
823         if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
824             !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
825                 /* system.ntfs_attrib */
826                 if (!buffer) {
827                         err = sizeof(u32);
828                 } else if (size < sizeof(u32)) {
829                         err = -ENODATA;
830                 } else {
831                         err = sizeof(u32);
832                         *(u32 *)buffer = le32_to_cpu(ni->std_fa);
833                 }
834                 goto out;
835         }
836
837         if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
838             !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
839                 /* system.ntfs_security*/
840                 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
841                 size_t sd_size = 0;
842
843                 if (!is_ntfs3(ni->mi.sbi)) {
844                         /* We should get nt4 security. */
845                         err = -EINVAL;
846                         goto out;
847                 } else if (le32_to_cpu(ni->std_security_id) <
848                            SECURITY_ID_FIRST) {
849                         err = -ENOENT;
850                         goto out;
851                 }
852
853                 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
854                                               &sd, &sd_size);
855                 if (err)
856                         goto out;
857
858                 if (!is_sd_valid(sd, sd_size)) {
859                         ntfs_inode_warn(
860                                 inode,
861                                 "looks like you get incorrect security descriptor id=%u",
862                                 ni->std_security_id);
863                 }
864
865                 if (!buffer) {
866                         err = sd_size;
867                 } else if (size < sd_size) {
868                         err = -ENODATA;
869                 } else {
870                         err = sd_size;
871                         memcpy(buffer, sd, sd_size);
872                 }
873                 kfree(sd);
874                 goto out;
875         }
876
877 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
878         if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
879              !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
880                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
881             (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
882              !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
883                      sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
884                 /* TODO: init_user_ns? */
885                 err = ntfs_xattr_get_acl(
886                         &init_user_ns, inode,
887                         name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
888                                 ? ACL_TYPE_ACCESS
889                                 : ACL_TYPE_DEFAULT,
890                         buffer, size);
891                 goto out;
892         }
893 #endif
894         /* Deal with NTFS extended attribute. */
895         err = ntfs_get_ea(inode, name, name_len, buffer, size, NULL);
896
897 out:
898         return err;
899 }
900
901 /*
902  * ntfs_setxattr - inode_operations::setxattr
903  */
904 static noinline int ntfs_setxattr(const struct xattr_handler *handler,
905                                   struct user_namespace *mnt_userns,
906                                   struct dentry *de, struct inode *inode,
907                                   const char *name, const void *value,
908                                   size_t size, int flags)
909 {
910         int err = -EINVAL;
911         struct ntfs_inode *ni = ntfs_i(inode);
912         size_t name_len = strlen(name);
913         enum FILE_ATTRIBUTE new_fa;
914
915         /* Dispatch request. */
916         if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
917             !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
918                 if (sizeof(u8) != size)
919                         goto out;
920                 new_fa = cpu_to_le32(*(u8 *)value);
921                 goto set_new_fa;
922         }
923
924         if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
925             !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
926                 if (size != sizeof(u32))
927                         goto out;
928                 new_fa = cpu_to_le32(*(u32 *)value);
929
930                 if (S_ISREG(inode->i_mode)) {
931                         /* Process compressed/sparsed in special way. */
932                         ni_lock(ni);
933                         err = ni_new_attr_flags(ni, new_fa);
934                         ni_unlock(ni);
935                         if (err)
936                                 goto out;
937                 }
938 set_new_fa:
939                 /*
940                  * Thanks Mark Harmstone:
941                  * Keep directory bit consistency.
942                  */
943                 if (S_ISDIR(inode->i_mode))
944                         new_fa |= FILE_ATTRIBUTE_DIRECTORY;
945                 else
946                         new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
947
948                 if (ni->std_fa != new_fa) {
949                         ni->std_fa = new_fa;
950                         if (new_fa & FILE_ATTRIBUTE_READONLY)
951                                 inode->i_mode &= ~0222;
952                         else
953                                 inode->i_mode |= 0222;
954                         /* Std attribute always in primary record. */
955                         ni->mi.dirty = true;
956                         mark_inode_dirty(inode);
957                 }
958                 err = 0;
959
960                 goto out;
961         }
962
963         if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
964             !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
965                 /* system.ntfs_security*/
966                 __le32 security_id;
967                 bool inserted;
968                 struct ATTR_STD_INFO5 *std;
969
970                 if (!is_ntfs3(ni->mi.sbi)) {
971                         /*
972                          * We should replace ATTR_SECURE.
973                          * Skip this way cause it is nt4 feature.
974                          */
975                         err = -EINVAL;
976                         goto out;
977                 }
978
979                 if (!is_sd_valid(value, size)) {
980                         err = -EINVAL;
981                         ntfs_inode_warn(
982                                 inode,
983                                 "you try to set invalid security descriptor");
984                         goto out;
985                 }
986
987                 err = ntfs_insert_security(ni->mi.sbi, value, size,
988                                            &security_id, &inserted);
989                 if (err)
990                         goto out;
991
992                 ni_lock(ni);
993                 std = ni_std5(ni);
994                 if (!std) {
995                         err = -EINVAL;
996                 } else if (std->security_id != security_id) {
997                         std->security_id = ni->std_security_id = security_id;
998                         /* Std attribute always in primary record. */
999                         ni->mi.dirty = true;
1000                         mark_inode_dirty(&ni->vfs_inode);
1001                 }
1002                 ni_unlock(ni);
1003                 goto out;
1004         }
1005
1006 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1007         if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
1008              !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
1009                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
1010             (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
1011              !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
1012                      sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
1013                 err = ntfs_xattr_set_acl(
1014                         mnt_userns, inode,
1015                         name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
1016                                 ? ACL_TYPE_ACCESS
1017                                 : ACL_TYPE_DEFAULT,
1018                         value, size);
1019                 goto out;
1020         }
1021 #endif
1022         /* Deal with NTFS extended attribute. */
1023         err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0);
1024
1025 out:
1026         return err;
1027 }
1028
1029 /*
1030  * ntfs_save_wsl_perm
1031  *
1032  * save uid/gid/mode in xattr
1033  */
1034 int ntfs_save_wsl_perm(struct inode *inode)
1035 {
1036         int err;
1037         __le32 value;
1038
1039         value = cpu_to_le32(i_uid_read(inode));
1040         err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
1041                           sizeof(value), 0, 0);
1042         if (err)
1043                 goto out;
1044
1045         value = cpu_to_le32(i_gid_read(inode));
1046         err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
1047                           sizeof(value), 0, 0);
1048         if (err)
1049                 goto out;
1050
1051         value = cpu_to_le32(inode->i_mode);
1052         err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
1053                           sizeof(value), 0, 0);
1054         if (err)
1055                 goto out;
1056
1057         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1058                 value = cpu_to_le32(inode->i_rdev);
1059                 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
1060                                   sizeof(value), 0, 0);
1061                 if (err)
1062                         goto out;
1063         }
1064
1065 out:
1066         /* In case of error should we delete all WSL xattr? */
1067         return err;
1068 }
1069
1070 /*
1071  * ntfs_get_wsl_perm
1072  *
1073  * get uid/gid/mode from xattr
1074  * it is called from ntfs_iget5->ntfs_read_mft
1075  */
1076 void ntfs_get_wsl_perm(struct inode *inode)
1077 {
1078         size_t sz;
1079         __le32 value[3];
1080
1081         if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
1082                         sizeof(value[0]), &sz) == sizeof(value[0]) &&
1083             ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
1084                         sizeof(value[1]), &sz) == sizeof(value[1]) &&
1085             ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1086                         sizeof(value[2]), &sz) == sizeof(value[2])) {
1087                 i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1088                 i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1089                 inode->i_mode = le32_to_cpu(value[2]);
1090
1091                 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1092                                 &value[0], sizeof(value),
1093                                 &sz) == sizeof(value[0])) {
1094                         inode->i_rdev = le32_to_cpu(value[0]);
1095                 }
1096         }
1097 }
1098
1099 static bool ntfs_xattr_user_list(struct dentry *dentry)
1100 {
1101         return true;
1102 }
1103
1104 // clang-format off
1105 static const struct xattr_handler ntfs_xattr_handler = {
1106         .prefix = "",
1107         .get    = ntfs_getxattr,
1108         .set    = ntfs_setxattr,
1109         .list   = ntfs_xattr_user_list,
1110 };
1111
1112 const struct xattr_handler *ntfs_xattr_handlers[] = {
1113         &ntfs_xattr_handler,
1114         NULL,
1115 };
1116 // clang-format on