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