1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
9 #include <linux/posix_acl.h>
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/xattr.h>
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"
24 static inline size_t unpacked_ea_size(const struct EA_FULL *ea)
26 return ea->size ? le32_to_cpu(ea->size)
27 : ALIGN(struct_size(ea, name,
29 le16_to_cpu(ea->elength)),
33 static inline size_t packed_ea_size(const struct EA_FULL *ea)
35 return struct_size(ea, name,
36 1 + ea->name_len + le16_to_cpu(ea->elength)) -
37 offsetof(struct EA_FULL, flags);
43 * Assume there is at least one xattr in the list.
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)
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)) {
69 * ntfs_read_ea - Read all extended attributes.
70 * @ea: New allocated memory.
71 * @info: Pointer into resident data.
73 static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
74 size_t add_bytes, const struct EA_INFO **info)
77 struct ntfs_sb_info *sbi = ni->mi.sbi;
78 struct ATTR_LIST_ENTRY *le = NULL;
79 struct ATTRIB *attr_info, *attr_ea;
81 u32 size, off, ea_size;
83 static_assert(le32_to_cpu(ATTR_EA_INFO) < le32_to_cpu(ATTR_EA));
89 ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, NULL);
91 ni_find_attr(ni, attr_info, &le, ATTR_EA, NULL, 0, NULL, NULL);
93 if (!attr_ea || !attr_info)
96 *info = resident_data_ex(attr_info, sizeof(struct EA_INFO));
100 /* Check Ea limit. */
101 size = le32_to_cpu((*info)->size);
102 if (size > sbi->ea_max_size) {
107 if (attr_size(attr_ea) > sbi->ea_max_size) {
113 /* EA info persists, but xattr is empty. Looks like EA problem. */
117 /* Allocate memory for packed Ea. */
118 ea_p = kmalloc(size_add(size, add_bytes), GFP_NOFS);
122 if (attr_ea->non_res) {
123 struct runs_tree run;
127 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &run, 0, size);
129 err = ntfs_read_run_nb(sbi, &run, 0, ea_p, size, NULL);
135 void *p = resident_data_ex(attr_ea, size);
139 memcpy(ea_p, p, size);
142 memset(Add2Ptr(ea_p, size), 0, add_bytes);
144 /* Check all attributes for consistency. */
145 for (off = 0; off < size; off += ea_size) {
146 const struct EA_FULL *ef = Add2Ptr(ea_p, off);
147 u32 bytes = size - off;
149 /* Check if we can use field ea->size. */
150 if (bytes < sizeof(ef->size))
154 ea_size = le32_to_cpu(ef->size);
160 /* Check if we can use fields ef->name_len and ef->elength. */
161 if (bytes < offsetof(struct EA_FULL, name))
164 ea_size = ALIGN(struct_size(ef, name,
166 le16_to_cpu(ef->elength)),
178 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
185 * Copy a list of xattrs names into the buffer
186 * provided, or compute the buffer size required.
189 * * Number of bytes used / required on
190 * * -ERRNO - on failure
192 static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer,
193 size_t bytes_per_buffer)
195 const struct EA_INFO *info;
196 struct EA_FULL *ea_all = NULL;
197 const struct EA_FULL *ea;
203 err = ntfs_read_ea(ni, &ea_all, 0, &info);
207 if (!info || !ea_all)
210 size = le32_to_cpu(info->size);
212 /* Enumerate all xattrs. */
213 for (ret = 0, off = 0; off < size; off += ea_size) {
214 ea = Add2Ptr(ea_all, off);
215 ea_size = unpacked_ea_size(ea);
218 if (ret + ea->name_len + 1 > bytes_per_buffer) {
223 memcpy(buffer + ret, ea->name, ea->name_len);
224 buffer[ret + ea->name_len] = 0;
227 ret += ea->name_len + 1;
232 return err ? err : ret;
235 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
236 void *buffer, size_t size, size_t *required)
238 struct ntfs_inode *ni = ntfs_i(inode);
239 const struct EA_INFO *info;
240 struct EA_FULL *ea_all = NULL;
241 const struct EA_FULL *ea;
245 if (!(ni->ni_flags & NI_FLAG_EA))
253 if (name_len > 255) {
258 err = ntfs_read_ea(ni, &ea_all, 0, &info);
265 /* Enumerate all xattrs. */
266 if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off,
271 ea = Add2Ptr(ea_all, off);
273 len = le16_to_cpu(ea->elength);
286 memcpy(buffer, ea->name + ea->name_len + 1, len);
294 return err ? err : len;
297 static noinline int ntfs_set_ea(struct inode *inode, const char *name,
298 size_t name_len, const void *value,
299 size_t val_size, int flags, bool locked)
301 struct ntfs_inode *ni = ntfs_i(inode);
302 struct ntfs_sb_info *sbi = ni->mi.sbi;
304 struct EA_INFO ea_info;
305 const struct EA_INFO *info;
306 struct EA_FULL *new_ea;
307 struct EA_FULL *ea_all = NULL;
308 size_t add, new_pack;
309 u32 off, size, ea_sz;
312 struct ATTR_LIST_ENTRY *le;
313 struct mft_inode *mi;
314 struct runs_tree ea_run;
323 if (name_len > 255) {
328 add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
330 err = ntfs_read_ea(ni, &ea_all, add, &info);
335 memset(&ea_info, 0, sizeof(ea_info));
339 memcpy(&ea_info, info, sizeof(ea_info));
340 size = le32_to_cpu(ea_info.size);
341 size_pack = ea_info.size_pack;
344 if (info && find_ea(ea_all, size, name, name_len, &off, &ea_sz)) {
347 if (flags & XATTR_CREATE) {
352 ea = Add2Ptr(ea_all, off);
355 * Check simple case when we try to insert xattr with the same value
356 * e.g. ntfs_save_wsl_perm
358 if (val_size && le16_to_cpu(ea->elength) == val_size &&
359 !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
360 /* xattr already contains the required value. */
364 /* Remove current xattr. */
365 if (ea->flags & FILE_NEED_EA)
366 le16_add_cpu(&ea_info.count, -1);
368 le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
370 memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
373 memset(Add2Ptr(ea_all, size), 0, ea_sz);
375 ea_info.size = cpu_to_le32(size);
377 if ((flags & XATTR_REPLACE) && !val_size) {
382 if (flags & XATTR_REPLACE) {
388 ea_all = kzalloc(add, GFP_NOFS);
396 /* Append new xattr. */
397 new_ea = Add2Ptr(ea_all, size);
398 new_ea->size = cpu_to_le32(add);
400 new_ea->name_len = name_len;
401 new_ea->elength = cpu_to_le16(val_size);
402 memcpy(new_ea->name, name, name_len);
403 new_ea->name[name_len] = 0;
404 memcpy(new_ea->name + name_len + 1, value, val_size);
405 new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
406 ea_info.size_pack = cpu_to_le16(new_pack);
407 /* New size of ATTR_EA. */
409 ea_info.size = cpu_to_le32(size);
412 * 1. Check ea_info.size_pack for overflow.
413 * 2. New attibute size must fit value from $AttrDef
415 if (new_pack > 0xffff || size > sbi->ea_max_size) {
418 "The size of extended attributes must not exceed 64KiB");
419 err = -EFBIG; // -EINVAL?
432 err = ni_insert_resident(ni, sizeof(struct EA_INFO),
433 ATTR_EA_INFO, NULL, 0, NULL, NULL,
438 err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
445 err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
451 attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
458 /* Delete xattr, ATTR_EA_INFO */
459 ni_remove_attr_le(ni, attr, mi, le);
461 p = resident_data_ex(attr, sizeof(struct EA_INFO));
466 memcpy(p, &ea_info, sizeof(struct EA_INFO));
471 attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
478 /* Delete xattr, ATTR_EA */
479 ni_remove_attr_le(ni, attr, mi, le);
480 } else if (attr->non_res) {
481 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0,
486 err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0);
490 p = resident_data_ex(attr, size);
495 memcpy(p, ea_all, size);
499 /* Check if we delete the last xattr. */
501 ni->ni_flags |= NI_FLAG_EA;
503 ni->ni_flags &= ~NI_FLAG_EA;
505 if (ea_info.size_pack != size_pack)
506 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
507 mark_inode_dirty(&ni->vfs_inode);
519 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
520 static struct posix_acl *ntfs_get_acl_ex(struct inode *inode, int type,
523 struct ntfs_inode *ni = ntfs_i(inode);
526 struct posix_acl *acl;
531 /* Allocate PATH_MAX bytes. */
534 return ERR_PTR(-ENOMEM);
536 /* Possible values of 'type' was already checked above. */
537 if (type == ACL_TYPE_ACCESS) {
538 name = XATTR_NAME_POSIX_ACL_ACCESS;
539 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
541 name = XATTR_NAME_POSIX_ACL_DEFAULT;
542 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
548 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
553 /* Translate extended attribute to acl. */
555 acl = posix_acl_from_xattr(&init_user_ns, buf, err);
556 } else if (err == -ENODATA) {
563 set_cached_acl(inode, type, acl);
571 * ntfs_get_acl - inode_operations::get_acl
573 struct posix_acl *ntfs_get_acl(struct inode *inode, int type, bool rcu)
576 return ERR_PTR(-ECHILD);
578 return ntfs_get_acl_ex(inode, type, 0);
581 static noinline int ntfs_set_acl_ex(struct user_namespace *mnt_userns,
582 struct inode *inode, struct posix_acl *acl,
583 int type, bool init_acl)
586 size_t size, name_len;
592 if (S_ISLNK(inode->i_mode))
595 mode = inode->i_mode;
597 case ACL_TYPE_ACCESS:
598 /* Do not change i_mode if we are in init_acl */
599 if (acl && !init_acl) {
600 err = posix_acl_update_mode(mnt_userns, inode, &mode,
605 name = XATTR_NAME_POSIX_ACL_ACCESS;
606 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
609 case ACL_TYPE_DEFAULT:
610 if (!S_ISDIR(inode->i_mode))
611 return acl ? -EACCES : 0;
612 name = XATTR_NAME_POSIX_ACL_DEFAULT;
613 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
621 /* Remove xattr if it can be presented via mode. */
624 flags = XATTR_REPLACE;
626 size = posix_acl_xattr_size(acl->a_count);
627 value = kmalloc(size, GFP_NOFS);
630 err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
636 err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0);
637 if (err == -ENODATA && !size)
638 err = 0; /* Removing non existed xattr. */
640 set_cached_acl(inode, type, acl);
641 inode->i_mode = mode;
642 inode->i_ctime = current_time(inode);
643 mark_inode_dirty(inode);
653 * ntfs_set_acl - inode_operations::set_acl
655 int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
656 struct posix_acl *acl, int type)
658 struct user_namespace *mnt_userns = mnt_idmap_owner(idmap);
660 return ntfs_set_acl_ex(mnt_userns, d_inode(dentry), acl, type, false);
664 * ntfs_init_acl - Initialize the ACLs of a new inode.
666 * Called from ntfs_create_inode().
668 int ntfs_init_acl(struct user_namespace *mnt_userns, struct inode *inode,
671 struct posix_acl *default_acl, *acl;
674 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
679 err = ntfs_set_acl_ex(mnt_userns, inode, default_acl,
680 ACL_TYPE_DEFAULT, true);
681 posix_acl_release(default_acl);
683 inode->i_default_acl = NULL;
688 err = ntfs_set_acl_ex(mnt_userns, inode, acl,
689 ACL_TYPE_ACCESS, true);
690 posix_acl_release(acl);
700 * ntfs_acl_chmod - Helper for ntfs3_setattr().
702 int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry)
704 struct inode *inode = d_inode(dentry);
705 struct super_block *sb = inode->i_sb;
707 if (!(sb->s_flags & SB_POSIXACL))
710 if (S_ISLNK(inode->i_mode))
713 return posix_acl_chmod(idmap, dentry, inode->i_mode);
717 * ntfs_permission - inode_operations::permission
719 int ntfs_permission(struct mnt_idmap *idmap, struct inode *inode,
722 if (ntfs_sb(inode->i_sb)->options->noacsrules) {
723 /* "No access rules" mode - Allow all changes. */
727 return generic_permission(idmap, inode, mask);
731 * ntfs_listxattr - inode_operations::listxattr
733 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
735 struct inode *inode = d_inode(dentry);
736 struct ntfs_inode *ni = ntfs_i(inode);
739 if (!(ni->ni_flags & NI_FLAG_EA)) {
740 /* no xattr in file */
746 ret = ntfs_list_ea(ni, buffer, size);
753 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
754 struct inode *inode, const char *name, void *buffer,
758 struct ntfs_inode *ni = ntfs_i(inode);
760 /* Dispatch request. */
761 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
762 /* system.dos_attrib */
765 } else if (size < sizeof(u8)) {
769 *(u8 *)buffer = le32_to_cpu(ni->std_fa);
774 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
775 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
776 /* system.ntfs_attrib */
779 } else if (size < sizeof(u32)) {
783 *(u32 *)buffer = le32_to_cpu(ni->std_fa);
784 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
785 *(u32 *)buffer = cpu_to_be32(*(u32 *)buffer);
790 if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
791 /* system.ntfs_security*/
792 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
795 if (!is_ntfs3(ni->mi.sbi)) {
796 /* We should get nt4 security. */
799 } else if (le32_to_cpu(ni->std_security_id) <
805 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
810 if (!is_sd_valid(sd, sd_size)) {
813 "looks like you get incorrect security descriptor id=%u",
814 ni->std_security_id);
819 } else if (size < sd_size) {
823 memcpy(buffer, sd, sd_size);
829 /* Deal with NTFS extended attribute. */
830 err = ntfs_get_ea(inode, name, strlen(name), buffer, size, NULL);
837 * ntfs_setxattr - inode_operations::setxattr
839 static noinline int ntfs_setxattr(const struct xattr_handler *handler,
840 struct user_namespace *mnt_userns,
841 struct dentry *de, struct inode *inode,
842 const char *name, const void *value,
843 size_t size, int flags)
846 struct ntfs_inode *ni = ntfs_i(inode);
847 enum FILE_ATTRIBUTE new_fa;
849 /* Dispatch request. */
850 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
851 if (sizeof(u8) != size)
853 new_fa = cpu_to_le32(*(u8 *)value);
857 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
858 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
859 if (size != sizeof(u32))
861 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
862 new_fa = cpu_to_le32(be32_to_cpu(*(u32 *)value));
864 new_fa = cpu_to_le32(*(u32 *)value);
866 if (S_ISREG(inode->i_mode)) {
867 /* Process compressed/sparsed in special way. */
869 err = ni_new_attr_flags(ni, new_fa);
876 * Thanks Mark Harmstone:
877 * Keep directory bit consistency.
879 if (S_ISDIR(inode->i_mode))
880 new_fa |= FILE_ATTRIBUTE_DIRECTORY;
882 new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
884 if (ni->std_fa != new_fa) {
886 if (new_fa & FILE_ATTRIBUTE_READONLY)
887 inode->i_mode &= ~0222;
889 inode->i_mode |= 0222;
890 /* Std attribute always in primary record. */
892 mark_inode_dirty(inode);
899 if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
900 /* system.ntfs_security*/
903 struct ATTR_STD_INFO5 *std;
905 if (!is_ntfs3(ni->mi.sbi)) {
907 * We should replace ATTR_SECURE.
908 * Skip this way cause it is nt4 feature.
914 if (!is_sd_valid(value, size)) {
918 "you try to set invalid security descriptor");
922 err = ntfs_insert_security(ni->mi.sbi, value, size,
923 &security_id, &inserted);
931 } else if (std->security_id != security_id) {
932 std->security_id = ni->std_security_id = security_id;
933 /* Std attribute always in primary record. */
935 mark_inode_dirty(&ni->vfs_inode);
941 /* Deal with NTFS extended attribute. */
942 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0);
945 inode->i_ctime = current_time(inode);
946 mark_inode_dirty(inode);
954 * save uid/gid/mode in xattr
956 int ntfs_save_wsl_perm(struct inode *inode)
960 struct ntfs_inode *ni = ntfs_i(inode);
963 value = cpu_to_le32(i_uid_read(inode));
964 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
965 sizeof(value), 0, true); /* true == already locked. */
969 value = cpu_to_le32(i_gid_read(inode));
970 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
971 sizeof(value), 0, true);
975 value = cpu_to_le32(inode->i_mode);
976 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
977 sizeof(value), 0, true);
981 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
982 value = cpu_to_le32(inode->i_rdev);
983 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
984 sizeof(value), 0, true);
991 /* In case of error should we delete all WSL xattr? */
998 * get uid/gid/mode from xattr
999 * it is called from ntfs_iget5->ntfs_read_mft
1001 void ntfs_get_wsl_perm(struct inode *inode)
1006 if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
1007 sizeof(value[0]), &sz) == sizeof(value[0]) &&
1008 ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
1009 sizeof(value[1]), &sz) == sizeof(value[1]) &&
1010 ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1011 sizeof(value[2]), &sz) == sizeof(value[2])) {
1012 i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1013 i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1014 inode->i_mode = le32_to_cpu(value[2]);
1016 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1017 &value[0], sizeof(value),
1018 &sz) == sizeof(value[0])) {
1019 inode->i_rdev = le32_to_cpu(value[0]);
1024 static bool ntfs_xattr_user_list(struct dentry *dentry)
1030 static const struct xattr_handler ntfs_other_xattr_handler = {
1032 .get = ntfs_getxattr,
1033 .set = ntfs_setxattr,
1034 .list = ntfs_xattr_user_list,
1037 const struct xattr_handler *ntfs_xattr_handlers[] = {
1038 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1039 &posix_acl_access_xattr_handler,
1040 &posix_acl_default_xattr_handler,
1042 &ntfs_other_xattr_handler,