0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/fs.h>
0009 #include <linux/posix_acl.h>
0010 #include <linux/posix_acl_xattr.h>
0011 #include <linux/xattr.h>
0012
0013 #include "debug.h"
0014 #include "ntfs.h"
0015 #include "ntfs_fs.h"
0016
0017
0018 #define SYSTEM_DOS_ATTRIB "system.dos_attrib"
0019 #define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib"
0020 #define SYSTEM_NTFS_SECURITY "system.ntfs_security"
0021
0022
0023 static inline size_t unpacked_ea_size(const struct EA_FULL *ea)
0024 {
0025 return ea->size ? le32_to_cpu(ea->size)
0026 : ALIGN(struct_size(ea, name,
0027 1 + ea->name_len +
0028 le16_to_cpu(ea->elength)),
0029 4);
0030 }
0031
0032 static inline size_t packed_ea_size(const struct EA_FULL *ea)
0033 {
0034 return struct_size(ea, name,
0035 1 + ea->name_len + le16_to_cpu(ea->elength)) -
0036 offsetof(struct EA_FULL, flags);
0037 }
0038
0039
0040
0041
0042
0043
0044 static inline bool find_ea(const struct EA_FULL *ea_all, u32 bytes,
0045 const char *name, u8 name_len, u32 *off)
0046 {
0047 *off = 0;
0048
0049 if (!ea_all || !bytes)
0050 return false;
0051
0052 for (;;) {
0053 const struct EA_FULL *ea = Add2Ptr(ea_all, *off);
0054 u32 next_off = *off + unpacked_ea_size(ea);
0055
0056 if (next_off > bytes)
0057 return false;
0058
0059 if (ea->name_len == name_len &&
0060 !memcmp(ea->name, name, name_len))
0061 return true;
0062
0063 *off = next_off;
0064 if (next_off >= bytes)
0065 return false;
0066 }
0067 }
0068
0069
0070
0071
0072
0073
0074 static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea,
0075 size_t add_bytes, const struct EA_INFO **info)
0076 {
0077 int err;
0078 struct ntfs_sb_info *sbi = ni->mi.sbi;
0079 struct ATTR_LIST_ENTRY *le = NULL;
0080 struct ATTRIB *attr_info, *attr_ea;
0081 void *ea_p;
0082 u32 size;
0083
0084 static_assert(le32_to_cpu(ATTR_EA_INFO) < le32_to_cpu(ATTR_EA));
0085
0086 *ea = NULL;
0087 *info = NULL;
0088
0089 attr_info =
0090 ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, NULL);
0091 attr_ea =
0092 ni_find_attr(ni, attr_info, &le, ATTR_EA, NULL, 0, NULL, NULL);
0093
0094 if (!attr_ea || !attr_info)
0095 return 0;
0096
0097 *info = resident_data_ex(attr_info, sizeof(struct EA_INFO));
0098 if (!*info)
0099 return -EINVAL;
0100
0101
0102 size = le32_to_cpu((*info)->size);
0103 if (size > sbi->ea_max_size)
0104 return -EFBIG;
0105
0106 if (attr_size(attr_ea) > sbi->ea_max_size)
0107 return -EFBIG;
0108
0109
0110 ea_p = kmalloc(size + add_bytes, GFP_NOFS);
0111 if (!ea_p)
0112 return -ENOMEM;
0113
0114 if (!size) {
0115
0116 } else if (attr_ea->non_res) {
0117 struct runs_tree run;
0118
0119 run_init(&run);
0120
0121 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &run, 0, size);
0122 if (!err)
0123 err = ntfs_read_run_nb(sbi, &run, 0, ea_p, size, NULL);
0124 run_close(&run);
0125
0126 if (err)
0127 goto out;
0128 } else {
0129 void *p = resident_data_ex(attr_ea, size);
0130
0131 if (!p) {
0132 err = -EINVAL;
0133 goto out;
0134 }
0135 memcpy(ea_p, p, size);
0136 }
0137
0138 memset(Add2Ptr(ea_p, size), 0, add_bytes);
0139 *ea = ea_p;
0140 return 0;
0141
0142 out:
0143 kfree(ea_p);
0144 *ea = NULL;
0145 return err;
0146 }
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer,
0159 size_t bytes_per_buffer)
0160 {
0161 const struct EA_INFO *info;
0162 struct EA_FULL *ea_all = NULL;
0163 const struct EA_FULL *ea;
0164 u32 off, size;
0165 int err;
0166 size_t ret;
0167
0168 err = ntfs_read_ea(ni, &ea_all, 0, &info);
0169 if (err)
0170 return err;
0171
0172 if (!info || !ea_all)
0173 return 0;
0174
0175 size = le32_to_cpu(info->size);
0176
0177
0178 for (ret = 0, off = 0; off < size; off += unpacked_ea_size(ea)) {
0179 ea = Add2Ptr(ea_all, off);
0180
0181 if (buffer) {
0182 if (ret + ea->name_len + 1 > bytes_per_buffer) {
0183 err = -ERANGE;
0184 goto out;
0185 }
0186
0187 memcpy(buffer + ret, ea->name, ea->name_len);
0188 buffer[ret + ea->name_len] = 0;
0189 }
0190
0191 ret += ea->name_len + 1;
0192 }
0193
0194 out:
0195 kfree(ea_all);
0196 return err ? err : ret;
0197 }
0198
0199 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
0200 void *buffer, size_t size, size_t *required)
0201 {
0202 struct ntfs_inode *ni = ntfs_i(inode);
0203 const struct EA_INFO *info;
0204 struct EA_FULL *ea_all = NULL;
0205 const struct EA_FULL *ea;
0206 u32 off, len;
0207 int err;
0208
0209 if (!(ni->ni_flags & NI_FLAG_EA))
0210 return -ENODATA;
0211
0212 if (!required)
0213 ni_lock(ni);
0214
0215 len = 0;
0216
0217 if (name_len > 255) {
0218 err = -ENAMETOOLONG;
0219 goto out;
0220 }
0221
0222 err = ntfs_read_ea(ni, &ea_all, 0, &info);
0223 if (err)
0224 goto out;
0225
0226 if (!info)
0227 goto out;
0228
0229
0230 if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off)) {
0231 err = -ENODATA;
0232 goto out;
0233 }
0234 ea = Add2Ptr(ea_all, off);
0235
0236 len = le16_to_cpu(ea->elength);
0237 if (!buffer) {
0238 err = 0;
0239 goto out;
0240 }
0241
0242 if (len > size) {
0243 err = -ERANGE;
0244 if (required)
0245 *required = len;
0246 goto out;
0247 }
0248
0249 memcpy(buffer, ea->name + ea->name_len + 1, len);
0250 err = 0;
0251
0252 out:
0253 kfree(ea_all);
0254 if (!required)
0255 ni_unlock(ni);
0256
0257 return err ? err : len;
0258 }
0259
0260 static noinline int ntfs_set_ea(struct inode *inode, const char *name,
0261 size_t name_len, const void *value,
0262 size_t val_size, int flags, bool locked)
0263 {
0264 struct ntfs_inode *ni = ntfs_i(inode);
0265 struct ntfs_sb_info *sbi = ni->mi.sbi;
0266 int err;
0267 struct EA_INFO ea_info;
0268 const struct EA_INFO *info;
0269 struct EA_FULL *new_ea;
0270 struct EA_FULL *ea_all = NULL;
0271 size_t add, new_pack;
0272 u32 off, size;
0273 __le16 size_pack;
0274 struct ATTRIB *attr;
0275 struct ATTR_LIST_ENTRY *le;
0276 struct mft_inode *mi;
0277 struct runs_tree ea_run;
0278 u64 new_sz;
0279 void *p;
0280
0281 if (!locked)
0282 ni_lock(ni);
0283
0284 run_init(&ea_run);
0285
0286 if (name_len > 255) {
0287 err = -ENAMETOOLONG;
0288 goto out;
0289 }
0290
0291 add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
0292
0293 err = ntfs_read_ea(ni, &ea_all, add, &info);
0294 if (err)
0295 goto out;
0296
0297 if (!info) {
0298 memset(&ea_info, 0, sizeof(ea_info));
0299 size = 0;
0300 size_pack = 0;
0301 } else {
0302 memcpy(&ea_info, info, sizeof(ea_info));
0303 size = le32_to_cpu(ea_info.size);
0304 size_pack = ea_info.size_pack;
0305 }
0306
0307 if (info && find_ea(ea_all, size, name, name_len, &off)) {
0308 struct EA_FULL *ea;
0309 size_t ea_sz;
0310
0311 if (flags & XATTR_CREATE) {
0312 err = -EEXIST;
0313 goto out;
0314 }
0315
0316 ea = Add2Ptr(ea_all, off);
0317
0318
0319
0320
0321
0322 if (val_size && le16_to_cpu(ea->elength) == val_size &&
0323 !memcmp(ea->name + ea->name_len + 1, value, val_size)) {
0324
0325 goto out;
0326 }
0327
0328
0329 if (ea->flags & FILE_NEED_EA)
0330 le16_add_cpu(&ea_info.count, -1);
0331
0332 ea_sz = unpacked_ea_size(ea);
0333
0334 le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea));
0335
0336 memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz);
0337
0338 size -= ea_sz;
0339 memset(Add2Ptr(ea_all, size), 0, ea_sz);
0340
0341 ea_info.size = cpu_to_le32(size);
0342
0343 if ((flags & XATTR_REPLACE) && !val_size) {
0344
0345 goto update_ea;
0346 }
0347 } else {
0348 if (flags & XATTR_REPLACE) {
0349 err = -ENODATA;
0350 goto out;
0351 }
0352
0353 if (!ea_all) {
0354 ea_all = kzalloc(add, GFP_NOFS);
0355 if (!ea_all) {
0356 err = -ENOMEM;
0357 goto out;
0358 }
0359 }
0360 }
0361
0362
0363 new_ea = Add2Ptr(ea_all, size);
0364 new_ea->size = cpu_to_le32(add);
0365 new_ea->flags = 0;
0366 new_ea->name_len = name_len;
0367 new_ea->elength = cpu_to_le16(val_size);
0368 memcpy(new_ea->name, name, name_len);
0369 new_ea->name[name_len] = 0;
0370 memcpy(new_ea->name + name_len + 1, value, val_size);
0371 new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea);
0372 ea_info.size_pack = cpu_to_le16(new_pack);
0373
0374 size += add;
0375 ea_info.size = cpu_to_le32(size);
0376
0377
0378
0379
0380
0381 if (new_pack > 0xffff || size > sbi->ea_max_size) {
0382 ntfs_inode_warn(
0383 inode,
0384 "The size of extended attributes must not exceed 64KiB");
0385 err = -EFBIG;
0386 goto out;
0387 }
0388
0389 update_ea:
0390
0391 if (!info) {
0392
0393 if (!size) {
0394 err = 0;
0395 goto out;
0396 }
0397
0398 err = ni_insert_resident(ni, sizeof(struct EA_INFO),
0399 ATTR_EA_INFO, NULL, 0, NULL, NULL,
0400 NULL);
0401 if (err)
0402 goto out;
0403
0404 err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL,
0405 NULL);
0406 if (err)
0407 goto out;
0408 }
0409
0410 new_sz = size;
0411 err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz,
0412 false, NULL);
0413 if (err)
0414 goto out;
0415
0416 le = NULL;
0417 attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi);
0418 if (!attr) {
0419 err = -EINVAL;
0420 goto out;
0421 }
0422
0423 if (!size) {
0424
0425 ni_remove_attr_le(ni, attr, mi, le);
0426 } else {
0427 p = resident_data_ex(attr, sizeof(struct EA_INFO));
0428 if (!p) {
0429 err = -EINVAL;
0430 goto out;
0431 }
0432 memcpy(p, &ea_info, sizeof(struct EA_INFO));
0433 mi->dirty = true;
0434 }
0435
0436 le = NULL;
0437 attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi);
0438 if (!attr) {
0439 err = -EINVAL;
0440 goto out;
0441 }
0442
0443 if (!size) {
0444
0445 ni_remove_attr_le(ni, attr, mi, le);
0446 } else if (attr->non_res) {
0447 err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0,
0448 size);
0449 if (err)
0450 goto out;
0451
0452 err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0);
0453 if (err)
0454 goto out;
0455 } else {
0456 p = resident_data_ex(attr, size);
0457 if (!p) {
0458 err = -EINVAL;
0459 goto out;
0460 }
0461 memcpy(p, ea_all, size);
0462 mi->dirty = true;
0463 }
0464
0465
0466 if (size)
0467 ni->ni_flags |= NI_FLAG_EA;
0468 else
0469 ni->ni_flags &= ~NI_FLAG_EA;
0470
0471 if (ea_info.size_pack != size_pack)
0472 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
0473 mark_inode_dirty(&ni->vfs_inode);
0474
0475 out:
0476 if (!locked)
0477 ni_unlock(ni);
0478
0479 run_close(&ea_run);
0480 kfree(ea_all);
0481
0482 return err;
0483 }
0484
0485 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
0486 static struct posix_acl *ntfs_get_acl_ex(struct inode *inode, int type,
0487 int locked)
0488 {
0489 struct ntfs_inode *ni = ntfs_i(inode);
0490 const char *name;
0491 size_t name_len;
0492 struct posix_acl *acl;
0493 size_t req;
0494 int err;
0495 void *buf;
0496
0497
0498 buf = __getname();
0499 if (!buf)
0500 return ERR_PTR(-ENOMEM);
0501
0502
0503 if (type == ACL_TYPE_ACCESS) {
0504 name = XATTR_NAME_POSIX_ACL_ACCESS;
0505 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
0506 } else {
0507 name = XATTR_NAME_POSIX_ACL_DEFAULT;
0508 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
0509 }
0510
0511 if (!locked)
0512 ni_lock(ni);
0513
0514 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req);
0515
0516 if (!locked)
0517 ni_unlock(ni);
0518
0519
0520 if (err >= 0) {
0521 acl = posix_acl_from_xattr(&init_user_ns, buf, err);
0522 } else if (err == -ENODATA) {
0523 acl = NULL;
0524 } else {
0525 acl = ERR_PTR(err);
0526 }
0527
0528 if (!IS_ERR(acl))
0529 set_cached_acl(inode, type, acl);
0530
0531 __putname(buf);
0532
0533 return acl;
0534 }
0535
0536
0537
0538
0539 struct posix_acl *ntfs_get_acl(struct inode *inode, int type, bool rcu)
0540 {
0541 if (rcu)
0542 return ERR_PTR(-ECHILD);
0543
0544 return ntfs_get_acl_ex(inode, type, 0);
0545 }
0546
0547 static noinline int ntfs_set_acl_ex(struct user_namespace *mnt_userns,
0548 struct inode *inode, struct posix_acl *acl,
0549 int type, bool init_acl)
0550 {
0551 const char *name;
0552 size_t size, name_len;
0553 void *value;
0554 int err;
0555 int flags;
0556 umode_t mode;
0557
0558 if (S_ISLNK(inode->i_mode))
0559 return -EOPNOTSUPP;
0560
0561 mode = inode->i_mode;
0562 switch (type) {
0563 case ACL_TYPE_ACCESS:
0564
0565 if (acl && !init_acl) {
0566 err = posix_acl_update_mode(mnt_userns, inode, &mode,
0567 &acl);
0568 if (err)
0569 return err;
0570 }
0571 name = XATTR_NAME_POSIX_ACL_ACCESS;
0572 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
0573 break;
0574
0575 case ACL_TYPE_DEFAULT:
0576 if (!S_ISDIR(inode->i_mode))
0577 return acl ? -EACCES : 0;
0578 name = XATTR_NAME_POSIX_ACL_DEFAULT;
0579 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
0580 break;
0581
0582 default:
0583 return -EINVAL;
0584 }
0585
0586 if (!acl) {
0587
0588 size = 0;
0589 value = NULL;
0590 flags = XATTR_REPLACE;
0591 } else {
0592 size = posix_acl_xattr_size(acl->a_count);
0593 value = kmalloc(size, GFP_NOFS);
0594 if (!value)
0595 return -ENOMEM;
0596 err = posix_acl_to_xattr(&init_user_ns, acl, value, size);
0597 if (err < 0)
0598 goto out;
0599 flags = 0;
0600 }
0601
0602 err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0);
0603 if (err == -ENODATA && !size)
0604 err = 0;
0605 if (!err) {
0606 set_cached_acl(inode, type, acl);
0607 if (inode->i_mode != mode) {
0608 inode->i_mode = mode;
0609 mark_inode_dirty(inode);
0610 }
0611 }
0612
0613 out:
0614 kfree(value);
0615
0616 return err;
0617 }
0618
0619
0620
0621
0622 int ntfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0623 struct posix_acl *acl, int type)
0624 {
0625 return ntfs_set_acl_ex(mnt_userns, inode, acl, type, false);
0626 }
0627
0628 static int ntfs_xattr_get_acl(struct user_namespace *mnt_userns,
0629 struct inode *inode, int type, void *buffer,
0630 size_t size)
0631 {
0632 struct posix_acl *acl;
0633 int err;
0634
0635 if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
0636 ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
0637 return -EOPNOTSUPP;
0638 }
0639
0640 acl = ntfs_get_acl(inode, type, false);
0641 if (IS_ERR(acl))
0642 return PTR_ERR(acl);
0643
0644 if (!acl)
0645 return -ENODATA;
0646
0647 err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
0648 posix_acl_release(acl);
0649
0650 return err;
0651 }
0652
0653 static int ntfs_xattr_set_acl(struct user_namespace *mnt_userns,
0654 struct inode *inode, int type, const void *value,
0655 size_t size)
0656 {
0657 struct posix_acl *acl;
0658 int err;
0659
0660 if (!(inode->i_sb->s_flags & SB_POSIXACL)) {
0661 ntfs_inode_warn(inode, "add mount option \"acl\" to use acl");
0662 return -EOPNOTSUPP;
0663 }
0664
0665 if (!inode_owner_or_capable(mnt_userns, inode))
0666 return -EPERM;
0667
0668 if (!value) {
0669 acl = NULL;
0670 } else {
0671 acl = posix_acl_from_xattr(&init_user_ns, value, size);
0672 if (IS_ERR(acl))
0673 return PTR_ERR(acl);
0674
0675 if (acl) {
0676 err = posix_acl_valid(&init_user_ns, acl);
0677 if (err)
0678 goto release_and_out;
0679 }
0680 }
0681
0682 err = ntfs_set_acl(mnt_userns, inode, acl, type);
0683
0684 release_and_out:
0685 posix_acl_release(acl);
0686 return err;
0687 }
0688
0689
0690
0691
0692
0693
0694 int ntfs_init_acl(struct user_namespace *mnt_userns, struct inode *inode,
0695 struct inode *dir)
0696 {
0697 struct posix_acl *default_acl, *acl;
0698 int err;
0699
0700 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
0701 if (err)
0702 return err;
0703
0704 if (default_acl) {
0705 err = ntfs_set_acl_ex(mnt_userns, inode, default_acl,
0706 ACL_TYPE_DEFAULT, true);
0707 posix_acl_release(default_acl);
0708 } else {
0709 inode->i_default_acl = NULL;
0710 }
0711
0712 if (acl) {
0713 if (!err)
0714 err = ntfs_set_acl_ex(mnt_userns, inode, acl,
0715 ACL_TYPE_ACCESS, true);
0716 posix_acl_release(acl);
0717 } else {
0718 inode->i_acl = NULL;
0719 }
0720
0721 return err;
0722 }
0723 #endif
0724
0725
0726
0727
0728 int ntfs_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode)
0729 {
0730 struct super_block *sb = inode->i_sb;
0731
0732 if (!(sb->s_flags & SB_POSIXACL))
0733 return 0;
0734
0735 if (S_ISLNK(inode->i_mode))
0736 return -EOPNOTSUPP;
0737
0738 return posix_acl_chmod(mnt_userns, inode, inode->i_mode);
0739 }
0740
0741
0742
0743
0744 int ntfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
0745 int mask)
0746 {
0747 if (ntfs_sb(inode->i_sb)->options->noacsrules) {
0748
0749 return 0;
0750 }
0751
0752 return generic_permission(mnt_userns, inode, mask);
0753 }
0754
0755
0756
0757
0758 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
0759 {
0760 struct inode *inode = d_inode(dentry);
0761 struct ntfs_inode *ni = ntfs_i(inode);
0762 ssize_t ret;
0763
0764 if (!(ni->ni_flags & NI_FLAG_EA)) {
0765
0766 return 0;
0767 }
0768
0769 ni_lock(ni);
0770
0771 ret = ntfs_list_ea(ni, buffer, size);
0772
0773 ni_unlock(ni);
0774
0775 return ret;
0776 }
0777
0778 static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de,
0779 struct inode *inode, const char *name, void *buffer,
0780 size_t size)
0781 {
0782 int err;
0783 struct ntfs_inode *ni = ntfs_i(inode);
0784 size_t name_len = strlen(name);
0785
0786
0787 if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
0788 !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
0789
0790 if (!buffer) {
0791 err = sizeof(u8);
0792 } else if (size < sizeof(u8)) {
0793 err = -ENODATA;
0794 } else {
0795 err = sizeof(u8);
0796 *(u8 *)buffer = le32_to_cpu(ni->std_fa);
0797 }
0798 goto out;
0799 }
0800
0801 if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
0802 !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
0803
0804 if (!buffer) {
0805 err = sizeof(u32);
0806 } else if (size < sizeof(u32)) {
0807 err = -ENODATA;
0808 } else {
0809 err = sizeof(u32);
0810 *(u32 *)buffer = le32_to_cpu(ni->std_fa);
0811 }
0812 goto out;
0813 }
0814
0815 if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
0816 !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
0817
0818 struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL;
0819 size_t sd_size = 0;
0820
0821 if (!is_ntfs3(ni->mi.sbi)) {
0822
0823 err = -EINVAL;
0824 goto out;
0825 } else if (le32_to_cpu(ni->std_security_id) <
0826 SECURITY_ID_FIRST) {
0827 err = -ENOENT;
0828 goto out;
0829 }
0830
0831 err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id,
0832 &sd, &sd_size);
0833 if (err)
0834 goto out;
0835
0836 if (!is_sd_valid(sd, sd_size)) {
0837 ntfs_inode_warn(
0838 inode,
0839 "looks like you get incorrect security descriptor id=%u",
0840 ni->std_security_id);
0841 }
0842
0843 if (!buffer) {
0844 err = sd_size;
0845 } else if (size < sd_size) {
0846 err = -ENODATA;
0847 } else {
0848 err = sd_size;
0849 memcpy(buffer, sd, sd_size);
0850 }
0851 kfree(sd);
0852 goto out;
0853 }
0854
0855 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
0856 if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
0857 !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
0858 sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
0859 (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
0860 !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
0861 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
0862
0863 err = ntfs_xattr_get_acl(
0864 &init_user_ns, inode,
0865 name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
0866 ? ACL_TYPE_ACCESS
0867 : ACL_TYPE_DEFAULT,
0868 buffer, size);
0869 goto out;
0870 }
0871 #endif
0872
0873 err = ntfs_get_ea(inode, name, name_len, buffer, size, NULL);
0874
0875 out:
0876 return err;
0877 }
0878
0879
0880
0881
0882 static noinline int ntfs_setxattr(const struct xattr_handler *handler,
0883 struct user_namespace *mnt_userns,
0884 struct dentry *de, struct inode *inode,
0885 const char *name, const void *value,
0886 size_t size, int flags)
0887 {
0888 int err = -EINVAL;
0889 struct ntfs_inode *ni = ntfs_i(inode);
0890 size_t name_len = strlen(name);
0891 enum FILE_ATTRIBUTE new_fa;
0892
0893
0894 if (name_len == sizeof(SYSTEM_DOS_ATTRIB) - 1 &&
0895 !memcmp(name, SYSTEM_DOS_ATTRIB, sizeof(SYSTEM_DOS_ATTRIB))) {
0896 if (sizeof(u8) != size)
0897 goto out;
0898 new_fa = cpu_to_le32(*(u8 *)value);
0899 goto set_new_fa;
0900 }
0901
0902 if (name_len == sizeof(SYSTEM_NTFS_ATTRIB) - 1 &&
0903 !memcmp(name, SYSTEM_NTFS_ATTRIB, sizeof(SYSTEM_NTFS_ATTRIB))) {
0904 if (size != sizeof(u32))
0905 goto out;
0906 new_fa = cpu_to_le32(*(u32 *)value);
0907
0908 if (S_ISREG(inode->i_mode)) {
0909
0910 ni_lock(ni);
0911 err = ni_new_attr_flags(ni, new_fa);
0912 ni_unlock(ni);
0913 if (err)
0914 goto out;
0915 }
0916 set_new_fa:
0917
0918
0919
0920
0921 if (S_ISDIR(inode->i_mode))
0922 new_fa |= FILE_ATTRIBUTE_DIRECTORY;
0923 else
0924 new_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
0925
0926 if (ni->std_fa != new_fa) {
0927 ni->std_fa = new_fa;
0928 if (new_fa & FILE_ATTRIBUTE_READONLY)
0929 inode->i_mode &= ~0222;
0930 else
0931 inode->i_mode |= 0222;
0932
0933 ni->mi.dirty = true;
0934 mark_inode_dirty(inode);
0935 }
0936 err = 0;
0937
0938 goto out;
0939 }
0940
0941 if (name_len == sizeof(SYSTEM_NTFS_SECURITY) - 1 &&
0942 !memcmp(name, SYSTEM_NTFS_SECURITY, sizeof(SYSTEM_NTFS_SECURITY))) {
0943
0944 __le32 security_id;
0945 bool inserted;
0946 struct ATTR_STD_INFO5 *std;
0947
0948 if (!is_ntfs3(ni->mi.sbi)) {
0949
0950
0951
0952
0953 err = -EINVAL;
0954 goto out;
0955 }
0956
0957 if (!is_sd_valid(value, size)) {
0958 err = -EINVAL;
0959 ntfs_inode_warn(
0960 inode,
0961 "you try to set invalid security descriptor");
0962 goto out;
0963 }
0964
0965 err = ntfs_insert_security(ni->mi.sbi, value, size,
0966 &security_id, &inserted);
0967 if (err)
0968 goto out;
0969
0970 ni_lock(ni);
0971 std = ni_std5(ni);
0972 if (!std) {
0973 err = -EINVAL;
0974 } else if (std->security_id != security_id) {
0975 std->security_id = ni->std_security_id = security_id;
0976
0977 ni->mi.dirty = true;
0978 mark_inode_dirty(&ni->vfs_inode);
0979 }
0980 ni_unlock(ni);
0981 goto out;
0982 }
0983
0984 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
0985 if ((name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1 &&
0986 !memcmp(name, XATTR_NAME_POSIX_ACL_ACCESS,
0987 sizeof(XATTR_NAME_POSIX_ACL_ACCESS))) ||
0988 (name_len == sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1 &&
0989 !memcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT,
0990 sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)))) {
0991 err = ntfs_xattr_set_acl(
0992 mnt_userns, inode,
0993 name_len == sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1
0994 ? ACL_TYPE_ACCESS
0995 : ACL_TYPE_DEFAULT,
0996 value, size);
0997 goto out;
0998 }
0999 #endif
1000
1001 err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0);
1002
1003 out:
1004 inode->i_ctime = current_time(inode);
1005 mark_inode_dirty(inode);
1006
1007 return err;
1008 }
1009
1010
1011
1012
1013
1014
1015 int ntfs_save_wsl_perm(struct inode *inode)
1016 {
1017 int err;
1018 __le32 value;
1019 struct ntfs_inode *ni = ntfs_i(inode);
1020
1021 ni_lock(ni);
1022 value = cpu_to_le32(i_uid_read(inode));
1023 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value,
1024 sizeof(value), 0, true);
1025 if (err)
1026 goto out;
1027
1028 value = cpu_to_le32(i_gid_read(inode));
1029 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value,
1030 sizeof(value), 0, true);
1031 if (err)
1032 goto out;
1033
1034 value = cpu_to_le32(inode->i_mode);
1035 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value,
1036 sizeof(value), 0, true);
1037 if (err)
1038 goto out;
1039
1040 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1041 value = cpu_to_le32(inode->i_rdev);
1042 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value,
1043 sizeof(value), 0, true);
1044 if (err)
1045 goto out;
1046 }
1047
1048 out:
1049 ni_unlock(ni);
1050
1051 return err;
1052 }
1053
1054
1055
1056
1057
1058
1059
1060 void ntfs_get_wsl_perm(struct inode *inode)
1061 {
1062 size_t sz;
1063 __le32 value[3];
1064
1065 if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0],
1066 sizeof(value[0]), &sz) == sizeof(value[0]) &&
1067 ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1],
1068 sizeof(value[1]), &sz) == sizeof(value[1]) &&
1069 ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2],
1070 sizeof(value[2]), &sz) == sizeof(value[2])) {
1071 i_uid_write(inode, (uid_t)le32_to_cpu(value[0]));
1072 i_gid_write(inode, (gid_t)le32_to_cpu(value[1]));
1073 inode->i_mode = le32_to_cpu(value[2]);
1074
1075 if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1,
1076 &value[0], sizeof(value),
1077 &sz) == sizeof(value[0])) {
1078 inode->i_rdev = le32_to_cpu(value[0]);
1079 }
1080 }
1081 }
1082
1083 static bool ntfs_xattr_user_list(struct dentry *dentry)
1084 {
1085 return true;
1086 }
1087
1088
1089 static const struct xattr_handler ntfs_xattr_handler = {
1090 .prefix = "",
1091 .get = ntfs_getxattr,
1092 .set = ntfs_setxattr,
1093 .list = ntfs_xattr_user_list,
1094 };
1095
1096 const struct xattr_handler *ntfs_xattr_handlers[] = {
1097 &ntfs_xattr_handler,
1098 NULL,
1099 };
1100