/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef V_ALPM_NATIVE_SANDBOX_H #define V_ALPM_NATIVE_SANDBOX_H #include #include #include #include #include #include #include #include #include #include #if defined(__linux__) #include #endif typedef struct v_alpm_saved_signal { struct sigaction action; } v_alpm_saved_signal; typedef struct v_alpm_saved_mask { sigset_t mask; } v_alpm_saved_mask; static int v_alpm_signal_ignore(int number, v_alpm_saved_signal *saved) { struct sigaction action = {.sa_handler = SIG_IGN}; sigemptyset(&action.sa_mask); return sigaction(number, &action, &saved->action); } static int v_alpm_signal_restore(int number, const v_alpm_saved_signal *saved) { return sigaction(number, &saved->action, NULL); } static int v_alpm_signal_default(int number) { struct sigaction action = {.sa_handler = SIG_DFL}; sigemptyset(&action.sa_mask); return sigaction(number, &action, NULL); } static int v_alpm_signal_block(int number, v_alpm_saved_mask *saved) { sigset_t mask; sigemptyset(&mask); sigaddset(&mask, number); return sigprocmask(SIG_BLOCK, &mask, &saved->mask); } static int v_alpm_mask_restore(const v_alpm_saved_mask *saved) { return sigprocmask(SIG_SETMASK, &saved->mask, NULL); } static int v_alpm_pipe_ready(int fd, int timeout) { struct pollfd item = {.fd = fd, .events = POLLIN}; return poll(&item, 1, timeout); } static int v_alpm_wait_child(int child, int32_t *status, int flags) { int result; int value = 0; result = waitpid(child, &value, flags); *status = value; return result; } static int v_alpm_exit_status(int32_t status) { return WIFEXITED(status) ? WEXITSTATUS(status) : -1; } static int v_alpm_term_signal(int32_t status) { return WIFSIGNALED(status) ? WTERMSIG(status) : 0; } static int v_alpm_close_range(unsigned int first, unsigned int last) { #if defined(__linux__) && defined(__NR_close_range) return syscall(__NR_close_range, first, last, 0U); #else errno = ENOSYS; return -1; #endif } static int v_alpm_duplicate_pipe(int fd) { return fcntl(fd, F_DUPFD_CLOEXEC, 3); } static int v_alpm_callback_pipe(int *fds) { #if defined(__linux__) return pipe2(fds, O_CLOEXEC); #else if(pipe(fds) != 0) { return -1; } if(fcntl(fds[0], F_SETFD, FD_CLOEXEC) < 0 || fcntl(fds[1], F_SETFD, FD_CLOEXEC) < 0) { int saved = errno; close(fds[0]); close(fds[1]); errno = saved; return -1; } return 0; #endif } #if defined(__linux__) #include #include #include #include /* typed access to variadic syscall/seccomp interfaces and packed kernel records. */ static int v_alpm_no_new_privileges(void) { return prctl(PR_SET_NO_NEW_PRIVS, 1L, 0L, 0L, 0L); } static int v_alpm_landlock_abi(void) { #ifdef __NR_landlock_create_ruleset return syscall(__NR_landlock_create_ruleset, NULL, 0, 1U); #else errno = ENOSYS; return -1; #endif } static int v_alpm_landlock_create(uint64_t access) { #ifdef __NR_landlock_create_ruleset struct { uint64_t handled_access_fs; } attr = {access}; return syscall(__NR_landlock_create_ruleset, &attr, sizeof(attr), 0U); #else errno = ENOSYS; return -1; #endif } static int v_alpm_landlock_path(int ruleset, int directory, uint64_t access) { #ifdef __NR_landlock_add_rule struct __attribute__((packed)) { uint64_t allowed_access; int32_t parent_fd; } attr = {access, directory}; return syscall(__NR_landlock_add_rule, ruleset, 1, &attr, 0U); #else errno = ENOSYS; return -1; #endif } static int v_alpm_landlock_apply(int ruleset) { #ifdef __NR_landlock_restrict_self return syscall(__NR_landlock_restrict_self, ruleset, 0U); #else errno = ENOSYS; return -1; #endif } static int v_alpm_seccomp_deny(void *context, int number, int error) { return seccomp_rule_add(context, SCMP_ACT_ERRNO(error), number, 0U); } #endif static int v_alpm_reflink(int destination, int source) { #if defined(__linux__) && defined(FICLONE) return ioctl(destination, FICLONE, source); #else errno = ENOTSUP; return -1; #endif } static int v_alpm_copy_file_times(int destination, int source) { struct stat info; struct timespec times[2]; if(fstat(source, &info) != 0) { return -1; } #if defined(__APPLE__) times[0] = info.st_atimespec; times[1] = info.st_mtimespec; #else times[0] = info.st_atim; times[1] = info.st_mtim; #endif return futimens(destination, times); } #endif