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