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