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);
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;
150 /* Check if we can use field ea->size. */
151 if (bytes < sizeof(ef->size))
155 ea_size = le32_to_cpu(ef->size);
161 /* Check if we can use fields ef->name_len and ef->elength. */
162 if (bytes < offsetof(struct EA_FULL, name))
165 ea_size = ALIGN(struct_size(ef, name,
167 le16_to_cpu(ef->elength)),
179 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
186 * Copy a list of xattrs names into the buffer
187 * provided, or compute the buffer size required.
190 * * Number of bytes used / required on
191 * * -ERRNO - on failure
193 static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer,
194 size_t bytes_per_buffer)
196 const struct EA_INFO *info;
197 struct EA_FULL *ea_all = NULL;
198 const struct EA_FULL *ea;
204 err = ntfs_read_ea(ni, &ea_all, 0, &info);
208 if (!info || !ea_all)
211 size = le32_to_cpu(info->size);
213 /* Enumerate all xattrs. */
215 for (off = 0; off + sizeof(struct EA_FULL) < size; off += ea_size) {
216 ea = Add2Ptr(ea_all, off);
217 ea_size = unpacked_ea_size(ea);
222 if (ea->name_len > ea_size)
226 /* Check if we can use field ea->name */
227 if (off + ea_size > size)
230 if (ret + ea->name_len + 1 > bytes_per_buffer) {
235 memcpy(buffer + ret, ea->name, ea->name_len);
236 buffer[ret + ea->name_len] = 0;
239 ret += ea->name_len + 1;
244 return err ? err : ret;
247 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
248 void *buffer, size_t size, size_t *required)
250 struct ntfs_inode *ni = ntfs_i(inode);
251 const struct EA_INFO *info;
252 struct EA_FULL *ea_all = NULL;
253 const struct EA_FULL *ea;
257 if (!(ni->ni_flags & NI_FLAG_EA))
265 if (name_len > 255) {
270 err = ntfs_read_ea(ni, &ea_all, 0, &info);
277 /* Enumerate all xattrs. */
278 if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off,
283 ea = Add2Ptr(ea_all, off);
285 len = le16_to_cpu(ea->elength);
298 memcpy(buffer, ea->name + ea->name_len + 1, len);
306 return err ? err : len;
309 static noinline int ntfs_set_ea(struct inode *inode, const char *name,
310 size_t name_len, const void *value,
311 size_t val_size, int flags, bool locked,
314 struct ntfs_inode *ni = ntfs_i(inode);
315 struct ntfs_sb_info *sbi = ni->mi.sbi;
317 struct EA_INFO ea_info;
318 const struct EA_INFO *info;
319 struct EA_FULL *new_ea;
320 struct EA_FULL *ea_all = NULL;
321 size_t add, new_pack;
322 u32 off, size, ea_sz;
325 struct ATTR_LIST_ENTRY *le;
326 struct mft_inode *mi;
327 struct runs_tree ea_run;
336 if (name_len > 255) {
341 add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
343 err = ntfs_read_ea(ni, &ea_all, add, &info);
348 memset(&ea_info, 0, sizeof(ea_info));
352 memcpy(&ea_info, info, sizeof(ea_info));
353 size = le32_to_cpu(ea_info.size);
354 size_pack = ea_info.size_pack;
357 if (info && find_ea(ea_all, size, name, name_len, &off, &ea_sz)) {
360 if (flags & XATTR_CREATE) {
365 ea = Add2Ptr(ea_all, off);
368 * Check simple case when we try to insert xattr with the same value
369 * e.g. ntfs_save_wsl_perm
371 if (val_size && le16_to_cpu(ea->elength) == val_size &&
372 !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
373 /* xattr already contains the required value. */
377 /* Remove current xattr. */
378 if (ea->flags & FILE_NEED_EA)
379 le16_add_cpu(&ea_info.count, -1);
381 le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
383 memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
386 memset(Add2Ptr(ea_all, size), 0, ea_sz);
388 ea_info.size = cpu_to_le32(size);
390 if ((flags & XATTR_REPLACE) && !val_size) {
395 if (flags & XATTR_REPLACE) {
401 ea_all = kzalloc(add, GFP_NOFS);
409 /* Append new xattr. */
410 new_ea = Add2Ptr(ea_all, size);
411 new_ea->size = cpu_to_le32(add);
413 new_ea->name_len = name_len;
414 new_ea->elength = cpu_to_le16(val_size);
415 memcpy(new_ea->name, name, name_len);
416 new_ea->name[name_len] = 0;
417 memcpy(new_ea->name + name_len + 1, value, val_size);
418 new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
419 ea_info.size_pack = cpu_to_le16(new_pack);
420 /* New size of ATTR_EA. */
422 ea_info.size = cpu_to_le32(size);
425 * 1. Check ea_info.size_pack for overflow.
426 * 2. New attribute size must fit value from $AttrDef
428 if (new_pack > 0xffff || size > sbi->ea_max_size) {
431 "The size of extended attributes must not exceed 64KiB");
432 err = -EFBIG; // -EINVAL?
445 err = ni_insert_resident(ni, sizeof(struct EA_INFO),
446 ATTR_EA_INFO, NULL, 0, NULL, NULL,
451 err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
458 err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
464 attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
471 /* Delete xattr, ATTR_EA_INFO */
472 ni_remove_attr_le(ni, attr, mi, le);
474 p = resident_data_ex(attr, sizeof(struct EA_INFO));
479 memcpy(p, &ea_info, sizeof(struct EA_INFO));
484 attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
491 /* Delete xattr, ATTR_EA */
492 ni_remove_attr_le(ni, attr, mi, le);
493 } else if (attr->non_res) {
494 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0,
499 err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0);
503 p = resident_data_ex(attr, size);
508 memcpy(p, ea_all, size);
512 /* Check if we delete the last xattr. */
514 ni->ni_flags |= NI_FLAG_EA;
516 ni->ni_flags &= ~NI_FLAG_EA;
518 if (ea_info.size_pack != size_pack)
519 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
521 *ea_size = ea_info.size_pack;
522 mark_inode_dirty(&ni->vfs_inode);
534 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
537 * ntfs_get_acl - inode_operations::get_acl
539 struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
542 struct inode *inode = d_inode(dentry);
543 struct ntfs_inode *ni = ntfs_i(inode);
546 struct posix_acl *acl;
551 /* Allocate PATH_MAX bytes. */
554 return ERR_PTR(-ENOMEM);
556 /* Possible values of 'type' was already checked above. */
557 if (type == ACL_TYPE_ACCESS) {
558 name = XATTR_NAME_POSIX_ACL_ACCESS;
559 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
561 name = XATTR_NAME_POSIX_ACL_DEFAULT;
562 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
567 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
571 /* Translate extended attribute to acl. */
573 acl = posix_acl_from_xattr(&init_user_ns, buf, err);
574 } else if (err == -ENODATA) {
581 set_cached_acl(inode, type, acl);
588 static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
589 struct inode *inode, struct posix_acl *acl,
590 int type, bool init_acl)
593 size_t size, name_len;
599 if (S_ISLNK(inode->i_mode))
602 mode = inode->i_mode;
604 case ACL_TYPE_ACCESS:
605 /* Do not change i_mode if we are in init_acl */
606 if (acl && !init_acl) {
607 err = posix_acl_update_mode(idmap, inode, &mode, &acl);
611 name = XATTR_NAME_POSIX_ACL_ACCESS;
612 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
615 case ACL_TYPE_DEFAULT:
616 if (!S_ISDIR(inode->i_mode))
617 return acl ? -EACCES : 0;
618 name = XATTR_NAME_POSIX_ACL_DEFAULT;
619 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
627 /* Remove xattr if it can be presented via mode. */
630 flags = XATTR_REPLACE;
632 size = posix_acl_xattr_size(acl->a_count);
633 value = kmalloc(size, GFP_NOFS);
636 err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
642 err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0, NULL);
643 if (err == -ENODATA && !size)
644 err = 0; /* Removing non existed xattr. */
646 set_cached_acl(inode, type, acl);
647 inode->i_mode = mode;
648 inode_set_ctime_current(inode);
649 mark_inode_dirty(inode);
659 * ntfs_set_acl - inode_operations::set_acl
661 int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
662 struct posix_acl *acl, int type)
664 return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false);
668 * ntfs_init_acl - Initialize the ACLs of a new inode.
670 * Called from ntfs_create_inode().
672 int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode,
675 struct posix_acl *default_acl, *acl;
678 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
683 err = ntfs_set_acl_ex(idmap, inode, default_acl,
684 ACL_TYPE_DEFAULT, true);
685 posix_acl_release(default_acl);
687 inode->i_default_acl = NULL;
692 err = ntfs_set_acl_ex(idmap, inode, acl,
693 ACL_TYPE_ACCESS, true);
694 posix_acl_release(acl);
704 * ntfs_acl_chmod - Helper for ntfs3_setattr().
706 int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry)
708 struct inode *inode = d_inode(dentry);
709 struct super_block *sb = inode->i_sb;
711 if (!(sb->s_flags & SB_POSIXACL))
714 if (S_ISLNK(inode->i_mode))
717 return posix_acl_chmod(idmap, dentry, inode->i_mode);
721 * ntfs_listxattr - inode_operations::listxattr
723 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
725 struct inode *inode = d_inode(dentry);
726 struct ntfs_inode *ni = ntfs_i(inode);
729 if (!(ni->ni_flags & NI_FLAG_EA)) {
730 /* no xattr in file */
736 ret = ntfs_list_ea(ni, buffer, size);
743 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
744 struct inode *inode, const char *name, void *buffer,
748 struct ntfs_inode *ni = ntfs_i(inode);
750 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
753 /* Dispatch request. */
754 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
755 /* system.dos_attrib */
758 } else if (size < sizeof(u8)) {
762 *(u8 *)buffer = le32_to_cpu(ni->std_fa);
767 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
768 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
769 /* system.ntfs_attrib */
772 } else if (size < sizeof(u32)) {
776 *(u32 *)buffer = le32_to_cpu(ni->std_fa);
777 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
778 *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer);
783 if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
784 /* system.ntfs_security*/
785 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
788 if (!is_ntfs3(ni->mi.sbi)) {
789 /* We should get nt4 security. */
792 } else if (le32_to_cpu(ni->std_security_id) <
798 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
803 if (!is_sd_valid(sd, sd_size)) {
806 "looks like you get incorrect security descriptor id=%u",
807 ni->std_security_id);
812 } else if (size < sd_size) {
816 memcpy(buffer, sd, sd_size);
822 /* Deal with NTFS extended attribute. */
823 err = ntfs_get_ea(inode, name, strlen(name), buffer, size, NULL);
830 * ntfs_setxattr - inode_operations::setxattr
832 static noinline int ntfs_setxattr(const struct xattr_handler *handler,
833 struct mnt_idmap *idmap, struct dentry *de,
834 struct inode *inode, const char *name,
835 const void *value, size_t size, int flags)
838 struct ntfs_inode *ni = ntfs_i(inode);
839 enum FILE_ATTRIBUTE new_fa;
841 /* Dispatch request. */
842 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
843 if (sizeof(u8) != size)
845 new_fa = cpu_to_le32(*(u8 *)value);
849 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
850 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
851 if (size != sizeof(u32))
853 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
854 new_fa = cpu_to_le32(be32_to_cpu(*(__be32 *)value));
856 new_fa = cpu_to_le32(*(u32 *)value);
858 if (S_ISREG(inode->i_mode)) {
859 /* Process compressed/sparsed in special way. */
861 err = ni_new_attr_flags(ni, new_fa);
868 * Thanks Mark Harmstone:
869 * Keep directory bit consistency.
871 if (S_ISDIR(inode->i_mode))
872 new_fa |= FILE_ATTRIBUTE_DIRECTORY;
874 new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
876 if (ni->std_fa != new_fa) {
878 if (new_fa & FILE_ATTRIBUTE_READONLY)
879 inode->i_mode &= ~0222;
881 inode->i_mode |= 0222;
882 /* Std attribute always in primary record. */
884 mark_inode_dirty(inode);
891 if (!strcmp(name, SYSTEM_NTFS_SECURITY)) {
892 /* system.ntfs_security*/
895 struct ATTR_STD_INFO5 *std;
897 if (!is_ntfs3(ni->mi.sbi)) {
899 * We should replace ATTR_SECURE.
900 * Skip this way cause it is nt4 feature.
906 if (!is_sd_valid(value, size)) {
910 "you try to set invalid security descriptor");
914 err = ntfs_insert_security(ni->mi.sbi, value, size,
915 &security_id, &inserted);
923 } else if (std->security_id != security_id) {
924 std->security_id = ni->std_security_id = security_id;
925 /* Std attribute always in primary record. */
927 mark_inode_dirty(&ni->vfs_inode);
933 /* Deal with NTFS extended attribute. */
934 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0,
938 inode_set_ctime_current(inode);
939 mark_inode_dirty(inode);
947 * save uid/gid/mode in xattr
949 int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size)
953 struct ntfs_inode *ni = ntfs_i(inode);
956 value = cpu_to_le32(i_uid_read(inode));
957 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
958 sizeof(value), 0, true, ea_size);
962 value = cpu_to_le32(i_gid_read(inode));
963 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
964 sizeof(value), 0, true, ea_size);
968 value = cpu_to_le32(inode->i_mode);
969 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
970 sizeof(value), 0, true, ea_size);
974 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
975 value = cpu_to_le32(inode->i_rdev);
976 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
977 sizeof(value), 0, true, ea_size);
984 /* In case of error should we delete all WSL xattr? */
991 * get uid/gid/mode from xattr
992 * it is called from ntfs_iget5->ntfs_read_mft
994 void ntfs_get_wsl_perm(struct inode *inode)
999 if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
1000 sizeof(value[0]), &sz) == sizeof(value[0]) &&
1001 ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
1002 sizeof(value[1]), &sz) == sizeof(value[1]) &&
1003 ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1004 sizeof(value[2]), &sz) == sizeof(value[2])) {
1005 i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1006 i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1007 inode->i_mode = le32_to_cpu(value[2]);
1009 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1010 &value[0], sizeof(value),
1011 &sz) == sizeof(value[0])) {
1012 inode->i_rdev = le32_to_cpu(value[0]);
1017 static bool ntfs_xattr_user_list(struct dentry *dentry)
1023 static const struct xattr_handler ntfs_other_xattr_handler = {
1025 .get = ntfs_getxattr,
1026 .set = ntfs_setxattr,
1027 .list = ntfs_xattr_user_list,
1030 const struct xattr_handler * const ntfs_xattr_handlers[] = {
1031 &ntfs_other_xattr_handler,