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