| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/xattr.h> |
| 5 |
#include <threads.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fs-util.h" |
| 11 |
#include "nulstr-util.h" |
| 12 |
#include "parse-util.h" |
| 13 |
#include "sparse-endian.h" |
| 14 |
#include "stat-util.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "strv.h" |
| 17 |
#include "time-util.h" |
| 18 |
#include "xattr-util.h" |
| 19 |
|
| 20 |
/* Use a single cache for all of *xattrat syscalls (added in kernel 6.13) */ |
| 21 |
static thread_local bool have_xattrat = true; |
| 22 |
|
| 23 |
static int normalize_and_maybe_pin_inode( |
| 24 |
int *fd, |
| 25 |
const char **path, |
| 26 |
int *at_flags, |
| 27 |
int *ret_tfd, |
| 28 |
bool *ret_opath) { |
| 29 |
|
| 30 |
int r; |
| 31 |
|
| 32 |
assert(fd); |
| 33 |
assert(*fd >= 0 || *fd == AT_FDCWD); |
| 34 |
assert(path); |
| 35 |
assert(at_flags); |
| 36 |
assert(ret_tfd); |
| 37 |
assert(ret_opath); |
| 38 |
|
| 39 |
if (isempty(*path)) |
| 40 |
*path = NULL; /* Normalize "" to NULL */ |
| 41 |
|
| 42 |
if (*fd == AT_FDCWD) { |
| 43 |
if (!*path) /* Both unspecified? Then operate on current working directory */ |
| 44 |
*path = "."; |
| 45 |
|
| 46 |
*ret_tfd = -EBADF; |
| 47 |
*ret_opath = false; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
*at_flags |= AT_EMPTY_PATH; |
| 52 |
|
| 53 |
if (!*path) { |
| 54 |
r = fd_is_opath(*fd); |
| 55 |
if (r < 0) |
| 56 |
return r; |
| 57 |
*ret_opath = r; |
| 58 |
*ret_tfd = -EBADF; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
/* If both have been specified, then we go via O_PATH */ |
| 63 |
|
| 64 |
int tfd = openat(*fd, *path, O_PATH|O_CLOEXEC|(FLAGS_SET(*at_flags, AT_SYMLINK_FOLLOW) ? 0 : O_NOFOLLOW)); |
| 65 |
if (tfd < 0) |
| 66 |
return -errno; |
| 67 |
|
| 68 |
*fd = *ret_tfd = tfd; |
| 69 |
*path = NULL; |
| 70 |
*ret_opath = true; |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
static ssize_t getxattr_pinned_internal( |
| 76 |
int fd, |
| 77 |
const char *path, |
| 78 |
int at_flags, |
| 79 |
bool by_procfs, |
| 80 |
const char *name, |
| 81 |
char *buf, |
| 82 |
size_t size) { |
| 83 |
|
| 84 |
ssize_t n; |
| 85 |
|
| 86 |
assert(!path || !isempty(path)); |
| 87 |
assert((fd >= 0) == !path); |
| 88 |
assert(path || FLAGS_SET(at_flags, AT_EMPTY_PATH)); |
| 89 |
assert(name); |
| 90 |
assert(buf || size == 0); |
| 91 |
|
| 92 |
if (path) |
| 93 |
n = FLAGS_SET(at_flags, AT_SYMLINK_FOLLOW) ? getxattr(path, name, buf, size) |
| 94 |
: lgetxattr(path, name, buf, size); |
| 95 |
else |
| 96 |
n = by_procfs ? getxattr(FORMAT_PROC_FD_PATH(fd), name, buf, size) |
| 97 |
: fgetxattr(fd, name, buf, size); |
| 98 |
if (n < 0) |
| 99 |
return -errno; |
| 100 |
|
| 101 |
assert(size == 0 || (size_t) n <= size); |
| 102 |
return n; |
| 103 |
} |
| 104 |
|
| 105 |
int getxattr_at_malloc( |
| 106 |
int fd, |
| 107 |
const char *path, |
| 108 |
const char *name, |
| 109 |
int at_flags, |
| 110 |
char **ret, |
| 111 |
size_t *ret_size) { |
| 112 |
|
| 113 |
_cleanup_close_ int opened_fd = -EBADF; |
| 114 |
bool by_procfs; |
| 115 |
int r; |
| 116 |
|
| 117 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 118 |
assert(name); |
| 119 |
assert((at_flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0); |
| 120 |
assert(ret); |
| 121 |
|
| 122 |
/* So, this is single function that does what getxattr()/lgetxattr()/fgetxattr() does, but in one go, |
| 123 |
* and with additional bells and whistles. Specifically: |
| 124 |
* |
| 125 |
* 1. This works on O_PATH fds (via /proc/self/fd/, since getxattrat() syscall refuses them...) |
| 126 |
* 2. As extension to openat()-style semantics implies AT_EMPTY_PATH if path is empty |
| 127 |
* 3. Does a malloc() loop, automatically sizing the allocation |
| 128 |
* 4. NUL-terminates the returned buffer (for safety) |
| 129 |
*/ |
| 130 |
|
| 131 |
r = normalize_and_maybe_pin_inode(&fd, &path, &at_flags, &opened_fd, &by_procfs); |
| 132 |
if (r < 0) |
| 133 |
return r; |
| 134 |
|
| 135 |
size_t l = 100; |
| 136 |
for (unsigned n_attempts = 7;;) { |
| 137 |
_cleanup_free_ char *v = NULL; |
| 138 |
|
| 139 |
if (n_attempts == 0) /* If someone is racing against us, give up eventually */ |
| 140 |
return -EBUSY; |
| 141 |
n_attempts--; |
| 142 |
|
| 143 |
v = new(char, l+1); |
| 144 |
if (!v) |
| 145 |
return -ENOMEM; |
| 146 |
|
| 147 |
l = MALLOC_ELEMENTSOF(v) - 1; |
| 148 |
|
| 149 |
ssize_t n; |
| 150 |
n = getxattr_pinned_internal(fd, path, at_flags, by_procfs, name, v, l); |
| 151 |
if (n >= 0) { |
| 152 |
/* Refuse extended attributes with embedded NUL bytes if the caller isn't interested |
| 153 |
* in the size. After all this must mean the caller assumes we return a NUL |
| 154 |
* terminated strings, but if there's a NUL byte embedded they are definitely not |
| 155 |
* regular strings */ |
| 156 |
if (!ret_size && n > 1 && memchr(v, 0, n - 1)) |
| 157 |
return -EBADMSG; |
| 158 |
|
| 159 |
v[n] = 0; /* NUL terminate */ |
| 160 |
*ret = TAKE_PTR(v); |
| 161 |
if (ret_size) |
| 162 |
*ret_size = (size_t) n; |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |
| 166 |
if (n != -ERANGE) |
| 167 |
return (int) n; |
| 168 |
|
| 169 |
n = getxattr_pinned_internal(fd, path, at_flags, by_procfs, name, NULL, 0); |
| 170 |
if (n < 0) |
| 171 |
return (int) n; |
| 172 |
|
| 173 |
l = (size_t) n; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
int getxattr_at_bool(int fd, const char *path, const char *name, int at_flags) { |
| 178 |
_cleanup_free_ char *v = NULL; |
| 179 |
int r; |
| 180 |
|
| 181 |
r = getxattr_at_malloc(fd, path, name, at_flags, &v, /* ret_size= */ NULL); |
| 182 |
if (r < 0) |
| 183 |
return r; |
| 184 |
|
| 185 |
return parse_boolean(v); |
| 186 |
} |
| 187 |
|
| 188 |
int getxattr_at_strv(int fd, const char *path, const char *name, int at_flags, char ***ret_strv) { |
| 189 |
_cleanup_free_ char *nulstr = NULL; |
| 190 |
size_t nulstr_size; |
| 191 |
int r; |
| 192 |
|
| 193 |
assert(ret_strv); |
| 194 |
|
| 195 |
r = getxattr_at_malloc(fd, path, name, at_flags, &nulstr, &nulstr_size); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
_cleanup_strv_free_ char **l = strv_parse_nulstr(nulstr, nulstr_size); |
| 200 |
if (!l) |
| 201 |
return -ENOMEM; |
| 202 |
|
| 203 |
*ret_strv = TAKE_PTR(l); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
static int listxattr_pinned_internal( |
| 208 |
int fd, |
| 209 |
const char *path, |
| 210 |
int at_flags, |
| 211 |
bool by_procfs, |
| 212 |
char *buf, |
| 213 |
size_t size) { |
| 214 |
|
| 215 |
ssize_t n; |
| 216 |
|
| 217 |
assert(!path || !isempty(path)); |
| 218 |
assert((fd >= 0) == !path); |
| 219 |
assert(path || FLAGS_SET(at_flags, AT_EMPTY_PATH)); |
| 220 |
assert(buf || size == 0); |
| 221 |
|
| 222 |
if (path) |
| 223 |
n = FLAGS_SET(at_flags, AT_SYMLINK_FOLLOW) ? listxattr(path, buf, size) |
| 224 |
: llistxattr(path, buf, size); |
| 225 |
else |
| 226 |
n = by_procfs ? listxattr(FORMAT_PROC_FD_PATH(fd), buf, size) |
| 227 |
: flistxattr(fd, buf, size); |
| 228 |
if (n < 0) |
| 229 |
return -errno; |
| 230 |
|
| 231 |
assert(size == 0 || (size_t) n <= size); |
| 232 |
|
| 233 |
if (n > INT_MAX) /* We couldn't return this as 'int' anymore */ |
| 234 |
return -E2BIG; |
| 235 |
|
| 236 |
return (int) n; |
| 237 |
} |
| 238 |
|
| 239 |
int listxattr_at_malloc(int fd, const char *path, int at_flags, char **ret) { |
| 240 |
_cleanup_close_ int opened_fd = -EBADF; |
| 241 |
bool by_procfs; |
| 242 |
int r; |
| 243 |
|
| 244 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 245 |
assert((at_flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0); |
| 246 |
assert(ret); |
| 247 |
|
| 248 |
/* This is to listxattr()/llistattr()/flistattr() what getxattr_at_malloc() is to getxattr()/… */ |
| 249 |
|
| 250 |
r = normalize_and_maybe_pin_inode(&fd, &path, &at_flags, &opened_fd, &by_procfs); |
| 251 |
if (r < 0) |
| 252 |
return r; |
| 253 |
|
| 254 |
size_t l = 100; |
| 255 |
for (unsigned n_attempts = 7;;) { |
| 256 |
_cleanup_free_ char *v = NULL; |
| 257 |
|
| 258 |
if (n_attempts == 0) /* If someone is racing against us, give up eventually */ |
| 259 |
return -EBUSY; |
| 260 |
n_attempts--; |
| 261 |
|
| 262 |
v = new(char, l+1); |
| 263 |
if (!v) |
| 264 |
return -ENOMEM; |
| 265 |
|
| 266 |
l = MALLOC_ELEMENTSOF(v) - 1; |
| 267 |
|
| 268 |
r = listxattr_pinned_internal(fd, path, at_flags, by_procfs, v, l); |
| 269 |
if (r >= 0) { |
| 270 |
v[r] = 0; /* NUL terminate */ |
| 271 |
*ret = TAKE_PTR(v); |
| 272 |
return r; |
| 273 |
} |
| 274 |
if (r != -ERANGE) |
| 275 |
return r; |
| 276 |
|
| 277 |
r = listxattr_pinned_internal(fd, path, at_flags, by_procfs, NULL, 0); |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
l = (size_t) r; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
int xsetxattr_full( |
| 286 |
int fd, |
| 287 |
const char *path, |
| 288 |
int at_flags, |
| 289 |
const char *name, |
| 290 |
const char *value, |
| 291 |
size_t size, |
| 292 |
int xattr_flags) { |
| 293 |
|
| 294 |
int r; |
| 295 |
|
| 296 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 297 |
assert((at_flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0); |
| 298 |
assert(name); |
| 299 |
assert(value); |
| 300 |
|
| 301 |
if (size == SIZE_MAX) |
| 302 |
size = strlen(value); |
| 303 |
|
| 304 |
/* Skip the write if the xattr already has the correct value, to avoid |
| 305 |
* unnecessary timestamp changes on the file. Only do this for plain |
| 306 |
* replace mode (xattr_flags == 0) — XATTR_CREATE callers expect |
| 307 |
* -EEXIST when the xattr already exists. */ |
| 308 |
_cleanup_free_ char *old_value = NULL; |
| 309 |
size_t old_size; |
| 310 |
|
| 311 |
if (xattr_flags == 0 && |
| 312 |
getxattr_at_malloc(fd, path, name, at_flags, &old_value, &old_size) >= 0 && |
| 313 |
memcmp_nn(old_value, old_size, value, size) == 0) |
| 314 |
return 0; |
| 315 |
|
| 316 |
if (have_xattrat && !isempty(path)) { |
| 317 |
struct xattr_args args = { |
| 318 |
.value = PTR_TO_UINT64(value), |
| 319 |
.size = size, |
| 320 |
.flags = xattr_flags, |
| 321 |
}; |
| 322 |
|
| 323 |
r = RET_NERRNO(setxattrat(fd, path, |
| 324 |
at_flags_normalize_nofollow(at_flags), |
| 325 |
name, |
| 326 |
&args, sizeof(args))); |
| 327 |
if (r != -ENOSYS) /* No ERRNO_IS_NOT_SUPPORTED here, as EOPNOTSUPP denotes the fs doesn't |
| 328 |
support xattr */ |
| 329 |
return r; |
| 330 |
|
| 331 |
have_xattrat = false; |
| 332 |
} |
| 333 |
|
| 334 |
_cleanup_close_ int opened_fd = -EBADF; |
| 335 |
bool by_procfs; |
| 336 |
|
| 337 |
r = normalize_and_maybe_pin_inode(&fd, &path, &at_flags, &opened_fd, &by_procfs); |
| 338 |
if (r < 0) |
| 339 |
return r; |
| 340 |
|
| 341 |
if (path) |
| 342 |
r = FLAGS_SET(at_flags, AT_SYMLINK_FOLLOW) ? setxattr(path, name, value, size, xattr_flags) |
| 343 |
: lsetxattr(path, name, value, size, xattr_flags); |
| 344 |
else |
| 345 |
r = by_procfs ? setxattr(FORMAT_PROC_FD_PATH(fd), name, value, size, xattr_flags) |
| 346 |
: fsetxattr(fd, name, value, size, xattr_flags); |
| 347 |
if (r < 0) |
| 348 |
return -errno; |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
int xsetxattr_strv(int fd, const char *path, int at_flags, const char *name, char * const *l) { |
| 354 |
_cleanup_free_ char *nulstr = NULL; |
| 355 |
size_t size; |
| 356 |
int r; |
| 357 |
|
| 358 |
assert(name); |
| 359 |
|
| 360 |
r = strv_make_nulstr(l, &nulstr, &size); |
| 361 |
if (r < 0) |
| 362 |
return r; |
| 363 |
|
| 364 |
return xsetxattr_full(fd, path, at_flags, name, nulstr, size, /* xattr_flags= */ 0); |
| 365 |
} |
| 366 |
|
| 367 |
int xremovexattr(int fd, const char *path, int at_flags, const char *name) { |
| 368 |
int r; |
| 369 |
|
| 370 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 371 |
assert((at_flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0); |
| 372 |
assert(name); |
| 373 |
|
| 374 |
if (have_xattrat && !isempty(path)) { |
| 375 |
r = RET_NERRNO(removexattrat(fd, path, |
| 376 |
at_flags_normalize_nofollow(at_flags), |
| 377 |
name)); |
| 378 |
if (r != -ENOSYS) /* No ERRNO_IS_NOT_SUPPORTED here, as EOPNOTSUPP denotes the fs doesn't |
| 379 |
support xattr */ |
| 380 |
return r; |
| 381 |
|
| 382 |
have_xattrat = false; |
| 383 |
} |
| 384 |
|
| 385 |
_cleanup_close_ int tfd = -EBADF; |
| 386 |
bool by_procfs; |
| 387 |
|
| 388 |
r = normalize_and_maybe_pin_inode(&fd, &path, &at_flags, &tfd, &by_procfs); |
| 389 |
if (r < 0) |
| 390 |
return r; |
| 391 |
|
| 392 |
if (path) |
| 393 |
r = FLAGS_SET(at_flags, AT_SYMLINK_FOLLOW) ? removexattr(path, name) |
| 394 |
: lremovexattr(path, name); |
| 395 |
else |
| 396 |
r = by_procfs ? removexattr(FORMAT_PROC_FD_PATH(fd), name) |
| 397 |
: fremovexattr(fd, name); |
| 398 |
if (r < 0) |
| 399 |
return -errno; |
| 400 |
|
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
static int parse_crtime(le64_t le, usec_t *ret) { |
| 405 |
usec_t u; |
| 406 |
|
| 407 |
assert(ret); |
| 408 |
|
| 409 |
assert_cc(sizeof(usec_t) == sizeof(uint64_t)); |
| 410 |
assert_cc((usec_t) UINT64_MAX == USEC_INFINITY); |
| 411 |
|
| 412 |
u = (usec_t) le64toh(le); |
| 413 |
if (!timestamp_is_set(u)) |
| 414 |
return -EIO; |
| 415 |
|
| 416 |
*ret = u; |
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
int getcrtime_at( |
| 421 |
int fd, |
| 422 |
const char *path, |
| 423 |
int at_flags, |
| 424 |
usec_t *ret) { |
| 425 |
|
| 426 |
_cleanup_free_ le64_t *le = NULL; |
| 427 |
struct statx sx; |
| 428 |
usec_t a, b; |
| 429 |
int r; |
| 430 |
|
| 431 |
assert(fd >= 0 || fd == AT_FDCWD); |
| 432 |
assert((at_flags & ~(AT_SYMLINK_FOLLOW|AT_EMPTY_PATH)) == 0); |
| 433 |
|
| 434 |
/* So here's the deal: the creation/birth time (crtime/btime) of a file is a relatively newly supported concept |
| 435 |
* on Linux (or more strictly speaking: a concept that only recently got supported in the API, it was |
| 436 |
* implemented on various file systems on the lower level since a while, but never was accessible). However, we |
| 437 |
* needed a concept like that for vacuuming algorithms and such, hence we emulated it via a user xattr for a |
| 438 |
* long time. Starting with Linux 4.11 there's statx() which exposes the timestamp to userspace for the first |
| 439 |
* time, where it is available. This function will read it, but it tries to keep some compatibility with older |
| 440 |
* systems: we try to read both the crtime/btime and the xattr, and then use whatever is older. After all the |
| 441 |
* concept is useful for determining how "old" a file really is, and hence using the older of the two makes |
| 442 |
* most sense. */ |
| 443 |
|
| 444 |
r = xstatx_full(fd, |
| 445 |
path, |
| 446 |
at_flags_normalize_nofollow(at_flags)|AT_STATX_DONT_SYNC, |
| 447 |
/* xstatx_flags= */ 0, |
| 448 |
/* mandatory_mask= */ 0, |
| 449 |
STATX_BTIME, |
| 450 |
/* mandatory_attributes= */ 0, |
| 451 |
&sx); |
| 452 |
if (r > 0 && sx.stx_btime.tv_sec != 0) /* > 0: all optional masks are supported */ |
| 453 |
a = statx_timestamp_load(&sx.stx_btime); |
| 454 |
else |
| 455 |
a = USEC_INFINITY; |
| 456 |
|
| 457 |
size_t le_size; |
| 458 |
r = getxattr_at_malloc(fd, path, "user.crtime_usec", at_flags, (char**) &le, &le_size); |
| 459 |
if (r >= 0) { |
| 460 |
if (le_size != sizeof(*le)) |
| 461 |
r = -EIO; |
| 462 |
else |
| 463 |
r = parse_crtime(*le, &b); |
| 464 |
} |
| 465 |
if (r < 0) { |
| 466 |
if (a != USEC_INFINITY) { |
| 467 |
if (ret) |
| 468 |
*ret = a; |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
return r; |
| 473 |
} |
| 474 |
|
| 475 |
if (ret) |
| 476 |
*ret = MIN(a, b); |
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
int fd_setcrtime(int fd, usec_t usec) { |
| 481 |
le64_t le; |
| 482 |
|
| 483 |
assert(fd >= 0); |
| 484 |
|
| 485 |
if (!timestamp_is_set(usec)) |
| 486 |
usec = now(CLOCK_REALTIME); |
| 487 |
|
| 488 |
le = htole64((uint64_t) usec); |
| 489 |
return xsetxattr_full(fd, /* path= */ NULL, AT_EMPTY_PATH, |
| 490 |
"user.crtime_usec", (const char*) &le, sizeof(le), |
| 491 |
/* xattr_flags= */ 0); |
| 492 |
} |
| 493 |
|
| 494 |
bool xattr_is_acl(const char *name) { |
| 495 |
return STR_IN_SET( |
| 496 |
ASSERT_PTR(name), |
| 497 |
"system.posix_acl_access", |
| 498 |
"system.posix_acl_default"); |
| 499 |
} |
| 500 |
|
| 501 |
bool xattr_is_selinux(const char *name) { |
| 502 |
return streq(ASSERT_PTR(name), "security.selinux"); |
| 503 |
} |