nspkg / engine / native_sandbox.h
153 lines · 149 sloc · 4.56 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2#ifndef V_ALPM_NATIVE_SANDBOX_H
3#define V_ALPM_NATIVE_SANDBOX_H
4#include <stdint.h>
5#include <errno.h>
6#include <unistd.h>
7#include <grp.h>
8#include <fcntl.h>
9#include <poll.h>
10#include <signal.h>
11#include <sys/stat.h>
12#include <sys/wait.h>
13#include <sys/ioctl.h>
14#if defined(__linux__)
15#include <sys/syscall.h>
16#endif
17
18typedef struct v_alpm_saved_signal { struct sigaction action; } v_alpm_saved_signal;
19typedef struct v_alpm_saved_mask { sigset_t mask; } v_alpm_saved_mask;
20static int v_alpm_signal_ignore(int number, v_alpm_saved_signal *saved) {
21 struct sigaction action = {.sa_handler = SIG_IGN};
22 sigemptyset(&action.sa_mask);
23 return sigaction(number, &action, &saved->action);
24}
25static int v_alpm_signal_restore(int number, const v_alpm_saved_signal *saved) {
26 return sigaction(number, &saved->action, NULL);
27}
28static int v_alpm_signal_default(int number) {
29 struct sigaction action = {.sa_handler = SIG_DFL};
30 sigemptyset(&action.sa_mask);
31 return sigaction(number, &action, NULL);
32}
33static int v_alpm_signal_block(int number, v_alpm_saved_mask *saved) {
34 sigset_t mask;
35 sigemptyset(&mask);
36 sigaddset(&mask, number);
37 return sigprocmask(SIG_BLOCK, &mask, &saved->mask);
38}
39static int v_alpm_mask_restore(const v_alpm_saved_mask *saved) {
40 return sigprocmask(SIG_SETMASK, &saved->mask, NULL);
41}
42static int v_alpm_pipe_ready(int fd, int timeout) {
43 struct pollfd item = {.fd = fd, .events = POLLIN};
44 return poll(&item, 1, timeout);
45}
46static int v_alpm_wait_child(int child, int32_t *status, int flags) {
47 int result;
48 int value = 0;
49 result = waitpid(child, &value, flags);
50 *status = value;
51 return result;
52}
53static int v_alpm_exit_status(int32_t status) { return WIFEXITED(status) ? WEXITSTATUS(status) : -1; }
54static int v_alpm_term_signal(int32_t status) { return WIFSIGNALED(status) ? WTERMSIG(status) : 0; }
55
56static int v_alpm_close_range(unsigned int first, unsigned int last) {
57#if defined(__linux__) && defined(__NR_close_range)
58 return syscall(__NR_close_range, first, last, 0U);
59#else
60 errno = ENOSYS;
61 return -1;
62#endif
63}
64static int v_alpm_duplicate_pipe(int fd) { return fcntl(fd, F_DUPFD_CLOEXEC, 3); }
65static int v_alpm_callback_pipe(int *fds) {
66#if defined(__linux__)
67 return pipe2(fds, O_CLOEXEC);
68#else
69 if(pipe(fds) != 0) { return -1; }
70 if(fcntl(fds[0], F_SETFD, FD_CLOEXEC) < 0 || fcntl(fds[1], F_SETFD, FD_CLOEXEC) < 0) {
71 int saved = errno;
72 close(fds[0]);
73 close(fds[1]);
74 errno = saved;
75 return -1;
76 }
77 return 0;
78#endif
79}
80
81#if defined(__linux__)
82#include <sys/prctl.h>
83#include <sys/syscall.h>
84#include <seccomp.h>
85#include <linux/fs.h>
86
87/* typed access to variadic syscall/seccomp interfaces and packed kernel records. */
88static int v_alpm_no_new_privileges(void) {
89 return prctl(PR_SET_NO_NEW_PRIVS, 1L, 0L, 0L, 0L);
90}
91static int v_alpm_landlock_abi(void) {
92#ifdef __NR_landlock_create_ruleset
93 return syscall(__NR_landlock_create_ruleset, NULL, 0, 1U);
94#else
95 errno = ENOSYS;
96 return -1;
97#endif
98}
99static int v_alpm_landlock_create(uint64_t access) {
100#ifdef __NR_landlock_create_ruleset
101 struct { uint64_t handled_access_fs; } attr = {access};
102 return syscall(__NR_landlock_create_ruleset, &attr, sizeof(attr), 0U);
103#else
104 errno = ENOSYS;
105 return -1;
106#endif
107}
108static int v_alpm_landlock_path(int ruleset, int directory, uint64_t access) {
109#ifdef __NR_landlock_add_rule
110 struct __attribute__((packed)) {
111 uint64_t allowed_access;
112 int32_t parent_fd;
113 } attr = {access, directory};
114 return syscall(__NR_landlock_add_rule, ruleset, 1, &attr, 0U);
115#else
116 errno = ENOSYS;
117 return -1;
118#endif
119}
120static int v_alpm_landlock_apply(int ruleset) {
121#ifdef __NR_landlock_restrict_self
122 return syscall(__NR_landlock_restrict_self, ruleset, 0U);
123#else
124 errno = ENOSYS;
125 return -1;
126#endif
127}
128static int v_alpm_seccomp_deny(void *context, int number, int error) {
129 return seccomp_rule_add(context, SCMP_ACT_ERRNO(error), number, 0U);
130}
131#endif
132static int v_alpm_reflink(int destination, int source) {
133#if defined(__linux__) && defined(FICLONE)
134 return ioctl(destination, FICLONE, source);
135#else
136 errno = ENOTSUP;
137 return -1;
138#endif
139}
140static int v_alpm_copy_file_times(int destination, int source) {
141 struct stat info;
142 struct timespec times[2];
143 if(fstat(source, &info) != 0) { return -1; }
144#if defined(__APPLE__)
145 times[0] = info.st_atimespec;
146 times[1] = info.st_mtimespec;
147#else
148 times[0] = info.st_atim;
149 times[1] = info.st_mtim;
150#endif
151 return futimens(destination, times);
152}
153#endif
154