forked from strace/strace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_seccomp.c
768 lines (678 loc) · 19.8 KB
/
filter_seccomp.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
* Copyright (c) 2018 Chen Jingpiao <[email protected]>
* Copyright (c) 2019 Paul Chaignon <[email protected]>
* Copyright (c) 2018-2020 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "defs.h"
#include "ptrace.h"
#include <signal.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <linux/filter.h>
#include "filter_seccomp.h"
#include "number_set.h"
#include "syscall.h"
#include "scno.h"
bool seccomp_filtering;
bool seccomp_before_sysentry;
#ifdef HAVE_LINUX_SECCOMP_H
# include <linux/seccomp.h>
/* PERSONALITY*_AUDIT_ARCH definitions depend on AUDIT_ARCH_* constants. */
# ifdef PERSONALITY0_AUDIT_ARCH
# include <linux/audit.h>
# define XLAT_MACROS_ONLY
# include "xlat/elf_em.h"
# include "xlat/audit_arch.h"
# undef XLAT_MACROS_ONLY
# endif
# ifndef BPF_MAXINSNS
# define BPF_MAXINSNS 4096
# endif
# define JMP_PLACEHOLDER_NEXT ((unsigned char) -1)
# define JMP_PLACEHOLDER_TRACE ((unsigned char) -2)
# define JMP_PLACEHOLDER_ALLOW ((unsigned char) -3)
# define SET_BPF(filter, code, jt, jf, k) \
(*(filter) = (struct sock_filter) { code, jt, jf, k })
# define SET_BPF_STMT(filter, code, k) \
SET_BPF(filter, code, 0, 0, k)
# define SET_BPF_JUMP(filter, code, k, jt, jf) \
SET_BPF(filter, BPF_JMP | code, jt, jf, k)
struct audit_arch_t {
unsigned int arch;
unsigned int flag;
};
static const struct audit_arch_t audit_arch_vec[SUPPORTED_PERSONALITIES] = {
# if SUPPORTED_PERSONALITIES > 1
PERSONALITY0_AUDIT_ARCH,
PERSONALITY1_AUDIT_ARCH,
# if SUPPORTED_PERSONALITIES > 2
PERSONALITY2_AUDIT_ARCH,
# endif
# endif
};
typedef unsigned short (*filter_generator_t)(struct sock_filter *,
bool *overflow);
static unsigned short linear_filter_generator(struct sock_filter *,
bool *overflow);
static unsigned short binary_match_filter_generator(struct sock_filter *,
bool *overflow);
static filter_generator_t filter_generators[] = {
linear_filter_generator,
binary_match_filter_generator,
};
/*
* Keep some margin in seccomp_filter as programs larger than allowed may
* be constructed before we discard them.
*/
static struct sock_filter
filters[ARRAY_SIZE(filter_generators)][2 * BPF_MAXINSNS];
static struct sock_fprog bpf_prog = {
.len = USHRT_MAX,
.filter = NULL,
};
# ifdef HAVE_FORK
static void ATTRIBUTE_NORETURN
check_seccomp_order_do_child(void)
{
static const struct sock_filter filter[] = {
/* return (nr == __NR_gettid) ? RET_TRACE : RET_ALLOW; */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, nr)),
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_gettid, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_TRACE),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
};
static const struct sock_fprog prog = {
.len = ARRAY_SIZE(filter),
.filter = (struct sock_filter *) filter
};
/* Get everything ready before PTRACE_TRACEME. */
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
perror_func_msg_and_die("prctl(PR_SET_NO_NEW_PRIVS, 1");
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0)
perror_func_msg_and_die("prctl(PR_SET_SECCOMP)");
int pid = getpid();
if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) < 0) {
/* Exit with a nonzero exit status. */
perror_func_msg_and_die("PTRACE_TRACEME");
}
GCOV_DUMP;
kill(pid, SIGSTOP);
syscall(__NR_gettid);
_exit(0);
}
static int
check_seccomp_order_tracer(int pid)
{
unsigned int step;
for (step = 0; ; ++step) {
int status;
for (;;) {
long rc = waitpid(pid, &status, 0);
if (rc < 0 && errno == EINTR)
continue;
if (rc == pid)
break;
/* Cannot happen. */
perror_func_msg("#%d: unexpected wait result %ld",
step, rc);
return pid;
}
if (WIFEXITED(status)) {
/* The tracee is no more. */
pid = 0;
int exitstatus = WEXITSTATUS(status);
if (step == 5 && exitstatus == 0) {
seccomp_filtering = true;
} else {
error_func_msg("#%d: unexpected exit status %u",
step, exitstatus);
}
break;
}
if (WIFSIGNALED(status)) {
/* The tracee is no more. */
pid = 0;
error_func_msg("#%d: unexpected signal %u",
step, WTERMSIG(status));
break;
}
if (!WIFSTOPPED(status)) {
/* Cannot happen. */
error_func_msg("#%d: unexpected wait status %#x",
step, status);
break;
}
unsigned int event = (unsigned int) status >> 16;
switch (WSTOPSIG(status)) {
case SIGSTOP:
if (step != 0) {
error_func_msg("#%d: unexpected signal stop",
step);
return pid;
}
if (ptrace(PTRACE_SETOPTIONS, pid, 0L,
PTRACE_O_TRACESYSGOOD|
PTRACE_O_TRACESECCOMP) < 0) {
perror_func_msg("PTRACE_SETOPTIONS");
return pid;
}
break;
case SIGTRAP:
if (event != PTRACE_EVENT_SECCOMP) {
error_func_msg("#%d: unexpected trap %#x",
step, event);
return pid;
}
switch (step) {
case 1: /* Seccomp stop before entering gettid. */
seccomp_before_sysentry = true;
break;
case 2: /* Seccomp stop after entering gettid. */
if (!seccomp_before_sysentry)
break;
ATTRIBUTE_FALLTHROUGH;
default:
error_func_msg("#%d: unexpected seccomp stop",
step);
return pid;
}
break;
case SIGTRAP | 0x80:
switch (step) {
case 3: /* Exiting gettid. */
case 4: /* Entering exit_group. */
break;
case 1: /* Entering gettid before seccomp stop. */
seccomp_before_sysentry = false;
break;
case 2: /* Entering gettid after seccomp stop. */
if (seccomp_before_sysentry)
break;
ATTRIBUTE_FALLTHROUGH;
default:
error_func_msg("#%d: unexpected syscall stop",
step);
return pid;
}
break;
default:
error_func_msg("#%d: unexpected stop signal %#x",
step, WSTOPSIG(status));
return pid;
}
if (ptrace(PTRACE_SYSCALL, pid, 0L, 0L) < 0) {
/* Cannot happen. */
perror_func_msg("#%d: PTRACE_SYSCALL", step);
break;
}
}
return pid;
}
# endif /* HAVE_FORK */
static void
check_seccomp_order(void)
{
seccomp_filtering = false;
/* NOMMU provides no forks necessary for the test. */
# ifdef HAVE_FORK
int pid = fork();
if (pid < 0) {
perror_func_msg("fork");
return;
}
if (pid == 0)
check_seccomp_order_do_child();
pid = check_seccomp_order_tracer(pid);
if (pid) {
kill(pid, SIGKILL);
for (;;) {
long rc = waitpid(pid, NULL, 0);
if (rc < 0 && errno == EINTR)
continue;
break;
}
}
# endif /* HAVE_FORK */
}
static bool
traced_by_seccomp(unsigned int scno, unsigned int p)
{
unsigned int always_trace_flags =
TRACE_INDIRECT_SUBCALL | TRACE_SECCOMP_DEFAULT |
(stack_trace_enabled ? MEMORY_MAPPING_CHANGE : 0);
return sysent_vec[p][scno].sys_flags & always_trace_flags ||
is_number_in_set_array(scno, trace_set, p);
}
static void
replace_jmp_placeholders(unsigned char *jmp_offset, unsigned char jmp_next,
unsigned char jmp_trace, unsigned char jmp_allow)
{
switch (*jmp_offset) {
case JMP_PLACEHOLDER_NEXT:
*jmp_offset = jmp_next;
break;
case JMP_PLACEHOLDER_TRACE:
*jmp_offset = jmp_trace;
break;
case JMP_PLACEHOLDER_ALLOW:
*jmp_offset = jmp_allow;
break;
default:
break;
}
}
static unsigned short
bpf_syscalls_cmp(struct sock_filter *filter,
unsigned int lower, unsigned int upper)
{
if (lower + 1 == upper) {
/* if (nr == lower) return RET_TRACE; */
SET_BPF_JUMP(filter, BPF_JEQ | BPF_K, lower,
JMP_PLACEHOLDER_TRACE, 0);
return 1;
} else {
/* if (nr >= lower && nr < upper) return RET_TRACE; */
SET_BPF_JUMP(filter, BPF_JGE | BPF_K, lower, 0, 1);
SET_BPF_JUMP(filter + 1, BPF_JGE | BPF_K, upper, 0,
JMP_PLACEHOLDER_TRACE);
return 2;
}
}
static unsigned short
linear_filter_generator(struct sock_filter *filter, bool *overflow)
{
/*
* Generated program looks like:
* if (arch == AUDIT_ARCH_A && nr >= flag) {
* if (nr == 59)
* return SECCOMP_RET_TRACE;
* if (nr >= 321 && nr <= 323)
* return SECCOMP_RET_TRACE;
* ...
* return SECCOMP_RET_ALLOW;
* }
* if (arch == AUDIT_ARCH_A) {
* ...
* }
* if (arch == AUDIT_ARCH_B) {
* ...
* }
* return SECCOMP_RET_TRACE;
*/
unsigned short pos = 0;
# if SUPPORTED_PERSONALITIES > 1
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, arch));
# endif
/*
* Personalities are iterated in reverse-order in the BPF program so
* that the x86 case is naturally handled. On x86, the first and third
* personalities have the same arch identifier. The third can be
* distinguished based on its associated syscall flag, so we check it
* first. The only drawback here is that the first personality is more
* common, which may make the BPF program slower to match syscalls on
* average.
*/
for (int p = SUPPORTED_PERSONALITIES - 1; p >= 0; --p) {
unsigned int lower = UINT_MAX;
unsigned short start = pos, end;
# if SUPPORTED_PERSONALITIES > 1
/* if (arch != audit_arch_vec[p].arch) goto next; */
SET_BPF_JUMP(&filter[pos++], BPF_JEQ | BPF_K,
audit_arch_vec[p].arch, 0, JMP_PLACEHOLDER_NEXT);
# endif
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, nr));
# if SUPPORTED_PERSONALITIES > 1
if (audit_arch_vec[p].flag) {
/* if (nr < audit_arch_vec[p].flag) goto next; */
SET_BPF_JUMP(&filter[pos++], BPF_JGE | BPF_K,
audit_arch_vec[p].flag, 2, 0);
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, arch));
SET_BPF_JUMP(&filter[pos++], BPF_JA,
JMP_PLACEHOLDER_NEXT, 0, 0);
}
# endif
for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
if (traced_by_seccomp(i, p)) {
if (lower == UINT_MAX)
lower = i;
continue;
}
if (lower == UINT_MAX)
continue;
pos += bpf_syscalls_cmp(filter + pos,
lower | audit_arch_vec[p].flag,
i | audit_arch_vec[p].flag);
lower = UINT_MAX;
}
if (lower != UINT_MAX)
pos += bpf_syscalls_cmp(filter + pos,
lower | audit_arch_vec[p].flag,
nsyscall_vec[p]
| audit_arch_vec[p].flag);
end = pos;
/* if (nr >= max_nr) return RET_TRACE; */
SET_BPF_JUMP(&filter[pos++], BPF_JGE | BPF_K,
nsyscall_vec[p] | audit_arch_vec[p].flag, 1, 0);
SET_BPF_STMT(&filter[pos++], BPF_RET | BPF_K,
SECCOMP_RET_ALLOW);
SET_BPF_STMT(&filter[pos++], BPF_RET | BPF_K,
SECCOMP_RET_TRACE);
/*
* Within generated BPF programs, the origin and destination of
* jumps are always in the same personality section. The
* largest jump is therefore the jump from the first
* instruction of the section to the last, to skip the
* personality and try to compare .arch to the next
* personality.
* If we have a personality section with more than 255
* instructions, the jump offset will overflow. Such program
* is unlikely to happen, so we simply disable seccomp-filter
* in such a case.
*/
if (pos - start > UCHAR_MAX) {
*overflow = true;
return pos;
}
for (unsigned int i = start; i < end; ++i) {
if (BPF_CLASS(filter[i].code) != BPF_JMP)
continue;
unsigned char jmp_next = pos - i - 1;
unsigned char jmp_trace = pos - i - 2;
unsigned char jmp_allow = pos - i - 3;
replace_jmp_placeholders(&filter[i].jt, jmp_next,
jmp_trace, jmp_allow);
replace_jmp_placeholders(&filter[i].jf, jmp_next,
jmp_trace, jmp_allow);
if (BPF_OP(filter[i].code) == BPF_JA)
filter[i].k = (unsigned int) jmp_next;
}
}
# if SUPPORTED_PERSONALITIES > 1
/* Jumps conditioned on .arch default to this RET_TRACE. */
SET_BPF_STMT(&filter[pos++], BPF_RET | BPF_K, SECCOMP_RET_TRACE);
# endif
return pos;
}
static unsigned short
bpf_syscalls_match(struct sock_filter *filter, unsigned int bitarray,
unsigned int bitarray_idx)
{
if (!bitarray) {
/* return RET_ALLOW; */
SET_BPF_JUMP(filter, BPF_JMP | BPF_JEQ | BPF_K, bitarray_idx,
JMP_PLACEHOLDER_ALLOW, 0);
return 1;
}
if (bitarray == UINT_MAX) {
/* return RET_TRACE; */
SET_BPF_JUMP(filter, BPF_JMP | BPF_JEQ | BPF_K, bitarray_idx,
JMP_PLACEHOLDER_TRACE, 0);
return 1;
}
/*
* if (A == nr / 32)
* return (X & bitarray) ? RET_TRACE : RET_ALLOW;
*/
SET_BPF_JUMP(filter, BPF_JMP | BPF_JEQ | BPF_K, bitarray_idx,
0, 2);
SET_BPF_STMT(filter + 1, BPF_MISC | BPF_TXA, 0);
SET_BPF_JUMP(filter + 2, BPF_JMP | BPF_JSET | BPF_K, bitarray,
JMP_PLACEHOLDER_TRACE, JMP_PLACEHOLDER_ALLOW);
return 3;
}
static unsigned short
binary_match_filter_generator(struct sock_filter *filter, bool *overflow)
{
unsigned short pos = 0;
# if SUPPORTED_PERSONALITIES > 1
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, arch));
# endif
/* Personalities are iterated in reverse-order in the BPF program so that
* the x86 case is naturally handled. In x86, the first and third
* personalities have the same arch identifier. The third can be
* distinguished based on its associated bit mask, so we check it first.
* The only drawback here is that the first personality is more common,
* which may make the BPF program slower to match syscalls on average. */
for (int p = SUPPORTED_PERSONALITIES - 1;
p >= 0 && pos <= BPF_MAXINSNS;
--p) {
unsigned short start = pos, end;
unsigned int bitarray = 0;
unsigned int i;
# if SUPPORTED_PERSONALITIES > 1
SET_BPF_JUMP(&filter[pos++], BPF_JMP | BPF_JEQ | BPF_K,
audit_arch_vec[p].arch, 0, JMP_PLACEHOLDER_NEXT);
# endif
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, nr));
# if SUPPORTED_PERSONALITIES > 1
if (audit_arch_vec[p].flag) {
SET_BPF_JUMP(&filter[pos++], BPF_JMP | BPF_JGE | BPF_K,
audit_arch_vec[p].flag, 2, 0);
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, arch));
SET_BPF_JUMP(&filter[pos++], BPF_JMP | BPF_JA,
JMP_PLACEHOLDER_NEXT, 0, 0);
/* nr = nr & ~mask */
SET_BPF_STMT(&filter[pos++], BPF_ALU | BPF_AND | BPF_K,
~audit_arch_vec[p].flag);
}
# endif
/* X = 1 << nr % 32 = 1 << nr & 0x1F; */
SET_BPF_STMT(&filter[pos++], BPF_ALU | BPF_AND | BPF_K, 0x1F);
SET_BPF_STMT(&filter[pos++], BPF_MISC | BPF_TAX, 0);
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_IMM, 1);
SET_BPF_STMT(&filter[pos++], BPF_ALU | BPF_LSH | BPF_X, 0);
SET_BPF_STMT(&filter[pos++], BPF_MISC | BPF_TAX, 0);
/* A = nr / 32 = n >> 5; */
SET_BPF_STMT(&filter[pos++], BPF_LD | BPF_W | BPF_ABS,
offsetof(struct seccomp_data, nr));
if (audit_arch_vec[p].flag) {
/* nr = nr & ~mask */
SET_BPF_STMT(&filter[pos++], BPF_ALU | BPF_AND | BPF_K,
~audit_arch_vec[p].flag);
}
SET_BPF_STMT(&filter[pos++], BPF_ALU | BPF_RSH | BPF_K, 5);
for (i = 0; i < nsyscall_vec[p] && pos <= BPF_MAXINSNS; ++i) {
if (traced_by_seccomp(i, p))
bitarray |= (1 << i % 32);
if (i % 32 == 31) {
pos += bpf_syscalls_match(filter + pos,
bitarray, i / 32);
bitarray = 0;
}
}
if (i % 32 != 0)
pos += bpf_syscalls_match(filter + pos, bitarray,
i / 32);
end = pos;
SET_BPF_STMT(&filter[pos++], BPF_RET | BPF_K,
SECCOMP_RET_ALLOW);
SET_BPF_STMT(&filter[pos++], BPF_RET | BPF_K,
SECCOMP_RET_TRACE);
if (pos - start > UCHAR_MAX) {
*overflow = true;
return pos;
}
for (unsigned int i = start; i < end; ++i) {
if (BPF_CLASS(filter[i].code) != BPF_JMP)
continue;
unsigned char jmp_next = pos - i - 1;
unsigned char jmp_trace = pos - i - 2;
unsigned char jmp_allow = pos - i - 3;
replace_jmp_placeholders(&filter[i].jt, jmp_next,
jmp_trace, jmp_allow);
replace_jmp_placeholders(&filter[i].jf, jmp_next,
jmp_trace, jmp_allow);
if (BPF_OP(filter[i].code) == BPF_JA)
filter[i].k = (unsigned int)jmp_next;
}
}
# if SUPPORTED_PERSONALITIES > 1
SET_BPF_STMT(&filter[pos++], BPF_RET | BPF_K, SECCOMP_RET_TRACE);
# endif
return pos;
}
static void
check_seccomp_filter_properties(void)
{
int rc = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, 0, 0);
seccomp_filtering = rc < 0 && errno != EINVAL;
if (!seccomp_filtering) {
debug_func_perror_msg("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
return;
}
for (unsigned int i = 0; i < ARRAY_SIZE(filter_generators); ++i) {
bool overflow = false;
unsigned short len = filter_generators[i](filters[i],
&overflow);
if (len < bpf_prog.len && !overflow) {
bpf_prog.len = len;
bpf_prog.filter = filters[i];
}
}
if (bpf_prog.len == USHRT_MAX) {
debug_msg("seccomp filter disabled due to jump offset "
"overflow");
seccomp_filtering = false;
} else if (bpf_prog.len > BPF_MAXINSNS) {
debug_msg("seccomp filter disabled due to BPF program "
"being oversized (%u > %d)", bpf_prog.len,
BPF_MAXINSNS);
seccomp_filtering = false;
}
if (seccomp_filtering)
check_seccomp_order();
}
static void
dump_seccomp_bpf(void)
{
const struct sock_filter *filter = bpf_prog.filter;
for (unsigned int i = 0; i < bpf_prog.len; ++i) {
switch (filter[i].code) {
case BPF_LD | BPF_W | BPF_ABS:
switch (filter[i].k) {
case offsetof(struct seccomp_data, arch):
error_msg("STMT(BPF_LDWABS, data->arch)");
break;
case offsetof(struct seccomp_data, nr):
error_msg("STMT(BPF_LDWABS, data->nr)");
break;
default:
error_msg("STMT(BPF_LDWABS, 0x%x)",
filter[i].k);
}
break;
case BPF_LD + BPF_W + BPF_IMM:
error_msg("STMT(BPF_LDWIMM, 0x%x)", filter[i].k);
break;
case BPF_RET | BPF_K:
switch (filter[i].k) {
case SECCOMP_RET_TRACE:
error_msg("STMT(BPF_RET, SECCOMP_RET_TRACE)");
break;
case SECCOMP_RET_ALLOW:
error_msg("STMT(BPF_RET, SECCOMP_RET_ALLOW)");
break;
default:
error_msg("STMT(BPF_RET, 0x%x)", filter[i].k);
}
break;
case BPF_JMP | BPF_JEQ | BPF_K:
error_msg("JUMP(BPF_JEQ, %u, %u, %u)",
filter[i].jt, filter[i].jf,
filter[i].k);
break;
case BPF_JMP | BPF_JGE | BPF_K:
error_msg("JUMP(BPF_JGE, %u, %u, %u)",
filter[i].jt, filter[i].jf,
filter[i].k);
break;
case BPF_JMP + BPF_JSET + BPF_K:
error_msg("JUMP(BPF_JSET, %u, %u, 0x%x)",
filter[i].jt, filter[i].jf,
filter[i].k);
break;
case BPF_JMP | BPF_JA:
error_msg("JUMP(BPF_JA, %u)", filter[i].k);
break;
case BPF_ALU + BPF_RSH + BPF_K:
error_msg("STMT(BPF_RSH, %u)", filter[i].k);
break;
case BPF_ALU + BPF_LSH + BPF_X:
error_msg("STMT(BPF_LSH, X)");
break;
case BPF_ALU + BPF_AND + BPF_K:
error_msg("STMT(BPF_AND, 0x%x)", filter[i].k);
break;
case BPF_MISC + BPF_TAX:
error_msg("STMT(BPF_TAX)");
break;
case BPF_MISC + BPF_TXA:
error_msg("STMT(BPF_TXA)");
break;
default:
error_msg("STMT(0x%x, %u, %u, 0x%x)", filter[i].code,
filter[i].jt, filter[i].jf, filter[i].k);
}
}
}
void
init_seccomp_filter(void)
{
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
perror_func_msg_and_die("prctl(PR_SET_NO_NEW_PRIVS)");
if (debug_flag)
dump_seccomp_bpf();
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bpf_prog) < 0)
perror_func_msg_and_die("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
}
int
seccomp_filter_restart_operator(const struct tcb *tcp)
{
if (exiting(tcp) && tcp->scno < nsyscall_vec[current_personality]
&& traced_by_seccomp(tcp->scno, current_personality))
return PTRACE_SYSCALL;
return PTRACE_CONT;
}
#else /* !HAVE_LINUX_SECCOMP_H */
# warning <linux/seccomp.h> is not available, seccomp filtering is not supported
static void
check_seccomp_filter_properties(void)
{
seccomp_filtering = false;
}
void
init_seccomp_filter(void)
{
}
int
seccomp_filter_restart_operator(const struct tcb *tcp)
{
return PTRACE_SYSCALL;
}
#endif
void
check_seccomp_filter(void)
{
/* Let's avoid enabling seccomp if all syscalls are traced. */
seccomp_filtering = !is_complete_set_array(trace_set, nsyscall_vec,
SUPPORTED_PERSONALITIES);
if (!seccomp_filtering) {
error_msg("Seccomp filter is requested "
"but there are no syscalls to filter. "
"See -e trace to filter syscalls.");
return;
}
check_seccomp_filter_properties();
if (!seccomp_filtering)
error_msg("seccomp filter is requested but unavailable");
}