-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandboxed-prefix.c
393 lines (383 loc) · 21.8 KB
/
sandboxed-prefix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#define _GNU_SOURCE
#define USE_APPARMOR
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <seccomp.h>
#include <string.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <linux/prctl.h>
#include <linux/landlock.h>
#ifdef USE_APPARMOR
#include <sys/apparmor.h>
#endif
#include <errno.h>
int landlock_create_ruleset(struct landlock_ruleset_attr *rsattr,size_t size,__u32 flags) {
return syscall(__NR_landlock_create_ruleset,rsattr,size,flags);
}
int landlock_add_rule(int fd,enum landlock_rule_type t,void *attr,__u32 flags) {
return syscall(__NR_landlock_add_rule,fd,t,attr,flags);
}
int landlock_restrict_self(int fd,__u32 flags) {
return syscall(__NR_landlock_restrict_self,fd,flags);
}
int add_read_access_rule(int rset_fd,int allowed_fd) {
int result;
struct landlock_path_beneath_attr target;
target.parent_fd = allowed_fd;
target.allowed_access = LANDLOCK_ACCESS_FS_READ_FILE | LANDLOCK_ACCESS_FS_READ_DIR;
result = landlock_add_rule(rset_fd,LANDLOCK_RULE_PATH_BENEATH,&target,0);
return result;
}
int add_write_access_rule(int rset_fd,int allowed_fd,int restricted) {
int result;
struct landlock_path_beneath_attr target;
target.parent_fd = allowed_fd;
if (restricted==0) target.allowed_access = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SOCK | LANDLOCK_ACCESS_FS_MAKE_FIFO | LANDLOCK_ACCESS_FS_MAKE_BLOCK | LANDLOCK_ACCESS_FS_MAKE_SYM;
else if (restricted==1) target.allowed_access = LANDLOCK_ACCESS_FS_WRITE_FILE | LANDLOCK_ACCESS_FS_REMOVE_FILE | LANDLOCK_ACCESS_FS_REMOVE_DIR | LANDLOCK_ACCESS_FS_MAKE_CHAR | LANDLOCK_ACCESS_FS_MAKE_DIR | LANDLOCK_ACCESS_FS_MAKE_REG | LANDLOCK_ACCESS_FS_MAKE_SYM;
result = landlock_add_rule(rset_fd,LANDLOCK_RULE_PATH_BENEATH,&target,0);
return result;
}
int add_execute_rule(int rset_fd,int allowed_fd) {
int result;
struct landlock_path_beneath_attr target;
target.parent_fd = allowed_fd;
target.allowed_access = LANDLOCK_ACCESS_FS_EXECUTE;
result = landlock_add_rule(rset_fd,LANDLOCK_RULE_PATH_BENEATH,&target,0);
return result;
}
void stop_error(int error_class) {
if (error_class==1) {
printf("Failed to add a syscall rule to the filter. Exiting...\n");
exit(3);
}
if (error_class==2) {
printf("Failed to add a path to the ruleset. Exiting...\n");
exit(4);
}
#ifdef USE_APPARMOR
if (error_class==3) {
printf("Failed to schedule an AppArmor profile transition. Exiting...\n");
exit(5);
}
#endif
if (error_class==4) {
printf("Failed to enable the \"No New Privileges\" restriction. Exiting...\n");
exit(6);
}
if (error_class==5) {
printf("Failed to enable Landlock self-restriction. Exiting...\n");
exit(7);
}
if (error_class==6) {
printf("Failed to load the seccomp filter to the kernel. Exiting...\n");
exit(8);
}
printf("Unknown error. Exiting...\n");
exit(1);
}
void populate_landlock_ruleset(int ruleset_fd) {
int current_fd;
current_fd = open("/sys",O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
close(current_fd);
current_fd = open("/run",O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
if (add_write_access_rule(ruleset_fd,current_fd,1)!=0) stop_error(2);
close(current_fd);
current_fd = open("/usr",O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
if (add_execute_rule(ruleset_fd,current_fd)!=0) stop_error(2);
close(current_fd);
current_fd = open("/opt",O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
if (add_execute_rule(ruleset_fd,current_fd)!=0) stop_error(2);
close(current_fd);
current_fd = open("/run",O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
if (add_write_access_rule(ruleset_fd,current_fd,0)!=0) stop_error(2);
close(current_fd);
// Currently the entire /etc is accessible, should be more fine-grained later
current_fd = open("/etc",O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
close(current_fd);
char *wine_dir = secure_getenv("WINEPREFIX");
if (wine_dir == NULL) {
char *home_dir = secure_getenv("HOME");
size_t path_l = strlen(home_dir);
char *wine_def = (char*)malloc((path_l+7)*sizeof(char));
strncpy(wine_def,home_dir,path_l);
strncat(wine_def,"/.wine",7);
current_fd = open(wine_def,O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
if (add_write_access_rule(ruleset_fd,current_fd,1)!=0) stop_error(2);
if (add_execute_rule(ruleset_fd,current_fd)!=0) stop_error(2);
close(current_fd);
}
else {
current_fd = open(wine_dir,O_PATH | O_CLOEXEC);
if (add_read_access_rule(ruleset_fd,current_fd)!=0) stop_error(2);
if (add_write_access_rule(ruleset_fd,current_fd,1)!=0) stop_error(2);
if (add_execute_rule(ruleset_fd,current_fd)!=0) stop_error(2);
close(current_fd);
}
return;
}
void populate_seccomp_filter(scmp_filter_ctx *filter) {
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(read),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(futex),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(poll),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(_newselect),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(select),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(pselect6),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clock_gettime),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(write),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(rt_sigprocmask),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(epoll_wait),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(nanosleep),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(recvmsg),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(writev),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(ioctl),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getpid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getdents64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(lstat64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(lstat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(openat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(stat64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(close),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(epoll_ctl),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fstat64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(mmap2),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(munmap),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(pread64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sched_yield),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(mprotect),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(_llseek),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(lseek),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(readlink),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(access),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sendmsg),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fstatfs),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fstatfs64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fcntl64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(recv),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(pipe2),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getxattr),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(unlink),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(madvise),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(waitpid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getrandom),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(readlinkat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(dup),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fadvise64_64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fadvise64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(tgkill),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fchdir),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(setsockopt),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(rt_sigaction),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(gettid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clone),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(set_robust_list),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(socket),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(brk),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(set_thread_area),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(rt_sigreturn),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sigaltstack),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sched_setaffinity),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(connect),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(socketpair),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(uname),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sysinfo),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(pipe),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(ftruncate64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(send),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(kill),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(prctl),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getuid32),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sendto),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getsockname),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(prlimit64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(umask),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(geteuid32),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getegid32),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(time),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(ftruncate),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(memfd_create),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getgid32),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clock_getres),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(pwrite64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getpeername),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(mkdir),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(shmget),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(chdir),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(open),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(shmat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(bind),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(ugetrlimit),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(set_tid_address),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(accept),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(shutdown),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getcwd),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fstatat64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(dup2),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getsockopt),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(shmdt),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(shmctl),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getresuid32),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getresuid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getresgid32),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getresgid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(timerfd_create),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(execve),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sched_setscheduler),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sched_get_priority_max),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sched_get_priority_min),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sched_getaffinity),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(chmod),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(rename),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(setsid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(symlink),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(epoll_create),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(listen),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(utimensat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(wait4),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(removexattr),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getppid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(sigreturn),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(faccessat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(faccessat2),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(exit_group),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(exit),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(gettimeofday),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(restart_syscall),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fsync),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fdatasync),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(recvfrom),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(times),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fstat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(stat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getuid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(geteuid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getgid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getegid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(getppid),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(mmap),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fcntl),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(arch_prctl),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clock_getres_time64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clock_nanosleep_time64),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clock_nanosleep),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(newfstatat),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(statx),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(rseq),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(fork),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clone),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(clone3),0)!=0) stop_error(1);
if (seccomp_rule_add(*filter,SCMP_ACT_ALLOW,SCMP_SYS(ptrace),0)!=0) stop_error(1);
return;
}
int main(int argc,char *argv[]) {
if (argc<2) {
#ifdef USE_APPARMOR
printf("Usage: wine-sandbox [--allow-syscall=<syscall>] [--allow-log-syscall=<syscall>] [--disallow-syscall-silent=<syscall>] [--disallow-syscall-failure=<syscall>] [--disallow-syscall-kill=<syscall>] [--allow-path=<path>] [--allow-read-only=<path>] [--wine-binary=<path>] [--apparmor=<profile>] <command-line arguments for Wine>\n");
#endif
#ifndef USE_APPARMOR
printf("Usage: wine-sandbox [--allow-syscall=<syscall>] [--allow-log-syscall=<syscall>] [--disallow-syscall-silent=<syscall>] [--disallow-syscall-failure=<syscall>] [--disallow-syscall-kill=<syscall>] [--allow-path=<path>] [--allow-read-only=<path>] [--wine-binary=<path>] <command-line arguments for Wine>\n");
#endif
return 2;
}
uint32_t d_action;
char *find_mode=secure_getenv("SECCOMP_MODE");
if (find_mode!=NULL && strncmp(find_mode,"SILENT",6)==0) d_action=SCMP_ACT_ERRNO(0);
else if (find_mode!=NULL && strncmp(find_mode,"ERROR",5)==0) d_action=SCMP_ACT_ERRNO(-EDEADLOCK);
else if (find_mode!=NULL && strncmp(find_mode,"LOG",3)==0) d_action=SCMP_ACT_LOG;
else d_action=SCMP_ACT_KILL;
scmp_filter_ctx syscall_filter = seccomp_init(d_action);
unsetenv("SECCOMP_MODE");
struct landlock_ruleset_attr current_attr;
current_attr.handled_access_fs = 8191;
seccomp_arch_add(syscall_filter,SCMP_ARCH_X86);
int path_ruleset = landlock_create_ruleset(¤t_attr,sizeof(current_attr),0);
char *new_arg[argc];
new_arg[0] = "wine\0";
int new_arg_count = 1;
int def_bin = 1;
int bin_arg = -1;
int opt_nnp = 1;
int opt_seccomp = 1;
int opt_landlock = 1;
char wine_bin[50] = "/usr/bin/wine";
populate_landlock_ruleset(path_ruleset);
populate_seccomp_filter(&syscall_filter);
for (int i=1;i<argc;i++) {
if (strncmp(argv[i],"--allow-syscall=",16)==0) {
if (seccomp_rule_add(syscall_filter,SCMP_ACT_ALLOW,seccomp_syscall_resolve_name(&argv[i][16]),0)!=0) stop_error(1);
}
else if (strncmp(argv[i],"--allow-log-syscall=",20)==0) {
if (seccomp_rule_add(syscall_filter,SCMP_ACT_LOG,seccomp_syscall_resolve_name(&argv[i][16]),0)!=0) stop_error(1);
}
else if (strncmp(argv[i],"--disallow-syscall-silent=",26)==0) {
if (seccomp_rule_add(syscall_filter,SCMP_ACT_ERRNO(0),seccomp_syscall_resolve_name(&argv[i][16]),0)!=0) stop_error(1);
}
else if (strncmp(argv[i],"--disallow-syscall-failure=",27)==0) {
if (seccomp_rule_add(syscall_filter,SCMP_ACT_ERRNO(-EDEADLOCK),seccomp_syscall_resolve_name(&argv[i][16]),0)!=0) stop_error(1);
}
else if (strncmp(argv[i],"--disallow-syscall-kill=",24)==0) {
if (seccomp_rule_add(syscall_filter,SCMP_ACT_KILL,seccomp_syscall_resolve_name(&argv[i][16]),0)!=0) stop_error(1);
}
else if (strncmp(argv[i],"--allow-path=",13)==0) {
int temp_fd = open(&argv[i][13],O_PATH | O_CLOEXEC);
if (add_read_access_rule(path_ruleset,temp_fd)!=0) stop_error(2);
if (add_write_access_rule(path_ruleset,temp_fd,1)!=0) stop_error(2);
close(temp_fd);
}
else if (strncmp(argv[i],"--allow-read-only-path=",21)==0) {
int temp_fd = open(&argv[i][21],O_PATH | O_CLOEXEC);
if (add_read_access_rule(path_ruleset,temp_fd)!=0) stop_error(2);
close(temp_fd);
}
else if (strncmp(argv[i],"--wine-binary=",14)==0) {
def_bin = 0;
bin_arg = i;
}
else if (strncmp(argv[i],"--skip-nnp",11)==0) {
opt_nnp = 0;
}
else if (strncmp(argv[i],"--skip-seccomp",15)==0) {
opt_seccomp = 0;
}
else if (strncmp(argv[i],"--skip-landlock",16)==0) {
opt_landlock = 0;
}
#ifdef USE_APPARMOR
else if (strncmp(argv[i],"--apparmor=",11)==0) {
if (aa_stack_onexec(argv[11])!=0) stop_error(3);
}
#endif
else {
new_arg[new_arg_count]=argv[i];
new_arg_count++;
}
}
// printf("%d\n",argc);
new_arg[new_arg_count+1] = NULL;
// Debug code
// for (int i=0;i<new_arg_count;i++) printf("%s\n",new_arg[i]);
if (opt_nnp == 1) {
if (prctl(PR_SET_NO_NEW_PRIVS,1,0,0,0)<0) stop_error(4);
}
if (opt_landlock == 1) {
if (landlock_restrict_self(path_ruleset,0)!=0) stop_error(5);
}
if (opt_seccomp == 1) {
if (seccomp_load(syscall_filter)!=0) stop_error(6);
}
if (def_bin==1) execve(wine_bin,new_arg,environ);
else execve(argv[bin_arg][14],new_arg,environ);
stop_error(7);
return 124;
}