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