| 441 | } |
| 442 | |
| 443 | static bool posix_log_path_open(const char *log_path, posix_log_path_t *path) { |
| 444 | size_t length = 0; |
| 445 | if (!path || !bounded_length(log_path, sizeof(path->storage), &length) || length == 0) { |
| 446 | return false; |
| 447 | } |
| 448 | memset(path, 0, sizeof(*path)); |
| 449 | path->dir_fd = -1; |
| 450 | memcpy(path->storage, log_path, length + 1); |
| 451 | char *slash = strrchr(path->storage, '/'); |
| 452 | const char *parent = "."; |
| 453 | if (slash) { |
| 454 | path->base = slash + 1; |
| 455 | if (slash == path->storage) { |
| 456 | parent = "/"; |
| 457 | } else { |
| 458 | *slash = '\0'; |
| 459 | parent = path->storage; |
| 460 | } |
| 461 | } else { |
| 462 | path->base = path->storage; |
| 463 | } |
| 464 | size_t base_length = strlen(path->base); |
| 465 | if (base_length == 0 || base_length > NAME_MAX - 5 || strcmp(path->base, ".") == 0 || |
| 466 | strcmp(path->base, "..") == 0) { |
| 467 | return false; |
| 468 | } |
| 469 | int rotated_written = snprintf(path->rotated, sizeof(path->rotated), "%s.1", path->base); |
| 470 | int lock_written = snprintf(path->lock, sizeof(path->lock), "%s.lock", path->base); |
| 471 | if (rotated_written < 0 || (size_t)rotated_written >= sizeof(path->rotated) || |
| 472 | lock_written < 0 || (size_t)lock_written >= sizeof(path->lock)) { |
| 473 | return false; |
| 474 | } |
| 475 | path->dir_fd = open(parent, O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW); |
| 476 | struct stat parent_status; |
| 477 | if (path->dir_fd < 0 || !fd_cloexec(path->dir_fd) || fstat(path->dir_fd, &parent_status) != 0 || |
| 478 | !S_ISDIR(parent_status.st_mode) || parent_status.st_uid != geteuid() || |
| 479 | (parent_status.st_mode & 0022) != 0) { |
| 480 | posix_log_path_close(path); |
| 481 | return false; |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | static int posix_lock_file_open(const posix_log_path_t *path, bool *created_out) { |
| 487 | int flags = O_RDWR | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK; |
no test coverage detected