forked from dagwieers/vsftpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seccompsandbox.c
703 lines (640 loc) · 15.3 KB
/
seccompsandbox.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
* Part of Very Secure FTPd
* Licence: GPL v2
* Author: Chris Evans
* seccompsandbox.c
*
* Code to lock down the accessible kernel API in a Linux seccomp filter
* sandbox. Works in Ubuntu 11.10 and newer.
*/
#include "seccompsandbox.h"
#if defined(__linux__) && defined(__x86_64__)
#include "session.h"
#include "sysutil.h"
#include "tunables.h"
#include "utility.h"
#include <errno.h>
#include <netinet/in.h>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/filter.h>
#include <asm/unistd.h>
/* #define DEBUG_SIGSYS 1 */
#ifndef PR_SET_SECCOMP
#define PR_SET_SECCOMP 22
#endif
#ifndef PR_SET_NO_NEW_PRIVS
#define PR_SET_NO_NEW_PRIVS 38
#endif
#ifndef __NR_openat
#define __NR_openat 257
#endif
#ifndef O_LARGEFILE
#define O_LARGEFILE 00100000
#endif
#ifndef O_DIRECTORY
#define O_DIRECTORY 00200000
#endif
#ifndef O_CLOEXEC
#define O_CLOEXEC 002000000
#endif
#define kMaxSyscalls 100
#ifdef DEBUG_SIGSYS
#include <signal.h>
#include <string.h>
void
handle_sigsys(int sig)
{
(void) sig;
}
#endif
static const int kOpenFlags =
O_CREAT|O_EXCL|O_APPEND|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_LARGEFILE;
static size_t s_syscall_index;
static size_t s_1_arg_validations;
static size_t s_2_arg_validations;
static size_t s_3_arg_validations;
static int s_syscalls[kMaxSyscalls];
static int s_errnos[kMaxSyscalls];
static int s_args_1[kMaxSyscalls];
static int s_vals_1[kMaxSyscalls];
static int s_args_2[kMaxSyscalls];
static int s_vals_2[kMaxSyscalls];
static int s_args_3[kMaxSyscalls];
static int s_vals_3[kMaxSyscalls];
static void
allow_nr(int nr)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
s_errnos[s_syscall_index] = 0;
s_syscalls[s_syscall_index++] = nr;
}
static void
reject_nr(int nr, int errcode)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
if (errcode < 0 || errcode > 255)
{
bug("bad errcode");
}
s_errnos[s_syscall_index] = errcode;
s_syscalls[s_syscall_index++] = nr;
}
static void
allow_nr_1_arg_match(int nr, int arg, int val)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
if (arg < 1 || arg > 6)
{
bug("arg out of range");
}
s_args_1[s_syscall_index] = arg;
s_vals_1[s_syscall_index] = val;
s_errnos[s_syscall_index] = 0;
s_syscalls[s_syscall_index++] = nr;
s_1_arg_validations++;
}
static void
allow_nr_1_arg_mask(int nr, int arg, int val)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
if (arg < 1 || arg > 6)
{
bug("arg out of range");
}
s_args_1[s_syscall_index] = 100 + arg;
s_vals_1[s_syscall_index] = val;
s_errnos[s_syscall_index] = 0;
s_syscalls[s_syscall_index++] = nr;
s_1_arg_validations++;
}
static void
allow_nr_2_arg_match(int nr, int arg1, int val1, int arg2, int val2)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
if (arg1 < 1 || arg1 > 6)
{
bug("arg1 out of range");
}
if (arg2 < 1 || arg2 > 6)
{
bug("arg2 out of range");
}
s_args_1[s_syscall_index] = arg1;
s_vals_1[s_syscall_index] = val1;
s_args_2[s_syscall_index] = arg2;
s_vals_2[s_syscall_index] = val2;
s_errnos[s_syscall_index] = 0;
s_syscalls[s_syscall_index++] = nr;
s_2_arg_validations++;
}
static void
allow_nr_2_arg_mask_match(int nr, int arg1, int val1, int arg2, int val2)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
if (arg1 < 1 || arg1 > 6)
{
bug("arg1 out of range");
}
if (arg2 < 1 || arg2 > 6)
{
bug("arg2 out of range");
}
s_args_1[s_syscall_index] = 100 + arg1;
s_vals_1[s_syscall_index] = val1;
s_args_2[s_syscall_index] = arg2;
s_vals_2[s_syscall_index] = val2;
s_errnos[s_syscall_index] = 0;
s_syscalls[s_syscall_index++] = nr;
s_2_arg_validations++;
}
static void
allow_nr_3_arg_match(int nr, int arg1, int val1, int arg2, int val2, int arg3,
int val3)
{
if (s_syscall_index >= kMaxSyscalls)
{
bug("out of syscall space");
}
if (nr < 0)
{
bug("negative syscall");
}
if (arg1 < 1 || arg1 > 6)
{
bug("arg1 out of range");
}
if (arg2 < 1 || arg2 > 6)
{
bug("arg2 out of range");
}
if (arg3 < 1 || arg3 > 6)
{
bug("arg3 out of range");
}
s_args_1[s_syscall_index] = arg1;
s_vals_1[s_syscall_index] = val1;
s_args_2[s_syscall_index] = arg2;
s_vals_2[s_syscall_index] = val2;
s_args_3[s_syscall_index] = arg3;
s_vals_3[s_syscall_index] = val3;
s_errnos[s_syscall_index] = 0;
s_syscalls[s_syscall_index++] = nr;
s_3_arg_validations++;
}
static void
seccomp_sandbox_setup_data_connections()
{
allow_nr_3_arg_match(__NR_socket, 1, PF_INET, 2, SOCK_STREAM, 3, IPPROTO_TCP);
allow_nr_3_arg_match(__NR_socket,
1, PF_INET6,
2, SOCK_STREAM,
3, IPPROTO_TCP);
allow_nr(__NR_bind);
allow_nr(__NR_select);
if (tunable_port_enable)
{
allow_nr(__NR_connect);
allow_nr_2_arg_match(__NR_getsockopt, 2, SOL_SOCKET, 3, SO_ERROR);
allow_nr_2_arg_match(__NR_setsockopt, 2, SOL_SOCKET, 3, SO_REUSEADDR);
allow_nr_1_arg_match(__NR_fcntl, 2, F_GETFL);
allow_nr_2_arg_match(__NR_fcntl, 2, F_SETFL, 3, O_RDWR|O_NONBLOCK);
allow_nr_2_arg_match(__NR_fcntl, 2, F_SETFL, 3, O_RDWR);
}
if (tunable_pasv_enable)
{
allow_nr(__NR_listen);
allow_nr(__NR_accept);
}
}
static void
seccomp_sandbox_setup_base()
{
/* Simple reads and writes on existing descriptors. */
allow_nr(__NR_read);
allow_nr(__NR_write);
/* Needed for memory management. */
allow_nr_2_arg_match(__NR_mmap,
3, PROT_READ|PROT_WRITE,
4, MAP_PRIVATE|MAP_ANON);
allow_nr_1_arg_mask(__NR_mprotect, 3, PROT_READ);
allow_nr(__NR_munmap);
allow_nr(__NR_brk);
/* glibc falls back gracefully if mremap() fails during realloc(). */
reject_nr(__NR_mremap, ENOSYS);
/* Misc simple low-risk calls. */
allow_nr(__NR_rt_sigreturn); /* Used to handle SIGPIPE. */
allow_nr(__NR_restart_syscall);
allow_nr(__NR_close);
/* Always need to be able to exit ! */
allow_nr(__NR_exit_group);
}
void
seccomp_sandbox_init()
{
if (s_syscall_index != 0)
{
bug("bad state in seccomp_sandbox_init");
}
}
void
seccomp_sandbox_setup_prelogin(const struct vsf_session* p_sess)
{
(void) p_sess;
seccomp_sandbox_setup_base();
/* Peeking FTP commands from the network. */
allow_nr_1_arg_match(__NR_recvfrom, 4, MSG_PEEK);
/* Misc simple low-risk calls */
allow_nr(__NR_nanosleep); /* Used for bandwidth / login throttling. */
allow_nr(__NR_getpid); /* Used by logging. */
allow_nr(__NR_shutdown); /* Used for QUIT or a timeout. */
allow_nr_1_arg_match(__NR_fcntl, 2, F_GETFL);
/* It's safe to allow O_RDWR in fcntl because these flags cannot be changed.
* Also, sockets are O_RDWR.
*/
allow_nr_2_arg_mask_match(__NR_fcntl, 3, kOpenFlags|O_ACCMODE, 2, F_SETFL);
/* Config-dependent items follow. */
if (tunable_idle_session_timeout > 0)
{
allow_nr(__NR_rt_sigaction);
allow_nr(__NR_alarm);
}
if (tunable_xferlog_enable || tunable_dual_log_enable)
{
/* For file locking. */
allow_nr_1_arg_match(__NR_fcntl, 2, F_SETLKW);
allow_nr_1_arg_match(__NR_fcntl, 2, F_SETLK);
}
if (tunable_ssl_enable)
{
allow_nr_1_arg_match(__NR_recvmsg, 3, 0);
}
}
void
seccomp_sandbox_setup_postlogin(const struct vsf_session* p_sess)
{
int is_anon = p_sess->is_anonymous;
int open_flag = kOpenFlags;
if (tunable_write_enable)
{
open_flag |= O_ACCMODE;
}
/* Put lstat() first because it is a very hot syscall for large directory
* listings. And the current BPF only allows a linear scan of allowed
* syscalls.
*/
allow_nr(__NR_lstat);
/* Allow all the simple pre-login things and then expand upon them. */
seccomp_sandbox_setup_prelogin(p_sess);
/* Simple file descriptor-based operations. */
if (tunable_xferlog_enable || tunable_dual_log_enable ||
tunable_lock_upload_files)
{
allow_nr_1_arg_match(__NR_fcntl, 2, F_SETLKW);
allow_nr_1_arg_match(__NR_fcntl, 2, F_SETLK);
}
if (tunable_async_abor_enable)
{
allow_nr_2_arg_match(__NR_fcntl, 2, F_SETOWN, 3, vsf_sysutil_getpid());
}
allow_nr_2_arg_match(__NR_setsockopt, 2, SOL_SOCKET, 3, SO_KEEPALIVE);
allow_nr_2_arg_match(__NR_setsockopt, 2, SOL_SOCKET, 3, SO_LINGER);
allow_nr_2_arg_match(__NR_setsockopt, 2, IPPROTO_IP, 3, IP_TOS);
allow_nr(__NR_fstat);
allow_nr(__NR_lseek);
/* Since we use chroot() to restrict filesystem access, we can just blanket
* allow open().
*/
allow_nr_1_arg_mask(__NR_open, 2, open_flag);
allow_nr_1_arg_mask(__NR_openat, 3, open_flag);
/* Other pathname-based metadata queries. */
allow_nr(__NR_stat);
allow_nr(__NR_readlink);
/* Directory handling: query, change, read. */
allow_nr(__NR_getcwd);
allow_nr(__NR_chdir);
allow_nr(__NR_getdents);
/* Misc */
allow_nr(__NR_umask);
/* Config-dependent items follow. */
if (tunable_use_sendfile)
{
allow_nr(__NR_sendfile);
}
if (tunable_idle_session_timeout > 0 ||
tunable_data_connection_timeout > 0 ||
tunable_async_abor_enable)
{
allow_nr(__NR_rt_sigaction);
}
if (tunable_idle_session_timeout > 0 || tunable_data_connection_timeout > 0)
{
allow_nr(__NR_alarm);
}
if (tunable_one_process_model)
{
seccomp_sandbox_setup_data_connections();
if (is_anon && tunable_chown_uploads)
{
allow_nr(__NR_fchmod);
allow_nr(__NR_fchown);
}
}
else
{
/* Need to receieve file descriptors from privileged broker. */
allow_nr_1_arg_match(__NR_recvmsg, 3, 0);
if ((is_anon && tunable_chown_uploads) || tunable_ssl_enable)
{
/* Need to send file descriptors to privileged broker. */
allow_nr_1_arg_match(__NR_sendmsg, 3, 0);
}
}
if (tunable_text_userdb_names)
{
reject_nr(__NR_socket, EACCES);
allow_nr_2_arg_match(__NR_mmap, 3, PROT_READ, 4, MAP_SHARED);
}
if (tunable_write_enable)
{
if (!is_anon || tunable_anon_mkdir_write_enable)
{
allow_nr(__NR_mkdir);
}
if (!is_anon ||
tunable_anon_other_write_enable ||
tunable_delete_failed_uploads)
{
allow_nr(__NR_unlink);
}
if (!is_anon || tunable_anon_other_write_enable)
{
allow_nr(__NR_rmdir);
allow_nr(__NR_rename);
allow_nr(__NR_ftruncate);
if (tunable_mdtm_write)
{
allow_nr(__NR_utime);
allow_nr(__NR_utimes);
}
}
if (!is_anon && tunable_chmod_enable)
{
allow_nr(__NR_chmod);
}
}
}
void
seccomp_sandbox_setup_postlogin_broker()
{
seccomp_sandbox_setup_base();
seccomp_sandbox_setup_data_connections();
allow_nr_1_arg_match(__NR_sendmsg, 3, 0);
}
void
seccomp_sandbox_lockdown()
{
size_t len = (s_syscall_index * 2) +
(s_1_arg_validations * 3) +
(s_2_arg_validations * 5) +
(s_3_arg_validations * 7) +
5;
struct sock_filter filters[len];
struct sock_filter* p_filter = filters;
struct sock_fprog prog;
size_t i;
int ret;
prog.len = len;
prog.filter = filters;
/* Validate the syscall architecture. */
p_filter->code = BPF_LD+BPF_W+BPF_ABS;
p_filter->jt = 0;
p_filter->jf = 0;
/* Offset 4 for syscall architecture. */
p_filter->k = 4;
p_filter++;
p_filter->code = BPF_JMP+BPF_JEQ+BPF_K;
p_filter->jt = 1;
p_filter->jf = 0;
/* AUDIT_ARCH_X86_64 */
p_filter->k = 0xc000003e;
p_filter++;
p_filter->code = BPF_RET+BPF_K;
p_filter->jt = 0;
p_filter->jf = 0;
/* SECCOMP_RET_KILL */
p_filter->k = 0;
p_filter++;
/* Load the syscall number. */
p_filter->code = BPF_LD+BPF_W+BPF_ABS;
p_filter->jt = 0;
p_filter->jf = 0;
/* Offset 0 for syscall number. */
p_filter->k = 0;
p_filter++;
for (i = 0; i < s_syscall_index; ++i)
{
int block_size = 1;
if (s_args_3[i])
{
block_size = 8;
}
else if (s_args_2[i])
{
block_size = 6;
}
else if (s_args_1[i])
{
block_size = 4;
}
/* Check for syscall number match. */
p_filter->code = BPF_JMP+BPF_JEQ+BPF_K;
p_filter->jt = 0;
p_filter->jf = block_size;
p_filter->k = s_syscalls[i];
p_filter++;
/* Check argument matches if necessary. */
if (s_args_3[i])
{
p_filter->code = BPF_LD+BPF_W+BPF_ABS;
p_filter->jt = 0;
p_filter->jf = 0;
p_filter->k = 16 + ((s_args_3[i] - 1) * 8);
p_filter++;
p_filter->code = BPF_JMP+BPF_JEQ+BPF_K;
p_filter->jt = 0;
p_filter->jf = 5;
p_filter->k = s_vals_3[i];
p_filter++;
}
if (s_args_2[i])
{
p_filter->code = BPF_LD+BPF_W+BPF_ABS;
p_filter->jt = 0;
p_filter->jf = 0;
p_filter->k = 16 + ((s_args_2[i] - 1) * 8);
p_filter++;
p_filter->code = BPF_JMP+BPF_JEQ+BPF_K;
p_filter->jt = 0;
p_filter->jf = 3;
p_filter->k = s_vals_2[i];
p_filter++;
}
if (s_args_1[i])
{
int arg = s_args_1[i];
int code = BPF_JMP+BPF_JEQ+BPF_K;
int val = s_vals_1[i];
int jt = 0;
int jf = 1;
if (arg > 100)
{
arg -= 100;
code = BPF_JMP+BPF_JSET+BPF_K;
val = ~val;
jt = 1;
jf = 0;
}
p_filter->code = BPF_LD+BPF_W+BPF_ABS;
p_filter->jt = 0;
p_filter->jf = 0;
p_filter->k = 16 + ((arg - 1) * 8);
p_filter++;
p_filter->code = code;
p_filter->jt = jt;
p_filter->jf = jf;
p_filter->k = val;
p_filter++;
}
p_filter->code = BPF_RET+BPF_K;
p_filter->jt = 0;
p_filter->jf = 0;
if (!s_errnos[i])
{
/* SECCOMP_RET_ALLOW */
p_filter->k = 0x7fff0000;
}
else
{
/* SECCOMP_RET_ERRNO */
p_filter->k = 0x00050000 + s_errnos[i];
}
p_filter++;
if (s_args_1[i])
{
/* We trashed the accumulator so put it back. */
p_filter->code = BPF_LD+BPF_W+BPF_ABS;
p_filter->jt = 0;
p_filter->jf = 0;
p_filter->k = 0;
p_filter++;
}
}
/* No "allow" matches so kill. */
p_filter->code = BPF_RET+BPF_K;
p_filter->jt = 0;
p_filter->jf = 0;
#ifdef DEBUG_SIGSYS
/* SECCOMP_RET_TRAP */
p_filter->k = 0x00030000;
#else
/* SECCOMP_RET_KILL */
p_filter->k = 0;
#endif
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
if (ret != 0)
{
if (errno == EINVAL)
{
/* Kernel isn't good enough. */
return;
}
die("prctl PR_SET_NO_NEW_PRIVS");
}
if (!tunable_seccomp_sandbox)
{
return;
}
#ifdef DEBUG_SIGSYS
{
struct sigaction sa;
memset(&sa, '\0', sizeof(sa));
sa.sa_handler = handle_sigsys;
sigaction(SIGSYS, &sa, NULL);
}
#endif
ret = prctl(PR_SET_SECCOMP, 2, &prog, 0, 0);
if (ret != 0)
{
die("prctl PR_SET_SECCOMP failed");
}
}
#else /* __linux__ && __x86_64__ */
void
seccomp_sandbox_init()
{
}
void
seccomp_sandbox_setup_prelogin(const struct vsf_session* p_sess)
{
(void) p_sess;
}
void
seccomp_sandbox_setup_postlogin(const struct vsf_session* p_sess)
{
(void) p_sess;
}
void
seccomp_sandbox_setup_postlogin_broker()
{
}
void
seccomp_sandbox_lockdown()
{
}
#endif /* __linux__ && __x86_64__ */