| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "rsync.h" |
| 24 |
|
| 25 |
|
| 26 |
#ifdef RSYNC_TEST_NO_AT_FDCWD |
| 27 |
#undef AT_FDCWD |
| 28 |
#undef AT_SYMLINK_NOFOLLOW |
| 29 |
#undef HAVE_LINKAT |
| 30 |
#undef HAVE_OPENAT2 |
| 31 |
#undef HAVE_UTIMENSAT |
| 32 |
#undef O_RESOLVE_BENEATH |
| 33 |
#endif |
| 34 |
|
| 35 |
#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H |
| 36 |
#include <sys/un.h> |
| 37 |
#endif |
| 38 |
#ifdef HAVE_SYS_ATTR_H |
| 39 |
#include <sys/attr.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE |
| 43 |
#include <sys/syscall.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef __linux__ |
| 47 |
#include <sys/syscall.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "ifuncs.h" |
| 51 |
|
| 52 |
extern int dry_run; |
| 53 |
extern int am_root; |
| 54 |
extern int am_sender; |
| 55 |
extern int read_only; |
| 56 |
extern int list_only; |
| 57 |
extern int inplace; |
| 58 |
extern int preallocate_files; |
| 59 |
extern int sparse_files; |
| 60 |
extern int preserve_perms; |
| 61 |
extern int preserve_executability; |
| 62 |
extern int open_noatime; |
| 63 |
extern int copy_links; |
| 64 |
extern int copy_unsafe_links; |
| 65 |
extern int am_daemon; |
| 66 |
extern int am_chrooted; |
| 67 |
extern int insecure_links; |
| 68 |
extern int module_id; |
| 69 |
extern unsigned int module_dirlen; |
| 70 |
extern char *module_dir; |
| 71 |
extern int module_dirfd; |
| 72 |
extern char *confine_root; |
| 73 |
extern unsigned int confine_rootlen; |
| 74 |
extern char curr_dir[MAXPATHLEN]; |
| 75 |
extern int operator_path_resolve; |
| 76 |
|
| 77 |
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static int open_anchor_dirfd(const char *path) |
| 86 |
{ |
| 87 |
if (module_dirfd >= 0 && am_daemon && module_dir && strcmp(path, module_dir) == 0) |
| 88 |
return dup(module_dirfd); |
| 89 |
return openat(AT_FDCWD, path, O_RDONLY | O_DIRECTORY); |
| 90 |
} |
| 91 |
#endif |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
int secure_relpath_active(void) |
| 101 |
{ |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if (symlink_optout_allowed()) |
| 110 |
return 0; |
| 111 |
if (am_daemon && am_chrooted && module_dirlen) |
| 112 |
return 1; |
| 113 |
return !am_chrooted && (am_daemon || !am_sender); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int symlink_optout_allowed(void) |
| 123 |
{ |
| 124 |
if (am_daemon) |
| 125 |
return module_id >= 0 && lp_insecure_links(module_id); |
| 126 |
return insecure_links; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static const char *confinement_root(unsigned int *lenp) |
| 137 |
{ |
| 138 |
if (am_daemon) { |
| 139 |
*lenp = module_dirlen; |
| 140 |
return module_dir; |
| 141 |
} |
| 142 |
*lenp = confine_rootlen; |
| 143 |
return confine_root; |
| 144 |
} |
| 145 |
|
| •146 |
|
| 147 |
|
| 148 |
|
| 149 |
static const char *fd_pin_tail(const char *p) |
| 150 |
{ |
| 151 |
const char *s; |
| 152 |
|
| •153 |
if (strncmp(p, "/dev/fd", 7) == 0) { |
| 154 |
s = p + 7; |
| 155 |
return (*s == '\0' || *s == '/') ? s : NULL; |
| 156 |
} |
| 157 |
|
| 158 |
if (strncmp(p, "/proc/", 6) != 0) |
| 159 |
return NULL; |
| 160 |
s = p + 6; |
| 161 |
if (strncmp(s, "self/", 5) == 0) |
| 162 |
s += 4; |
| 163 |
else { |
| 164 |
const char *d = s; |
| 165 |
while (*s >= '0' && *s <= '9') |
| 166 |
s++; |
| 167 |
if (s == d || *s != '/') |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
if (strncmp(s, "/fd", 3) != 0) |
| 171 |
return NULL; |
| 172 |
s += 3; |
| 173 |
return (*s == '\0' || *s == '/') ? s : NULL; |
| 174 |
} |
| 175 |
|
| •176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static int is_exact_fd_pin(const char *p) |
| 182 |
{ |
| 183 |
const char *tail = fd_pin_tail(p); |
| 184 |
|
| 185 |
if (!tail || *tail != '/') |
| 186 |
return 0; |
| 187 |
for (++tail; *tail >= '0' && *tail <= '9'; tail++) {} |
| 188 |
return *tail == '\0' && tail[-1] != '/'; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static int abspath_outside_confinement(const char *abspath) |
| 203 |
{ |
| 204 |
unsigned int rootlen; |
| 205 |
const char *root = confinement_root(&rootlen); |
| 206 |
char pinned[MAXPATHLEN]; |
| 207 |
|
| 208 |
if (!root || !abspath) |
| 209 |
return 0; |
| 210 |
if (rootlen <= 1) |
| 211 |
return 0; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (!am_daemon) { |
| 219 |
const char *tail = fd_pin_tail(abspath); |
| 220 |
if (tail && !*tail) |
| 221 |
return 0; |
| 222 |
if (is_exact_fd_pin(abspath)) { |
| 223 |
ssize_t n = readlink(abspath, pinned, sizeof pinned - 1); |
| 224 |
if (n <= 0 || pinned[0] != '/') |
| 225 |
return operator_path_resolve ? 1 : 0; |
| 226 |
pinned[n] = '\0'; |
| 227 |
abspath = pinned; |
| 228 |
} |
| 229 |
} |
| 230 |
if (strncmp(abspath, root, rootlen) == 0 |
| 231 |
&& (abspath[rootlen] == '\0' || abspath[rootlen] == '/')) |
| 232 |
return 0; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
size_t alen = strlen(abspath); |
| 241 |
if (alen == 0 |
| 242 |
|| (strncmp(abspath, root, alen) == 0 && root[alen] == '/')) |
| 243 |
return 0; |
| 244 |
return operator_path_resolve ? 1 : 0; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static int abspath_step(char *abspath, size_t cap, const char *comp, size_t comp_len) |
| 252 |
{ |
| 253 |
if (comp_len == 1 && comp[0] == '.') |
| 254 |
return 0; |
| 255 |
if (comp_len == 2 && comp[0] == '.' && comp[1] == '.') { |
| 256 |
char *s = strrchr(abspath, '/'); |
| 257 |
if (s) |
| 258 |
*s = '\0'; |
| 259 |
else |
| 260 |
abspath[0] = '\0'; |
| 261 |
return 0; |
| 262 |
} |
| 263 |
size_t al = strlen(abspath); |
| 264 |
size_t off = (al > 0 && abspath[al-1] == '/') ? al : al + 1; |
| 265 |
if (off + comp_len >= cap) { |
| 266 |
errno = ENAMETOOLONG; |
| 267 |
return -1; |
| 268 |
} |
| 269 |
if (off != al) |
| 270 |
abspath[al] = '/'; |
| 271 |
memcpy(abspath + off, comp, comp_len + 1); |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, size_t out_cap) |
| 292 |
{ |
| 293 |
#if defined AT_FDCWD && defined O_NOFOLLOW |
| 294 |
|
| 295 |
|
| 296 |
#ifndef O_CLOEXEC |
| 297 |
#define O_CLOEXEC 0 |
| 298 |
#endif |
| 299 |
if (!path || !*path) { |
| 300 |
errno = EINVAL; |
| 301 |
return -1; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if (symlink_optout_allowed()) |
| 307 |
return open(path, flags, mode); |
| 308 |
|
| 309 |
const uid_t trusted_uid = geteuid(); |
| 310 |
int dfd = AT_FDCWD; |
| 311 |
int dfd_owns = 0; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
char abspath[MAXPATHLEN]; |
| 318 |
abspath[0] = '\0'; |
| 319 |
if (am_daemon && module_dir && module_dir[0] == '/') |
| 320 |
strlcpy(abspath, module_dir, sizeof abspath); |
| 321 |
else if (confine_root) { |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
if (!getcwd(abspath, sizeof abspath)) |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
int pin_transit = !am_daemon && confine_root && fd_pin_tail(path) != NULL; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
char remaining[MAXPATHLEN * 2]; |
| 349 |
if (strlcpy(remaining, path, sizeof remaining) >= sizeof remaining) { |
| 350 |
errno = ENAMETOOLONG; |
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
if (remaining[0] == '/') { |
| 356 |
dfd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 357 |
if (dfd < 0) |
| 358 |
return -1; |
| 359 |
dfd_owns = 1; |
| 360 |
abspath[0] = '\0'; |
| 361 |
char *p = remaining; |
| 362 |
while (*p == '/') p++; |
| 363 |
memmove(remaining, p, strlen(p) + 1); |
| 364 |
} |
| 365 |
|
| 366 |
int loops = 40; |
| 367 |
|
| 368 |
|
| 369 |
int retfd = -1; |
| 370 |
int saved_errno = 0; |
| 371 |
|
| 372 |
while (*remaining) { |
| 373 |
|
| 374 |
char *slash = strchr(remaining, '/'); |
| 375 |
size_t comp_len = slash ? (size_t)(slash - remaining) : strlen(remaining); |
| 376 |
char comp[MAXPATHLEN]; |
| 377 |
if (comp_len == 0 || comp_len >= sizeof comp) { |
| 378 |
saved_errno = comp_len == 0 ? EINVAL : ENAMETOOLONG; |
| 379 |
goto out; |
| 380 |
} |
| 381 |
memcpy(comp, remaining, comp_len); |
| 382 |
comp[comp_len] = '\0'; |
| 383 |
int is_last = (slash == NULL); |
| 384 |
|
| 385 |
|
| 386 |
STRUCT_STAT lst; |
| 387 |
if (fstatat(dfd, comp, &lst, AT_SYMLINK_NOFOLLOW) < 0) { |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if (is_last && errno == ENOENT && (flags & O_CREAT)) { |
| 392 |
if (abspath_step(abspath, sizeof abspath, comp, comp_len) < 0) { |
| 393 |
saved_errno = errno; |
| 394 |
goto out; |
| 395 |
} |
| 396 |
if (!pin_transit && abspath_outside_confinement(abspath)) { |
| 397 |
saved_errno = ELOOP; |
| 398 |
goto out; |
| 399 |
} |
| 400 |
retfd = openat(dfd, comp, flags | O_NOFOLLOW, mode); |
| 401 |
saved_errno = errno; |
| 402 |
goto out; |
| 403 |
} |
| 404 |
saved_errno = errno; |
| 405 |
goto out; |
| 406 |
} |
| 407 |
|
| 408 |
if (S_ISLNK(lst.st_mode)) { |
| •409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
int namespace_pin = pin_transit |
| 414 |
&& ((strcmp(abspath, "/proc") == 0 && strcmp(comp, "self") == 0) |
| 415 |
|| (strcmp(abspath, "/dev") == 0 && strcmp(comp, "fd") == 0)); |
| 416 |
if (!namespace_pin && lst.st_uid != 0 && lst.st_uid != trusted_uid) { |
| 417 |
saved_errno = ELOOP; |
| 418 |
goto out; |
| 419 |
} |
| 420 |
if (--loops < 0) { |
| 421 |
saved_errno = ELOOP; |
| 422 |
goto out; |
| 423 |
} |
| 424 |
char target[MAXPATHLEN]; |
| 425 |
ssize_t n = readlinkat(dfd, comp, target, sizeof target - 1); |
| 426 |
if (n < 0) { |
| 427 |
saved_errno = errno; |
| 428 |
goto out; |
| 429 |
} |
| 430 |
target[n] = '\0'; |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
char tail[MAXPATHLEN]; |
| 435 |
tail[0] = '\0'; |
| 436 |
if (slash) |
| 437 |
strlcpy(tail, slash, sizeof tail); |
| 438 |
|
| 439 |
char rebuilt[MAXPATHLEN * 2]; |
| 440 |
if (snprintf(rebuilt, sizeof rebuilt, "%s%s", |
| 441 |
target, tail) >= (int)sizeof rebuilt) { |
| 442 |
saved_errno = ENAMETOOLONG; |
| 443 |
goto out; |
| 444 |
} |
| 445 |
|
| 446 |
if (target[0] == '/') { |
| 447 |
if (dfd_owns) close(dfd); |
| 448 |
dfd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 449 |
if (dfd < 0) { |
| 450 |
saved_errno = errno; |
| 451 |
dfd_owns = 0; |
| 452 |
goto out; |
| 453 |
} |
| 454 |
dfd_owns = 1; |
| 455 |
abspath[0] = '\0'; |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
pin_transit = pin_transit && fd_pin_tail(rebuilt) != NULL; |
| 460 |
char *p = rebuilt; |
| 461 |
while (*p == '/') p++; |
| 462 |
strlcpy(remaining, p, sizeof remaining); |
| 463 |
} else { |
| 464 |
strlcpy(remaining, rebuilt, sizeof remaining); |
| 465 |
} |
| 466 |
continue; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
if (is_last) { |
| 471 |
if (abspath_step(abspath, sizeof abspath, comp, comp_len) < 0) { |
| 472 |
saved_errno = errno; |
| 473 |
goto out; |
| 474 |
} |
| 475 |
if (!pin_transit && abspath_outside_confinement(abspath)) { |
| 476 |
saved_errno = ELOOP; |
| 477 |
goto out; |
| 478 |
} |
| 479 |
retfd = openat(dfd, comp, flags | O_NOFOLLOW, mode); |
| 480 |
saved_errno = errno; |
| 481 |
|
| 482 |
|
| 483 |
if (retfd >= 0 && out_abs && out_cap) |
| 484 |
|
| 485 |
|
| 486 |
strlcpy(out_abs, (am_daemon && !abspath[0]) ? "/" : abspath, out_cap); |
| 487 |
goto out; |
| 488 |
} |
| 489 |
|
| 490 |
if (!S_ISDIR(lst.st_mode)) { |
| 491 |
saved_errno = ENOTDIR; |
| 492 |
goto out; |
| 493 |
} |
| 494 |
|
| 495 |
if (abspath_step(abspath, sizeof abspath, comp, comp_len) < 0) { |
| 496 |
saved_errno = errno; |
| 497 |
goto out; |
| 498 |
} |
| 499 |
if (!pin_transit && abspath_outside_confinement(abspath)) { |
| 500 |
saved_errno = ELOOP; |
| 501 |
goto out; |
| 502 |
} |
| 503 |
int next = openat(dfd, comp, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 504 |
if (next < 0) { |
| 505 |
saved_errno = errno; |
| 506 |
goto out; |
| 507 |
} |
| 508 |
if (dfd_owns) close(dfd); |
| 509 |
dfd = next; |
| 510 |
dfd_owns = 1; |
| 511 |
|
| 512 |
|
| 513 |
if (slash) { |
| 514 |
char *p = slash; |
| 515 |
while (*p == '/') p++; |
| 516 |
memmove(remaining, p, strlen(p) + 1); |
| 517 |
} else { |
| 518 |
remaining[0] = '\0'; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
if (flags & O_DIRECTORY) { |
| 526 |
retfd = dfd; |
| 527 |
dfd_owns = 0; |
| 528 |
saved_errno = 0; |
| 529 |
if (out_abs && out_cap) |
| 530 |
|
| 531 |
|
| 532 |
strlcpy(out_abs, (am_daemon && !abspath[0]) ? "/" : abspath, out_cap); |
| 533 |
} else { |
| 534 |
saved_errno = EISDIR; |
| 535 |
} |
| 536 |
|
| 537 |
out: |
| 538 |
if (dfd_owns) close(dfd); |
| 539 |
errno = saved_errno; |
| 540 |
return retfd; |
| 541 |
#else |
| 542 |
|
| 543 |
(void)out_abs; (void)out_cap; |
| 544 |
return open(path, flags, mode); |
| 545 |
#endif |
| 546 |
} |
| 547 |
|
| 548 |
int open_no_attacker_symlinks(const char *path, int flags, mode_t mode) |
| 549 |
{ |
| 550 |
return ona_open(path, flags, mode, NULL, 0); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
int operator_path_resolve = 0; |
| 563 |
|
| 564 |
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
int owner_walk_parent(const char *path, const char **bname) |
| 569 |
{ |
| 570 |
const char *slash = strrchr(path, '/'); |
| 571 |
char dir[MAXPATHLEN], pabs[MAXPATHLEN]; |
| 572 |
size_t dlen; |
| 573 |
int dfd; |
| 574 |
|
| 575 |
*bname = slash ? slash + 1 : path; |
| 576 |
pabs[0] = '\0'; |
| 577 |
if (!slash) |
| 578 |
dfd = ona_open(".", O_RDONLY | O_DIRECTORY, 0, pabs, sizeof pabs); |
| 579 |
else { |
| 580 |
dlen = slash == path ? 1 : (size_t)(slash - path); |
| 581 |
if (dlen >= sizeof dir) { |
| 582 |
errno = ENAMETOOLONG; |
| 583 |
return -1; |
| 584 |
} |
| 585 |
memcpy(dir, path, dlen); |
| 586 |
dir[dlen] = '\0'; |
| 587 |
dfd = ona_open(dir, O_RDONLY | O_DIRECTORY, 0, pabs, sizeof pabs); |
| 588 |
} |
| 589 |
if (dfd < 0) |
| 590 |
return -1; |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
if (pabs[0]) { |
| 596 |
char leafabs[MAXPATHLEN]; |
| 597 |
if (snprintf(leafabs, sizeof leafabs, "%s/%s", pabs, *bname) >= (int)sizeof leafabs) { |
| 598 |
close(dfd); |
| 599 |
errno = ENAMETOOLONG; |
| 600 |
return -1; |
| 601 |
} |
| 602 |
if (abspath_outside_confinement(leafabs)) { |
| 603 |
close(dfd); |
| 604 |
errno = ELOOP; |
| 605 |
return -1; |
| 606 |
} |
| 607 |
} |
| 608 |
return dfd; |
| 609 |
} |
| 610 |
#endif |
| 611 |
|
| 612 |
#ifndef S_BLKSIZE |
| 613 |
# if defined hpux || defined __hpux__ || defined __hpux |
| 614 |
# define S_BLKSIZE 1024 |
| 615 |
# elif defined _AIX && defined _I386 |
| 616 |
# define S_BLKSIZE 4096 |
| 617 |
# else |
| 618 |
# define S_BLKSIZE 512 |
| 619 |
# endif |
| 620 |
#endif |
| 621 |
|
| 622 |
#ifdef SUPPORT_CRTIMES |
| 623 |
#ifdef HAVE_GETATTRLIST |
| 624 |
#pragma pack(push, 4) |
| 625 |
struct create_time { |
| 626 |
uint32 length; |
| 627 |
struct timespec crtime; |
| 628 |
}; |
| 629 |
#pragma pack(pop) |
| 630 |
#elif defined __CYGWIN__ |
| 631 |
#include <windows.h> |
| 632 |
#endif |
| 633 |
#endif |
| 634 |
|
| 635 |
#define RETURN_ERROR_IF(x,e) \ |
| 636 |
do { \ |
| 637 |
if (x) { \ |
| 638 |
errno = (e); \ |
| 639 |
return -1; \ |
| 640 |
} \ |
| 641 |
} while (0) |
| 642 |
|
| 643 |
#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS) |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
#define RETURN_ERROR_IF_NULL(p) RETURN_ERROR_IF(!(p), EFAULT) |
| 649 |
|
| 650 |
int do_unlink(const char *path) |
| 651 |
{ |
| 652 |
if (dry_run) return 0; |
| 653 |
RETURN_ERROR_IF_RO_OR_LO; |
| 654 |
return unlink(path); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
int do_unlink_at(const char *path) |
| 669 |
{ |
| 670 |
#ifdef AT_FDCWD |
| 671 |
extern int am_daemon, am_chrooted; |
| 672 |
char dirpath[MAXPATHLEN]; |
| 673 |
const char *bname; |
| 674 |
const char *slash; |
| 675 |
int dfd, ret, e; |
| 676 |
size_t dlen; |
| 677 |
|
| 678 |
if (dry_run) return 0; |
| 679 |
RETURN_ERROR_IF_RO_OR_LO; |
| 680 |
RETURN_ERROR_IF_NULL(path); |
| 681 |
|
| 682 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 683 |
if (operator_path_resolve) { |
| 684 |
if (symlink_optout_allowed()) |
| 685 |
return unlink(path); |
| 686 |
dfd = owner_walk_parent(path, &bname); |
| 687 |
if (dfd < 0) |
| 688 |
return -1; |
| 689 |
ret = unlinkat(dfd, bname, 0); |
| 690 |
e = errno; |
| 691 |
close(dfd); |
| 692 |
errno = e; |
| 693 |
return ret; |
| 694 |
} |
| 695 |
#endif |
| 696 |
|
| 697 |
if (!secure_relpath_active()) |
| 698 |
return unlink(path); |
| 699 |
|
| 700 |
if (!path || !*path || *path == '/') |
| 701 |
return unlink(path); |
| 702 |
|
| 703 |
slash = strrchr(path, '/'); |
| 704 |
if (!slash) |
| 705 |
return unlink(path); |
| 706 |
|
| 707 |
dlen = slash - path; |
| 708 |
if (dlen >= sizeof dirpath) { |
| 709 |
errno = ENAMETOOLONG; |
| 710 |
return -1; |
| 711 |
} |
| 712 |
memcpy(dirpath, path, dlen); |
| 713 |
dirpath[dlen] = '\0'; |
| 714 |
bname = slash + 1; |
| 715 |
|
| 716 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 717 |
if (dfd < 0) |
| 718 |
return -1; |
| 719 |
|
| 720 |
ret = unlinkat(dfd, bname, 0); |
| 721 |
e = errno; |
| 722 |
close(dfd); |
| 723 |
errno = e; |
| 724 |
return ret; |
| 725 |
#else |
| 726 |
return do_unlink(path); |
| 727 |
#endif |
| 728 |
} |
| 729 |
|
| 730 |
#ifdef SUPPORT_LINKS |
| 731 |
int do_symlink(const char *lnk, const char *path) |
| 732 |
{ |
| 733 |
if (dry_run) return 0; |
| 734 |
RETURN_ERROR_IF_RO_OR_LO; |
| 735 |
RETURN_ERROR_IF_NULL(lnk); |
| 736 |
RETURN_ERROR_IF_NULL(path); |
| 737 |
|
| 738 |
#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS |
| 739 |
|
| 740 |
|
| 741 |
if (am_root < 0) { |
| 742 |
int ok, len = strlen(lnk); |
| 743 |
int fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); |
| 744 |
if (fd < 0) |
| 745 |
return -1; |
| 746 |
ok = write(fd, lnk, len) == len; |
| 747 |
if (close(fd) < 0) |
| 748 |
ok = 0; |
| 749 |
return ok ? 0 : -1; |
| 750 |
} |
| 751 |
#endif |
| 752 |
|
| 753 |
return symlink(lnk, path); |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
int do_symlink_at(const char *lnk, const char *path) |
| 776 |
{ |
| 777 |
#ifdef AT_FDCWD |
| 778 |
extern int am_daemon, am_chrooted; |
| 779 |
char dirpath[MAXPATHLEN]; |
| 780 |
const char *bname; |
| 781 |
const char *slash; |
| 782 |
int dfd = AT_FDCWD, ret, e; |
| 783 |
BOOL owns = False; |
| 784 |
size_t dlen; |
| 785 |
|
| 786 |
if (dry_run) return 0; |
| 787 |
RETURN_ERROR_IF_RO_OR_LO; |
| 788 |
|
| 789 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 790 |
if (operator_path_resolve) { |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
if (symlink_optout_allowed()) |
| 795 |
return do_symlink(lnk, path); |
| 796 |
dfd = owner_walk_parent(path, &bname); |
| 797 |
if (dfd < 0) |
| 798 |
return -1; |
| 799 |
owns = True; |
| 800 |
} else |
| 801 |
#endif |
| 802 |
{ |
| 803 |
if (!secure_relpath_active()) |
| 804 |
return do_symlink(lnk, path); |
| 805 |
|
| 806 |
if (!path || !*path || *path == '/') |
| 807 |
return do_symlink(lnk, path); |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
slash = strrchr(path, '/'); |
| 814 |
if (slash) { |
| 815 |
dlen = slash - path; |
| 816 |
if (dlen >= sizeof dirpath) { |
| 817 |
errno = ENAMETOOLONG; |
| 818 |
return -1; |
| 819 |
} |
| 820 |
memcpy(dirpath, path, dlen); |
| 821 |
dirpath[dlen] = '\0'; |
| 822 |
bname = slash + 1; |
| 823 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 824 |
if (dfd < 0) |
| 825 |
return -1; |
| 826 |
owns = True; |
| 827 |
} else { |
| 828 |
bname = path; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
if (am_root < 0) { |
| 838 |
int len = strlen(lnk); |
| 839 |
int fd = openat(dfd, bname, |
| 840 |
O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, |
| 841 |
S_IWUSR | S_IRUSR); |
| 842 |
if (fd < 0) { |
| 843 |
e = errno; |
| 844 |
if (owns) close(dfd); |
| 845 |
errno = e; |
| 846 |
return -1; |
| 847 |
} |
| 848 |
ret = (write(fd, lnk, len) == len) ? 0 : -1; |
| 849 |
if (close(fd) < 0) |
| 850 |
ret = -1; |
| 851 |
e = errno; |
| 852 |
if (owns) close(dfd); |
| 853 |
errno = e; |
| 854 |
return ret; |
| 855 |
} |
| 856 |
#endif |
| 857 |
|
| 858 |
ret = symlinkat(lnk, dfd, bname); |
| 859 |
e = errno; |
| 860 |
if (owns) close(dfd); |
| 861 |
errno = e; |
| 862 |
return ret; |
| 863 |
#else |
| 864 |
return do_symlink(lnk, path); |
| 865 |
#endif |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS |
| 871 |
ssize_t do_readlink(const char *path, char *buf, size_t bufsiz) |
| 872 |
{ |
| 873 |
|
| 874 |
if (am_root < 0) { |
| 875 |
int fd = do_open_nofollow(path, O_RDONLY); |
| 876 |
if (fd >= 0) { |
| 877 |
int len = read(fd, buf, bufsiz); |
| 878 |
close(fd); |
| 879 |
return len; |
| 880 |
} |
| 881 |
if (!NOFOLLOW_HIT_SYMLINK(errno)) |
| 882 |
return -1; |
| 883 |
|
| 884 |
|
| 885 |
if (!am_sender) |
| 886 |
return 0; |
| 887 |
|
| 888 |
} |
| 889 |
|
| 890 |
return readlink(path, buf, bufsiz); |
| 891 |
} |
| 892 |
#endif |
| 893 |
|
| 894 |
ssize_t do_readlink_atfd(int dfd, const char *name, char *buf, size_t bufsiz) |
| 895 |
{ |
| 896 |
#ifdef AT_FDCWD |
| 897 |
# if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS |
| 898 |
if (am_root < 0) { |
| 899 |
int fd = openat(dfd, name, O_RDONLY | O_NOFOLLOW); |
| 900 |
if (fd >= 0) { |
| 901 |
int len = read(fd, buf, bufsiz); |
| 902 |
close(fd); |
| 903 |
return len; |
| 904 |
} |
| 905 |
if (!NOFOLLOW_HIT_SYMLINK(errno)) |
| 906 |
return -1; |
| 907 |
if (!am_sender) |
| 908 |
return 0; |
| 909 |
} |
| 910 |
# endif |
| 911 |
return readlinkat(dfd, name, buf, bufsiz); |
| 912 |
#else |
| 913 |
(void)dfd; |
| 914 |
return do_readlink(name, buf, bufsiz); |
| 915 |
#endif |
| 916 |
} |
| 917 |
#endif |
| 918 |
|
| 919 |
#if defined HAVE_LINK || defined HAVE_LINKAT |
| 920 |
int do_link(const char *old_path, const char *new_path) |
| 921 |
{ |
| 922 |
if (dry_run) return 0; |
| 923 |
RETURN_ERROR_IF_RO_OR_LO; |
| 924 |
RETURN_ERROR_IF_NULL(old_path); |
| 925 |
RETURN_ERROR_IF_NULL(new_path); |
| 926 |
#ifdef HAVE_LINKAT |
| 927 |
return linkat(AT_FDCWD, old_path, AT_FDCWD, new_path, 0); |
| 928 |
#else |
| 929 |
return link(old_path, new_path); |
| 930 |
#endif |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
int do_link_at(const char *old_path, const char *new_path) |
| 948 |
{ |
| 949 |
#if defined AT_FDCWD && defined HAVE_LINKAT |
| 950 |
extern int am_daemon, am_chrooted; |
| 951 |
char old_dirpath[MAXPATHLEN], new_dirpath[MAXPATHLEN]; |
| 952 |
const char *old_bname, *new_bname; |
| 953 |
const char *old_slash, *new_slash; |
| 954 |
int old_dfd = AT_FDCWD, new_dfd = AT_FDCWD; |
| 955 |
BOOL old_owns = False, new_owns = False; |
| 956 |
int ret, e; |
| 957 |
size_t old_dlen = 0, new_dlen = 0; |
| 958 |
|
| 959 |
if (dry_run) return 0; |
| 960 |
RETURN_ERROR_IF_RO_OR_LO; |
| 961 |
|
| 962 |
if (!secure_relpath_active()) |
| 963 |
return do_link(old_path, new_path); |
| 964 |
|
| 965 |
if (!old_path || !*old_path || !new_path || !*new_path) |
| 966 |
return do_link(old_path, new_path); |
| 967 |
|
| 968 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 969 |
|
| 970 |
|
| 971 |
if (operator_path_resolve) { |
| 972 |
if (symlink_optout_allowed()) |
| 973 |
return do_link(old_path, new_path); |
| 974 |
old_dfd = owner_walk_parent(old_path, &old_bname); |
| 975 |
if (old_dfd < 0) |
| 976 |
return -1; |
| 977 |
new_dfd = owner_walk_parent(new_path, &new_bname); |
| 978 |
if (new_dfd < 0) { |
| 979 |
e = errno; |
| 980 |
close(old_dfd); |
| 981 |
errno = e; |
| 982 |
return -1; |
| 983 |
} |
| 984 |
ret = linkat(old_dfd, old_bname, new_dfd, new_bname, 0); |
| 985 |
e = errno; |
| 986 |
close(new_dfd); |
| 987 |
close(old_dfd); |
| 988 |
errno = e; |
| 989 |
return ret; |
| 990 |
} |
| 991 |
#endif |
| 992 |
|
| 993 |
old_slash = strrchr(old_path, '/'); |
| 994 |
new_slash = strrchr(new_path, '/'); |
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
if (*old_path == '/') { |
| 1008 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1009 |
if (!symlink_optout_allowed()) { |
| 1010 |
operator_path_resolve = 1; |
| 1011 |
old_dfd = owner_walk_parent(old_path, &old_bname); |
| 1012 |
operator_path_resolve = 0; |
| 1013 |
if (old_dfd < 0) |
| 1014 |
return -1; |
| 1015 |
old_owns = True; |
| 1016 |
} else |
| 1017 |
#endif |
| 1018 |
old_bname = old_path; |
| 1019 |
} else if (old_slash) { |
| 1020 |
old_dlen = old_slash - old_path; |
| 1021 |
if (old_dlen >= sizeof old_dirpath) { errno = ENAMETOOLONG; return -1; } |
| 1022 |
memcpy(old_dirpath, old_path, old_dlen); |
| 1023 |
old_dirpath[old_dlen] = '\0'; |
| 1024 |
old_bname = old_slash + 1; |
| 1025 |
old_dfd = secure_relative_open(NULL, old_dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1026 |
if (old_dfd < 0) |
| 1027 |
return -1; |
| 1028 |
old_owns = True; |
| 1029 |
} else { |
| 1030 |
old_bname = old_path; |
| 1031 |
} |
| 1032 |
|
| 1033 |
if (*new_path == '/') { |
| 1034 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1035 |
if (!symlink_optout_allowed()) { |
| 1036 |
operator_path_resolve = 1; |
| 1037 |
new_dfd = owner_walk_parent(new_path, &new_bname); |
| 1038 |
operator_path_resolve = 0; |
| 1039 |
if (new_dfd < 0) { |
| 1040 |
e = errno; |
| 1041 |
if (old_owns) close(old_dfd); |
| 1042 |
errno = e; |
| 1043 |
return -1; |
| 1044 |
} |
| 1045 |
new_owns = True; |
| 1046 |
} else |
| 1047 |
#endif |
| 1048 |
new_bname = new_path; |
| 1049 |
} else if (new_slash) { |
| 1050 |
new_dlen = new_slash - new_path; |
| 1051 |
if (new_dlen >= sizeof new_dirpath) { |
| 1052 |
e = ENAMETOOLONG; |
| 1053 |
if (old_owns) close(old_dfd); |
| 1054 |
errno = e; |
| 1055 |
return -1; |
| 1056 |
} |
| 1057 |
memcpy(new_dirpath, new_path, new_dlen); |
| 1058 |
new_dirpath[new_dlen] = '\0'; |
| 1059 |
new_bname = new_slash + 1; |
| 1060 |
if (old_owns && old_dlen == new_dlen |
| 1061 |
&& memcmp(old_dirpath, new_dirpath, old_dlen) == 0) { |
| 1062 |
new_dfd = old_dfd; |
| 1063 |
} else { |
| 1064 |
new_dfd = secure_relative_open(NULL, new_dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1065 |
if (new_dfd < 0) { |
| 1066 |
e = errno; |
| 1067 |
if (old_owns) close(old_dfd); |
| 1068 |
errno = e; |
| 1069 |
return -1; |
| 1070 |
} |
| 1071 |
new_owns = True; |
| 1072 |
} |
| 1073 |
} else { |
| 1074 |
new_bname = new_path; |
| 1075 |
} |
| 1076 |
|
| 1077 |
ret = linkat(old_dfd, old_bname, new_dfd, new_bname, 0); |
| 1078 |
e = errno; |
| 1079 |
if (new_owns) |
| 1080 |
close(new_dfd); |
| 1081 |
if (old_owns) |
| 1082 |
close(old_dfd); |
| 1083 |
errno = e; |
| 1084 |
return ret; |
| 1085 |
#else |
| 1086 |
return do_link(old_path, new_path); |
| 1087 |
#endif |
| 1088 |
} |
| 1089 |
#endif |
| 1090 |
|
| 1091 |
int do_lchown(const char *path, uid_t owner, gid_t group) |
| 1092 |
{ |
| 1093 |
if (dry_run) return 0; |
| 1094 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1095 |
RETURN_ERROR_IF_NULL(path); |
| 1096 |
#ifndef HAVE_LCHOWN |
| 1097 |
#define lchown chown |
| 1098 |
#endif |
| 1099 |
return lchown(path, owner, group); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
int do_lchown_at(const char *fname, uid_t owner, gid_t group) |
| 1117 |
{ |
| 1118 |
#if defined AT_FDCWD && defined AT_SYMLINK_NOFOLLOW |
| 1119 |
extern int am_daemon, am_chrooted; |
| 1120 |
char dirpath[MAXPATHLEN]; |
| 1121 |
const char *bname; |
| 1122 |
const char *slash; |
| 1123 |
int dfd, ret, e; |
| 1124 |
size_t dlen; |
| 1125 |
|
| 1126 |
if (dry_run) return 0; |
| 1127 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1128 |
|
| 1129 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
if (operator_path_resolve && fname && *fname) { |
| 1135 |
if (symlink_optout_allowed()) |
| 1136 |
return do_lchown(fname, owner, group); |
| 1137 |
dfd = owner_walk_parent(fname, &bname); |
| 1138 |
if (dfd < 0) |
| 1139 |
return -1; |
| 1140 |
ret = fchownat(dfd, bname, owner, group, AT_SYMLINK_NOFOLLOW); |
| 1141 |
e = errno; |
| 1142 |
close(dfd); |
| 1143 |
errno = e; |
| 1144 |
return ret; |
| 1145 |
} |
| 1146 |
#endif |
| 1147 |
|
| 1148 |
if (!secure_relpath_active()) |
| 1149 |
return do_lchown(fname, owner, group); |
| 1150 |
|
| 1151 |
if (!fname || !*fname || *fname == '/') |
| 1152 |
return do_lchown(fname, owner, group); |
| 1153 |
|
| 1154 |
slash = strrchr(fname, '/'); |
| 1155 |
if (!slash) |
| 1156 |
return do_lchown(fname, owner, group); |
| 1157 |
|
| 1158 |
dlen = slash - fname; |
| 1159 |
if (dlen >= sizeof dirpath) { |
| 1160 |
errno = ENAMETOOLONG; |
| 1161 |
return -1; |
| 1162 |
} |
| 1163 |
memcpy(dirpath, fname, dlen); |
| 1164 |
dirpath[dlen] = '\0'; |
| 1165 |
bname = slash + 1; |
| 1166 |
|
| 1167 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1168 |
if (dfd < 0) |
| 1169 |
return -1; |
| 1170 |
|
| 1171 |
ret = fchownat(dfd, bname, owner, group, AT_SYMLINK_NOFOLLOW); |
| 1172 |
e = errno; |
| 1173 |
close(dfd); |
| 1174 |
errno = e; |
| 1175 |
return ret; |
| 1176 |
#else |
| 1177 |
return do_lchown(fname, owner, group); |
| 1178 |
#endif |
| 1179 |
} |
| 1180 |
|
| 1181 |
int do_mknod(const char *pathname, mode_t mode, dev_t dev) |
| 1182 |
{ |
| 1183 |
if (dry_run) return 0; |
| 1184 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1185 |
RETURN_ERROR_IF_NULL(pathname); |
| 1186 |
|
| 1187 |
|
| 1188 |
if (am_root < 0) { |
| 1189 |
int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR); |
| 1190 |
if (fd < 0 || close(fd) < 0) |
| 1191 |
return -1; |
| 1192 |
return 0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
#ifdef HAVE_MKNOD |
| 1201 |
if (mknod(pathname, mode, dev) == 0) |
| 1202 |
return 0; |
| 1203 |
#endif |
| 1204 |
#ifdef HAVE_MKFIFO |
| 1205 |
if (S_ISFIFO(mode)) |
| 1206 |
return mkfifo(pathname, mode); |
| 1207 |
#endif |
| 1208 |
#ifdef HAVE_SYS_UN_H |
| 1209 |
if (S_ISSOCK(mode)) { |
| 1210 |
int sock; |
| 1211 |
struct sockaddr_un saddr; |
| 1212 |
unsigned int len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path); |
| 1213 |
if (len >= sizeof saddr.sun_path) { |
| 1214 |
errno = ENAMETOOLONG; |
| 1215 |
return -1; |
| 1216 |
} |
| 1217 |
#ifdef HAVE_SOCKADDR_UN_LEN |
| 1218 |
saddr.sun_len = len + 1; |
| 1219 |
#endif |
| 1220 |
saddr.sun_family = AF_UNIX; |
| 1221 |
|
| 1222 |
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 |
| 1223 |
|| (unlink(pathname) < 0 && errno != ENOENT) |
| 1224 |
|| (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0) |
| 1225 |
return -1; |
| 1226 |
close(sock); |
| 1227 |
#ifdef HAVE_CHMOD |
| 1228 |
return do_chmod(pathname, mode); |
| 1229 |
#else |
| 1230 |
return 0; |
| 1231 |
#endif |
| 1232 |
} |
| 1233 |
#endif |
| 1234 |
#ifdef HAVE_MKNOD |
| 1235 |
return -1; |
| 1236 |
#else |
| 1237 |
errno = ENOSYS; |
| 1238 |
return -1; |
| 1239 |
#endif |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
int do_mknod_at(const char *pathname, mode_t mode, dev_t dev) |
| 1264 |
{ |
| 1265 |
|
| 1266 |
|
| 1267 |
#if defined(AT_FDCWD) && defined(HAVE_MKNODAT) |
| 1268 |
extern int am_daemon, am_chrooted; |
| 1269 |
char dirpath[MAXPATHLEN]; |
| 1270 |
const char *bname; |
| 1271 |
const char *slash; |
| 1272 |
int dfd = AT_FDCWD, ret, e; |
| 1273 |
BOOL owns = False; |
| 1274 |
size_t dlen; |
| 1275 |
|
| 1276 |
if (dry_run) return 0; |
| 1277 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1278 |
|
| 1279 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1280 |
if (operator_path_resolve) { |
| 1281 |
if (symlink_optout_allowed()) |
| 1282 |
return do_mknod(pathname, mode, dev); |
| 1283 |
dfd = owner_walk_parent(pathname, &bname); |
| 1284 |
if (dfd < 0) |
| 1285 |
return -1; |
| 1286 |
if (am_root < 0) { |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
int fd = openat(dfd, bname, |
| 1292 |
O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, |
| 1293 |
S_IWUSR | S_IRUSR); |
| 1294 |
ret = fd < 0 ? -1 : close(fd); |
| 1295 |
} else { |
| 1296 |
ret = mknodat(dfd, bname, mode, dev); |
| 1297 |
} |
| 1298 |
if (ret < 0 && am_root >= 0) { |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
#ifdef HAVE_MKFIFOAT |
| 1304 |
if (S_ISFIFO(mode)) |
| 1305 |
ret = mkfifoat(dfd, bname, mode); |
| 1306 |
else |
| 1307 |
#endif |
| 1308 |
if (S_ISSOCK(mode)) |
| 1309 |
errno = EOPNOTSUPP; |
| 1310 |
} |
| 1311 |
e = errno; |
| 1312 |
close(dfd); |
| 1313 |
errno = e; |
| 1314 |
return ret; |
| 1315 |
} |
| 1316 |
#endif |
| 1317 |
|
| 1318 |
if (!secure_relpath_active()) |
| 1319 |
return do_mknod(pathname, mode, dev); |
| 1320 |
|
| 1321 |
if (!pathname || !*pathname || *pathname == '/') |
| 1322 |
return do_mknod(pathname, mode, dev); |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
slash = strrchr(pathname, '/'); |
| 1329 |
if (slash) { |
| 1330 |
dlen = slash - pathname; |
| 1331 |
if (dlen >= sizeof dirpath) { |
| 1332 |
errno = ENAMETOOLONG; |
| 1333 |
return -1; |
| 1334 |
} |
| 1335 |
memcpy(dirpath, pathname, dlen); |
| 1336 |
dirpath[dlen] = '\0'; |
| 1337 |
bname = slash + 1; |
| 1338 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1339 |
if (dfd < 0) |
| 1340 |
return -1; |
| 1341 |
owns = True; |
| 1342 |
} else { |
| 1343 |
bname = pathname; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if (am_root < 0) { |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
int fd = openat(dfd, bname, |
| 1354 |
O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, |
| 1355 |
S_IWUSR | S_IRUSR); |
| 1356 |
if (fd < 0) { |
| 1357 |
e = errno; |
| 1358 |
if (owns) close(dfd); |
| 1359 |
errno = e; |
| 1360 |
return -1; |
| 1361 |
} |
| 1362 |
ret = (close(fd) < 0) ? -1 : 0; |
| 1363 |
e = errno; |
| 1364 |
if (owns) close(dfd); |
| 1365 |
errno = e; |
| 1366 |
return ret; |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
ret = mknodat(dfd, bname, mode, dev); |
| 1373 |
if (ret < 0) { |
| 1374 |
#ifdef HAVE_MKFIFOAT |
| 1375 |
if (S_ISFIFO(mode)) |
| 1376 |
ret = mkfifoat(dfd, bname, mode); |
| 1377 |
else |
| 1378 |
#endif |
| 1379 |
if (S_ISSOCK(mode)) { |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
if (dfd == AT_FDCWD) |
| 1385 |
ret = do_mknod(pathname, mode, dev); |
| 1386 |
else |
| 1387 |
errno = EOPNOTSUPP; |
| 1388 |
} |
| 1389 |
|
| 1390 |
} |
| 1391 |
e = errno; |
| 1392 |
if (owns) close(dfd); |
| 1393 |
errno = e; |
| 1394 |
return ret; |
| 1395 |
#else |
| 1396 |
return do_mknod(pathname, mode, dev); |
| 1397 |
#endif |
| 1398 |
} |
| 1399 |
|
| 1400 |
int do_rmdir(const char *pathname) |
| 1401 |
{ |
| 1402 |
if (dry_run) return 0; |
| 1403 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1404 |
return rmdir(pathname); |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
int do_rmdir_at(const char *pathname) |
| 1413 |
{ |
| 1414 |
#ifdef AT_FDCWD |
| 1415 |
extern int am_daemon, am_chrooted; |
| 1416 |
char dirpath[MAXPATHLEN]; |
| 1417 |
const char *bname; |
| 1418 |
const char *slash; |
| 1419 |
int dfd, ret, e; |
| 1420 |
size_t dlen; |
| 1421 |
|
| 1422 |
if (dry_run) return 0; |
| 1423 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1424 |
RETURN_ERROR_IF_NULL(pathname); |
| 1425 |
|
| 1426 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1427 |
if (operator_path_resolve) { |
| 1428 |
if (symlink_optout_allowed()) |
| 1429 |
return do_rmdir(pathname); |
| 1430 |
dfd = owner_walk_parent(pathname, &bname); |
| 1431 |
if (dfd < 0) |
| 1432 |
return -1; |
| 1433 |
ret = unlinkat(dfd, bname, AT_REMOVEDIR); |
| 1434 |
e = errno; |
| 1435 |
close(dfd); |
| 1436 |
errno = e; |
| 1437 |
return ret; |
| 1438 |
} |
| 1439 |
#endif |
| 1440 |
|
| 1441 |
if (!secure_relpath_active()) |
| 1442 |
return rmdir(pathname); |
| 1443 |
|
| 1444 |
if (!pathname || !*pathname || *pathname == '/') |
| 1445 |
return rmdir(pathname); |
| 1446 |
|
| 1447 |
slash = strrchr(pathname, '/'); |
| 1448 |
if (!slash) |
| 1449 |
return rmdir(pathname); |
| 1450 |
|
| 1451 |
dlen = slash - pathname; |
| 1452 |
if (dlen >= sizeof dirpath) { |
| 1453 |
errno = ENAMETOOLONG; |
| 1454 |
return -1; |
| 1455 |
} |
| 1456 |
memcpy(dirpath, pathname, dlen); |
| 1457 |
dirpath[dlen] = '\0'; |
| 1458 |
bname = slash + 1; |
| 1459 |
|
| 1460 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1461 |
if (dfd < 0) |
| 1462 |
return -1; |
| 1463 |
|
| 1464 |
ret = unlinkat(dfd, bname, AT_REMOVEDIR); |
| 1465 |
e = errno; |
| 1466 |
close(dfd); |
| 1467 |
errno = e; |
| 1468 |
return ret; |
| 1469 |
#else |
| 1470 |
return do_rmdir(pathname); |
| 1471 |
#endif |
| 1472 |
} |
| 1473 |
|
| 1474 |
int do_open(const char *pathname, int flags, mode_t mode) |
| 1475 |
{ |
| 1476 |
RETURN_ERROR_IF_NULL(pathname); |
| 1477 |
if (flags != O_RDONLY) { |
| 1478 |
RETURN_ERROR_IF(dry_run, 0); |
| 1479 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1480 |
} |
| 1481 |
|
| 1482 |
#ifdef O_NOATIME |
| 1483 |
if (open_noatime) |
| 1484 |
flags |= O_NOATIME; |
| 1485 |
#endif |
| 1486 |
|
| 1487 |
return open(pathname, flags | O_BINARY, mode); |
| 1488 |
} |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
int do_open_at(const char *pathname, int flags, mode_t mode) |
| 1508 |
{ |
| 1509 |
#ifdef AT_FDCWD |
| 1510 |
extern int am_daemon, am_chrooted; |
| 1511 |
char dirpath[MAXPATHLEN]; |
| 1512 |
const char *bname; |
| 1513 |
const char *slash; |
| 1514 |
int dfd, ret, e; |
| 1515 |
size_t dlen; |
| 1516 |
|
| 1517 |
if (flags != O_RDONLY) { |
| 1518 |
RETURN_ERROR_IF(dry_run, 0); |
| 1519 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1520 |
} |
| 1521 |
|
| 1522 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1523 |
if (operator_path_resolve) { |
| 1524 |
if (symlink_optout_allowed()) |
| 1525 |
return do_open(pathname, flags, mode); |
| 1526 |
dfd = owner_walk_parent(pathname, &bname); |
| 1527 |
if (dfd < 0) |
| 1528 |
return -1; |
| 1529 |
ret = openat(dfd, bname, flags | O_NOFOLLOW, mode); |
| 1530 |
e = errno; |
| 1531 |
close(dfd); |
| 1532 |
errno = e; |
| 1533 |
return ret; |
| 1534 |
} |
| 1535 |
#endif |
| 1536 |
|
| 1537 |
if (!secure_relpath_active()) |
| 1538 |
return do_open(pathname, flags, mode); |
| 1539 |
|
| 1540 |
if (!pathname || !*pathname || *pathname == '/') |
| 1541 |
return do_open(pathname, flags, mode); |
| 1542 |
|
| 1543 |
slash = strrchr(pathname, '/'); |
| 1544 |
if (!slash) |
| 1545 |
return do_open(pathname, flags, mode); |
| 1546 |
|
| 1547 |
dlen = slash - pathname; |
| 1548 |
if (dlen >= sizeof dirpath) { |
| 1549 |
errno = ENAMETOOLONG; |
| 1550 |
return -1; |
| 1551 |
} |
| 1552 |
memcpy(dirpath, pathname, dlen); |
| 1553 |
dirpath[dlen] = '\0'; |
| 1554 |
bname = slash + 1; |
| 1555 |
|
| 1556 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1557 |
if (dfd < 0) |
| 1558 |
return -1; |
| 1559 |
|
| 1560 |
#ifdef O_NOATIME |
| 1561 |
if (open_noatime) |
| 1562 |
flags |= O_NOATIME; |
| 1563 |
#endif |
| 1564 |
|
| 1565 |
ret = openat(dfd, bname, flags | O_NOFOLLOW | O_BINARY, mode); |
| 1566 |
e = errno; |
| 1567 |
close(dfd); |
| 1568 |
errno = e; |
| 1569 |
return ret; |
| 1570 |
#else |
| 1571 |
return do_open(pathname, flags, mode); |
| 1572 |
#endif |
| 1573 |
} |
| 1574 |
|
| 1575 |
#ifdef HAVE_CHMOD |
| 1576 |
int do_chmod(const char *path, mode_t mode) |
| 1577 |
{ |
| 1578 |
static int switch_step = 0; |
| 1579 |
int code; |
| 1580 |
|
| 1581 |
if (dry_run) return 0; |
| 1582 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1583 |
RETURN_ERROR_IF_NULL(path); |
| 1584 |
|
| 1585 |
switch (switch_step) { |
| 1586 |
#ifdef HAVE_LCHMOD |
| 1587 |
case 0: |
| 1588 |
if ((code = lchmod(path, mode & CHMOD_BITS)) == 0) |
| 1589 |
break; |
| 1590 |
if (errno == ENOSYS) |
| 1591 |
switch_step++; |
| 1592 |
else if (errno != ENOTSUP) |
| 1593 |
break; |
| 1594 |
#endif |
| 1595 |
|
| 1596 |
default: |
| 1597 |
if (S_ISLNK(mode)) { |
| 1598 |
# if defined HAVE_SETATTRLIST |
| 1599 |
struct attrlist attrList; |
| 1600 |
uint32_t m = mode & CHMOD_BITS; |
| 1601 |
|
| 1602 |
memset(&attrList, 0, sizeof attrList); |
| 1603 |
attrList.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 1604 |
attrList.commonattr = ATTR_CMN_ACCESSMASK; |
| 1605 |
if ((code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW)) == 0) |
| 1606 |
break; |
| 1607 |
if (errno == ENOTSUP) |
| 1608 |
code = 1; |
| 1609 |
# else |
| 1610 |
code = 1; |
| 1611 |
# endif |
| 1612 |
} else |
| 1613 |
code = chmod(path, mode & CHMOD_BITS); |
| 1614 |
break; |
| 1615 |
} |
| 1616 |
if (code != 0 && (preserve_perms || preserve_executability)) |
| 1617 |
return code; |
| 1618 |
return 0; |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
static int do_fchmodat_nofollow(int dfd, const char *name, mode_t mode) |
| 1643 |
{ |
| 1644 |
#if defined AT_FDCWD && defined AT_SYMLINK_NOFOLLOW |
| 1645 |
mode &= CHMOD_BITS; |
| 1646 |
# ifdef O_NOFOLLOW |
| 1647 |
{ |
| 1648 |
STRUCT_STAT st; |
| 1649 |
int oflags = O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_NOCTTY; |
| 1650 |
if (do_lstat_atfd(dfd, name, &st) < 0) |
| 1651 |
return -1; |
| 1652 |
if (S_ISLNK(st.st_mode)) { |
| 1653 |
errno = ELOOP; |
| 1654 |
return -1; |
| 1655 |
} |
| 1656 |
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISFIFO(st.st_mode)) { |
| 1657 |
int fd; |
| 1658 |
# ifndef __linux__ |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
if (S_ISFIFO(st.st_mode)) { |
| 1686 |
if (fchmodat(dfd, name, mode, AT_SYMLINK_NOFOLLOW) == 0) |
| 1687 |
return 0; |
| 1688 |
if (errno == EPERM && (mode & S_ISGID) |
| 1689 |
&& fchmodat(dfd, name, mode & ~S_ISGID, |
| 1690 |
AT_SYMLINK_NOFOLLOW) == 0) |
| 1691 |
return 0; |
| 1692 |
return -1; |
| 1693 |
} |
| 1694 |
# endif |
| 1695 |
# ifdef O_CLOEXEC |
| 1696 |
oflags |= O_CLOEXEC; |
| 1697 |
# endif |
| 1698 |
fd = openat(dfd, name, oflags); |
| 1699 |
if (fd >= 0) { |
| 1700 |
int r = fchmod(fd, mode), e = errno; |
| 1701 |
close(fd); |
| 1702 |
errno = e; |
| 1703 |
return r; |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
if (errno == ELOOP |
| 1710 |
# ifdef EMLINK |
| 1711 |
|| errno == EMLINK |
| 1712 |
# endif |
| 1713 |
# ifdef EFTYPE |
| 1714 |
|| errno == EFTYPE |
| 1715 |
# endif |
| 1716 |
) |
| 1717 |
return -1; |
| 1718 |
|
| 1719 |
} |
| 1720 |
} |
| 1721 |
# endif |
| 1722 |
# if defined __linux__ |
| 1723 |
{ |
| 1724 |
int r = fchmodat(dfd, name, mode, AT_SYMLINK_NOFOLLOW); |
| 1725 |
if (r == 0) |
| 1726 |
return 0; |
| 1727 |
if (errno != ENOTSUP && errno != EOPNOTSUPP && errno != ENOSYS) |
| 1728 |
return r; |
| 1729 |
} |
| 1730 |
# ifdef SYS_fchmodat2 |
| 1731 |
{ |
| 1732 |
int r = syscall(SYS_fchmodat2, dfd, name, (unsigned int)mode, AT_SYMLINK_NOFOLLOW); |
| 1733 |
if (r == 0) |
| 1734 |
return 0; |
| 1735 |
if (errno != ENOSYS && errno != EPERM && errno != EOPNOTSUPP) |
| 1736 |
return r; |
| 1737 |
} |
| 1738 |
# endif |
| 1739 |
|
| 1740 |
rprintf(FWARNING, "do_chmod: no symlink-safe chmod for \"%s\"; mode not set\n", name); |
| 1741 |
return 1; |
| 1742 |
# else |
| 1743 |
return fchmodat(dfd, name, mode, AT_SYMLINK_NOFOLLOW); |
| 1744 |
# endif |
| 1745 |
#else |
| 1746 |
(void)dfd; |
| 1747 |
(void)mode; |
| 1748 |
|
| 1749 |
rprintf(FWARNING, "do_chmod: no symlink-safe chmod for \"%s\"; mode not set\n", name); |
| 1750 |
return 1; |
| 1751 |
#endif |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
int do_chmod_at(const char *fname, mode_t mode) |
| 1781 |
{ |
| 1782 |
#ifdef AT_FDCWD |
| 1783 |
extern int am_daemon, am_chrooted; |
| 1784 |
char dirpath[MAXPATHLEN]; |
| 1785 |
const char *bname; |
| 1786 |
const char *slash; |
| 1787 |
int dfd, ret, e; |
| 1788 |
size_t dlen; |
| 1789 |
|
| 1790 |
if (dry_run) return 0; |
| 1791 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1792 |
|
| 1793 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
if (operator_path_resolve && fname && *fname && !S_ISLNK(mode)) { |
| 1800 |
if (symlink_optout_allowed()) |
| 1801 |
return do_chmod(fname, mode); |
| 1802 |
dfd = owner_walk_parent(fname, &bname); |
| 1803 |
if (dfd < 0) |
| 1804 |
return -1; |
| 1805 |
ret = do_fchmodat_nofollow(dfd, bname, mode); |
| 1806 |
e = errno; |
| 1807 |
close(dfd); |
| 1808 |
errno = e; |
| 1809 |
return ret; |
| 1810 |
} |
| 1811 |
#endif |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
if (!secure_relpath_active()) |
| 1820 |
return do_chmod(fname, mode); |
| 1821 |
|
| 1822 |
if (!fname || !*fname || *fname == '/' || S_ISLNK(mode)) |
| 1823 |
return do_chmod(fname, mode); |
| 1824 |
|
| 1825 |
slash = strrchr(fname, '/'); |
| 1826 |
if (!slash) |
| 1827 |
return do_chmod(fname, mode); |
| 1828 |
|
| 1829 |
dlen = slash - fname; |
| 1830 |
if (dlen >= sizeof dirpath) { |
| 1831 |
errno = ENAMETOOLONG; |
| 1832 |
return -1; |
| 1833 |
} |
| 1834 |
memcpy(dirpath, fname, dlen); |
| 1835 |
dirpath[dlen] = '\0'; |
| 1836 |
bname = slash + 1; |
| 1837 |
|
| 1838 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1839 |
if (dfd < 0) |
| 1840 |
return -1; |
| 1841 |
|
| 1842 |
ret = do_fchmodat_nofollow(dfd, bname, mode); |
| 1843 |
e = errno; |
| 1844 |
close(dfd); |
| 1845 |
errno = e; |
| 1846 |
return ret; |
| 1847 |
#else |
| 1848 |
return do_chmod(fname, mode); |
| 1849 |
#endif |
| 1850 |
} |
| 1851 |
#endif |
| 1852 |
|
| 1853 |
int do_rename(const char *old_path, const char *new_path) |
| 1854 |
{ |
| 1855 |
if (dry_run) return 0; |
| 1856 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1857 |
return rename(old_path, new_path); |
| 1858 |
} |
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
int do_rename_at(const char *old_path, const char *new_path) |
| 1877 |
{ |
| 1878 |
#ifdef AT_FDCWD |
| 1879 |
extern int am_daemon, am_chrooted; |
| 1880 |
char old_dirpath[MAXPATHLEN], new_dirpath[MAXPATHLEN]; |
| 1881 |
const char *old_bname, *new_bname; |
| 1882 |
const char *old_slash, *new_slash; |
| 1883 |
int old_dfd = AT_FDCWD, new_dfd = AT_FDCWD; |
| 1884 |
BOOL old_owns = False, new_owns = False; |
| 1885 |
int ret = -1, e; |
| 1886 |
size_t old_dlen = 0, new_dlen = 0; |
| 1887 |
|
| 1888 |
if (dry_run) return 0; |
| 1889 |
RETURN_ERROR_IF_RO_OR_LO; |
| 1890 |
|
| 1891 |
if (!secure_relpath_active()) |
| 1892 |
return do_rename(old_path, new_path); |
| 1893 |
|
| 1894 |
if (!old_path || !*old_path || !new_path || !*new_path) |
| 1895 |
return do_rename(old_path, new_path); |
| 1896 |
|
| 1897 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
if (operator_path_resolve) { |
| 1902 |
if (symlink_optout_allowed()) |
| 1903 |
return do_rename(old_path, new_path); |
| 1904 |
old_dfd = owner_walk_parent(old_path, &old_bname); |
| 1905 |
if (old_dfd < 0) |
| 1906 |
return -1; |
| 1907 |
new_dfd = owner_walk_parent(new_path, &new_bname); |
| 1908 |
if (new_dfd < 0) { |
| 1909 |
e = errno; |
| 1910 |
close(old_dfd); |
| 1911 |
errno = e; |
| 1912 |
return -1; |
| 1913 |
} |
| 1914 |
ret = renameat(old_dfd, old_bname, new_dfd, new_bname); |
| 1915 |
e = errno; |
| 1916 |
close(new_dfd); |
| 1917 |
close(old_dfd); |
| 1918 |
errno = e; |
| 1919 |
return ret; |
| 1920 |
} |
| 1921 |
#endif |
| 1922 |
|
| 1923 |
old_slash = strrchr(old_path, '/'); |
| 1924 |
new_slash = strrchr(new_path, '/'); |
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
if (*old_path == '/') { |
| 1935 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1936 |
if (!symlink_optout_allowed()) { |
| 1937 |
operator_path_resolve = 1; |
| 1938 |
old_dfd = owner_walk_parent(old_path, &old_bname); |
| 1939 |
operator_path_resolve = 0; |
| 1940 |
if (old_dfd < 0) |
| 1941 |
return -1; |
| 1942 |
old_owns = True; |
| 1943 |
} else |
| 1944 |
#endif |
| 1945 |
old_bname = old_path; |
| 1946 |
} else if (old_slash) { |
| 1947 |
old_dlen = old_slash - old_path; |
| 1948 |
if (old_dlen >= sizeof old_dirpath) { |
| 1949 |
errno = ENAMETOOLONG; |
| 1950 |
return -1; |
| 1951 |
} |
| 1952 |
memcpy(old_dirpath, old_path, old_dlen); |
| 1953 |
old_dirpath[old_dlen] = '\0'; |
| 1954 |
old_bname = old_slash + 1; |
| 1955 |
old_dfd = secure_relative_open(NULL, old_dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1956 |
if (old_dfd < 0) |
| 1957 |
return -1; |
| 1958 |
old_owns = True; |
| 1959 |
} else { |
| 1960 |
old_bname = old_path; |
| 1961 |
} |
| 1962 |
|
| 1963 |
if (*new_path == '/') { |
| 1964 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 1965 |
if (!symlink_optout_allowed()) { |
| 1966 |
operator_path_resolve = 1; |
| 1967 |
new_dfd = owner_walk_parent(new_path, &new_bname); |
| 1968 |
operator_path_resolve = 0; |
| 1969 |
if (new_dfd < 0) { |
| 1970 |
e = errno; |
| 1971 |
if (old_owns) close(old_dfd); |
| 1972 |
errno = e; |
| 1973 |
return -1; |
| 1974 |
} |
| 1975 |
new_owns = True; |
| 1976 |
} else |
| 1977 |
#endif |
| 1978 |
new_bname = new_path; |
| 1979 |
} else if (new_slash) { |
| 1980 |
new_dlen = new_slash - new_path; |
| 1981 |
if (new_dlen >= sizeof new_dirpath) { |
| 1982 |
e = ENAMETOOLONG; |
| 1983 |
if (old_owns) close(old_dfd); |
| 1984 |
errno = e; |
| 1985 |
return -1; |
| 1986 |
} |
| 1987 |
memcpy(new_dirpath, new_path, new_dlen); |
| 1988 |
new_dirpath[new_dlen] = '\0'; |
| 1989 |
new_bname = new_slash + 1; |
| 1990 |
if (old_owns && old_dlen == new_dlen |
| 1991 |
&& memcmp(old_dirpath, new_dirpath, old_dlen) == 0) { |
| 1992 |
new_dfd = old_dfd; |
| 1993 |
} else { |
| 1994 |
new_dfd = secure_relative_open(NULL, new_dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 1995 |
if (new_dfd < 0) { |
| 1996 |
e = errno; |
| 1997 |
if (old_owns) close(old_dfd); |
| 1998 |
errno = e; |
| 1999 |
return -1; |
| 2000 |
} |
| 2001 |
new_owns = True; |
| 2002 |
} |
| 2003 |
} else { |
| 2004 |
new_bname = new_path; |
| 2005 |
} |
| 2006 |
|
| 2007 |
ret = renameat(old_dfd, old_bname, new_dfd, new_bname); |
| 2008 |
e = errno; |
| 2009 |
if (new_owns) |
| 2010 |
close(new_dfd); |
| 2011 |
if (old_owns) |
| 2012 |
close(old_dfd); |
| 2013 |
errno = e; |
| 2014 |
return ret; |
| 2015 |
#else |
| 2016 |
return do_rename(old_path, new_path); |
| 2017 |
#endif |
| 2018 |
} |
| 2019 |
|
| 2020 |
#ifdef HAVE_FTRUNCATE |
| 2021 |
int do_ftruncate(int fd, OFF_T size) |
| 2022 |
{ |
| 2023 |
int ret; |
| 2024 |
|
| 2025 |
if (dry_run) return 0; |
| 2026 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2027 |
|
| 2028 |
do { |
| 2029 |
ret = ftruncate(fd, size); |
| 2030 |
} while (ret < 0 && errno == EINTR); |
| 2031 |
|
| 2032 |
return ret; |
| 2033 |
} |
| 2034 |
#endif |
| 2035 |
|
| 2036 |
void trim_trailing_slashes(char *name) |
| 2037 |
{ |
| 2038 |
int l; |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
l = strlen(name); |
| 2047 |
while (l > 1) { |
| 2048 |
if (name[--l] != '/') |
| 2049 |
break; |
| 2050 |
name[l] = '\0'; |
| 2051 |
} |
| 2052 |
} |
| 2053 |
|
| 2054 |
int do_mkdir(char *path, mode_t mode) |
| 2055 |
{ |
| 2056 |
if (dry_run) return 0; |
| 2057 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2058 |
RETURN_ERROR_IF_NULL(path); |
| 2059 |
trim_trailing_slashes(path); |
| 2060 |
return mkdir(path, mode); |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
int do_mkdir_at(char *path, mode_t mode) |
| 2077 |
{ |
| 2078 |
#ifdef AT_FDCWD |
| 2079 |
extern int am_daemon, am_chrooted; |
| 2080 |
char dirpath[MAXPATHLEN]; |
| 2081 |
const char *bname; |
| 2082 |
const char *slash; |
| 2083 |
int dfd, ret, e; |
| 2084 |
size_t dlen; |
| 2085 |
|
| 2086 |
if (dry_run) return 0; |
| 2087 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2088 |
RETURN_ERROR_IF_NULL(path); |
| 2089 |
trim_trailing_slashes(path); |
| 2090 |
|
| 2091 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 2092 |
if (operator_path_resolve) { |
| 2093 |
if (symlink_optout_allowed()) |
| 2094 |
return mkdir(path, mode); |
| 2095 |
dfd = owner_walk_parent(path, &bname); |
| 2096 |
if (dfd < 0) |
| 2097 |
return -1; |
| 2098 |
ret = mkdirat(dfd, bname, mode); |
| 2099 |
e = errno; |
| 2100 |
close(dfd); |
| 2101 |
errno = e; |
| 2102 |
return ret; |
| 2103 |
} |
| 2104 |
#endif |
| 2105 |
|
| 2106 |
if (!secure_relpath_active()) |
| 2107 |
return mkdir(path, mode); |
| 2108 |
|
| 2109 |
if (!path || !*path || *path == '/') |
| 2110 |
return mkdir(path, mode); |
| 2111 |
|
| 2112 |
slash = strrchr(path, '/'); |
| 2113 |
if (!slash) |
| 2114 |
return mkdir(path, mode); |
| 2115 |
|
| 2116 |
dlen = slash - path; |
| 2117 |
if (dlen >= sizeof dirpath) { |
| 2118 |
errno = ENAMETOOLONG; |
| 2119 |
return -1; |
| 2120 |
} |
| 2121 |
memcpy(dirpath, path, dlen); |
| 2122 |
dirpath[dlen] = '\0'; |
| 2123 |
bname = slash + 1; |
| 2124 |
|
| 2125 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 2126 |
if (dfd < 0) |
| 2127 |
return -1; |
| 2128 |
|
| 2129 |
ret = mkdirat(dfd, bname, mode); |
| 2130 |
e = errno; |
| 2131 |
close(dfd); |
| 2132 |
errno = e; |
| 2133 |
return ret; |
| 2134 |
#else |
| 2135 |
return do_mkdir(path, mode); |
| 2136 |
#endif |
| 2137 |
} |
| 2138 |
|
| 2139 |
|
| 2140 |
int do_mkstemp(char *template, mode_t perms) |
| 2141 |
{ |
| 2142 |
RETURN_ERROR_IF(dry_run, 0); |
| 2143 |
RETURN_ERROR_IF(read_only, EROFS); |
| 2144 |
perms |= S_IWUSR; |
| 2145 |
|
| 2146 |
#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64) |
| 2147 |
{ |
| 2148 |
int fd = mkstemp(template); |
| 2149 |
if (fd == -1) |
| 2150 |
return -1; |
| 2151 |
if (fchmod(fd, perms) != 0 && preserve_perms) { |
| 2152 |
int errno_save = errno; |
| 2153 |
close(fd); |
| 2154 |
unlink(template); |
| 2155 |
errno = errno_save; |
| 2156 |
return -1; |
| 2157 |
} |
| 2158 |
#if defined HAVE_SETMODE && O_BINARY |
| 2159 |
setmode(fd, O_BINARY); |
| 2160 |
#endif |
| 2161 |
return fd; |
| 2162 |
} |
| 2163 |
#else |
| 2164 |
if (!mktemp(template)) |
| 2165 |
return -1; |
| 2166 |
return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms); |
| 2167 |
#endif |
| 2168 |
} |
| 2169 |
|
| 2170 |
int do_stat(const char *path, STRUCT_STAT *st) |
| 2171 |
{ |
| 2172 |
RETURN_ERROR_IF_NULL(path); |
| 2173 |
#ifdef USE_STAT64_FUNCS |
| 2174 |
return stat64(path, st); |
| 2175 |
#else |
| 2176 |
return stat(path, st); |
| 2177 |
#endif |
| 2178 |
} |
| 2179 |
|
| 2180 |
int do_lstat(const char *path, STRUCT_STAT *st) |
| 2181 |
{ |
| 2182 |
RETURN_ERROR_IF_NULL(path); |
| 2183 |
#ifdef SUPPORT_LINKS |
| 2184 |
# ifdef USE_STAT64_FUNCS |
| 2185 |
return lstat64(path, st); |
| 2186 |
# else |
| 2187 |
return lstat(path, st); |
| 2188 |
# endif |
| 2189 |
#else |
| 2190 |
return do_stat(path, st); |
| 2191 |
#endif |
| 2192 |
} |
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
static int do_xstat_at(const char *path, STRUCT_STAT *st, int at_flags, int (*fallback)(const char *, STRUCT_STAT *)) |
| 2208 |
{ |
| 2209 |
#ifdef AT_FDCWD |
| 2210 |
extern int am_daemon, am_chrooted; |
| 2211 |
char dirpath[MAXPATHLEN]; |
| 2212 |
const char *bname; |
| 2213 |
const char *slash; |
| 2214 |
int dfd, ret, e; |
| 2215 |
size_t dlen; |
| 2216 |
|
| 2217 |
#if defined O_NOFOLLOW && defined O_DIRECTORY |
| 2218 |
if (operator_path_resolve) { |
| 2219 |
if (symlink_optout_allowed()) |
| 2220 |
return fallback(path, st); |
| 2221 |
dfd = owner_walk_parent(path, &bname); |
| 2222 |
if (dfd < 0) |
| 2223 |
return -1; |
| 2224 |
ret = fstatat(dfd, bname, st, at_flags); |
| 2225 |
e = errno; |
| 2226 |
close(dfd); |
| 2227 |
errno = e; |
| 2228 |
return ret; |
| 2229 |
} |
| 2230 |
#endif |
| 2231 |
|
| 2232 |
if (!secure_relpath_active()) |
| 2233 |
return fallback(path, st); |
| 2234 |
|
| 2235 |
if (!path || !*path || *path == '/') |
| 2236 |
return fallback(path, st); |
| 2237 |
|
| 2238 |
slash = strrchr(path, '/'); |
| 2239 |
if (!slash) |
| 2240 |
return fallback(path, st); |
| 2241 |
|
| 2242 |
dlen = slash - path; |
| 2243 |
if (dlen >= sizeof dirpath) { |
| 2244 |
errno = ENAMETOOLONG; |
| 2245 |
return -1; |
| 2246 |
} |
| 2247 |
memcpy(dirpath, path, dlen); |
| 2248 |
dirpath[dlen] = '\0'; |
| 2249 |
bname = slash + 1; |
| 2250 |
|
| 2251 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 2252 |
if (dfd < 0) |
| 2253 |
return -1; |
| 2254 |
|
| 2255 |
ret = fstatat(dfd, bname, st, at_flags); |
| 2256 |
e = errno; |
| 2257 |
close(dfd); |
| 2258 |
errno = e; |
| 2259 |
return ret; |
| 2260 |
#else |
| 2261 |
(void)at_flags; |
| 2262 |
return fallback(path, st); |
| 2263 |
#endif |
| 2264 |
} |
| 2265 |
|
| 2266 |
int do_stat_at(const char *path, STRUCT_STAT *st) |
| 2267 |
{ |
| 2268 |
return do_xstat_at(path, st, 0, do_stat); |
| 2269 |
} |
| 2270 |
|
| 2271 |
int do_lstat_at(const char *path, STRUCT_STAT *st) |
| 2272 |
{ |
| 2273 |
#if defined SUPPORT_LINKS && defined AT_FDCWD && defined AT_SYMLINK_NOFOLLOW |
| 2274 |
return do_xstat_at(path, st, AT_SYMLINK_NOFOLLOW, do_lstat); |
| 2275 |
#elif defined SUPPORT_LINKS |
| 2276 |
return do_lstat(path, st); |
| 2277 |
#else |
| 2278 |
return do_xstat_at(path, st, 0, do_stat); |
| 2279 |
#endif |
| 2280 |
} |
| 2281 |
|
| 2282 |
int do_fstat(int fd, STRUCT_STAT *st) |
| 2283 |
{ |
| 2284 |
#ifdef USE_STAT64_FUNCS |
| 2285 |
return fstat64(fd, st); |
| 2286 |
#else |
| 2287 |
return fstat(fd, st); |
| 2288 |
#endif |
| 2289 |
} |
| 2290 |
|
| 2291 |
OFF_T do_lseek(int fd, OFF_T offset, int whence) |
| 2292 |
{ |
| 2293 |
#ifdef HAVE_LSEEK64 |
| 2294 |
return lseek64(fd, offset, whence); |
| 2295 |
#else |
| 2296 |
return lseek(fd, offset, whence); |
| 2297 |
#endif |
| 2298 |
} |
| 2299 |
|
| 2300 |
#ifdef HAVE_SETATTRLIST |
| 2301 |
int do_setattrlist_times(const char *path, STRUCT_STAT *stp) |
| 2302 |
{ |
| 2303 |
struct attrlist attrList; |
| 2304 |
struct timespec ts[2]; |
| 2305 |
|
| 2306 |
if (dry_run) return 0; |
| 2307 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
if (secure_relpath_active()) { |
| 2318 |
errno = ENOSYS; |
| 2319 |
return -1; |
| 2320 |
} |
| 2321 |
|
| 2322 |
|
| 2323 |
ts[0].tv_sec = stp->st_mtime; |
| 2324 |
ts[0].tv_nsec = stp->ST_MTIME_NSEC; |
| 2325 |
|
| 2326 |
ts[1].tv_sec = stp->st_atime; |
| 2327 |
ts[1].tv_nsec = stp->ST_ATIME_NSEC; |
| 2328 |
|
| 2329 |
memset(&attrList, 0, sizeof attrList); |
| 2330 |
attrList.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 2331 |
attrList.commonattr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME; |
| 2332 |
return setattrlist(path, &attrList, ts, sizeof ts, FSOPT_NOFOLLOW); |
| 2333 |
} |
| 2334 |
|
| 2335 |
#ifdef SUPPORT_CRTIMES |
| 2336 |
int do_setattrlist_crtime(const char *path, time_t crtime) |
| 2337 |
{ |
| 2338 |
struct attrlist attrList; |
| 2339 |
struct timespec ts; |
| 2340 |
|
| 2341 |
if (dry_run) return 0; |
| 2342 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2343 |
|
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
|
| 2352 |
ts.tv_sec = crtime; |
| 2353 |
ts.tv_nsec = 0; |
| 2354 |
|
| 2355 |
memset(&attrList, 0, sizeof attrList); |
| 2356 |
attrList.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 2357 |
attrList.commonattr = ATTR_CMN_CRTIME; |
| 2358 |
return setattrlist(path, &attrList, &ts, sizeof ts, FSOPT_NOFOLLOW); |
| 2359 |
} |
| 2360 |
#endif |
| 2361 |
#endif |
| 2362 |
|
| 2363 |
#ifdef SUPPORT_CRTIMES |
| 2364 |
time_t get_create_time(const char *path, STRUCT_STAT *stp) |
| 2365 |
{ |
| 2366 |
#ifdef HAVE_GETATTRLIST |
| 2367 |
static struct create_time attrBuf; |
| 2368 |
struct attrlist attrList; |
| 2369 |
|
| 2370 |
(void)stp; |
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
memset(&attrList, 0, sizeof attrList); |
| 2376 |
attrList.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 2377 |
attrList.commonattr = ATTR_CMN_CRTIME; |
| 2378 |
if (getattrlist(path, &attrList, &attrBuf, sizeof attrBuf, FSOPT_NOFOLLOW) < 0) |
| 2379 |
return 0; |
| 2380 |
return attrBuf.crtime.tv_sec; |
| 2381 |
#elif defined __CYGWIN__ |
| 2382 |
(void)path; |
| 2383 |
return stp->st_birthtime; |
| 2384 |
#else |
| 2385 |
#error Unknown crtimes implementation |
| 2386 |
#endif |
| 2387 |
} |
| 2388 |
|
| 2389 |
#if defined __CYGWIN__ |
| 2390 |
int do_SetFileTime(const char *path, time_t crtime) |
| 2391 |
{ |
| 2392 |
if (dry_run) return 0; |
| 2393 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2394 |
|
| 2395 |
int cnt = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0); |
| 2396 |
if (cnt == 0) |
| 2397 |
return -1; |
| 2398 |
WCHAR *pathw = new_array(WCHAR, cnt); |
| 2399 |
if (!pathw) |
| 2400 |
return -1; |
| 2401 |
MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, cnt); |
| 2402 |
HANDLE handle = CreateFileW(pathw, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 2403 |
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 2404 |
free(pathw); |
| 2405 |
if (handle == INVALID_HANDLE_VALUE) |
| 2406 |
return -1; |
| 2407 |
int64 temp_time = (crtime * 10000000LL) + 116444736000000000LL; |
| 2408 |
FILETIME birth_time; |
| 2409 |
birth_time.dwLowDateTime = (DWORD)temp_time; |
| 2410 |
birth_time.dwHighDateTime = (DWORD)(temp_time >> 32); |
| 2411 |
int ok = SetFileTime(handle, &birth_time, NULL, NULL); |
| 2412 |
CloseHandle(handle); |
| 2413 |
return ok ? 0 : -1; |
| 2414 |
} |
| 2415 |
#endif |
| 2416 |
#endif |
| 2417 |
|
| 2418 |
#ifdef HAVE_UTIMENSAT |
| 2419 |
int do_utimensat(const char *path, STRUCT_STAT *stp) |
| 2420 |
{ |
| 2421 |
struct timespec t[2]; |
| 2422 |
|
| 2423 |
if (dry_run) return 0; |
| 2424 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2425 |
RETURN_ERROR_IF_NULL(path); |
| 2426 |
|
| 2427 |
t[0].tv_sec = stp->st_atime; |
| 2428 |
#ifdef ST_ATIME_NSEC |
| 2429 |
t[0].tv_nsec = stp->ST_ATIME_NSEC; |
| 2430 |
#else |
| 2431 |
t[0].tv_nsec = 0; |
| 2432 |
#endif |
| 2433 |
t[1].tv_sec = stp->st_mtime; |
| 2434 |
#ifdef ST_MTIME_NSEC |
| 2435 |
t[1].tv_nsec = stp->ST_MTIME_NSEC; |
| 2436 |
#else |
| 2437 |
t[1].tv_nsec = 0; |
| 2438 |
#endif |
| 2439 |
return utimensat(AT_FDCWD, path, t, AT_SYMLINK_NOFOLLOW); |
| 2440 |
} |
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
int do_utimensat_at(const char *path, STRUCT_STAT *stp) |
| 2458 |
{ |
| 2459 |
#ifdef AT_FDCWD |
| 2460 |
extern int am_daemon, am_chrooted; |
| 2461 |
struct timespec t[2]; |
| 2462 |
char dirpath[MAXPATHLEN]; |
| 2463 |
const char *bname; |
| 2464 |
const char *slash; |
| 2465 |
int dfd, ret, e; |
| 2466 |
size_t dlen; |
| 2467 |
|
| 2468 |
if (dry_run) return 0; |
| 2469 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2470 |
|
| 2471 |
if (!secure_relpath_active()) |
| 2472 |
return do_utimensat(path, stp); |
| 2473 |
|
| 2474 |
if (!path || !*path || *path == '/') |
| 2475 |
return do_utimensat(path, stp); |
| 2476 |
|
| 2477 |
slash = strrchr(path, '/'); |
| 2478 |
if (!slash) |
| 2479 |
return do_utimensat(path, stp); |
| 2480 |
|
| 2481 |
dlen = slash - path; |
| 2482 |
if (dlen >= sizeof dirpath) { |
| 2483 |
errno = ENAMETOOLONG; |
| 2484 |
return -1; |
| 2485 |
} |
| 2486 |
memcpy(dirpath, path, dlen); |
| 2487 |
dirpath[dlen] = '\0'; |
| 2488 |
bname = slash + 1; |
| 2489 |
|
| 2490 |
t[0].tv_sec = stp->st_atime; |
| 2491 |
#ifdef ST_ATIME_NSEC |
| 2492 |
t[0].tv_nsec = stp->ST_ATIME_NSEC; |
| 2493 |
#else |
| 2494 |
t[0].tv_nsec = 0; |
| 2495 |
#endif |
| 2496 |
t[1].tv_sec = stp->st_mtime; |
| 2497 |
#ifdef ST_MTIME_NSEC |
| 2498 |
t[1].tv_nsec = stp->ST_MTIME_NSEC; |
| 2499 |
#else |
| 2500 |
t[1].tv_nsec = 0; |
| 2501 |
#endif |
| 2502 |
|
| 2503 |
dfd = secure_relative_open(NULL, dirpath, O_RDONLY | O_DIRECTORY, 0); |
| 2504 |
if (dfd < 0) |
| 2505 |
return -1; |
| 2506 |
|
| 2507 |
ret = utimensat(dfd, bname, t, AT_SYMLINK_NOFOLLOW); |
| 2508 |
e = errno; |
| 2509 |
close(dfd); |
| 2510 |
errno = e; |
| 2511 |
return ret; |
| 2512 |
#else |
| 2513 |
return do_utimensat(path, stp); |
| 2514 |
#endif |
| 2515 |
} |
| 2516 |
#endif |
| 2517 |
|
| 2518 |
#ifdef HAVE_LUTIMES |
| 2519 |
int do_lutimes(const char *path, STRUCT_STAT *stp) |
| 2520 |
{ |
| 2521 |
struct timeval t[2]; |
| 2522 |
|
| 2523 |
if (dry_run) return 0; |
| 2524 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2525 |
|
| 2526 |
t[0].tv_sec = stp->st_atime; |
| 2527 |
#ifdef ST_ATIME_NSEC |
| 2528 |
t[0].tv_usec = stp->ST_ATIME_NSEC / 1000; |
| 2529 |
#else |
| 2530 |
t[0].tv_usec = 0; |
| 2531 |
#endif |
| 2532 |
t[1].tv_sec = stp->st_mtime; |
| 2533 |
#ifdef ST_MTIME_NSEC |
| 2534 |
t[1].tv_usec = stp->ST_MTIME_NSEC / 1000; |
| 2535 |
#else |
| 2536 |
t[1].tv_usec = 0; |
| 2537 |
#endif |
| 2538 |
return lutimes(path, t); |
| 2539 |
} |
| 2540 |
#endif |
| 2541 |
|
| 2542 |
#ifdef HAVE_UTIMES |
| 2543 |
int do_utimes(const char *path, STRUCT_STAT *stp) |
| 2544 |
{ |
| 2545 |
struct timeval t[2]; |
| 2546 |
|
| 2547 |
if (dry_run) return 0; |
| 2548 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2549 |
|
| 2550 |
t[0].tv_sec = stp->st_atime; |
| 2551 |
#ifdef ST_ATIME_NSEC |
| 2552 |
t[0].tv_usec = stp->ST_ATIME_NSEC / 1000; |
| 2553 |
#else |
| 2554 |
t[0].tv_usec = 0; |
| 2555 |
#endif |
| 2556 |
t[1].tv_sec = stp->st_mtime; |
| 2557 |
#ifdef ST_MTIME_NSEC |
| 2558 |
t[1].tv_usec = stp->ST_MTIME_NSEC / 1000; |
| 2559 |
#else |
| 2560 |
t[1].tv_usec = 0; |
| 2561 |
#endif |
| 2562 |
return utimes(path, t); |
| 2563 |
} |
| 2564 |
|
| 2565 |
#elif defined HAVE_UTIME |
| 2566 |
int do_utime(const char *path, STRUCT_STAT *stp) |
| 2567 |
{ |
| 2568 |
#ifdef HAVE_STRUCT_UTIMBUF |
| 2569 |
struct utimbuf tbuf; |
| 2570 |
#else |
| 2571 |
time_t t[2]; |
| 2572 |
#endif |
| 2573 |
|
| 2574 |
if (dry_run) return 0; |
| 2575 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2576 |
|
| 2577 |
# ifdef HAVE_STRUCT_UTIMBUF |
| 2578 |
tbuf.actime = stp->st_atime; |
| 2579 |
tbuf.modtime = stp->st_mtime; |
| 2580 |
return utime(path, &tbuf); |
| 2581 |
# else |
| 2582 |
t[0] = stp->st_atime; |
| 2583 |
t[1] = stp->st_mtime; |
| 2584 |
return utime(path, t); |
| 2585 |
# endif |
| 2586 |
} |
| 2587 |
|
| 2588 |
#else |
| 2589 |
#error Need utimes or utime function. |
| 2590 |
#endif |
| 2591 |
|
| 2592 |
#ifdef SUPPORT_PREALLOCATION |
| 2593 |
#ifdef FALLOC_FL_KEEP_SIZE |
| 2594 |
#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE |
| 2595 |
#else |
| 2596 |
#define DO_FALLOC_OPTIONS 0 |
| 2597 |
#endif |
| 2598 |
|
| 2599 |
OFF_T do_fallocate(int fd, OFF_T offset, OFF_T length) |
| 2600 |
{ |
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
int opts = (inplace || preallocate_files) && sparse_files <= 0 ? DO_FALLOC_OPTIONS : 0; |
| 2608 |
int ret; |
| 2609 |
RETURN_ERROR_IF(dry_run, 0); |
| 2610 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2611 |
if (length & 1) |
| 2612 |
length++; |
| 2613 |
else |
| 2614 |
length--; |
| 2615 |
#if defined HAVE_FALLOCATE |
| 2616 |
ret = fallocate(fd, opts, offset, length); |
| 2617 |
#elif defined HAVE_SYS_FALLOCATE |
| 2618 |
ret = syscall(SYS_fallocate, fd, opts, (loff_t)offset, (loff_t)length); |
| 2619 |
#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE |
| 2620 |
ret = posix_fallocate(fd, offset, length); |
| 2621 |
#else |
| 2622 |
#error Coding error in SUPPORT_PREALLOCATION logic. |
| 2623 |
#endif |
| 2624 |
if (ret < 0) |
| 2625 |
return ret; |
| 2626 |
if (opts == 0) { |
| 2627 |
STRUCT_STAT st; |
| 2628 |
if (do_fstat(fd, &st) < 0) |
| 2629 |
return length; |
| 2630 |
return st.st_blocks * S_BLKSIZE; |
| 2631 |
} |
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
return length; |
| 2640 |
} |
| 2641 |
#endif |
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
int do_punch_hole(int fd, OFF_T pos, OFF_T len) |
| 2646 |
{ |
| 2647 |
#ifdef HAVE_FALLOCATE |
| 2648 |
# ifdef HAVE_FALLOC_FL_PUNCH_HOLE |
| 2649 |
if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos, len) == 0) { |
| 2650 |
if (do_lseek(fd, len, SEEK_CUR) != pos + len) |
| 2651 |
return -1; |
| 2652 |
return 0; |
| 2653 |
} |
| 2654 |
# endif |
| 2655 |
# ifdef HAVE_FALLOC_FL_ZERO_RANGE |
| 2656 |
if (fallocate(fd, FALLOC_FL_ZERO_RANGE, pos, len) == 0) { |
| 2657 |
if (do_lseek(fd, len, SEEK_CUR) != pos + len) |
| 2658 |
return -1; |
| 2659 |
return 0; |
| 2660 |
} |
| 2661 |
# endif |
| 2662 |
#else |
| 2663 |
(void)pos; |
| 2664 |
#endif |
| 2665 |
{ |
| 2666 |
char zeros[4096]; |
| 2667 |
memset(zeros, 0, sizeof zeros); |
| 2668 |
while (len > 0) { |
| 2669 |
int chunk = len > (int)sizeof zeros ? (int)sizeof zeros : len; |
| 2670 |
int wrote = write(fd, zeros, chunk); |
| 2671 |
if (wrote <= 0) { |
| 2672 |
if (wrote < 0 && errno == EINTR) |
| 2673 |
continue; |
| 2674 |
return -1; |
| 2675 |
} |
| 2676 |
len -= wrote; |
| 2677 |
} |
| 2678 |
} |
| 2679 |
return 0; |
| 2680 |
} |
| 2681 |
|
| 2682 |
int do_open_nofollow(const char *pathname, int flags) |
| 2683 |
{ |
| 2684 |
#ifndef O_NOFOLLOW |
| 2685 |
STRUCT_STAT f_st, l_st; |
| 2686 |
#endif |
| 2687 |
int fd; |
| 2688 |
|
| 2689 |
if (flags != O_RDONLY) { |
| 2690 |
RETURN_ERROR_IF(dry_run, 0); |
| 2691 |
RETURN_ERROR_IF_RO_OR_LO; |
| 2692 |
#ifndef O_NOFOLLOW |
| 2693 |
|
| 2694 |
errno = EINVAL; |
| 2695 |
return -1; |
| 2696 |
#endif |
| 2697 |
} |
| 2698 |
|
| 2699 |
#ifdef O_NOATIME |
| 2700 |
if (open_noatime) |
| 2701 |
flags |= O_NOATIME; |
| 2702 |
#endif |
| 2703 |
|
| 2704 |
#ifdef O_NOFOLLOW |
| 2705 |
fd = open(pathname, flags|O_NOFOLLOW); |
| 2706 |
#else |
| 2707 |
if (do_lstat(pathname, &l_st) < 0) |
| 2708 |
return -1; |
| 2709 |
if (S_ISLNK(l_st.st_mode)) { |
| 2710 |
errno = ELOOP; |
| 2711 |
return -1; |
| 2712 |
} |
| 2713 |
if ((fd = open(pathname, flags)) < 0) |
| 2714 |
return fd; |
| 2715 |
if (do_fstat(fd, &f_st) < 0) { |
| 2716 |
close_and_return_error: |
| 2717 |
{ |
| 2718 |
int save_errno = errno; |
| 2719 |
close(fd); |
| 2720 |
errno = save_errno; |
| 2721 |
} |
| 2722 |
return -1; |
| 2723 |
} |
| 2724 |
if (l_st.st_dev != f_st.st_dev || l_st.st_ino != f_st.st_ino) { |
| 2725 |
errno = EINVAL; |
| 2726 |
goto close_and_return_error; |
| 2727 |
} |
| 2728 |
#endif |
| 2729 |
|
| 2730 |
return fd; |
| 2731 |
} |
| 2732 |
|
| 2733 |
|
| 2734 |
|
| 2735 |
|
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
nc |
| 2741 |
|
| 2742 |
|
| 2743 |
|
| 2744 |
|
| 2745 |
|
| 2746 |
|
| 2747 |
|
| 2748 |
|
| 2749 |
|
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
static int path_has_dotdot_component(const char *path) |
| 2766 |
{ |
| 2767 |
const char *p = path; |
| 2768 |
|
| 2769 |
while (*p) { |
| 2770 |
const char *q; |
| 2771 |
if (*p == '/') { p++; continue; } |
| 2772 |
q = p; |
| 2773 |
while (*q && *q != '/') |
| 2774 |
q++; |
| 2775 |
if (q - p == 2 && p[0] == '.' && p[1] == '.') |
| 2776 |
return 1; |
| 2777 |
p = q; |
| 2778 |
} |
| 2779 |
return 0; |
| 2780 |
} |
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
char curr_dir[MAXPATHLEN]; |
| 2787 |
unsigned int curr_dir_len; |
| 2788 |
|
| 2789 |
#if defined(O_NOFOLLOW) && defined(O_DIRECTORY) && defined(AT_FDCWD) |
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
#ifndef SECURE_OPEN_MAXSYMLINKS |
| 2804 |
#define SECURE_OPEN_MAXSYMLINKS 40 |
| 2805 |
#endif |
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
#define DS_MAXDEPTH 1024 |
| 2812 |
|
| 2813 |
struct dirstack { |
| 2814 |
int fds[DS_MAXDEPTH]; |
| 2815 |
int top; |
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
char abspath[MAXPATHLEN]; |
| 2821 |
}; |
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
static int ds_path_push(struct dirstack *ds, const char *comp) |
| 2826 |
{ |
| 2827 |
size_t al = strlen(ds->abspath); |
| 2828 |
if (al == 0) |
| 2829 |
return 0; |
| 2830 |
size_t cl = strlen(comp); |
| 2831 |
if (al + 1 + cl >= sizeof ds->abspath) { |
| 2832 |
errno = ENAMETOOLONG; |
| 2833 |
return -1; |
| 2834 |
} |
| 2835 |
ds->abspath[al] = '/'; |
| 2836 |
memcpy(ds->abspath + al + 1, comp, cl + 1); |
| 2837 |
return 0; |
| 2838 |
} |
| 2839 |
|
| 2840 |
|
| 2841 |
static void ds_path_pop(struct dirstack *ds) |
| 2842 |
{ |
| 2843 |
char *slash; |
| 2844 |
if (!ds->abspath[0]) |
| 2845 |
return; |
| 2846 |
slash = strrchr(ds->abspath, '/'); |
| 2847 |
if (slash && slash != ds->abspath) |
| 2848 |
*slash = '\0'; |
| 2849 |
} |
| 2850 |
|
| 2851 |
|
| 2852 |
|
| 2853 |
static int ds_init(struct dirstack *ds, int anchor) |
| 2854 |
{ |
| 2855 |
ds->abspath[0] = '\0'; |
| 2856 |
ds->fds[0] = anchor; |
| 2857 |
ds->top = 0; |
| 2858 |
return 0; |
| 2859 |
} |
| 2860 |
|
| 2861 |
|
| 2862 |
static void ds_free(struct dirstack *ds) |
| 2863 |
{ |
| 2864 |
while (ds->top > 0) |
| 2865 |
close(ds->fds[ds->top--]); |
| 2866 |
} |
| 2867 |
|
| 2868 |
static int ds_cur(struct dirstack *ds) |
| 2869 |
{ |
| 2870 |
return ds->fds[ds->top]; |
| 2871 |
} |
| 2872 |
|
| 2873 |
static int ds_push(struct dirstack *ds, int fd) |
| 2874 |
{ |
| 2875 |
if (ds->top + 1 >= DS_MAXDEPTH) { |
| 2876 |
close(fd); |
| 2877 |
errno = ENOMEM; |
| 2878 |
return -1; |
| 2879 |
} |
| 2880 |
ds->fds[++ds->top] = fd; |
| 2881 |
return 0; |
| 2882 |
} |
| 2883 |
|
| 2884 |
|
| 2885 |
|
| 2886 |
static int ds_take(struct dirstack *ds) |
| 2887 |
{ |
| 2888 |
if (ds->top > 0) |
| 2889 |
return ds->fds[ds->top--]; |
| 2890 |
return openat(ds->fds[0], ".", O_RDONLY | O_DIRECTORY); |
| 2891 |
} |
| 2892 |
|
| 2893 |
static int ds_walk_path(struct dirstack *ds, char *path, int *hops); |
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
|
| 2900 |
|
| 2901 |
static int ds_descend(struct dirstack *ds, const char *part, int *hops) |
| 2902 |
{ |
| 2903 |
if (part[0] == '.' && part[1] == '\0') |
| 2904 |
return 0; |
| 2905 |
if (part[0] == '.' && part[1] == '.' && part[2] == '\0') { |
| 2906 |
if (ds->top == 0) { |
| 2907 |
errno = ELOOP; |
| 2908 |
return -1; |
| 2909 |
} |
| 2910 |
close(ds->fds[ds->top--]); |
| 2911 |
ds_path_pop(ds); |
| 2912 |
return 0; |
| 2913 |
} |
| 2914 |
|
| 2915 |
int fd = openat(ds_cur(ds), part, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); |
| 2916 |
if (fd != -1) { |
| 2917 |
if (ds_push(ds, fd) < 0) |
| 2918 |
return -1; |
| 2919 |
if (ds_path_push(ds, part) < 0) |
| 2920 |
return -1; |
| 2921 |
|
| 2922 |
|
| 2923 |
if (abspath_outside_confinement(ds->abspath)) { |
| 2924 |
errno = ELOOP; |
| 2925 |
return -1; |
| 2926 |
} |
| 2927 |
return 0; |
| 2928 |
} |
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
|
| 2933 |
if (errno != ENOTDIR && !NOFOLLOW_HIT_SYMLINK(errno)) { |
| 2934 |
if (errno == EMFILE || errno == ENFILE) { |
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
static int warned = 0; |
| 2939 |
if (!warned) { |
| 2940 |
int e = errno; |
| 2941 |
warned = 1; |
| 2942 |
rprintf(FWARNING, "out of file descriptors resolving a deep path;" |
| 2943 |
" raise the open-file limit (e.g. `ulimit -n`)\n"); |
| 2944 |
errno = e; |
| 2945 |
} |
| 2946 |
} |
| 2947 |
return -1; |
| 2948 |
} |
| 2949 |
int open_errno = errno; |
| 2950 |
|
| 2951 |
char buf[MAXPATHLEN]; |
| 2952 |
ssize_t n = readlinkat(ds_cur(ds), part, buf, sizeof buf - 1); |
| 2953 |
if (n < 0) { |
| 2954 |
if (errno == EINVAL) |
| 2955 |
errno = open_errno; |
| 2956 |
return -1; |
| 2957 |
} |
| 2958 |
if (n == 0 || (size_t)n >= sizeof buf - 1) { |
| 2959 |
errno = ELOOP; |
| 2960 |
return -1; |
| 2961 |
} |
| 2962 |
buf[n] = '\0'; |
| 2963 |
if (buf[0] == '/') { |
| 2964 |
errno = ELOOP; |
| 2965 |
return -1; |
| 2966 |
} |
| 2967 |
if (--(*hops) < 0) { |
| 2968 |
errno = ELOOP; |
| 2969 |
return -1; |
| 2970 |
} |
| 2971 |
return ds_walk_path(ds, buf, hops); |
| 2972 |
} |
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
static int ds_walk_path(struct dirstack *ds, char *path, int *hops) |
| 2977 |
{ |
| 2978 |
char *save = NULL; |
| 2979 |
for (char *c = strtok_r(path, "/", &save); c; c = strtok_r(NULL, "/", &save)) { |
| 2980 |
if (ds_descend(ds, c, hops) < 0) |
| 2981 |
return -1; |
| 2982 |
} |
| 2983 |
return 0; |
| 2984 |
} |
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
static int secure_walk_at(int anchor_fd, const char *anchor_abspath, |
| 2992 |
const char *relpath, int flags, mode_t mode, int *hops) |
| 2993 |
{ |
| 2994 |
struct dirstack ds; |
| 2995 |
int retfd = -1; |
| 2996 |
char *path_copy; |
| 2997 |
|
| 2998 |
if (ds_init(&ds, anchor_fd) < 0) |
| 2999 |
return -1; |
| 3000 |
|
| 3001 |
|
| 3002 |
if (anchor_abspath && anchor_abspath[0] == '/') |
| 3003 |
strlcpy(ds.abspath, anchor_abspath, sizeof ds.abspath); |
| 3004 |
path_copy = my_strdup(relpath, __FILE__, __LINE__); |
| 3005 |
if (!path_copy) { |
| 3006 |
ds_free(&ds); |
| 3007 |
return -1; |
| 3008 |
} |
| 3009 |
|
| 3010 |
|
| 3011 |
|
| 3012 |
size_t pclen = strlen(path_copy); |
| 3013 |
while (pclen > 1 && path_copy[pclen-1] == '/') |
| 3014 |
path_copy[--pclen] = '\0'; |
| 3015 |
char *last_slash = strrchr(path_copy, '/'); |
| 3016 |
size_t last_off = last_slash ? (size_t)(last_slash + 1 - path_copy) : 0; |
| 3017 |
|
| 3018 |
int saw_component = 0; |
| 3019 |
char *psave = NULL; |
| 3020 |
for (char *part = strtok_r(path_copy, "/", &psave); |
| 3021 |
part != NULL; |
| 3022 |
part = strtok_r(NULL, "/", &psave)) |
| 3023 |
{ |
| 3024 |
int is_last = (size_t)(part - path_copy) == last_off; |
| 3025 |
saw_component = 1; |
| 3026 |
|
| 3027 |
|
| 3028 |
|
| 3029 |
|
| 3030 |
|
| 3031 |
|
| 3032 |
if (part[0] == '.' |
| 3033 |
&& (part[1] == '\0' || (part[1] == '.' && part[2] == '\0'))) { |
| 3034 |
if (ds_descend(&ds, part, hops) < 0) |
| 3035 |
goto cleanup; |
| 3036 |
if (is_last) { |
| 3037 |
if (flags & O_DIRECTORY) |
| 3038 |
retfd = ds_take(&ds); |
| 3039 |
else |
| 3040 |
errno = EISDIR; |
| 3041 |
goto cleanup; |
| 3042 |
} |
| 3043 |
continue; |
| 3044 |
} |
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
if (is_last && !(flags & O_DIRECTORY)) { |
| 3049 |
if (ds.abspath[0]) { |
| 3050 |
char leafabs[MAXPATHLEN]; |
| 3051 |
if (snprintf(leafabs, sizeof leafabs, "%s/%s", ds.abspath, part) |
| 3052 |
< (int)sizeof leafabs |
| 3053 |
&& abspath_outside_confinement(leafabs)) { |
| 3054 |
errno = ELOOP; |
| 3055 |
goto cleanup; |
| 3056 |
} |
| 3057 |
} |
| 3058 |
int next_fd = openat(ds_cur(&ds), part, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); |
| 3059 |
if (next_fd == -1 && (errno == ENOTDIR || errno == ENOENT)) { |
| 3060 |
retfd = openat(ds_cur(&ds), part, flags | O_NOFOLLOW, mode); |
| 3061 |
goto cleanup; |
| 3062 |
} |
| 3063 |
if (next_fd == -1) |
| 3064 |
goto cleanup; |
| 3065 |
close(next_fd); |
| 3066 |
errno = EISDIR; |
| 3067 |
goto cleanup; |
| 3068 |
} |
| 3069 |
|
| 3070 |
|
| 3071 |
if (is_last && (flags & O_NOFOLLOW)) { |
| 3072 |
retfd = openat(ds_cur(&ds), part, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); |
| 3073 |
goto cleanup; |
| 3074 |
} |
| 3075 |
|
| 3076 |
|
| 3077 |
|
| 3078 |
if (ds_descend(&ds, part, hops) < 0) { |
| 3079 |
if (!is_last && errno == ENOTDIR) |
| 3080 |
errno = ELOOP; |
| 3081 |
goto cleanup; |
| 3082 |
} |
| 3083 |
if (is_last) { |
| 3084 |
retfd = ds_take(&ds); |
| 3085 |
goto cleanup; |
| 3086 |
} |
| 3087 |
} |
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
if (!saw_component) { |
| 3093 |
if ((flags & O_DIRECTORY) && anchor_fd != AT_FDCWD) |
| 3094 |
retfd = ds_take(&ds); |
| 3095 |
else |
| 3096 |
errno = EISDIR; |
| 3097 |
} |
| 3098 |
|
| 3099 |
cleanup: |
| 3100 |
free(path_copy); |
| 3101 |
ds_free(&ds); |
| 3102 |
return retfd; |
| 3103 |
} |
| 3104 |
#endif |
| 3105 |
|
| 3106 |
int secure_relative_open(const char *basedir, const char *relpath, int flags, mode_t mode) |
| 3107 |
{ |
| 3108 |
extern int am_daemon, am_chrooted; |
| 3109 |
extern char *module_dir; |
| 3110 |
extern unsigned int module_dirlen; |
| 3111 |
char modrel_buf[MAXPATHLEN]; |
| 3112 |
int reanchored = 0; |
| 3113 |
|
| 3114 |
if (!relpath || relpath[0] == '/') { |
| 3115 |
|
| 3116 |
errno = EINVAL; |
| 3117 |
return -1; |
| 3118 |
} |
| 3119 |
|
| 3120 |
|
| 3121 |
|
| 3122 |
|
| 3123 |
|
| 3124 |
|
| 3125 |
|
| 3126 |
|
| 3127 |
|
| 3128 |
|
| 3129 |
|
| 3130 |
|
| 3131 |
|
| 3132 |
|
| 3133 |
|
| 3134 |
if (am_daemon && (!am_chrooted || module_dirlen) |
| 3135 |
&& module_dir && module_dir[0] == '/' |
| 3136 |
&& (basedir == NULL || basedir[0] != '/') |
| 3137 |
&& (path_has_dotdot_component(relpath) |
| 3138 |
|| (basedir && path_has_dotdot_component(basedir)))) { |
| 3139 |
const char *p; |
| 3140 |
int n; |
| 3141 |
if (curr_dir_len >= module_dirlen |
| 3142 |
&& strncmp(curr_dir, module_dir, module_dirlen) == 0 |
| 3143 |
&& (curr_dir[module_dirlen] == '\0' || curr_dir[module_dirlen] == '/')) { |
| 3144 |
for (p = curr_dir + module_dirlen; *p == '/'; p++) {} |
| 3145 |
if (basedir) |
| 3146 |
n = snprintf(modrel_buf, sizeof modrel_buf, "%s%s%s/%s", |
| 3147 |
p, *p ? "/" : "", basedir, relpath); |
| 3148 |
else |
| 3149 |
n = snprintf(modrel_buf, sizeof modrel_buf, "%s%s%s", |
| 3150 |
p, *p ? "/" : "", relpath); |
| 3151 |
if (n < 0 || n >= (int)sizeof modrel_buf) { |
| 3152 |
errno = ENAMETOOLONG; |
| 3153 |
return -1; |
| 3154 |
} |
| 3155 |
basedir = module_dir; |
| 3156 |
relpath = modrel_buf; |
| 3157 |
reanchored = 1; |
| 3158 |
} |
| 3159 |
|
| 3160 |
|
| 3161 |
} |
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
if (!reanchored) { |
| 3171 |
if (path_has_dotdot_component(relpath)) { |
| 3172 |
errno = EINVAL; |
| 3173 |
return -1; |
| 3174 |
} |
| 3175 |
if (basedir && basedir[0] != '/' && path_has_dotdot_component(basedir)) { |
| 3176 |
errno = EINVAL; |
| 3177 |
return -1; |
| 3178 |
} |
| 3179 |
} |
| 3180 |
|
| 3181 |
#ifdef O_NOATIME |
| 3182 |
if (open_noatime) |
| 3183 |
flags |= O_NOATIME; |
| 3184 |
#endif |
| 3185 |
|
| 3186 |
#if !defined(O_NOFOLLOW) || !defined(O_DIRECTORY) || !defined(AT_FDCWD) |
| 3187 |
|
| 3188 |
if (!basedir) { |
| 3189 |
return open(relpath, flags, mode); |
| 3190 |
} |
| 3191 |
char fullpath[MAXPATHLEN]; |
| 3192 |
pathjoin(fullpath, sizeof fullpath, basedir, relpath); |
| 3193 |
return open(fullpath, flags, mode); |
| 3194 |
#else |
| 3195 |
int dirfd = AT_FDCWD; |
| 3196 |
int hops = SECURE_OPEN_MAXSYMLINKS; |
| 3197 |
if (basedir != NULL) { |
| 3198 |
if (basedir[0] == '/') { |
| 3199 |
|
| 3200 |
|
| 3201 |
|
| 3202 |
dirfd = open_anchor_dirfd(basedir); |
| 3203 |
if (dirfd == -1) |
| 3204 |
return -1; |
| 3205 |
} else { |
| 3206 |
|
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
struct dirstack bds; |
| 3211 |
char *bcopy; |
| 3212 |
if (ds_init(&bds, AT_FDCWD) < 0) |
| 3213 |
return -1; |
| 3214 |
bcopy = my_strdup(basedir, __FILE__, __LINE__); |
| 3215 |
if (!bcopy) { |
| 3216 |
ds_free(&bds); |
| 3217 |
return -1; |
| 3218 |
} |
| 3219 |
if (ds_walk_path(&bds, bcopy, &hops) < 0) { |
| 3220 |
int e = errno; |
| 3221 |
free(bcopy); |
| 3222 |
ds_free(&bds); |
| 3223 |
errno = e; |
| 3224 |
return -1; |
| 3225 |
} |
| 3226 |
free(bcopy); |
| 3227 |
dirfd = ds_take(&bds); |
| 3228 |
ds_free(&bds); |
| 3229 |
if (dirfd == -1) |
| 3230 |
return -1; |
| 3231 |
} |
| 3232 |
} |
| 3233 |
|
| 3234 |
|
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
const char *anchor_abspath = !basedir ? curr_dir |
| 3239 |
: (basedir[0] == '/' ? basedir : NULL); |
| 3240 |
int retfd = secure_walk_at(dirfd, anchor_abspath, relpath, flags, mode, &hops); |
| 3241 |
if (dirfd != AT_FDCWD) |
| 3242 |
close(dirfd); |
| 3243 |
return retfd; |
| 3244 |
#endif |
| 3245 |
} |
| 3246 |
|
| 3247 |
|
| 3248 |
|
| 3249 |
|
| 3250 |
|
| 3251 |
|
| 3252 |
static int secure_relative_open_at_internal(int anchor_fd, const char *relpath, |
| 3253 |
int flags, mode_t mode, int allow_dotdot) |
| 3254 |
{ |
| 3255 |
#if !defined(O_NOFOLLOW) || !defined(O_DIRECTORY) || !defined(AT_FDCWD) |
| 3256 |
(void)anchor_fd; (void)relpath; (void)flags; (void)mode; (void)allow_dotdot; |
| 3257 |
errno = ENOSYS; |
| 3258 |
return -1; |
| 3259 |
#else |
| 3260 |
int hops = SECURE_OPEN_MAXSYMLINKS; |
| 3261 |
if (!relpath || relpath[0] == '/') { |
| 3262 |
errno = EINVAL; |
| 3263 |
return -1; |
| 3264 |
} |
| 3265 |
if (!allow_dotdot && path_has_dotdot_component(relpath)) { |
| 3266 |
errno = EINVAL; |
| 3267 |
return -1; |
| 3268 |
} |
| 3269 |
#ifdef O_NOATIME |
| 3270 |
if (open_noatime) |
| 3271 |
flags |= O_NOATIME; |
| 3272 |
#endif |
| 3273 |
|
| 3274 |
|
| 3275 |
|
| 3276 |
return secure_walk_at(anchor_fd, NULL, relpath, flags, mode, &hops); |
| 3277 |
#endif |
| 3278 |
} |
| 3279 |
|
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
int secure_relative_open_at(int anchor_fd, const char *relpath, int flags, mode_t mode) |
| 3287 |
{ |
| 3288 |
return secure_relative_open_at_internal(anchor_fd, relpath, flags, mode, 0); |
| 3289 |
} |
| 3290 |
|
| 3291 |
|
| 3292 |
|
| 3293 |
|
| 3294 |
|
| 3295 |
int secure_relative_open_at_beneath(int anchor_fd, const char *relpath, |
| 3296 |
int flags, mode_t mode) |
| 3297 |
{ |
| 3298 |
return secure_relative_open_at_internal(anchor_fd, relpath, flags, mode, 1); |
| 3299 |
} |
| 3300 |
|
| 3301 |
#if defined O_NOFOLLOW && defined O_DIRECTORY && defined AT_FDCWD |
| 3302 |
|
| 3303 |
|
| 3304 |
|
| 3305 |
static void rand_bytes(unsigned char *buf, size_t len) |
| 3306 |
{ |
| 3307 |
#ifndef O_CLOEXEC |
| 3308 |
#define O_CLOEXEC 0 |
| 3309 |
#endif |
| 3310 |
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); |
| 3311 |
if (fd >= 0) { |
| 3312 |
ssize_t n = read(fd, buf, len); |
| 3313 |
close(fd); |
| 3314 |
if (n == (ssize_t)len) { |
| 3315 |
return; |
| 3316 |
} |
| 3317 |
} |
| 3318 |
for (size_t i = 0; i < len; i++) { |
| 3319 |
buf[i] = (unsigned char)rand(); |
| 3320 |
} |
| 3321 |
} |
| 3322 |
#endif |
| 3323 |
|
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
int do_mkstemp_atfd(int dfd, char *filename, mode_t perms) |
| 3330 |
{ |
| 3331 |
#ifdef AT_FDCWD |
| 3332 |
static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 3333 |
size_t filename_len = strlen(filename); |
| 3334 |
char *suffix; |
| 3335 |
int fd = -1; |
| 3336 |
|
| 3337 |
if (filename_len < 6) { |
| 3338 |
errno = EINVAL; |
| 3339 |
return -1; |
| 3340 |
} |
| 3341 |
suffix = filename + filename_len - 6; |
| 3342 |
if (strcmp(suffix, "XXXXXX") != 0) { |
| 3343 |
errno = EINVAL; |
| 3344 |
return -1; |
| 3345 |
} |
| 3346 |
|
| 3347 |
perms |= S_IWUSR; |
| 3348 |
for (int tries = 0; tries < 100; tries++) { |
| 3349 |
unsigned char rbytes[6]; |
| 3350 |
rand_bytes(rbytes, sizeof(rbytes)); |
| 3351 |
for (int i = 0; i < 6; i++) |
| 3352 |
suffix[i] = letters[rbytes[i] % (sizeof(letters) - 1)]; |
| 3353 |
|
| 3354 |
fd = openat(dfd, filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, perms); |
| 3355 |
if (fd >= 0) |
| 3356 |
break; |
| 3357 |
if (errno != EEXIST) |
| 3358 |
return -1; |
| 3359 |
} |
| 3360 |
|
| 3361 |
if (fd >= 0) { |
| 3362 |
if (fchmod(fd, perms) != 0 && preserve_perms) { |
| 3363 |
int errno_save = errno; |
| 3364 |
close(fd); |
| 3365 |
unlinkat(dfd, filename, 0); |
| 3366 |
errno = errno_save; |
| 3367 |
return -1; |
| 3368 |
} |
| 3369 |
#if defined HAVE_SETMODE && O_BINARY |
| 3370 |
setmode(fd, O_BINARY); |
| 3371 |
#endif |
| 3372 |
} |
| 3373 |
return fd; |
| 3374 |
#else |
| 3375 |
(void)dfd; (void)filename; (void)perms; |
| 3376 |
errno = ENOSYS; |
| 3377 |
return -1; |
| 3378 |
#endif |
| 3379 |
} |
| 3380 |
|
| 3381 |
|
| 3382 |
|
| 3383 |
|
| 3384 |
|
| 3385 |
|
| 3386 |
|
| 3387 |
|
| 3388 |
|
| 3389 |
int secure_mkstemp(char *template, mode_t perms, int operator_path) |
| 3390 |
{ |
| 3391 |
#if !defined(O_NOFOLLOW) || !defined(O_DIRECTORY) || !defined(AT_FDCWD) |
| 3392 |
|
| 3393 |
return do_mkstemp(template, perms); |
| 3394 |
#else |
| 3395 |
char *lastslash; |
| 3396 |
int dirfd = AT_FDCWD; |
| 3397 |
int fd = -1; |
| 3398 |
|
| 3399 |
if (!template) { |
| 3400 |
errno = EINVAL; |
| 3401 |
return -1; |
| 3402 |
} |
| 3403 |
if (strncmp(template, "../", 3) == 0 || strstr(template, "/../")) { |
| 3404 |
errno = EINVAL; |
| 3405 |
return -1; |
| 3406 |
} |
| 3407 |
|
| 3408 |
|
| 3409 |
|
| 3410 |
if (operator_path && symlink_optout_allowed()) |
| 3411 |
return do_mkstemp(template, perms); |
| 3412 |
|
| 3413 |
|
| 3414 |
|
| 3415 |
|
| 3416 |
|
| 3417 |
|
| 3418 |
|
| 3419 |
|
| 3420 |
lastslash = strrchr(template, '/'); |
| 3421 |
if (lastslash) { |
| 3422 |
char dirbuf[MAXPATHLEN]; |
| 3423 |
size_t dlen = lastslash - template; |
| 3424 |
const char *dir; |
| 3425 |
if (dlen == 0) |
| 3426 |
dir = "/"; |
| 3427 |
else { |
| 3428 |
if (dlen >= sizeof dirbuf) { |
| 3429 |
errno = ENAMETOOLONG; |
| 3430 |
return -1; |
| 3431 |
} |
| 3432 |
memcpy(dirbuf, template, dlen); |
| 3433 |
dirbuf[dlen] = '\0'; |
| 3434 |
dir = dirbuf; |
| 3435 |
} |
| 3436 |
dirfd = operator_path |
| 3437 |
? open_no_attacker_symlinks(dir, O_RDONLY | O_DIRECTORY, 0) |
| 3438 |
: secure_relative_open(dir, ".", O_RDONLY | O_DIRECTORY, 0); |
| 3439 |
if (dirfd < 0) |
| 3440 |
return -1; |
| 3441 |
} |
| 3442 |
|
| 3443 |
|
| 3444 |
{ |
| 3445 |
char *filename = lastslash ? lastslash + 1 : template; |
| 3446 |
int e; |
| 3447 |
fd = do_mkstemp_atfd(dirfd, filename, perms); |
| 3448 |
e = errno; |
| 3449 |
if (dirfd != AT_FDCWD) close(dirfd); |
| 3450 |
errno = e; |
| 3451 |
} |
| 3452 |
return fd; |
| 3453 |
#endif |
| 3454 |
} |
| 3455 |
|
| 3456 |
|
| 3457 |
|
| 3458 |
|
| 3459 |
|
| 3460 |
|
| 3461 |
|
| 3462 |
|
| 3463 |
|
| 3464 |
|
| 3465 |
|
| 3466 |
int do_open_checklinks(const char *pathname) |
| 3467 |
{ |
| 3468 |
if (copy_links || copy_unsafe_links) { |
| 3469 |
return do_open(pathname, O_RDONLY, 0); |
| 3470 |
} |
| 3471 |
return do_open_nofollow(pathname, O_RDONLY); |
| 3472 |
} |
| 3473 |
|
| 3474 |
|
| 3475 |
|
| 3476 |
|
| 3477 |
|
| 3478 |
|
| 3479 |
|
| 3480 |
|
| 3481 |
|
| 3482 |
|
| 3483 |
|
| 3484 |
|
| 3485 |
|
| 3486 |
|
| 3487 |
|
| 3488 |
|
| 3489 |
|
| 3490 |
|
| 3491 |
int open_dir_secure(const char *dirname) |
| 3492 |
{ |
| 3493 |
#ifdef AT_FDCWD |
| 3494 |
extern int am_daemon, am_chrooted; |
| 3495 |
int dfd; |
| 3496 |
|
| 3497 |
|
| 3498 |
|
| 3499 |
|
| 3500 |
if (!secure_relpath_active()) { |
| 3501 |
errno = 0; |
| 3502 |
return -1; |
| 3503 |
} |
| 3504 |
|
| 3505 |
if (!dirname || !*dirname) { |
| 3506 |
|
| 3507 |
dfd = openat(AT_FDCWD, ".", O_RDONLY | O_DIRECTORY); |
| 3508 |
} else if (dirname[0] == '/') { |
| 3509 |
|
| 3510 |
|
| 3511 |
errno = 0; |
| 3512 |
return -1; |
| 3513 |
} else { |
| 3514 |
dfd = secure_relative_open(NULL, dirname, O_RDONLY | O_DIRECTORY, 0); |
| 3515 |
} |
| 3516 |
|
| 3517 |
if (dfd >= 0) { |
| 3518 |
|
| 3519 |
|
| 3520 |
int fl = fcntl(dfd, F_GETFD); |
| 3521 |
if (fl >= 0) |
| 3522 |
fcntl(dfd, F_SETFD, fl | FD_CLOEXEC); |
| 3523 |
} |
| 3524 |
return dfd; |
| 3525 |
#else |
| 3526 |
(void)dirname; |
| 3527 |
errno = 0; |
| 3528 |
return -1; |
| 3529 |
#endif |
| 3530 |
} |
| 3531 |
|
| 3532 |
|
| 3533 |
|
| 3534 |
|
| 3535 |
|
| 3536 |
|
| 3537 |
|
| 3538 |
|
| 3539 |
|
| 3540 |
|
| 3541 |
|
| 3542 |
|
| 3543 |
|
| 3544 |
|
| 3545 |
|
| 3546 |
|
| 3547 |
|
| 3548 |
|
| 3549 |
|
| 3550 |
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY |
| 3551 |
#define DPC_MAXDEPTH 64 |
| 3552 |
static const char *dpc_anchor = (const char *)-2; |
| 3553 |
static int dpc_base = -1; |
| 3554 |
static int dpc_fd[DPC_MAXDEPTH]; |
| 3555 |
static char dpc_name[DPC_MAXDEPTH][256]; |
| 3556 |
static int dpc_depth = 0; |
| 3557 |
|
| 3558 |
void reset_dir_fd_cache(void) |
| 3559 |
{ |
| 3560 |
while (dpc_depth > 0) |
| 3561 |
close(dpc_fd[--dpc_depth]); |
| 3562 |
if (dpc_base >= 0) |
| 3563 |
close(dpc_base); |
| 3564 |
dpc_base = -1; |
| 3565 |
dpc_anchor = (const char *)-2; |
| 3566 |
} |
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
|
| 3573 |
static int dpc_dir_fd(const char *anchor, const char *dirpath) |
| 3574 |
{ |
| 3575 |
char copy[MAXPATHLEN]; |
| 3576 |
char *comps[DPC_MAXDEPTH]; |
| 3577 |
char *sv = NULL; |
| 3578 |
int nc = 0, p, i; |
| 3579 |
|
| 3580 |
if (anchor && anchor[0] != '/') { errno = 0; return -1; } |
| 3581 |
if (!dirpath) |
| 3582 |
dirpath = ""; |
| 3583 |
if (dirpath[0] == '/') { errno = 0; return -1; } |
| 3584 |
|
| 3585 |
if (anchor != dpc_anchor || dpc_base < 0) { |
| 3586 |
int fl; |
| 3587 |
reset_dir_fd_cache(); |
| 3588 |
dpc_base = open_anchor_dirfd(anchor ? anchor : "."); |
| 3589 |
if (dpc_base < 0) |
| 3590 |
return -1; |
| 3591 |
if ((fl = fcntl(dpc_base, F_GETFD)) >= 0) |
| 3592 |
fcntl(dpc_base, F_SETFD, fl | FD_CLOEXEC); |
| 3593 |
dpc_anchor = anchor; |
| 3594 |
} |
| 3595 |
|
| 3596 |
if (strlcpy(copy, dirpath, sizeof copy) >= sizeof copy) { errno = ENAMETOOLONG; return -1; } |
| 3597 |
for (char *c = strtok_r(copy, "/", &sv); c; c = strtok_r(NULL, "/", &sv)) { |
| 3598 |
if (c[0] == '.' && c[1] == '\0') |
| 3599 |
continue; |
| 3600 |
if (c[0] == '.' && c[1] == '.' && c[2] == '\0') { errno = 0; return -1; } |
| 3601 |
if (nc >= DPC_MAXDEPTH || strlen(c) >= sizeof dpc_name[0]) { |
| 3602 |
|
| 3603 |
|
| 3604 |
|
| 3605 |
|
| 3606 |
|
| 3607 |
reset_dir_fd_cache(); |
| 3608 |
errno = 0; |
| 3609 |
return -1; |
| 3610 |
} |
| 3611 |
comps[nc++] = c; |
| 3612 |
} |
| 3613 |
|
| 3614 |
|
| 3615 |
for (p = 0; p < dpc_depth && p < nc && strcmp(dpc_name[p], comps[p]) == 0; p++) |
| 3616 |
; |
| 3617 |
while (dpc_depth > p) |
| 3618 |
close(dpc_fd[--dpc_depth]); |
| 3619 |
|
| 3620 |
|
| 3621 |
for (i = p; i < nc; i++) { |
| 3622 |
int afd = dpc_depth > 0 ? dpc_fd[dpc_depth-1] : dpc_base; |
| 3623 |
struct dirstack ds; |
| 3624 |
int hops = SECURE_OPEN_MAXSYMLINKS; |
| 3625 |
int fd, fl; |
| 3626 |
if (ds_init(&ds, afd) < 0) |
| 3627 |
return -1; |
| 3628 |
if (ds_descend(&ds, comps[i], &hops) < 0) { |
| 3629 |
int e = errno; |
| 3630 |
ds_free(&ds); |
| 3631 |
errno = e; |
| 3632 |
return -1; |
| 3633 |
} |
| 3634 |
fd = ds_take(&ds); |
| 3635 |
ds_free(&ds); |
| 3636 |
if (fd < 0) |
| 3637 |
return -1; |
| 3638 |
if ((fl = fcntl(fd, F_GETFD)) >= 0) |
| 3639 |
fcntl(fd, F_SETFD, fl | FD_CLOEXEC); |
| 3640 |
strlcpy(dpc_name[dpc_depth], comps[i], sizeof dpc_name[0]); |
| 3641 |
dpc_fd[dpc_depth++] = fd; |
| 3642 |
} |
| 3643 |
|
| 3644 |
return nc > 0 ? dpc_fd[dpc_depth-1] : dpc_base; |
| 3645 |
} |
| 3646 |
|
| 3647 |
|
| 3648 |
|
| 3649 |
int held_dir_path_fd(const char *anchor, const char *dirpath) |
| 3650 |
{ |
| 3651 |
return dpc_dir_fd(anchor, dirpath); |
| 3652 |
} |
| 3653 |
|
| 3654 |
int get_dir_fd(const char *dirname) |
| 3655 |
{ |
| 3656 |
if (!secure_relpath_active()) { errno = 0; return -1; } |
| 3657 |
return dpc_dir_fd(NULL, dirname); |
| 3658 |
} |
| 3659 |
#else |
| 3660 |
void reset_dir_fd_cache(void) |
| 3661 |
{ |
| 3662 |
} |
| 3663 |
int held_dir_path_fd(const char *anchor, const char *dirpath) |
| 3664 |
{ |
| 3665 |
(void)anchor; |
| 3666 |
(void)dirpath; |
| 3667 |
errno = 0; |
| 3668 |
return -1; |
| 3669 |
} |
| 3670 |
int get_dir_fd(const char *dirname) |
| 3671 |
{ |
| 3672 |
(void)dirname; |
| 3673 |
errno = 0; |
| 3674 |
return -1; |
| 3675 |
} |
| 3676 |
#endif |
| 3677 |
|
| 3678 |
|
| 3679 |
|
| 3680 |
|
| 3681 |
|
| 3682 |
|
| 3683 |
|
| 3684 |
|
| 3685 |
|
| 3686 |
|
| 3687 |
int held_dfd_for(const char *path, const struct file_struct *file) |
| 3688 |
{ |
| 3689 |
const char *slash, *dn; |
| 3690 |
size_t plen; |
| 3691 |
|
| 3692 |
if (!path || *path == '/') |
| 3693 |
return -1; |
| 3694 |
dn = file && file->dirname ? file->dirname : ""; |
| 3695 |
slash = strrchr(path, '/'); |
| 3696 |
plen = slash ? (size_t)(slash - path) : 0; |
| 3697 |
if (strlen(dn) != plen || memcmp(path, dn, plen) != 0) |
| 3698 |
return -1; |
| 3699 |
return get_dir_fd(file ? file->dirname : NULL); |
| 3700 |
} |
| 3701 |
|
| 3702 |
int do_unlink_atfd(int dfd, const char *name, int flags) |
| 3703 |
{ |
| 3704 |
#ifdef AT_FDCWD |
| 3705 |
if (dry_run) return 0; |
| 3706 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3707 |
return unlinkat(dfd, name, flags); |
| 3708 |
#else |
| 3709 |
(void)dfd; (void)name; (void)flags; |
| 3710 |
errno = ENOSYS; |
| 3711 |
return -1; |
| 3712 |
#endif |
| 3713 |
} |
| 3714 |
|
| 3715 |
int do_mkdir_atfd(int dfd, const char *name, mode_t mode) |
| 3716 |
{ |
| 3717 |
#ifdef AT_FDCWD |
| 3718 |
if (dry_run) return 0; |
| 3719 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3720 |
return mkdirat(dfd, name, mode); |
| 3721 |
#else |
| 3722 |
(void)dfd; (void)name; (void)mode; |
| 3723 |
errno = ENOSYS; |
| 3724 |
return -1; |
| 3725 |
#endif |
| 3726 |
} |
| 3727 |
|
| 3728 |
#ifdef HAVE_CHMOD |
| 3729 |
int do_chmod_atfd(int dfd, const char *name, mode_t mode) |
| 3730 |
{ |
| 3731 |
#ifdef AT_FDCWD |
| 3732 |
if (dry_run) return 0; |
| 3733 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3734 |
|
| 3735 |
|
| 3736 |
|
| 3737 |
|
| 3738 |
return do_fchmodat_nofollow(dfd, name, mode); |
| 3739 |
#else |
| 3740 |
(void)dfd; (void)name; (void)mode; |
| 3741 |
errno = ENOSYS; |
| 3742 |
return -1; |
| 3743 |
#endif |
| 3744 |
} |
| 3745 |
#endif |
| 3746 |
|
| 3747 |
int do_lchown_atfd(int dfd, const char *name, uid_t owner, gid_t group) |
| 3748 |
{ |
| 3749 |
#ifdef AT_FDCWD |
| 3750 |
if (dry_run) return 0; |
| 3751 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3752 |
return fchownat(dfd, name, owner, group, AT_SYMLINK_NOFOLLOW); |
| 3753 |
#else |
| 3754 |
(void)dfd; (void)name; (void)owner; (void)group; |
| 3755 |
errno = ENOSYS; |
| 3756 |
return -1; |
| 3757 |
#endif |
| 3758 |
} |
| 3759 |
|
| 3760 |
|
| 3761 |
|
| 3762 |
|
| 3763 |
int do_fchown(int fd, uid_t owner, gid_t group) |
| 3764 |
{ |
| 3765 |
if (dry_run) return 0; |
| 3766 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3767 |
return fchown(fd, owner, group); |
| 3768 |
} |
| 3769 |
|
| 3770 |
#ifdef HAVE_CHMOD |
| 3771 |
int do_fchmod(int fd, mode_t mode) |
| 3772 |
{ |
| 3773 |
if (dry_run) return 0; |
| 3774 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3775 |
return fchmod(fd, mode); |
| 3776 |
} |
| 3777 |
#endif |
| 3778 |
|
| 3779 |
#ifdef HAVE_FUTIMENS |
| 3780 |
|
| 3781 |
|
| 3782 |
int do_futimens(int fd, STRUCT_STAT *stp) |
| 3783 |
{ |
| 3784 |
struct timespec t[2]; |
| 3785 |
|
| 3786 |
if (dry_run) return 0; |
| 3787 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3788 |
t[0].tv_sec = stp->st_atime; |
| 3789 |
#ifdef ST_ATIME_NSEC |
| 3790 |
t[0].tv_nsec = stp->ST_ATIME_NSEC; |
| 3791 |
#else |
| 3792 |
t[0].tv_nsec = 0; |
| 3793 |
#endif |
| 3794 |
t[1].tv_sec = stp->st_mtime; |
| 3795 |
#ifdef ST_MTIME_NSEC |
| 3796 |
t[1].tv_nsec = stp->ST_MTIME_NSEC; |
| 3797 |
#else |
| 3798 |
t[1].tv_nsec = 0; |
| 3799 |
#endif |
| 3800 |
return futimens(fd, t); |
| 3801 |
} |
| 3802 |
#endif |
| 3803 |
|
| 3804 |
#ifdef HAVE_UTIMENSAT |
| 3805 |
int do_utimensat_atfd(int dfd, const char *name, STRUCT_STAT *stp) |
| 3806 |
{ |
| 3807 |
#ifdef AT_FDCWD |
| 3808 |
struct timespec t[2]; |
| 3809 |
|
| 3810 |
if (dry_run) return 0; |
| 3811 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3812 |
|
| 3813 |
t[0].tv_sec = stp->st_atime; |
| 3814 |
#ifdef ST_ATIME_NSEC |
| 3815 |
t[0].tv_nsec = stp->ST_ATIME_NSEC; |
| 3816 |
#else |
| 3817 |
t[0].tv_nsec = 0; |
| 3818 |
#endif |
| 3819 |
t[1].tv_sec = stp->st_mtime; |
| 3820 |
#ifdef ST_MTIME_NSEC |
| 3821 |
t[1].tv_nsec = stp->ST_MTIME_NSEC; |
| 3822 |
#else |
| 3823 |
t[1].tv_nsec = 0; |
| 3824 |
#endif |
| 3825 |
return utimensat(dfd, name, t, AT_SYMLINK_NOFOLLOW); |
| 3826 |
#else |
| 3827 |
(void)dfd; (void)name; (void)stp; |
| 3828 |
errno = ENOSYS; |
| 3829 |
return -1; |
| 3830 |
#endif |
| 3831 |
} |
| 3832 |
#endif |
| 3833 |
|
| 3834 |
int do_open_atfd(int dfd, const char *name, int flags, mode_t mode) |
| 3835 |
{ |
| 3836 |
#ifdef AT_FDCWD |
| 3837 |
if (flags != O_RDONLY) { |
| 3838 |
RETURN_ERROR_IF(dry_run, 0); |
| 3839 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3840 |
} |
| 3841 |
#ifdef O_NOATIME |
| 3842 |
if (open_noatime) |
| 3843 |
flags |= O_NOATIME; |
| 3844 |
#endif |
| 3845 |
return openat(dfd, name, flags | O_NOFOLLOW | O_BINARY, mode); |
| 3846 |
#else |
| 3847 |
(void)dfd; (void)name; (void)flags; (void)mode; |
| 3848 |
errno = ENOSYS; |
| 3849 |
return -1; |
| 3850 |
#endif |
| 3851 |
} |
| 3852 |
|
| 3853 |
int do_symlink_atfd(const char *lnk, int dfd, const char *name) |
| 3854 |
{ |
| 3855 |
#ifdef AT_FDCWD |
| 3856 |
if (dry_run) return 0; |
| 3857 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3858 |
|
| 3859 |
#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS |
| 3860 |
|
| 3861 |
|
| 3862 |
|
| 3863 |
if (am_root < 0) { |
| 3864 |
int len = strlen(lnk); |
| 3865 |
int ok; |
| 3866 |
int fd = openat(dfd, name, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, |
| 3867 |
S_IWUSR | S_IRUSR); |
| 3868 |
if (fd < 0) |
| 3869 |
return -1; |
| 3870 |
ok = write(fd, lnk, len) == len; |
| 3871 |
if (close(fd) < 0) |
| 3872 |
ok = 0; |
| 3873 |
return ok ? 0 : -1; |
| 3874 |
} |
| 3875 |
#endif |
| 3876 |
return symlinkat(lnk, dfd, name); |
| 3877 |
#else |
| 3878 |
(void)lnk; (void)dfd; (void)name; |
| 3879 |
errno = ENOSYS; |
| 3880 |
return -1; |
| 3881 |
#endif |
| 3882 |
} |
| 3883 |
|
| 3884 |
int do_mknod_atfd(int dfd, const char *name, mode_t mode, dev_t dev) |
| 3885 |
{ |
| 3886 |
#ifdef AT_FDCWD |
| 3887 |
if (dry_run) return 0; |
| 3888 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3889 |
|
| 3890 |
if (am_root < 0) { |
| 3891 |
|
| 3892 |
int fd = openat(dfd, name, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, |
| 3893 |
S_IWUSR | S_IRUSR); |
| 3894 |
if (fd < 0) |
| 3895 |
return -1; |
| 3896 |
return (close(fd) < 0) ? -1 : 0; |
| 3897 |
} |
| 3898 |
|
| 3899 |
|
| 3900 |
|
| 3901 |
|
| 3902 |
|
| 3903 |
#ifdef HAVE_MKNODAT |
| 3904 |
if (mknodat(dfd, name, mode, dev) == 0) |
| 3905 |
return 0; |
| 3906 |
#endif |
| 3907 |
#ifdef HAVE_MKFIFOAT |
| 3908 |
if (S_ISFIFO(mode)) |
| 3909 |
return mkfifoat(dfd, name, mode); |
| 3910 |
#endif |
| 3911 |
if (S_ISSOCK(mode)) { |
| 3912 |
|
| 3913 |
|
| 3914 |
errno = EOPNOTSUPP; |
| 3915 |
return -1; |
| 3916 |
} |
| 3917 |
#ifdef HAVE_MKNODAT |
| 3918 |
return -1; |
| 3919 |
#else |
| 3920 |
|
| 3921 |
|
| 3922 |
(void)dev; |
| 3923 |
errno = ENOSYS; |
| 3924 |
return -1; |
| 3925 |
#endif |
| 3926 |
#else |
| 3927 |
(void)dfd; (void)name; (void)mode; (void)dev; |
| 3928 |
errno = ENOSYS; |
| 3929 |
return -1; |
| 3930 |
#endif |
| 3931 |
} |
| 3932 |
|
| 3933 |
int do_rename_atfd(int old_dfd, const char *old_name, int new_dfd, const char *new_name) |
| 3934 |
{ |
| 3935 |
#ifdef AT_FDCWD |
| 3936 |
if (dry_run) return 0; |
| 3937 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3938 |
return renameat(old_dfd, old_name, new_dfd, new_name); |
| 3939 |
#else |
| 3940 |
(void)old_dfd; (void)old_name; (void)new_dfd; (void)new_name; |
| 3941 |
errno = ENOSYS; |
| 3942 |
return -1; |
| 3943 |
#endif |
| 3944 |
} |
| 3945 |
|
| 3946 |
#if defined HAVE_LINK || defined HAVE_LINKAT |
| 3947 |
int do_link_atfd(int old_dfd, const char *old_name, int new_dfd, const char *new_name, int flags) |
| 3948 |
{ |
| 3949 |
#if defined AT_FDCWD && defined HAVE_LINKAT |
| 3950 |
if (dry_run) return 0; |
| 3951 |
RETURN_ERROR_IF_RO_OR_LO; |
| 3952 |
return linkat(old_dfd, old_name, new_dfd, new_name, flags); |
| 3953 |
#else |
| 3954 |
(void)old_dfd; (void)old_name; (void)new_dfd; (void)new_name; (void)flags; |
| 3955 |
errno = ENOSYS; |
| 3956 |
return -1; |
| 3957 |
#endif |
| 3958 |
} |
| 3959 |
#endif |
| 3960 |
|
| 3961 |
int do_lstat_atfd(int dfd, const char *name, STRUCT_STAT *st) |
| 3962 |
{ |
| 3963 |
#ifdef AT_FDCWD |
| 3964 |
# ifdef SUPPORT_LINKS |
| 3965 |
return fstatat(dfd, name, st, AT_SYMLINK_NOFOLLOW); |
| 3966 |
# else |
| 3967 |
return fstatat(dfd, name, st, 0); |
| 3968 |
# endif |
| 3969 |
#else |
| 3970 |
(void)dfd; (void)name; (void)st; |
| 3971 |
errno = ENOSYS; |
| 3972 |
return -1; |
| 3973 |
#endif |
| 3974 |
} |
| 3975 |
|
| 3976 |
int do_stat_atfd(int dfd, const char *name, STRUCT_STAT *st) |
| 3977 |
{ |
| 3978 |
#ifdef AT_FDCWD |
| 3979 |
return fstatat(dfd, name, st, 0); |
| 3980 |
#else |
| 3981 |
(void)dfd; (void)name; (void)st; |
| 3982 |
errno = ENOSYS; |
| 3983 |
return -1; |
| 3984 |
#endif |
| 3985 |
} |