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