1be71b5cbSKonstantin Komarov // SPDX-License-Identifier: GPL-2.0 2be71b5cbSKonstantin Komarov /* 3be71b5cbSKonstantin Komarov * 4be71b5cbSKonstantin Komarov * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5be71b5cbSKonstantin Komarov * 6be71b5cbSKonstantin Komarov */ 7be71b5cbSKonstantin Komarov 8be71b5cbSKonstantin Komarov #include <linux/fs.h> 9be71b5cbSKonstantin Komarov #include <linux/posix_acl.h> 10be71b5cbSKonstantin Komarov #include <linux/posix_acl_xattr.h> 11be71b5cbSKonstantin Komarov #include <linux/xattr.h> 12be71b5cbSKonstantin Komarov 13be71b5cbSKonstantin Komarov #include "debug.h" 14be71b5cbSKonstantin Komarov #include "ntfs.h" 15be71b5cbSKonstantin Komarov #include "ntfs_fs.h" 16be71b5cbSKonstantin Komarov 17be71b5cbSKonstantin Komarov // clang-format off 18be71b5cbSKonstantin Komarov #define SYSTEM_DOS_ATTRIB "system.dos_attrib" 19be71b5cbSKonstantin Komarov #define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" 200d19f3d7SDaniel Pinto #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" 21be71b5cbSKonstantin Komarov #define SYSTEM_NTFS_SECURITY "system.ntfs_security" 22be71b5cbSKonstantin Komarov // clang-format on 23be71b5cbSKonstantin Komarov 24be71b5cbSKonstantin Komarov static inline size_t unpacked_ea_size(const struct EA_FULL *ea) 25be71b5cbSKonstantin Komarov { 2696de65a9SKonstantin Komarov return ea->size ? le32_to_cpu(ea->size) : 2796de65a9SKonstantin Komarov ALIGN(struct_size(ea, name, 28d3624466SKonstantin Komarov 1 + ea->name_len + 29d3624466SKonstantin Komarov le16_to_cpu(ea->elength)), 30d3624466SKonstantin Komarov 4); 31be71b5cbSKonstantin Komarov } 32be71b5cbSKonstantin Komarov 33be71b5cbSKonstantin Komarov static inline size_t packed_ea_size(const struct EA_FULL *ea) 34be71b5cbSKonstantin Komarov { 35be71b5cbSKonstantin Komarov return struct_size(ea, name, 36be71b5cbSKonstantin Komarov 1 + ea->name_len + le16_to_cpu(ea->elength)) - 37be71b5cbSKonstantin Komarov offsetof(struct EA_FULL, flags); 38be71b5cbSKonstantin Komarov } 39be71b5cbSKonstantin Komarov 40be71b5cbSKonstantin Komarov /* 41be71b5cbSKonstantin Komarov * find_ea 42be71b5cbSKonstantin Komarov * 43e8b8e97fSKari Argillander * Assume there is at least one xattr in the list. 44be71b5cbSKonstantin Komarov */ 45be71b5cbSKonstantin Komarov static inline bool find_ea(const struct EA_FULL *ea_all, u32 bytes, 460e8235d2SKonstantin Komarov const char *name, u8 name_len, u32 *off, u32 *ea_sz) 47be71b5cbSKonstantin Komarov { 480e8235d2SKonstantin Komarov u32 ea_size; 490e8235d2SKonstantin Komarov 50be71b5cbSKonstantin Komarov *off = 0; 510e8235d2SKonstantin Komarov if (!ea_all) 52be71b5cbSKonstantin Komarov return false; 53be71b5cbSKonstantin Komarov 540e8235d2SKonstantin Komarov for (; *off < bytes; *off += ea_size) { 55be71b5cbSKonstantin Komarov const struct EA_FULL *ea = Add2Ptr(ea_all, *off); 560e8235d2SKonstantin Komarov ea_size = unpacked_ea_size(ea); 57be71b5cbSKonstantin Komarov if (ea->name_len == name_len && 580e8235d2SKonstantin Komarov !memcmp(ea->name, name, name_len)) { 590e8235d2SKonstantin Komarov if (ea_sz) 600e8235d2SKonstantin Komarov *ea_sz = ea_size; 61be71b5cbSKonstantin Komarov return true; 62be71b5cbSKonstantin Komarov } 63be71b5cbSKonstantin Komarov } 64be71b5cbSKonstantin Komarov 650e8235d2SKonstantin Komarov return false; 660e8235d2SKonstantin Komarov } 670e8235d2SKonstantin Komarov 68be71b5cbSKonstantin Komarov /* 69e8b8e97fSKari Argillander * ntfs_read_ea - Read all extended attributes. 70e8b8e97fSKari Argillander * @ea: New allocated memory. 71e8b8e97fSKari Argillander * @info: Pointer into resident data. 72be71b5cbSKonstantin Komarov */ 73be71b5cbSKonstantin Komarov static int ntfs_read_ea(struct ntfs_inode *ni, struct EA_FULL **ea, 74be71b5cbSKonstantin Komarov size_t add_bytes, const struct EA_INFO **info) 75be71b5cbSKonstantin Komarov { 760e8235d2SKonstantin Komarov int err = -EINVAL; 77cff32466SKonstantin Komarov struct ntfs_sb_info *sbi = ni->mi.sbi; 78be71b5cbSKonstantin Komarov struct ATTR_LIST_ENTRY *le = NULL; 79be71b5cbSKonstantin Komarov struct ATTRIB *attr_info, *attr_ea; 80be71b5cbSKonstantin Komarov void *ea_p; 810e8235d2SKonstantin Komarov u32 size, off, ea_size; 82be71b5cbSKonstantin Komarov 83be71b5cbSKonstantin Komarov static_assert(le32_to_cpu(ATTR_EA_INFO) < le32_to_cpu(ATTR_EA)); 84be71b5cbSKonstantin Komarov 85be71b5cbSKonstantin Komarov *ea = NULL; 86be71b5cbSKonstantin Komarov *info = NULL; 87be71b5cbSKonstantin Komarov 88be71b5cbSKonstantin Komarov attr_info = 89be71b5cbSKonstantin Komarov ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, NULL); 90be71b5cbSKonstantin Komarov attr_ea = 91be71b5cbSKonstantin Komarov ni_find_attr(ni, attr_info, &le, ATTR_EA, NULL, 0, NULL, NULL); 92be71b5cbSKonstantin Komarov 93be71b5cbSKonstantin Komarov if (!attr_ea || !attr_info) 94be71b5cbSKonstantin Komarov return 0; 95be71b5cbSKonstantin Komarov 96be71b5cbSKonstantin Komarov *info = resident_data_ex(attr_info, sizeof(struct EA_INFO)); 97be71b5cbSKonstantin Komarov if (!*info) 980e8235d2SKonstantin Komarov goto out; 99be71b5cbSKonstantin Komarov 100e8b8e97fSKari Argillander /* Check Ea limit. */ 101be71b5cbSKonstantin Komarov size = le32_to_cpu((*info)->size); 1020e8235d2SKonstantin Komarov if (size > sbi->ea_max_size) { 1030e8235d2SKonstantin Komarov err = -EFBIG; 1040e8235d2SKonstantin Komarov goto out; 1050e8235d2SKonstantin Komarov } 106be71b5cbSKonstantin Komarov 1070e8235d2SKonstantin Komarov if (attr_size(attr_ea) > sbi->ea_max_size) { 1080e8235d2SKonstantin Komarov err = -EFBIG; 1090e8235d2SKonstantin Komarov goto out; 1100e8235d2SKonstantin Komarov } 111be71b5cbSKonstantin Komarov 112cff32466SKonstantin Komarov if (!size) { 11387e21c99SKonstantin Komarov /* EA info persists, but xattr is empty. Looks like EA problem. */ 1140e8235d2SKonstantin Komarov goto out; 1150e8235d2SKonstantin Komarov } 116be71b5cbSKonstantin Komarov 117be71b5cbSKonstantin Komarov /* Allocate memory for packed Ea. */ 118e001e608SDan Carpenter ea_p = kmalloc(size_add(size, add_bytes), GFP_NOFS); 119be71b5cbSKonstantin Komarov if (!ea_p) 120be71b5cbSKonstantin Komarov return -ENOMEM; 121be71b5cbSKonstantin Komarov 1220e8235d2SKonstantin Komarov if (attr_ea->non_res) { 123be71b5cbSKonstantin Komarov struct runs_tree run; 124be71b5cbSKonstantin Komarov 125be71b5cbSKonstantin Komarov run_init(&run); 126be71b5cbSKonstantin Komarov 12742f86b12SKonstantin Komarov err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &run, 0, size); 128be71b5cbSKonstantin Komarov if (!err) 129cff32466SKonstantin Komarov err = ntfs_read_run_nb(sbi, &run, 0, ea_p, size, NULL); 130be71b5cbSKonstantin Komarov run_close(&run); 131be71b5cbSKonstantin Komarov 132be71b5cbSKonstantin Komarov if (err) 1330e8235d2SKonstantin Komarov goto out1; 134be71b5cbSKonstantin Komarov } else { 135be71b5cbSKonstantin Komarov void *p = resident_data_ex(attr_ea, size); 136be71b5cbSKonstantin Komarov 1370e8235d2SKonstantin Komarov if (!p) 1380e8235d2SKonstantin Komarov goto out1; 139be71b5cbSKonstantin Komarov memcpy(ea_p, p, size); 140be71b5cbSKonstantin Komarov } 141be71b5cbSKonstantin Komarov 142be71b5cbSKonstantin Komarov memset(Add2Ptr(ea_p, size), 0, add_bytes); 1430e8235d2SKonstantin Komarov 144c9db0ff0SEdward Lo err = -EINVAL; 1450e8235d2SKonstantin Komarov /* Check all attributes for consistency. */ 1460e8235d2SKonstantin Komarov for (off = 0; off < size; off += ea_size) { 1470e8235d2SKonstantin Komarov const struct EA_FULL *ef = Add2Ptr(ea_p, off); 1480e8235d2SKonstantin Komarov u32 bytes = size - off; 1490e8235d2SKonstantin Komarov 1500e8235d2SKonstantin Komarov /* Check if we can use field ea->size. */ 1510e8235d2SKonstantin Komarov if (bytes < sizeof(ef->size)) 1520e8235d2SKonstantin Komarov goto out1; 1530e8235d2SKonstantin Komarov 1540e8235d2SKonstantin Komarov if (ef->size) { 1550e8235d2SKonstantin Komarov ea_size = le32_to_cpu(ef->size); 1560e8235d2SKonstantin Komarov if (ea_size > bytes) 1570e8235d2SKonstantin Komarov goto out1; 1580e8235d2SKonstantin Komarov continue; 1590e8235d2SKonstantin Komarov } 1600e8235d2SKonstantin Komarov 1610e8235d2SKonstantin Komarov /* Check if we can use fields ef->name_len and ef->elength. */ 1620e8235d2SKonstantin Komarov if (bytes < offsetof(struct EA_FULL, name)) 1630e8235d2SKonstantin Komarov goto out1; 1640e8235d2SKonstantin Komarov 1650e8235d2SKonstantin Komarov ea_size = ALIGN(struct_size(ef, name, 1660e8235d2SKonstantin Komarov 1 + ef->name_len + 1670e8235d2SKonstantin Komarov le16_to_cpu(ef->elength)), 1680e8235d2SKonstantin Komarov 4); 1690e8235d2SKonstantin Komarov if (ea_size > bytes) 1700e8235d2SKonstantin Komarov goto out1; 1710e8235d2SKonstantin Komarov } 1720e8235d2SKonstantin Komarov 173be71b5cbSKonstantin Komarov *ea = ea_p; 174be71b5cbSKonstantin Komarov return 0; 175be71b5cbSKonstantin Komarov 1760e8235d2SKonstantin Komarov out1: 177195c52bdSKari Argillander kfree(ea_p); 1780e8235d2SKonstantin Komarov out: 1790e8235d2SKonstantin Komarov ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 180be71b5cbSKonstantin Komarov return err; 181be71b5cbSKonstantin Komarov } 182be71b5cbSKonstantin Komarov 183be71b5cbSKonstantin Komarov /* 184be71b5cbSKonstantin Komarov * ntfs_list_ea 185be71b5cbSKonstantin Komarov * 186e8b8e97fSKari Argillander * Copy a list of xattrs names into the buffer 187e8b8e97fSKari Argillander * provided, or compute the buffer size required. 188be71b5cbSKonstantin Komarov * 189e8b8e97fSKari Argillander * Return: 190e8b8e97fSKari Argillander * * Number of bytes used / required on 191e8b8e97fSKari Argillander * * -ERRNO - on failure 192be71b5cbSKonstantin Komarov */ 193be71b5cbSKonstantin Komarov static ssize_t ntfs_list_ea(struct ntfs_inode *ni, char *buffer, 194be71b5cbSKonstantin Komarov size_t bytes_per_buffer) 195be71b5cbSKonstantin Komarov { 196be71b5cbSKonstantin Komarov const struct EA_INFO *info; 197be71b5cbSKonstantin Komarov struct EA_FULL *ea_all = NULL; 198be71b5cbSKonstantin Komarov const struct EA_FULL *ea; 199be71b5cbSKonstantin Komarov u32 off, size; 200be71b5cbSKonstantin Komarov int err; 2010e8235d2SKonstantin Komarov int ea_size; 202be71b5cbSKonstantin Komarov size_t ret; 203be71b5cbSKonstantin Komarov 204be71b5cbSKonstantin Komarov err = ntfs_read_ea(ni, &ea_all, 0, &info); 205be71b5cbSKonstantin Komarov if (err) 206be71b5cbSKonstantin Komarov return err; 207be71b5cbSKonstantin Komarov 208be71b5cbSKonstantin Komarov if (!info || !ea_all) 209be71b5cbSKonstantin Komarov return 0; 210be71b5cbSKonstantin Komarov 211be71b5cbSKonstantin Komarov size = le32_to_cpu(info->size); 212be71b5cbSKonstantin Komarov 213e8b8e97fSKari Argillander /* Enumerate all xattrs. */ 2140e8235d2SKonstantin Komarov for (ret = 0, off = 0; off < size; off += ea_size) { 215be71b5cbSKonstantin Komarov ea = Add2Ptr(ea_all, off); 2160e8235d2SKonstantin Komarov ea_size = unpacked_ea_size(ea); 217be71b5cbSKonstantin Komarov 2183c675ddfSZeng Heng if (!ea->name_len) 2193c675ddfSZeng Heng break; 2203c675ddfSZeng Heng 221be71b5cbSKonstantin Komarov if (buffer) { 222be71b5cbSKonstantin Komarov if (ret + ea->name_len + 1 > bytes_per_buffer) { 223be71b5cbSKonstantin Komarov err = -ERANGE; 224be71b5cbSKonstantin Komarov goto out; 225be71b5cbSKonstantin Komarov } 226be71b5cbSKonstantin Komarov 227be71b5cbSKonstantin Komarov memcpy(buffer + ret, ea->name, ea->name_len); 228be71b5cbSKonstantin Komarov buffer[ret + ea->name_len] = 0; 229be71b5cbSKonstantin Komarov } 230be71b5cbSKonstantin Komarov 231be71b5cbSKonstantin Komarov ret += ea->name_len + 1; 232be71b5cbSKonstantin Komarov } 233be71b5cbSKonstantin Komarov 234be71b5cbSKonstantin Komarov out: 235195c52bdSKari Argillander kfree(ea_all); 236be71b5cbSKonstantin Komarov return err ? err : ret; 237be71b5cbSKonstantin Komarov } 238be71b5cbSKonstantin Komarov 239be71b5cbSKonstantin Komarov static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len, 240be71b5cbSKonstantin Komarov void *buffer, size_t size, size_t *required) 241be71b5cbSKonstantin Komarov { 242be71b5cbSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 243be71b5cbSKonstantin Komarov const struct EA_INFO *info; 244be71b5cbSKonstantin Komarov struct EA_FULL *ea_all = NULL; 245be71b5cbSKonstantin Komarov const struct EA_FULL *ea; 246be71b5cbSKonstantin Komarov u32 off, len; 247be71b5cbSKonstantin Komarov int err; 248be71b5cbSKonstantin Komarov 249be71b5cbSKonstantin Komarov if (!(ni->ni_flags & NI_FLAG_EA)) 250be71b5cbSKonstantin Komarov return -ENODATA; 251be71b5cbSKonstantin Komarov 252be71b5cbSKonstantin Komarov if (!required) 253be71b5cbSKonstantin Komarov ni_lock(ni); 254be71b5cbSKonstantin Komarov 255be71b5cbSKonstantin Komarov len = 0; 256be71b5cbSKonstantin Komarov 257be71b5cbSKonstantin Komarov if (name_len > 255) { 258be71b5cbSKonstantin Komarov err = -ENAMETOOLONG; 259be71b5cbSKonstantin Komarov goto out; 260be71b5cbSKonstantin Komarov } 261be71b5cbSKonstantin Komarov 262be71b5cbSKonstantin Komarov err = ntfs_read_ea(ni, &ea_all, 0, &info); 263be71b5cbSKonstantin Komarov if (err) 264be71b5cbSKonstantin Komarov goto out; 265be71b5cbSKonstantin Komarov 266be71b5cbSKonstantin Komarov if (!info) 267be71b5cbSKonstantin Komarov goto out; 268be71b5cbSKonstantin Komarov 269e8b8e97fSKari Argillander /* Enumerate all xattrs. */ 2700e8235d2SKonstantin Komarov if (!find_ea(ea_all, le32_to_cpu(info->size), name, name_len, &off, 2710e8235d2SKonstantin Komarov NULL)) { 272be71b5cbSKonstantin Komarov err = -ENODATA; 273be71b5cbSKonstantin Komarov goto out; 274be71b5cbSKonstantin Komarov } 275be71b5cbSKonstantin Komarov ea = Add2Ptr(ea_all, off); 276be71b5cbSKonstantin Komarov 277be71b5cbSKonstantin Komarov len = le16_to_cpu(ea->elength); 278be71b5cbSKonstantin Komarov if (!buffer) { 279be71b5cbSKonstantin Komarov err = 0; 280be71b5cbSKonstantin Komarov goto out; 281be71b5cbSKonstantin Komarov } 282be71b5cbSKonstantin Komarov 283be71b5cbSKonstantin Komarov if (len > size) { 284be71b5cbSKonstantin Komarov err = -ERANGE; 285be71b5cbSKonstantin Komarov if (required) 286be71b5cbSKonstantin Komarov *required = len; 287be71b5cbSKonstantin Komarov goto out; 288be71b5cbSKonstantin Komarov } 289be71b5cbSKonstantin Komarov 290be71b5cbSKonstantin Komarov memcpy(buffer, ea->name + ea->name_len + 1, len); 291be71b5cbSKonstantin Komarov err = 0; 292be71b5cbSKonstantin Komarov 293be71b5cbSKonstantin Komarov out: 294195c52bdSKari Argillander kfree(ea_all); 295be71b5cbSKonstantin Komarov if (!required) 296be71b5cbSKonstantin Komarov ni_unlock(ni); 297be71b5cbSKonstantin Komarov 298be71b5cbSKonstantin Komarov return err ? err : len; 299be71b5cbSKonstantin Komarov } 300be71b5cbSKonstantin Komarov 301be71b5cbSKonstantin Komarov static noinline int ntfs_set_ea(struct inode *inode, const char *name, 302be71b5cbSKonstantin Komarov size_t name_len, const void *value, 3031842fbc8SKonstantin Komarov size_t val_size, int flags, bool locked, 3041842fbc8SKonstantin Komarov __le16 *ea_size) 305be71b5cbSKonstantin Komarov { 306be71b5cbSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 307be71b5cbSKonstantin Komarov struct ntfs_sb_info *sbi = ni->mi.sbi; 308be71b5cbSKonstantin Komarov int err; 309be71b5cbSKonstantin Komarov struct EA_INFO ea_info; 310be71b5cbSKonstantin Komarov const struct EA_INFO *info; 311be71b5cbSKonstantin Komarov struct EA_FULL *new_ea; 312be71b5cbSKonstantin Komarov struct EA_FULL *ea_all = NULL; 313be71b5cbSKonstantin Komarov size_t add, new_pack; 3140e8235d2SKonstantin Komarov u32 off, size, ea_sz; 315be71b5cbSKonstantin Komarov __le16 size_pack; 316be71b5cbSKonstantin Komarov struct ATTRIB *attr; 317be71b5cbSKonstantin Komarov struct ATTR_LIST_ENTRY *le; 318be71b5cbSKonstantin Komarov struct mft_inode *mi; 319be71b5cbSKonstantin Komarov struct runs_tree ea_run; 320be71b5cbSKonstantin Komarov u64 new_sz; 321be71b5cbSKonstantin Komarov void *p; 322be71b5cbSKonstantin Komarov 3233a2154b2SKonstantin Komarov if (!locked) 324be71b5cbSKonstantin Komarov ni_lock(ni); 325be71b5cbSKonstantin Komarov 326be71b5cbSKonstantin Komarov run_init(&ea_run); 327be71b5cbSKonstantin Komarov 328be71b5cbSKonstantin Komarov if (name_len > 255) { 329be71b5cbSKonstantin Komarov err = -ENAMETOOLONG; 330be71b5cbSKonstantin Komarov goto out; 331be71b5cbSKonstantin Komarov } 332be71b5cbSKonstantin Komarov 333fa3cacf5SKari Argillander add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4); 334be71b5cbSKonstantin Komarov 335be71b5cbSKonstantin Komarov err = ntfs_read_ea(ni, &ea_all, add, &info); 336be71b5cbSKonstantin Komarov if (err) 337be71b5cbSKonstantin Komarov goto out; 338be71b5cbSKonstantin Komarov 339be71b5cbSKonstantin Komarov if (!info) { 340be71b5cbSKonstantin Komarov memset(&ea_info, 0, sizeof(ea_info)); 341be71b5cbSKonstantin Komarov size = 0; 342be71b5cbSKonstantin Komarov size_pack = 0; 343be71b5cbSKonstantin Komarov } else { 344be71b5cbSKonstantin Komarov memcpy(&ea_info, info, sizeof(ea_info)); 345be71b5cbSKonstantin Komarov size = le32_to_cpu(ea_info.size); 346be71b5cbSKonstantin Komarov size_pack = ea_info.size_pack; 347be71b5cbSKonstantin Komarov } 348be71b5cbSKonstantin Komarov 3490e8235d2SKonstantin Komarov if (info && find_ea(ea_all, size, name, name_len, &off, &ea_sz)) { 350be71b5cbSKonstantin Komarov struct EA_FULL *ea; 351be71b5cbSKonstantin Komarov 352be71b5cbSKonstantin Komarov if (flags & XATTR_CREATE) { 353be71b5cbSKonstantin Komarov err = -EEXIST; 354be71b5cbSKonstantin Komarov goto out; 355be71b5cbSKonstantin Komarov } 356be71b5cbSKonstantin Komarov 357be71b5cbSKonstantin Komarov ea = Add2Ptr(ea_all, off); 358be71b5cbSKonstantin Komarov 359be71b5cbSKonstantin Komarov /* 360be71b5cbSKonstantin Komarov * Check simple case when we try to insert xattr with the same value 361be71b5cbSKonstantin Komarov * e.g. ntfs_save_wsl_perm 362be71b5cbSKonstantin Komarov */ 363be71b5cbSKonstantin Komarov if (val_size && le16_to_cpu(ea->elength) == val_size && 364be71b5cbSKonstantin Komarov !memcmp(ea->name + ea->name_len + 1, value, val_size)) { 365e8b8e97fSKari Argillander /* xattr already contains the required value. */ 366be71b5cbSKonstantin Komarov goto out; 367be71b5cbSKonstantin Komarov } 368be71b5cbSKonstantin Komarov 369e8b8e97fSKari Argillander /* Remove current xattr. */ 370be71b5cbSKonstantin Komarov if (ea->flags & FILE_NEED_EA) 371be71b5cbSKonstantin Komarov le16_add_cpu(&ea_info.count, -1); 372be71b5cbSKonstantin Komarov 373be71b5cbSKonstantin Komarov le16_add_cpu(&ea_info.size_pack, 0 - packed_ea_size(ea)); 374be71b5cbSKonstantin Komarov 375be71b5cbSKonstantin Komarov memmove(ea, Add2Ptr(ea, ea_sz), size - off - ea_sz); 376be71b5cbSKonstantin Komarov 377be71b5cbSKonstantin Komarov size -= ea_sz; 378be71b5cbSKonstantin Komarov memset(Add2Ptr(ea_all, size), 0, ea_sz); 379be71b5cbSKonstantin Komarov 380be71b5cbSKonstantin Komarov ea_info.size = cpu_to_le32(size); 381be71b5cbSKonstantin Komarov 382be71b5cbSKonstantin Komarov if ((flags & XATTR_REPLACE) && !val_size) { 383e8b8e97fSKari Argillander /* Remove xattr. */ 384be71b5cbSKonstantin Komarov goto update_ea; 385be71b5cbSKonstantin Komarov } 386be71b5cbSKonstantin Komarov } else { 387be71b5cbSKonstantin Komarov if (flags & XATTR_REPLACE) { 388be71b5cbSKonstantin Komarov err = -ENODATA; 389be71b5cbSKonstantin Komarov goto out; 390be71b5cbSKonstantin Komarov } 391be71b5cbSKonstantin Komarov 392be71b5cbSKonstantin Komarov if (!ea_all) { 393195c52bdSKari Argillander ea_all = kzalloc(add, GFP_NOFS); 394be71b5cbSKonstantin Komarov if (!ea_all) { 395be71b5cbSKonstantin Komarov err = -ENOMEM; 396be71b5cbSKonstantin Komarov goto out; 397be71b5cbSKonstantin Komarov } 398be71b5cbSKonstantin Komarov } 399be71b5cbSKonstantin Komarov } 400be71b5cbSKonstantin Komarov 401e8b8e97fSKari Argillander /* Append new xattr. */ 402be71b5cbSKonstantin Komarov new_ea = Add2Ptr(ea_all, size); 403be71b5cbSKonstantin Komarov new_ea->size = cpu_to_le32(add); 404be71b5cbSKonstantin Komarov new_ea->flags = 0; 405be71b5cbSKonstantin Komarov new_ea->name_len = name_len; 406be71b5cbSKonstantin Komarov new_ea->elength = cpu_to_le16(val_size); 407be71b5cbSKonstantin Komarov memcpy(new_ea->name, name, name_len); 408be71b5cbSKonstantin Komarov new_ea->name[name_len] = 0; 409be71b5cbSKonstantin Komarov memcpy(new_ea->name + name_len + 1, value, val_size); 410be71b5cbSKonstantin Komarov new_pack = le16_to_cpu(ea_info.size_pack) + packed_ea_size(new_ea); 411be71b5cbSKonstantin Komarov ea_info.size_pack = cpu_to_le16(new_pack); 412e8b8e97fSKari Argillander /* New size of ATTR_EA. */ 413be71b5cbSKonstantin Komarov size += add; 414cff32466SKonstantin Komarov ea_info.size = cpu_to_le32(size); 415cff32466SKonstantin Komarov 416cff32466SKonstantin Komarov /* 417cff32466SKonstantin Komarov * 1. Check ea_info.size_pack for overflow. 418e479f0a6SYu Zhe * 2. New attribute size must fit value from $AttrDef 419cff32466SKonstantin Komarov */ 420cff32466SKonstantin Komarov if (new_pack > 0xffff || size > sbi->ea_max_size) { 421cff32466SKonstantin Komarov ntfs_inode_warn( 422cff32466SKonstantin Komarov inode, 423cff32466SKonstantin Komarov "The size of extended attributes must not exceed 64KiB"); 424be71b5cbSKonstantin Komarov err = -EFBIG; // -EINVAL? 425be71b5cbSKonstantin Komarov goto out; 426be71b5cbSKonstantin Komarov } 427be71b5cbSKonstantin Komarov 428be71b5cbSKonstantin Komarov update_ea: 429be71b5cbSKonstantin Komarov 430be71b5cbSKonstantin Komarov if (!info) { 431e8b8e97fSKari Argillander /* Create xattr. */ 432be71b5cbSKonstantin Komarov if (!size) { 433be71b5cbSKonstantin Komarov err = 0; 434be71b5cbSKonstantin Komarov goto out; 435be71b5cbSKonstantin Komarov } 436be71b5cbSKonstantin Komarov 437be71b5cbSKonstantin Komarov err = ni_insert_resident(ni, sizeof(struct EA_INFO), 43878ab59feSKonstantin Komarov ATTR_EA_INFO, NULL, 0, NULL, NULL, 43978ab59feSKonstantin Komarov NULL); 440be71b5cbSKonstantin Komarov if (err) 441be71b5cbSKonstantin Komarov goto out; 442be71b5cbSKonstantin Komarov 44378ab59feSKonstantin Komarov err = ni_insert_resident(ni, 0, ATTR_EA, NULL, 0, NULL, NULL, 44478ab59feSKonstantin Komarov NULL); 445be71b5cbSKonstantin Komarov if (err) 446be71b5cbSKonstantin Komarov goto out; 447be71b5cbSKonstantin Komarov } 448be71b5cbSKonstantin Komarov 449be71b5cbSKonstantin Komarov new_sz = size; 450be71b5cbSKonstantin Komarov err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz, 451be71b5cbSKonstantin Komarov false, NULL); 452be71b5cbSKonstantin Komarov if (err) 453be71b5cbSKonstantin Komarov goto out; 454be71b5cbSKonstantin Komarov 455be71b5cbSKonstantin Komarov le = NULL; 456be71b5cbSKonstantin Komarov attr = ni_find_attr(ni, NULL, &le, ATTR_EA_INFO, NULL, 0, NULL, &mi); 457be71b5cbSKonstantin Komarov if (!attr) { 458be71b5cbSKonstantin Komarov err = -EINVAL; 459be71b5cbSKonstantin Komarov goto out; 460be71b5cbSKonstantin Komarov } 461be71b5cbSKonstantin Komarov 462be71b5cbSKonstantin Komarov if (!size) { 463e8b8e97fSKari Argillander /* Delete xattr, ATTR_EA_INFO */ 46478ab59feSKonstantin Komarov ni_remove_attr_le(ni, attr, mi, le); 465be71b5cbSKonstantin Komarov } else { 466be71b5cbSKonstantin Komarov p = resident_data_ex(attr, sizeof(struct EA_INFO)); 467be71b5cbSKonstantin Komarov if (!p) { 468be71b5cbSKonstantin Komarov err = -EINVAL; 469be71b5cbSKonstantin Komarov goto out; 470be71b5cbSKonstantin Komarov } 471be71b5cbSKonstantin Komarov memcpy(p, &ea_info, sizeof(struct EA_INFO)); 472be71b5cbSKonstantin Komarov mi->dirty = true; 473be71b5cbSKonstantin Komarov } 474be71b5cbSKonstantin Komarov 475be71b5cbSKonstantin Komarov le = NULL; 476be71b5cbSKonstantin Komarov attr = ni_find_attr(ni, NULL, &le, ATTR_EA, NULL, 0, NULL, &mi); 477be71b5cbSKonstantin Komarov if (!attr) { 478be71b5cbSKonstantin Komarov err = -EINVAL; 479be71b5cbSKonstantin Komarov goto out; 480be71b5cbSKonstantin Komarov } 481be71b5cbSKonstantin Komarov 482be71b5cbSKonstantin Komarov if (!size) { 483e8b8e97fSKari Argillander /* Delete xattr, ATTR_EA */ 48478ab59feSKonstantin Komarov ni_remove_attr_le(ni, attr, mi, le); 485be71b5cbSKonstantin Komarov } else if (attr->non_res) { 48642f86b12SKonstantin Komarov err = attr_load_runs_range(ni, ATTR_EA, NULL, 0, &ea_run, 0, 48742f86b12SKonstantin Komarov size); 48842f86b12SKonstantin Komarov if (err) 48942f86b12SKonstantin Komarov goto out; 49042f86b12SKonstantin Komarov 49163544672SKonstantin Komarov err = ntfs_sb_write_run(sbi, &ea_run, 0, ea_all, size, 0); 492be71b5cbSKonstantin Komarov if (err) 493be71b5cbSKonstantin Komarov goto out; 494be71b5cbSKonstantin Komarov } else { 495be71b5cbSKonstantin Komarov p = resident_data_ex(attr, size); 496be71b5cbSKonstantin Komarov if (!p) { 497be71b5cbSKonstantin Komarov err = -EINVAL; 498be71b5cbSKonstantin Komarov goto out; 499be71b5cbSKonstantin Komarov } 500be71b5cbSKonstantin Komarov memcpy(p, ea_all, size); 501be71b5cbSKonstantin Komarov mi->dirty = true; 502be71b5cbSKonstantin Komarov } 503be71b5cbSKonstantin Komarov 504e8b8e97fSKari Argillander /* Check if we delete the last xattr. */ 505be71b5cbSKonstantin Komarov if (size) 506be71b5cbSKonstantin Komarov ni->ni_flags |= NI_FLAG_EA; 507be71b5cbSKonstantin Komarov else 508be71b5cbSKonstantin Komarov ni->ni_flags &= ~NI_FLAG_EA; 509be71b5cbSKonstantin Komarov 510be71b5cbSKonstantin Komarov if (ea_info.size_pack != size_pack) 511be71b5cbSKonstantin Komarov ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 5121842fbc8SKonstantin Komarov if (ea_size) 5131842fbc8SKonstantin Komarov *ea_size = ea_info.size_pack; 514be71b5cbSKonstantin Komarov mark_inode_dirty(&ni->vfs_inode); 515be71b5cbSKonstantin Komarov 516be71b5cbSKonstantin Komarov out: 5173a2154b2SKonstantin Komarov if (!locked) 518be71b5cbSKonstantin Komarov ni_unlock(ni); 519be71b5cbSKonstantin Komarov 520be71b5cbSKonstantin Komarov run_close(&ea_run); 521195c52bdSKari Argillander kfree(ea_all); 522be71b5cbSKonstantin Komarov 523be71b5cbSKonstantin Komarov return err; 524be71b5cbSKonstantin Komarov } 525be71b5cbSKonstantin Komarov 526be71b5cbSKonstantin Komarov #ifdef CONFIG_NTFS3_FS_POSIX_ACL 52775c5e0c9SKonstantin Komarov 52875c5e0c9SKonstantin Komarov /* 52975c5e0c9SKonstantin Komarov * ntfs_get_acl - inode_operations::get_acl 53075c5e0c9SKonstantin Komarov */ 531f0377761SKonstantin Komarov struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry, 532f0377761SKonstantin Komarov int type) 533be71b5cbSKonstantin Komarov { 53475c5e0c9SKonstantin Komarov struct inode *inode = d_inode(dentry); 535be71b5cbSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 536be71b5cbSKonstantin Komarov const char *name; 537be71b5cbSKonstantin Komarov size_t name_len; 538be71b5cbSKonstantin Komarov struct posix_acl *acl; 539be71b5cbSKonstantin Komarov size_t req; 540be71b5cbSKonstantin Komarov int err; 541be71b5cbSKonstantin Komarov void *buf; 542be71b5cbSKonstantin Komarov 543e8b8e97fSKari Argillander /* Allocate PATH_MAX bytes. */ 544be71b5cbSKonstantin Komarov buf = __getname(); 545be71b5cbSKonstantin Komarov if (!buf) 546be71b5cbSKonstantin Komarov return ERR_PTR(-ENOMEM); 547be71b5cbSKonstantin Komarov 548e8b8e97fSKari Argillander /* Possible values of 'type' was already checked above. */ 549be71b5cbSKonstantin Komarov if (type == ACL_TYPE_ACCESS) { 550be71b5cbSKonstantin Komarov name = XATTR_NAME_POSIX_ACL_ACCESS; 551be71b5cbSKonstantin Komarov name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 552be71b5cbSKonstantin Komarov } else { 553be71b5cbSKonstantin Komarov name = XATTR_NAME_POSIX_ACL_DEFAULT; 554be71b5cbSKonstantin Komarov name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 555be71b5cbSKonstantin Komarov } 556be71b5cbSKonstantin Komarov 557be71b5cbSKonstantin Komarov ni_lock(ni); 558be71b5cbSKonstantin Komarov 559be71b5cbSKonstantin Komarov err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX, &req); 560be71b5cbSKonstantin Komarov 561be71b5cbSKonstantin Komarov ni_unlock(ni); 562be71b5cbSKonstantin Komarov 563e8b8e97fSKari Argillander /* Translate extended attribute to acl. */ 5642926e429SDan Carpenter if (err >= 0) { 5650c3bc789SChristian Brauner acl = posix_acl_from_xattr(&init_user_ns, buf, err); 5660bd5fdb8SKonstantin Komarov } else if (err == -ENODATA) { 5670bd5fdb8SKonstantin Komarov acl = NULL; 5680bd5fdb8SKonstantin Komarov } else { 5690bd5fdb8SKonstantin Komarov acl = ERR_PTR(err); 5700bd5fdb8SKonstantin Komarov } 5710bd5fdb8SKonstantin Komarov 572be71b5cbSKonstantin Komarov if (!IS_ERR(acl)) 573be71b5cbSKonstantin Komarov set_cached_acl(inode, type, acl); 574be71b5cbSKonstantin Komarov 575be71b5cbSKonstantin Komarov __putname(buf); 576be71b5cbSKonstantin Komarov 577be71b5cbSKonstantin Komarov return acl; 578be71b5cbSKonstantin Komarov } 579be71b5cbSKonstantin Komarov 580700b7940SChristian Brauner static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap, 581be71b5cbSKonstantin Komarov struct inode *inode, struct posix_acl *acl, 5829186d472SKonstantin Komarov int type, bool init_acl) 583be71b5cbSKonstantin Komarov { 584be71b5cbSKonstantin Komarov const char *name; 585be71b5cbSKonstantin Komarov size_t size, name_len; 586460bbf29SKonstantin Komarov void *value; 587460bbf29SKonstantin Komarov int err; 588398c35f4SKonstantin Komarov int flags; 589460bbf29SKonstantin Komarov umode_t mode; 590be71b5cbSKonstantin Komarov 591be71b5cbSKonstantin Komarov if (S_ISLNK(inode->i_mode)) 592be71b5cbSKonstantin Komarov return -EOPNOTSUPP; 593be71b5cbSKonstantin Komarov 594460bbf29SKonstantin Komarov mode = inode->i_mode; 595be71b5cbSKonstantin Komarov switch (type) { 596be71b5cbSKonstantin Komarov case ACL_TYPE_ACCESS: 5979186d472SKonstantin Komarov /* Do not change i_mode if we are in init_acl */ 5989186d472SKonstantin Komarov if (acl && !init_acl) { 599f0377761SKonstantin Komarov err = posix_acl_update_mode(idmap, inode, &mode, &acl); 600ba77237eSKonstantin Komarov if (err) 601d4073595SDan Carpenter return err; 602be71b5cbSKonstantin Komarov } 603be71b5cbSKonstantin Komarov name = XATTR_NAME_POSIX_ACL_ACCESS; 604be71b5cbSKonstantin Komarov name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 605be71b5cbSKonstantin Komarov break; 606be71b5cbSKonstantin Komarov 607be71b5cbSKonstantin Komarov case ACL_TYPE_DEFAULT: 608be71b5cbSKonstantin Komarov if (!S_ISDIR(inode->i_mode)) 609be71b5cbSKonstantin Komarov return acl ? -EACCES : 0; 610be71b5cbSKonstantin Komarov name = XATTR_NAME_POSIX_ACL_DEFAULT; 611be71b5cbSKonstantin Komarov name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 612be71b5cbSKonstantin Komarov break; 613be71b5cbSKonstantin Komarov 614be71b5cbSKonstantin Komarov default: 615be71b5cbSKonstantin Komarov return -EINVAL; 616be71b5cbSKonstantin Komarov } 617be71b5cbSKonstantin Komarov 618be71b5cbSKonstantin Komarov if (!acl) { 619398c35f4SKonstantin Komarov /* Remove xattr if it can be presented via mode. */ 620be71b5cbSKonstantin Komarov size = 0; 621be71b5cbSKonstantin Komarov value = NULL; 622398c35f4SKonstantin Komarov flags = XATTR_REPLACE; 623be71b5cbSKonstantin Komarov } else { 624be71b5cbSKonstantin Komarov size = posix_acl_xattr_size(acl->a_count); 625195c52bdSKari Argillander value = kmalloc(size, GFP_NOFS); 626be71b5cbSKonstantin Komarov if (!value) 627be71b5cbSKonstantin Komarov return -ENOMEM; 6280c3bc789SChristian Brauner err = posix_acl_to_xattr(&init_user_ns, acl, value, size); 629be71b5cbSKonstantin Komarov if (err < 0) 630be71b5cbSKonstantin Komarov goto out; 631398c35f4SKonstantin Komarov flags = 0; 632be71b5cbSKonstantin Komarov } 633be71b5cbSKonstantin Komarov 6341842fbc8SKonstantin Komarov err = ntfs_set_ea(inode, name, name_len, value, size, flags, 0, NULL); 635398c35f4SKonstantin Komarov if (err == -ENODATA && !size) 636398c35f4SKonstantin Komarov err = 0; /* Removing non existed xattr. */ 637460bbf29SKonstantin Komarov if (!err) { 638be71b5cbSKonstantin Komarov set_cached_acl(inode, type, acl); 639460bbf29SKonstantin Komarov inode->i_mode = mode; 640*3d65c46fSJeff Layton inode_set_ctime_current(inode); 641460bbf29SKonstantin Komarov mark_inode_dirty(inode); 642460bbf29SKonstantin Komarov } 643be71b5cbSKonstantin Komarov 644be71b5cbSKonstantin Komarov out: 645195c52bdSKari Argillander kfree(value); 646be71b5cbSKonstantin Komarov 647be71b5cbSKonstantin Komarov return err; 648be71b5cbSKonstantin Komarov } 649be71b5cbSKonstantin Komarov 650be71b5cbSKonstantin Komarov /* 651e8b8e97fSKari Argillander * ntfs_set_acl - inode_operations::set_acl 652be71b5cbSKonstantin Komarov */ 65313e83a49SChristian Brauner int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 654be71b5cbSKonstantin Komarov struct posix_acl *acl, int type) 655be71b5cbSKonstantin Komarov { 656700b7940SChristian Brauner return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false); 657be71b5cbSKonstantin Komarov } 658be71b5cbSKonstantin Komarov 659be71b5cbSKonstantin Komarov /* 660e8b8e97fSKari Argillander * ntfs_init_acl - Initialize the ACLs of a new inode. 661e8b8e97fSKari Argillander * 662e8b8e97fSKari Argillander * Called from ntfs_create_inode(). 663be71b5cbSKonstantin Komarov */ 664700b7940SChristian Brauner int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode, 665be71b5cbSKonstantin Komarov struct inode *dir) 666be71b5cbSKonstantin Komarov { 667be71b5cbSKonstantin Komarov struct posix_acl *default_acl, *acl; 668be71b5cbSKonstantin Komarov int err; 669be71b5cbSKonstantin Komarov 67066019837SKonstantin Komarov err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 67166019837SKonstantin Komarov if (err) 67266019837SKonstantin Komarov return err; 673be71b5cbSKonstantin Komarov 67466019837SKonstantin Komarov if (default_acl) { 675700b7940SChristian Brauner err = ntfs_set_acl_ex(idmap, inode, default_acl, 6769186d472SKonstantin Komarov ACL_TYPE_DEFAULT, true); 67766019837SKonstantin Komarov posix_acl_release(default_acl); 67866019837SKonstantin Komarov } else { 67966019837SKonstantin Komarov inode->i_default_acl = NULL; 68066019837SKonstantin Komarov } 681be71b5cbSKonstantin Komarov 68219e890ffSYang Xu if (acl) { 68366019837SKonstantin Komarov if (!err) 684700b7940SChristian Brauner err = ntfs_set_acl_ex(idmap, inode, acl, 6859186d472SKonstantin Komarov ACL_TYPE_ACCESS, true); 686be71b5cbSKonstantin Komarov posix_acl_release(acl); 68719e890ffSYang Xu } else { 68819e890ffSYang Xu inode->i_acl = NULL; 68966019837SKonstantin Komarov } 690be71b5cbSKonstantin Komarov 691be71b5cbSKonstantin Komarov return err; 692be71b5cbSKonstantin Komarov } 693be71b5cbSKonstantin Komarov #endif 694be71b5cbSKonstantin Komarov 695be71b5cbSKonstantin Komarov /* 696e8b8e97fSKari Argillander * ntfs_acl_chmod - Helper for ntfs3_setattr(). 697be71b5cbSKonstantin Komarov */ 69813e83a49SChristian Brauner int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry) 699be71b5cbSKonstantin Komarov { 700138060baSChristian Brauner struct inode *inode = d_inode(dentry); 701be71b5cbSKonstantin Komarov struct super_block *sb = inode->i_sb; 702be71b5cbSKonstantin Komarov 703be71b5cbSKonstantin Komarov if (!(sb->s_flags & SB_POSIXACL)) 704be71b5cbSKonstantin Komarov return 0; 705be71b5cbSKonstantin Komarov 706be71b5cbSKonstantin Komarov if (S_ISLNK(inode->i_mode)) 707be71b5cbSKonstantin Komarov return -EOPNOTSUPP; 708be71b5cbSKonstantin Komarov 70913e83a49SChristian Brauner return posix_acl_chmod(idmap, dentry, inode->i_mode); 710be71b5cbSKonstantin Komarov } 711be71b5cbSKonstantin Komarov 712be71b5cbSKonstantin Komarov /* 713e8b8e97fSKari Argillander * ntfs_listxattr - inode_operations::listxattr 714be71b5cbSKonstantin Komarov */ 715be71b5cbSKonstantin Komarov ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 716be71b5cbSKonstantin Komarov { 717be71b5cbSKonstantin Komarov struct inode *inode = d_inode(dentry); 718be71b5cbSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 719be71b5cbSKonstantin Komarov ssize_t ret; 720be71b5cbSKonstantin Komarov 721be71b5cbSKonstantin Komarov if (!(ni->ni_flags & NI_FLAG_EA)) { 722be71b5cbSKonstantin Komarov /* no xattr in file */ 723be71b5cbSKonstantin Komarov return 0; 724be71b5cbSKonstantin Komarov } 725be71b5cbSKonstantin Komarov 726be71b5cbSKonstantin Komarov ni_lock(ni); 727be71b5cbSKonstantin Komarov 728be71b5cbSKonstantin Komarov ret = ntfs_list_ea(ni, buffer, size); 729be71b5cbSKonstantin Komarov 730be71b5cbSKonstantin Komarov ni_unlock(ni); 731be71b5cbSKonstantin Komarov 732be71b5cbSKonstantin Komarov return ret; 733be71b5cbSKonstantin Komarov } 734be71b5cbSKonstantin Komarov 735be71b5cbSKonstantin Komarov static int ntfs_getxattr(const struct xattr_handler *handler, struct dentry *de, 736be71b5cbSKonstantin Komarov struct inode *inode, const char *name, void *buffer, 737be71b5cbSKonstantin Komarov size_t size) 738be71b5cbSKonstantin Komarov { 739be71b5cbSKonstantin Komarov int err; 740be71b5cbSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 741be71b5cbSKonstantin Komarov 742e8b8e97fSKari Argillander /* Dispatch request. */ 743d45da67cSYuan Can if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 744be71b5cbSKonstantin Komarov /* system.dos_attrib */ 745be71b5cbSKonstantin Komarov if (!buffer) { 746be71b5cbSKonstantin Komarov err = sizeof(u8); 747be71b5cbSKonstantin Komarov } else if (size < sizeof(u8)) { 748be71b5cbSKonstantin Komarov err = -ENODATA; 749be71b5cbSKonstantin Komarov } else { 750be71b5cbSKonstantin Komarov err = sizeof(u8); 751be71b5cbSKonstantin Komarov *(u8 *)buffer = le32_to_cpu(ni->std_fa); 752be71b5cbSKonstantin Komarov } 753be71b5cbSKonstantin Komarov goto out; 754be71b5cbSKonstantin Komarov } 755be71b5cbSKonstantin Komarov 7560d19f3d7SDaniel Pinto if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 7570d19f3d7SDaniel Pinto !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 758be71b5cbSKonstantin Komarov /* system.ntfs_attrib */ 759be71b5cbSKonstantin Komarov if (!buffer) { 760be71b5cbSKonstantin Komarov err = sizeof(u32); 761be71b5cbSKonstantin Komarov } else if (size < sizeof(u32)) { 762be71b5cbSKonstantin Komarov err = -ENODATA; 763be71b5cbSKonstantin Komarov } else { 764be71b5cbSKonstantin Komarov err = sizeof(u32); 765be71b5cbSKonstantin Komarov *(u32 *)buffer = le32_to_cpu(ni->std_fa); 7660d19f3d7SDaniel Pinto if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 7670203471dSDaniel Pinto *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer); 768be71b5cbSKonstantin Komarov } 769be71b5cbSKonstantin Komarov goto out; 770be71b5cbSKonstantin Komarov } 771be71b5cbSKonstantin Komarov 772d45da67cSYuan Can if (!strcmp(name, SYSTEM_NTFS_SECURITY)) { 773be71b5cbSKonstantin Komarov /* system.ntfs_security*/ 774be71b5cbSKonstantin Komarov struct SECURITY_DESCRIPTOR_RELATIVE *sd = NULL; 775be71b5cbSKonstantin Komarov size_t sd_size = 0; 776be71b5cbSKonstantin Komarov 777be71b5cbSKonstantin Komarov if (!is_ntfs3(ni->mi.sbi)) { 778e8b8e97fSKari Argillander /* We should get nt4 security. */ 779be71b5cbSKonstantin Komarov err = -EINVAL; 780be71b5cbSKonstantin Komarov goto out; 781be71b5cbSKonstantin Komarov } else if (le32_to_cpu(ni->std_security_id) < 782be71b5cbSKonstantin Komarov SECURITY_ID_FIRST) { 783be71b5cbSKonstantin Komarov err = -ENOENT; 784be71b5cbSKonstantin Komarov goto out; 785be71b5cbSKonstantin Komarov } 786be71b5cbSKonstantin Komarov 787be71b5cbSKonstantin Komarov err = ntfs_get_security_by_id(ni->mi.sbi, ni->std_security_id, 788be71b5cbSKonstantin Komarov &sd, &sd_size); 789be71b5cbSKonstantin Komarov if (err) 790be71b5cbSKonstantin Komarov goto out; 791be71b5cbSKonstantin Komarov 792be71b5cbSKonstantin Komarov if (!is_sd_valid(sd, sd_size)) { 793be71b5cbSKonstantin Komarov ntfs_inode_warn( 794be71b5cbSKonstantin Komarov inode, 795be71b5cbSKonstantin Komarov "looks like you get incorrect security descriptor id=%u", 796be71b5cbSKonstantin Komarov ni->std_security_id); 797be71b5cbSKonstantin Komarov } 798be71b5cbSKonstantin Komarov 799be71b5cbSKonstantin Komarov if (!buffer) { 800be71b5cbSKonstantin Komarov err = sd_size; 801be71b5cbSKonstantin Komarov } else if (size < sd_size) { 802be71b5cbSKonstantin Komarov err = -ENODATA; 803be71b5cbSKonstantin Komarov } else { 804be71b5cbSKonstantin Komarov err = sd_size; 805be71b5cbSKonstantin Komarov memcpy(buffer, sd, sd_size); 806be71b5cbSKonstantin Komarov } 807195c52bdSKari Argillander kfree(sd); 808be71b5cbSKonstantin Komarov goto out; 809be71b5cbSKonstantin Komarov } 810be71b5cbSKonstantin Komarov 811e8b8e97fSKari Argillander /* Deal with NTFS extended attribute. */ 812d45da67cSYuan Can err = ntfs_get_ea(inode, name, strlen(name), buffer, size, NULL); 813be71b5cbSKonstantin Komarov 814be71b5cbSKonstantin Komarov out: 815be71b5cbSKonstantin Komarov return err; 816be71b5cbSKonstantin Komarov } 817be71b5cbSKonstantin Komarov 818be71b5cbSKonstantin Komarov /* 819e8b8e97fSKari Argillander * ntfs_setxattr - inode_operations::setxattr 820be71b5cbSKonstantin Komarov */ 821be71b5cbSKonstantin Komarov static noinline int ntfs_setxattr(const struct xattr_handler *handler, 822f0377761SKonstantin Komarov struct mnt_idmap *idmap, struct dentry *de, 823f0377761SKonstantin Komarov struct inode *inode, const char *name, 824f0377761SKonstantin Komarov const void *value, size_t size, int flags) 825be71b5cbSKonstantin Komarov { 826be71b5cbSKonstantin Komarov int err = -EINVAL; 827be71b5cbSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 828be71b5cbSKonstantin Komarov enum FILE_ATTRIBUTE new_fa; 829be71b5cbSKonstantin Komarov 830e8b8e97fSKari Argillander /* Dispatch request. */ 831d45da67cSYuan Can if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 832be71b5cbSKonstantin Komarov if (sizeof(u8) != size) 833be71b5cbSKonstantin Komarov goto out; 834be71b5cbSKonstantin Komarov new_fa = cpu_to_le32(*(u8 *)value); 835be71b5cbSKonstantin Komarov goto set_new_fa; 836be71b5cbSKonstantin Komarov } 837be71b5cbSKonstantin Komarov 8380d19f3d7SDaniel Pinto if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 8390d19f3d7SDaniel Pinto !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 840be71b5cbSKonstantin Komarov if (size != sizeof(u32)) 841be71b5cbSKonstantin Komarov goto out; 8420d19f3d7SDaniel Pinto if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 8430203471dSDaniel Pinto new_fa = cpu_to_le32(be32_to_cpu(*(__be32 *)value)); 8440d19f3d7SDaniel Pinto else 845be71b5cbSKonstantin Komarov new_fa = cpu_to_le32(*(u32 *)value); 846be71b5cbSKonstantin Komarov 847be71b5cbSKonstantin Komarov if (S_ISREG(inode->i_mode)) { 848e8b8e97fSKari Argillander /* Process compressed/sparsed in special way. */ 849be71b5cbSKonstantin Komarov ni_lock(ni); 850be71b5cbSKonstantin Komarov err = ni_new_attr_flags(ni, new_fa); 851be71b5cbSKonstantin Komarov ni_unlock(ni); 852be71b5cbSKonstantin Komarov if (err) 853be71b5cbSKonstantin Komarov goto out; 854be71b5cbSKonstantin Komarov } 855be71b5cbSKonstantin Komarov set_new_fa: 856be71b5cbSKonstantin Komarov /* 857be71b5cbSKonstantin Komarov * Thanks Mark Harmstone: 858e8b8e97fSKari Argillander * Keep directory bit consistency. 859be71b5cbSKonstantin Komarov */ 860be71b5cbSKonstantin Komarov if (S_ISDIR(inode->i_mode)) 861be71b5cbSKonstantin Komarov new_fa |= FILE_ATTRIBUTE_DIRECTORY; 862be71b5cbSKonstantin Komarov else 863be71b5cbSKonstantin Komarov new_fa &= ~FILE_ATTRIBUTE_DIRECTORY; 864be71b5cbSKonstantin Komarov 865be71b5cbSKonstantin Komarov if (ni->std_fa != new_fa) { 866be71b5cbSKonstantin Komarov ni->std_fa = new_fa; 867be71b5cbSKonstantin Komarov if (new_fa & FILE_ATTRIBUTE_READONLY) 868be71b5cbSKonstantin Komarov inode->i_mode &= ~0222; 869be71b5cbSKonstantin Komarov else 870be71b5cbSKonstantin Komarov inode->i_mode |= 0222; 871e8b8e97fSKari Argillander /* Std attribute always in primary record. */ 872be71b5cbSKonstantin Komarov ni->mi.dirty = true; 873be71b5cbSKonstantin Komarov mark_inode_dirty(inode); 874be71b5cbSKonstantin Komarov } 875be71b5cbSKonstantin Komarov err = 0; 876be71b5cbSKonstantin Komarov 877be71b5cbSKonstantin Komarov goto out; 878be71b5cbSKonstantin Komarov } 879be71b5cbSKonstantin Komarov 880d45da67cSYuan Can if (!strcmp(name, SYSTEM_NTFS_SECURITY)) { 881be71b5cbSKonstantin Komarov /* system.ntfs_security*/ 882be71b5cbSKonstantin Komarov __le32 security_id; 883be71b5cbSKonstantin Komarov bool inserted; 884be71b5cbSKonstantin Komarov struct ATTR_STD_INFO5 *std; 885be71b5cbSKonstantin Komarov 886be71b5cbSKonstantin Komarov if (!is_ntfs3(ni->mi.sbi)) { 887be71b5cbSKonstantin Komarov /* 888e8b8e97fSKari Argillander * We should replace ATTR_SECURE. 889e8b8e97fSKari Argillander * Skip this way cause it is nt4 feature. 890be71b5cbSKonstantin Komarov */ 891be71b5cbSKonstantin Komarov err = -EINVAL; 892be71b5cbSKonstantin Komarov goto out; 893be71b5cbSKonstantin Komarov } 894be71b5cbSKonstantin Komarov 895be71b5cbSKonstantin Komarov if (!is_sd_valid(value, size)) { 896be71b5cbSKonstantin Komarov err = -EINVAL; 897be71b5cbSKonstantin Komarov ntfs_inode_warn( 898be71b5cbSKonstantin Komarov inode, 899be71b5cbSKonstantin Komarov "you try to set invalid security descriptor"); 900be71b5cbSKonstantin Komarov goto out; 901be71b5cbSKonstantin Komarov } 902be71b5cbSKonstantin Komarov 903be71b5cbSKonstantin Komarov err = ntfs_insert_security(ni->mi.sbi, value, size, 904be71b5cbSKonstantin Komarov &security_id, &inserted); 905be71b5cbSKonstantin Komarov if (err) 906be71b5cbSKonstantin Komarov goto out; 907be71b5cbSKonstantin Komarov 908be71b5cbSKonstantin Komarov ni_lock(ni); 909be71b5cbSKonstantin Komarov std = ni_std5(ni); 910be71b5cbSKonstantin Komarov if (!std) { 911be71b5cbSKonstantin Komarov err = -EINVAL; 912be71b5cbSKonstantin Komarov } else if (std->security_id != security_id) { 913be71b5cbSKonstantin Komarov std->security_id = ni->std_security_id = security_id; 914e8b8e97fSKari Argillander /* Std attribute always in primary record. */ 915be71b5cbSKonstantin Komarov ni->mi.dirty = true; 916be71b5cbSKonstantin Komarov mark_inode_dirty(&ni->vfs_inode); 917be71b5cbSKonstantin Komarov } 918be71b5cbSKonstantin Komarov ni_unlock(ni); 919be71b5cbSKonstantin Komarov goto out; 920be71b5cbSKonstantin Komarov } 921be71b5cbSKonstantin Komarov 922e8b8e97fSKari Argillander /* Deal with NTFS extended attribute. */ 9231842fbc8SKonstantin Komarov err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, 0, 9241842fbc8SKonstantin Komarov NULL); 925be71b5cbSKonstantin Komarov 926be71b5cbSKonstantin Komarov out: 927*3d65c46fSJeff Layton inode_set_ctime_current(inode); 9282d44667cSKonstantin Komarov mark_inode_dirty(inode); 9292d44667cSKonstantin Komarov 930be71b5cbSKonstantin Komarov return err; 931be71b5cbSKonstantin Komarov } 932be71b5cbSKonstantin Komarov 933be71b5cbSKonstantin Komarov /* 934be71b5cbSKonstantin Komarov * ntfs_save_wsl_perm 935be71b5cbSKonstantin Komarov * 936be71b5cbSKonstantin Komarov * save uid/gid/mode in xattr 937be71b5cbSKonstantin Komarov */ 9381842fbc8SKonstantin Komarov int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size) 939be71b5cbSKonstantin Komarov { 940be71b5cbSKonstantin Komarov int err; 941be71b5cbSKonstantin Komarov __le32 value; 9423a2154b2SKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode); 943be71b5cbSKonstantin Komarov 9443a2154b2SKonstantin Komarov ni_lock(ni); 945be71b5cbSKonstantin Komarov value = cpu_to_le32(i_uid_read(inode)); 946be71b5cbSKonstantin Komarov err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value, 9471842fbc8SKonstantin Komarov sizeof(value), 0, true, ea_size); 948be71b5cbSKonstantin Komarov if (err) 949be71b5cbSKonstantin Komarov goto out; 950be71b5cbSKonstantin Komarov 951be71b5cbSKonstantin Komarov value = cpu_to_le32(i_gid_read(inode)); 952be71b5cbSKonstantin Komarov err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value, 9531842fbc8SKonstantin Komarov sizeof(value), 0, true, ea_size); 954be71b5cbSKonstantin Komarov if (err) 955be71b5cbSKonstantin Komarov goto out; 956be71b5cbSKonstantin Komarov 957be71b5cbSKonstantin Komarov value = cpu_to_le32(inode->i_mode); 958be71b5cbSKonstantin Komarov err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value, 9591842fbc8SKonstantin Komarov sizeof(value), 0, true, ea_size); 960be71b5cbSKonstantin Komarov if (err) 961be71b5cbSKonstantin Komarov goto out; 962be71b5cbSKonstantin Komarov 963be71b5cbSKonstantin Komarov if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 964be71b5cbSKonstantin Komarov value = cpu_to_le32(inode->i_rdev); 965be71b5cbSKonstantin Komarov err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &value, 9661842fbc8SKonstantin Komarov sizeof(value), 0, true, ea_size); 967be71b5cbSKonstantin Komarov if (err) 968be71b5cbSKonstantin Komarov goto out; 969be71b5cbSKonstantin Komarov } 970be71b5cbSKonstantin Komarov 971be71b5cbSKonstantin Komarov out: 9723a2154b2SKonstantin Komarov ni_unlock(ni); 973be71b5cbSKonstantin Komarov /* In case of error should we delete all WSL xattr? */ 974be71b5cbSKonstantin Komarov return err; 975be71b5cbSKonstantin Komarov } 976be71b5cbSKonstantin Komarov 977be71b5cbSKonstantin Komarov /* 978be71b5cbSKonstantin Komarov * ntfs_get_wsl_perm 979be71b5cbSKonstantin Komarov * 980be71b5cbSKonstantin Komarov * get uid/gid/mode from xattr 981be71b5cbSKonstantin Komarov * it is called from ntfs_iget5->ntfs_read_mft 982be71b5cbSKonstantin Komarov */ 983be71b5cbSKonstantin Komarov void ntfs_get_wsl_perm(struct inode *inode) 984be71b5cbSKonstantin Komarov { 985be71b5cbSKonstantin Komarov size_t sz; 986be71b5cbSKonstantin Komarov __le32 value[3]; 987be71b5cbSKonstantin Komarov 988be71b5cbSKonstantin Komarov if (ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &value[0], 989be71b5cbSKonstantin Komarov sizeof(value[0]), &sz) == sizeof(value[0]) && 990be71b5cbSKonstantin Komarov ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &value[1], 991be71b5cbSKonstantin Komarov sizeof(value[1]), &sz) == sizeof(value[1]) && 992be71b5cbSKonstantin Komarov ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &value[2], 993be71b5cbSKonstantin Komarov sizeof(value[2]), &sz) == sizeof(value[2])) { 994be71b5cbSKonstantin Komarov i_uid_write(inode, (uid_t)le32_to_cpu(value[0])); 995be71b5cbSKonstantin Komarov i_gid_write(inode, (gid_t)le32_to_cpu(value[1])); 996be71b5cbSKonstantin Komarov inode->i_mode = le32_to_cpu(value[2]); 997be71b5cbSKonstantin Komarov 998be71b5cbSKonstantin Komarov if (ntfs_get_ea(inode, "$LXDEV", sizeof("$$LXDEV") - 1, 999be71b5cbSKonstantin Komarov &value[0], sizeof(value), 1000be71b5cbSKonstantin Komarov &sz) == sizeof(value[0])) { 1001be71b5cbSKonstantin Komarov inode->i_rdev = le32_to_cpu(value[0]); 1002be71b5cbSKonstantin Komarov } 1003be71b5cbSKonstantin Komarov } 1004be71b5cbSKonstantin Komarov } 1005be71b5cbSKonstantin Komarov 1006be71b5cbSKonstantin Komarov static bool ntfs_xattr_user_list(struct dentry *dentry) 1007be71b5cbSKonstantin Komarov { 1008be71b5cbSKonstantin Komarov return true; 1009be71b5cbSKonstantin Komarov } 1010be71b5cbSKonstantin Komarov 1011be71b5cbSKonstantin Komarov // clang-format off 1012a26aa123SChristian Brauner static const struct xattr_handler ntfs_other_xattr_handler = { 1013be71b5cbSKonstantin Komarov .prefix = "", 1014be71b5cbSKonstantin Komarov .get = ntfs_getxattr, 1015be71b5cbSKonstantin Komarov .set = ntfs_setxattr, 1016be71b5cbSKonstantin Komarov .list = ntfs_xattr_user_list, 1017be71b5cbSKonstantin Komarov }; 1018be71b5cbSKonstantin Komarov 1019be71b5cbSKonstantin Komarov const struct xattr_handler *ntfs_xattr_handlers[] = { 1020a26aa123SChristian Brauner &ntfs_other_xattr_handler, 1021be71b5cbSKonstantin Komarov NULL, 1022be71b5cbSKonstantin Komarov }; 1023be71b5cbSKonstantin Komarov // clang-format on 1024