Debian Codegraph @

seccomp_internal.go

raw symbols SCIP
1 // Internal functions for libseccomp Go bindings
2 // No exported functions
3
4 package seccomp
5
6 import (
7 "errors"
8 "fmt"
9 "syscall"
10 )
11
12 // Unexported C wrapping code - provides the C-Golang interface
13 // Get the seccomp header in scope
14 // Need stdlib.h for free() on cstrings
15
16 // To compile libseccomp-golang against a specific version of libseccomp:
17 // cd ../libseccomp && mkdir -p prefix
18 // ./configure --prefix=$PWD/prefix && make && make install
19 // cd ../libseccomp-golang
20 // PKG_CONFIG_PATH=$PWD/../libseccomp/prefix/lib/pkgconfig/ make
21 // LD_PRELOAD=$PWD/../libseccomp/prefix/lib/libseccomp.so.2.5.0 PKG_CONFIG_PATH=$PWD/../libseccomp/prefix/lib/pkgconfig/ make test
22
23 // #cgo pkg-config: libseccomp
24 /*
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <seccomp.h>
28
29 #if (SCMP_VER_MAJOR < 2) || \
30 (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 3) || \
31 (SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR == 3 && SCMP_VER_MICRO < 1)
32 #error This package requires libseccomp >= v2.3.1
33 #endif
34
35 #define ARCH_BAD ~0
36
37 const uint32_t C_ARCH_BAD = ARCH_BAD;
38
39 #ifndef SCMP_ARCH_PPC
40 #define SCMP_ARCH_PPC ARCH_BAD
41 #endif
42
43 #ifndef SCMP_ARCH_PPC64
44 #define SCMP_ARCH_PPC64 ARCH_BAD
45 #endif
46
47 #ifndef SCMP_ARCH_PPC64LE
48 #define SCMP_ARCH_PPC64LE ARCH_BAD
49 #endif
50
51 #ifndef SCMP_ARCH_S390
52 #define SCMP_ARCH_S390 ARCH_BAD
53 #endif
54
55 #ifndef SCMP_ARCH_S390X
56 #define SCMP_ARCH_S390X ARCH_BAD
57 #endif
58
59 #ifndef SCMP_ARCH_PARISC
60 #define SCMP_ARCH_PARISC ARCH_BAD
61 #endif
62
63 #ifndef SCMP_ARCH_PARISC64
64 #define SCMP_ARCH_PARISC64 ARCH_BAD
65 #endif
66
67 #ifndef SCMP_ARCH_RISCV64
68 #define SCMP_ARCH_RISCV64 ARCH_BAD
69 #endif
70
71 #ifndef SCMP_ARCH_LOONGARCH64
72 #define SCMP_ARCH_LOONGARCH64 ARCH_BAD
73 #endif
74
75 #ifndef SCMP_ARCH_M68K
76 #define SCMP_ARCH_M68K ARCH_BAD
77 #endif
78
79 #ifndef SCMP_ARCH_SH
80 #define SCMP_ARCH_SH ARCH_BAD
81 #endif
82
83 #ifndef SCMP_ARCH_SHEB
84 #define SCMP_ARCH_SHEB ARCH_BAD
85 #endif
86
87 const uint32_t C_ARCH_NATIVE = SCMP_ARCH_NATIVE;
88 const uint32_t C_ARCH_X86 = SCMP_ARCH_X86;
89 const uint32_t C_ARCH_X86_64 = SCMP_ARCH_X86_64;
90 const uint32_t C_ARCH_X32 = SCMP_ARCH_X32;
91 const uint32_t C_ARCH_ARM = SCMP_ARCH_ARM;
92 const uint32_t C_ARCH_AARCH64 = SCMP_ARCH_AARCH64;
93 const uint32_t C_ARCH_MIPS = SCMP_ARCH_MIPS;
94 const uint32_t C_ARCH_MIPS64 = SCMP_ARCH_MIPS64;
95 const uint32_t C_ARCH_MIPS64N32 = SCMP_ARCH_MIPS64N32;
96 const uint32_t C_ARCH_MIPSEL = SCMP_ARCH_MIPSEL;
97 const uint32_t C_ARCH_MIPSEL64 = SCMP_ARCH_MIPSEL64;
98 const uint32_t C_ARCH_MIPSEL64N32 = SCMP_ARCH_MIPSEL64N32;
99 const uint32_t C_ARCH_PPC = SCMP_ARCH_PPC;
100 const uint32_t C_ARCH_PPC64 = SCMP_ARCH_PPC64;
101 const uint32_t C_ARCH_PPC64LE = SCMP_ARCH_PPC64LE;
102 const uint32_t C_ARCH_S390 = SCMP_ARCH_S390;
103 const uint32_t C_ARCH_S390X = SCMP_ARCH_S390X;
104 const uint32_t C_ARCH_PARISC = SCMP_ARCH_PARISC;
105 const uint32_t C_ARCH_PARISC64 = SCMP_ARCH_PARISC64;
106 const uint32_t C_ARCH_RISCV64 = SCMP_ARCH_RISCV64;
107 const uint32_t C_ARCH_LOONGARCH64 = SCMP_ARCH_LOONGARCH64;
108 const uint32_t C_ARCH_M68K = SCMP_ARCH_M68K;
109 const uint32_t C_ARCH_SH = SCMP_ARCH_SH;
110 const uint32_t C_ARCH_SHEB = SCMP_ARCH_SHEB;
111
112 #ifndef SCMP_ACT_LOG
113 #define SCMP_ACT_LOG 0x7ffc0000U
114 #endif
115
116 #ifndef SCMP_ACT_KILL_PROCESS
117 #define SCMP_ACT_KILL_PROCESS 0x80000000U
118 #endif
119
120 #ifndef SCMP_ACT_KILL_THREAD
121 #define SCMP_ACT_KILL_THREAD 0x00000000U
122 #endif
123
124 #ifndef SCMP_ACT_NOTIFY
125 #define SCMP_ACT_NOTIFY 0x7fc00000U
126 #endif
127
128 const uint32_t C_ACT_KILL = SCMP_ACT_KILL;
129 const uint32_t C_ACT_KILL_PROCESS = SCMP_ACT_KILL_PROCESS;
130 const uint32_t C_ACT_KILL_THREAD = SCMP_ACT_KILL_THREAD;
131 const uint32_t C_ACT_TRAP = SCMP_ACT_TRAP;
132 const uint32_t C_ACT_ERRNO = SCMP_ACT_ERRNO(0);
133 const uint32_t C_ACT_TRACE = SCMP_ACT_TRACE(0);
134 const uint32_t C_ACT_LOG = SCMP_ACT_LOG;
135 const uint32_t C_ACT_ALLOW = SCMP_ACT_ALLOW;
136 const uint32_t C_ACT_NOTIFY = SCMP_ACT_NOTIFY;
137
138 // The libseccomp SCMP_FLTATR_CTL_LOG member of the scmp_filter_attr enum was
139 // added in v2.4.0
140 #if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 4
141 #define SCMP_FLTATR_CTL_LOG _SCMP_FLTATR_MIN
142 #endif
143
144 // The following SCMP_FLTATR_* were added in libseccomp v2.5.0.
145 #if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 5
146 #define SCMP_FLTATR_CTL_SSB _SCMP_FLTATR_MIN
147 #define SCMP_FLTATR_CTL_OPTIMIZE _SCMP_FLTATR_MIN
148 #define SCMP_FLTATR_API_SYSRAWRC _SCMP_FLTATR_MIN
149 #endif
150
151 // Added in libseccomp v2.6.0.
152 #if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 6
153 #define SCMP_FLTATR_CTL_WAITKILL _SCMP_FLTATR_MIN
154 #endif
155
156 const uint32_t C_ATTRIBUTE_DEFAULT = (uint32_t)SCMP_FLTATR_ACT_DEFAULT;
157 const uint32_t C_ATTRIBUTE_BADARCH = (uint32_t)SCMP_FLTATR_ACT_BADARCH;
158 const uint32_t C_ATTRIBUTE_NNP = (uint32_t)SCMP_FLTATR_CTL_NNP;
159 const uint32_t C_ATTRIBUTE_TSYNC = (uint32_t)SCMP_FLTATR_CTL_TSYNC;
160 const uint32_t C_ATTRIBUTE_LOG = (uint32_t)SCMP_FLTATR_CTL_LOG;
161 const uint32_t C_ATTRIBUTE_SSB = (uint32_t)SCMP_FLTATR_CTL_SSB;
162 const uint32_t C_ATTRIBUTE_OPTIMIZE = (uint32_t)SCMP_FLTATR_CTL_OPTIMIZE;
163 const uint32_t C_ATTRIBUTE_SYSRAWRC = (uint32_t)SCMP_FLTATR_API_SYSRAWRC;
164 const uint32_t C_ATTRIBUTE_WAITKILL = (uint32_t)SCMP_FLTATR_CTL_WAITKILL;
165
166 const int C_CMP_NE = (int)SCMP_CMP_NE;
167 const int C_CMP_LT = (int)SCMP_CMP_LT;
168 const int C_CMP_LE = (int)SCMP_CMP_LE;
169 const int C_CMP_EQ = (int)SCMP_CMP_EQ;
170 const int C_CMP_GE = (int)SCMP_CMP_GE;
171 const int C_CMP_GT = (int)SCMP_CMP_GT;
172 const int C_CMP_MASKED_EQ = (int)SCMP_CMP_MASKED_EQ;
173
174 unsigned int get_major_version()
175 {
176 return seccomp_version()->major;
177 }
178
179 unsigned int get_minor_version()
180 {
181 return seccomp_version()->minor;
182 }
183
184 unsigned int get_micro_version()
185 {
186 return seccomp_version()->micro;
187 }
188
189 // The libseccomp API level functions were added in v2.4.0
190 #if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 4
191 const unsigned int seccomp_api_get(void)
192 {
193 // libseccomp-golang requires libseccomp v2.2.0, at a minimum, which
194 // supported API level 2. However, the kernel may not support API level
195 // 2 constructs which are the seccomp() system call and the TSYNC
196 // filter flag. Return the "reserved" value of 0 here to indicate that
197 // proper API level support is not available in libseccomp.
198 return 0;
199 }
200
201 int seccomp_api_set(unsigned int level)
202 {
203 return -EOPNOTSUPP;
204 }
205 #endif
206
207 typedef struct scmp_arg_cmp* scmp_cast_t;
208
209 void* make_arg_cmp_array(unsigned int length)
210 {
211 return calloc(length, sizeof(struct scmp_arg_cmp));
212 }
213
214 // Wrapper to add an scmp_arg_cmp struct to an existing arg_cmp array
215 void add_struct_arg_cmp(
216 struct scmp_arg_cmp* arr,
217 unsigned int pos,
218 unsigned int arg,
219 int compare,
220 uint64_t a,
221 uint64_t b
222 )
223 {
224 arr[pos].arg = arg;
225 arr[pos].op = compare;
226 arr[pos].datum_a = a;
227 arr[pos].datum_b = b;
228
229 return;
230 }
231
232 // The seccomp notify API functions were added in v2.5.0
233 #if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 5
234
235 struct seccomp_data {
236 int nr;
237 __u32 arch;
238 __u64 instruction_pointer;
239 __u64 args[6];
240 };
241
242 struct seccomp_notif {
243 __u64 id;
244 __u32 pid;
245 __u32 flags;
246 struct seccomp_data data;
247 };
248
249 struct seccomp_notif_resp {
250 __u64 id;
251 __s64 val;
252 __s32 error;
253 __u32 flags;
254 };
255
256 int seccomp_notify_alloc(struct seccomp_notif **req, struct seccomp_notif_resp **resp) {
257 return -EOPNOTSUPP;
258 }
259 int seccomp_notify_fd(const scmp_filter_ctx ctx) {
260 return -EOPNOTSUPP;
261 }
262 void seccomp_notify_free(struct seccomp_notif *req, struct seccomp_notif_resp *resp) {
263 }
264 int seccomp_notify_id_valid(int fd, uint64_t id) {
265 return -EOPNOTSUPP;
266 }
267 int seccomp_notify_receive(int fd, struct seccomp_notif *req) {
268 return -EOPNOTSUPP;
269 }
270 int seccomp_notify_respond(int fd, struct seccomp_notif_resp *resp) {
271 return -EOPNOTSUPP;
272 }
273
274 #endif
275 */
276 import "C"
277
278 // Nonexported types
279 type scmpFilterAttr uint32
280
281 // Nonexported constants
282
283 const (
284 filterAttrActDefault scmpFilterAttr = iota
285 filterAttrActBadArch
286 filterAttrNNP
287 filterAttrTsync
288 filterAttrLog
289 filterAttrSSB
290 filterAttrOptimize
291 filterAttrRawRC
292 filterAttrWaitKill
293 )
294
295 const (
296 // An error return from certain libseccomp functions
297 scmpError C.int = -1
298 // Comparison boundaries to check for architecture validity
299 archStart ScmpArch = ArchNative
300 archEnd ScmpArch = ArchSHEB
301 // Comparison boundaries to check for action validity
302 actionStart ScmpAction = ActKillThread
303 actionEnd ScmpAction = ActKillProcess
304 // Comparison boundaries to check for comparison operator validity
305 compareOpStart ScmpCompareOp = CompareNotEqual
306 compareOpEnd ScmpCompareOp = CompareMaskedEqual
307 )
308
309 var (
310 // errBadFilter is thrown on bad filter context.
311 errBadFilter = errors.New("filter is invalid or uninitialized")
312 errDefAction = errors.New("requested action matches default action of filter")
313 // Constants representing library major, minor, and micro versions
314 verMajor = uint(C.get_major_version())
315 verMinor = uint(C.get_minor_version())
316 verMicro = uint(C.get_micro_version())
317 )
318
319 // Nonexported functions
320
321 // checkVersion returns an error if the libseccomp version being used
322 // is less than the one specified by major, minor, and micro arguments.
323 // Argument op is an arbitrary non-empty operation description, which
324 // is used as a part of the error message returned.
325 //
326 // Most users should use checkAPI instead.
327 func checkVersion(op string, major, minor, micro uint) error {
328 if (verMajor > major) ||
329 (verMajor == major && verMinor > minor) ||
330 (verMajor == major && verMinor == minor && verMicro >= micro) {
331 return nil
332 }
333 return &VersionError{
334 op: op,
335 major: major,
336 minor: minor,
337 micro: micro,
338 }
339 }
340
341 func ensureSupportedVersion() error {
342 return checkVersion("seccomp", 2, 3, 1)
343 }
344
345 // Get the API level
346 func getAPI() (uint, error) {
347 api := C.seccomp_api_get()
348 if api == 0 {
349 return 0, errors.New("API level operations are not supported")
350 }
351
352 return uint(api), nil
353 }
354
355 // Set the API level
356 func setAPI(api uint) error {
357 if retCode := C.seccomp_api_set(C.uint(api)); retCode != 0 {
358 e := errRc(retCode)
359 if e == syscall.EOPNOTSUPP {
360 return errors.New("API level operations are not supported")
361 }
362
363 return fmt.Errorf("could not set API level: %w", e)
364 }
365
366 return nil
367 }
368
369 // Filter helpers
370
371 // Filter finalizer - ensure that kernel context for filters is freed
372 func filterFinalizer(f *ScmpFilter) {
373 f.Release()
374 }
375
376 func errRc(rc C.int) error {
377 return syscall.Errno(-1 * rc)
378 }
379
380 // Get a raw filter attribute
381 func (f *ScmpFilter) getFilterAttr(attr scmpFilterAttr) (C.uint32_t, error) {
382 f.lock.Lock()
383 defer f.lock.Unlock()
384
385 if !f.valid {
386 return 0x0, errBadFilter
387 }
388
389 var attribute C.uint32_t
390
391 retCode := C.seccomp_attr_get(f.filterCtx, attr.toNative(), &attribute)
392 if retCode != 0 {
393 return 0x0, errRc(retCode)
394 }
395
396 return attribute, nil
397 }
398
399 // Set a raw filter attribute
400 func (f *ScmpFilter) setFilterAttr(attr scmpFilterAttr, value C.uint32_t) error {
401 f.lock.Lock()
402 defer f.lock.Unlock()
403
404 if !f.valid {
405 return errBadFilter
406 }
407
408 retCode := C.seccomp_attr_set(f.filterCtx, attr.toNative(), value)
409 if retCode != 0 {
410 return errRc(retCode)
411 }
412
413 return nil
414 }
415
416 // DOES NOT LOCK OR CHECK VALIDITY
417 // Assumes caller has already done this
418 // Wrapper for seccomp_rule_add_... functions
419 func (f *ScmpFilter) addRuleWrapper(call ScmpSyscall, action ScmpAction, exact bool, length C.uint, cond C.scmp_cast_t) error {
420 if length != 0 && cond == nil {
421 return errors.New("null conditions list, but length is nonzero")
422 }
423
424 var retCode C.int
425 if exact {
426 retCode = C.seccomp_rule_add_exact_array(f.filterCtx, action.toNative(), C.int(call), length, cond)
427 } else {
428 retCode = C.seccomp_rule_add_array(f.filterCtx, action.toNative(), C.int(call), length, cond)
429 }
430
431 if retCode != 0 {
432 switch e := errRc(retCode); e {
433 case syscall.EFAULT:
434 return fmt.Errorf("unrecognized syscall %#x", int32(call))
435 // libseccomp >= v2.5.0 returns EACCES, older versions return EPERM.
436 // TODO: remove EPERM once libseccomp < v2.5.0 is not supported.
437 case syscall.EPERM, syscall.EACCES:
438 return errDefAction
439 case syscall.EINVAL:
440 return errors.New("two checks on same syscall argument")
441 default:
442 return e
443 }
444 }
445
446 return nil
447 }
448
449 // Generic add function for filter rules
450 func (f *ScmpFilter) addRuleGeneric(call ScmpSyscall, action ScmpAction, exact bool, conds []ScmpCondition) error {
451 f.lock.Lock()
452 defer f.lock.Unlock()
453
454 if !f.valid {
455 return errBadFilter
456 }
457
458 if len(conds) == 0 {
459 if err := f.addRuleWrapper(call, action, exact, 0, nil); err != nil {
460 return err
461 }
462 } else {
463 argsArr := C.make_arg_cmp_array(C.uint(len(conds)))
464 if argsArr == nil {
465 return errors.New("error allocating memory for conditions")
466 }
467 defer C.free(argsArr)
468
469 for i, cond := range conds {
470 C.add_struct_arg_cmp(C.scmp_cast_t(argsArr), C.uint(i),
471 C.uint(cond.Argument), cond.Op.toNative(),
472 C.uint64_t(cond.Operand1), C.uint64_t(cond.Operand2))
473 }
474
475 if err := f.addRuleWrapper(call, action, exact, C.uint(len(conds)), C.scmp_cast_t(argsArr)); err != nil {
476 return err
477 }
478 }
479
480 return nil
481 }
482
483 // Generic Helpers
484
485 // Helper - Sanitize Arch token input
486 func sanitizeArch(in ScmpArch) error {
487 if in < archStart || in > archEnd {
488 return fmt.Errorf("unrecognized architecture %#x", uint(in))
489 }
490
491 if in.toNative() == C.C_ARCH_BAD {
492 return fmt.Errorf("architecture %v is not supported on this version of the library", in)
493 }
494
495 return nil
496 }
497
498 func sanitizeAction(in ScmpAction) error {
499 inTmp := in & 0x0000FFFF
500 if inTmp < actionStart || inTmp > actionEnd {
501 return fmt.Errorf("unrecognized action %#x", uint(inTmp))
502 }
503
504 if inTmp != ActTrace && inTmp != ActErrno && (in&0xFFFF0000) != 0 {
505 return errors.New("highest 16 bits must be zeroed except for Trace and Errno")
506 }
507
508 return nil
509 }
510
511 func sanitizeCompareOp(in ScmpCompareOp) error {
512 if in < compareOpStart || in > compareOpEnd {
513 return fmt.Errorf("unrecognized comparison operator %#x", uint(in))
514 }
515
516 return nil
517 }
518
519 func archFromNative(a C.uint32_t) (ScmpArch, error) {
520 switch a {
521 case C.C_ARCH_X86:
522 return ArchX86, nil
523 case C.C_ARCH_X86_64:
524 return ArchAMD64, nil
525 case C.C_ARCH_X32:
526 return ArchX32, nil
527 case C.C_ARCH_ARM:
528 return ArchARM, nil
529 case C.C_ARCH_NATIVE:
530 return ArchNative, nil
531 case C.C_ARCH_AARCH64:
532 return ArchARM64, nil
533 case C.C_ARCH_MIPS:
534 return ArchMIPS, nil
535 case C.C_ARCH_MIPS64:
536 return ArchMIPS64, nil
537 case C.C_ARCH_MIPS64N32:
538 return ArchMIPS64N32, nil
539 case C.C_ARCH_MIPSEL:
540 return ArchMIPSEL, nil
541 case C.C_ARCH_MIPSEL64:
542 return ArchMIPSEL64, nil
543 case C.C_ARCH_MIPSEL64N32:
544 return ArchMIPSEL64N32, nil
545 case C.C_ARCH_PPC:
546 return ArchPPC, nil
547 case C.C_ARCH_PPC64:
548 return ArchPPC64, nil
549 case C.C_ARCH_PPC64LE:
550 return ArchPPC64LE, nil
551 case C.C_ARCH_S390:
552 return ArchS390, nil
553 case C.C_ARCH_S390X:
554 return ArchS390X, nil
555 case C.C_ARCH_PARISC:
556 return ArchPARISC, nil
557 case C.C_ARCH_PARISC64:
558 return ArchPARISC64, nil
559 case C.C_ARCH_RISCV64:
560 return ArchRISCV64, nil
561 case C.C_ARCH_LOONGARCH64:
562 return ArchLOONGARCH64, nil
563 case C.C_ARCH_M68K:
564 return ArchM68K, nil
565 case C.C_ARCH_SH:
566 return ArchSH, nil
567 case C.C_ARCH_SHEB:
568 return ArchSHEB, nil
569 default:
570 return 0x0, fmt.Errorf("unrecognized architecture %#x", uint32(a))
571 }
572 }
573
574 // Only use with sanitized arches, no error handling
575 func (a ScmpArch) toNative() C.uint32_t {
576 switch a {
577 case ArchX86:
578 return C.C_ARCH_X86
579 case ArchAMD64:
580 return C.C_ARCH_X86_64
581 case ArchX32:
582 return C.C_ARCH_X32
583 case ArchARM:
584 return C.C_ARCH_ARM
585 case ArchARM64:
586 return C.C_ARCH_AARCH64
587 case ArchMIPS:
588 return C.C_ARCH_MIPS
589 case ArchMIPS64:
590 return C.C_ARCH_MIPS64
591 case ArchMIPS64N32:
592 return C.C_ARCH_MIPS64N32
593 case ArchMIPSEL:
594 return C.C_ARCH_MIPSEL
595 case ArchMIPSEL64:
596 return C.C_ARCH_MIPSEL64
597 case ArchMIPSEL64N32:
598 return C.C_ARCH_MIPSEL64N32
599 case ArchPPC:
600 return C.C_ARCH_PPC
601 case ArchPPC64:
602 return C.C_ARCH_PPC64
603 case ArchPPC64LE:
604 return C.C_ARCH_PPC64LE
605 case ArchS390:
606 return C.C_ARCH_S390
607 case ArchS390X:
608 return C.C_ARCH_S390X
609 case ArchPARISC:
610 return C.C_ARCH_PARISC
611 case ArchPARISC64:
612 return C.C_ARCH_PARISC64
613 case ArchRISCV64:
614 return C.C_ARCH_RISCV64
615 case ArchLOONGARCH64:
616 return C.C_ARCH_LOONGARCH64
617 case ArchM68K:
618 return C.C_ARCH_M68K
619 case ArchSH:
620 return C.C_ARCH_SH
621 case ArchSHEB:
622 return C.C_ARCH_SHEB
623 case ArchNative:
624 return C.C_ARCH_NATIVE
625 default:
626 return 0x0
627 }
628 }
629
630 // Only use with sanitized ops, no error handling
631 func (a ScmpCompareOp) toNative() C.int {
632 switch a {
633 case CompareNotEqual:
634 return C.C_CMP_NE
635 case CompareLess:
636 return C.C_CMP_LT
637 case CompareLessOrEqual:
638 return C.C_CMP_LE
639 case CompareEqual:
640 return C.C_CMP_EQ
641 case CompareGreaterEqual:
642 return C.C_CMP_GE
643 case CompareGreater:
644 return C.C_CMP_GT
645 case CompareMaskedEqual:
646 return C.C_CMP_MASKED_EQ
647 default:
648 return 0x0
649 }
650 }
651
652 func actionFromNative(a C.uint32_t) (ScmpAction, error) {
653 aTmp := a & 0xFFFF
654 switch a & 0xFFFF0000 {
655 case C.C_ACT_KILL_PROCESS:
656 return ActKillProcess, nil
657 case C.C_ACT_KILL_THREAD:
658 return ActKillThread, nil
659 case C.C_ACT_TRAP:
660 return ActTrap, nil
661 case C.C_ACT_ERRNO:
662 return ActErrno.SetReturnCode(int16(aTmp)), nil
663 case C.C_ACT_TRACE:
664 return ActTrace.SetReturnCode(int16(aTmp)), nil
665 case C.C_ACT_LOG:
666 return ActLog, nil
667 case C.C_ACT_ALLOW:
668 return ActAllow, nil
669 case C.C_ACT_NOTIFY:
670 return ActNotify, nil
671 default:
672 return 0x0, fmt.Errorf("unrecognized action %#x", uint32(a))
673 }
674 }
675
676 // Only use with sanitized actions, no error handling
677 func (a ScmpAction) toNative() C.uint32_t {
678 switch a & 0xFFFF {
679 case ActKillProcess:
680 return C.C_ACT_KILL_PROCESS
681 case ActKillThread:
682 return C.C_ACT_KILL_THREAD
683 case ActTrap:
684 return C.C_ACT_TRAP
685 case ActErrno:
686 return C.C_ACT_ERRNO | (C.uint32_t(a) >> 16)
687 case ActTrace:
688 return C.C_ACT_TRACE | (C.uint32_t(a) >> 16)
689 case ActLog:
690 return C.C_ACT_LOG
691 case ActAllow:
692 return C.C_ACT_ALLOW
693 case ActNotify:
694 return C.C_ACT_NOTIFY
695 default:
696 return 0x0
697 }
698 }
699
700 // Internal only, assumes safe attribute
701 func (a scmpFilterAttr) toNative() uint32 {
702 switch a {
703 case filterAttrActDefault:
704 return uint32(C.C_ATTRIBUTE_DEFAULT)
705 case filterAttrActBadArch:
706 return uint32(C.C_ATTRIBUTE_BADARCH)
707 case filterAttrNNP:
708 return uint32(C.C_ATTRIBUTE_NNP)
709 case filterAttrTsync:
710 return uint32(C.C_ATTRIBUTE_TSYNC)
711 case filterAttrLog:
712 return uint32(C.C_ATTRIBUTE_LOG)
713 case filterAttrSSB:
714 return uint32(C.C_ATTRIBUTE_SSB)
715 case filterAttrOptimize:
716 return uint32(C.C_ATTRIBUTE_OPTIMIZE)
717 case filterAttrRawRC:
718 return uint32(C.C_ATTRIBUTE_SYSRAWRC)
719 case filterAttrWaitKill:
720 return uint32(C.C_ATTRIBUTE_WAITKILL)
721 default:
722 return 0x0
723 }
724 }
725
726 func syscallFromNative(a C.int) ScmpSyscall {
727 return ScmpSyscall(a)
728 }
729
730 func notifReqFromNative(req *C.struct_seccomp_notif) (*ScmpNotifReq, error) {
731 scmpArgs := make([]uint64, 6)
732 for i := 0; i < len(scmpArgs); i++ {
733 scmpArgs[i] = uint64(req.data.args[i])
734 }
735
736 arch, err := archFromNative(req.data.arch)
737 if err != nil {
738 return nil, err
739 }
740
741 scmpData := ScmpNotifData{
742 Syscall: syscallFromNative(req.data.nr),
743 Arch: arch,
744 InstrPointer: uint64(req.data.instruction_pointer),
745 Args: scmpArgs,
746 }
747
748 scmpReq := &ScmpNotifReq{
749 ID: uint64(req.id),
750 Pid: uint32(req.pid),
751 Flags: uint32(req.flags),
752 Data: scmpData,
753 }
754
755 return scmpReq, nil
756 }
757
758 func (scmpResp *ScmpNotifResp) toNative(resp *C.struct_seccomp_notif_resp) {
759 resp.id = C.__u64(scmpResp.ID)
760 resp.val = C.__s64(scmpResp.Val)
761 resp.error = (C.__s32(scmpResp.Error) * -1) // kernel requires a negated value
762 resp.flags = C.__u32(scmpResp.Flags)
763 }
764
765 // checkAPI checks that both the API level and the seccomp version is equal to
766 // or greater than the specified minLevel and major, minor, micro,
767 // respectively, and returns an error otherwise. Argument op is an arbitrary
768 // non-empty operation description, used as a part of the error message
769 // returned.
770 func checkAPI(op string, minLevel uint, major, minor, micro uint) error {
771 // Ignore error from getAPI, as it returns level == 0 in case of error.
772 level, _ := getAPI()
773 if level >= minLevel {
774 return checkVersion(op, major, minor, micro)
775 }
776 return &VersionError{
777 op: op,
778 curAPI: level,
779 minAPI: minLevel,
780 major: major,
781 minor: minor,
782 micro: micro,
783 }
784 }
785
786 // Userspace Notification API
787 // Calls to C.seccomp_notify* hidden from seccomp.go
788
789 func notifSupported() error {
790 return checkAPI("seccomp notification", 6, 2, 5, 0)
791 }
792
793 func (f *ScmpFilter) getNotifFd() (ScmpFd, error) {
794 f.lock.Lock()
795 defer f.lock.Unlock()
796
797 if !f.valid {
798 return -1, errBadFilter
799 }
800 if err := notifSupported(); err != nil {
801 return -1, err
802 }
803
804 fd := C.seccomp_notify_fd(f.filterCtx)
805
806 return ScmpFd(fd), nil
807 }
808
809 func notifReceive(fd ScmpFd) (*ScmpNotifReq, error) {
810 var req *C.struct_seccomp_notif
811 var resp *C.struct_seccomp_notif_resp
812
813 if err := notifSupported(); err != nil {
814 return nil, err
815 }
816
817 // we only use the request here; the response is unused
818 if retCode := C.seccomp_notify_alloc(&req, &resp); retCode != 0 {
819 return nil, errRc(retCode)
820 }
821 defer C.seccomp_notify_free(req, resp)
822
823 for {
824 retCode, errno := C.seccomp_notify_receive(C.int(fd), req)
825 if retCode == 0 {
826 break
827 }
828
829 if errno == syscall.EINTR {
830 continue
831 }
832
833 if errno == syscall.ENOENT {
834 return nil, errno
835 }
836
837 return nil, errRc(retCode)
838 }
839
840 return notifReqFromNative(req)
841 }
842
843 func notifRespond(fd ScmpFd, scmpResp *ScmpNotifResp) error {
844 var req *C.struct_seccomp_notif
845 var resp *C.struct_seccomp_notif_resp
846
847 if err := notifSupported(); err != nil {
848 return err
849 }
850
851 // we only use the response here; the request is discarded
852 if retCode := C.seccomp_notify_alloc(&req, &resp); retCode != 0 {
853 return errRc(retCode)
854 }
855 defer C.seccomp_notify_free(req, resp)
856
857 scmpResp.toNative(resp)
858
859 for {
860 retCode, errno := C.seccomp_notify_respond(C.int(fd), resp)
861 if retCode == 0 {
862 break
863 }
864
865 if errno == syscall.EINTR {
866 continue
867 }
868
869 if errno == syscall.ENOENT {
870 return errno
871 }
872
873 return errRc(retCode)
874 }
875
876 return nil
877 }
878
879 func notifIDValid(fd ScmpFd, id uint64) error {
880 if err := notifSupported(); err != nil {
881 return err
882 }
883
884 for {
885 retCode, errno := C.seccomp_notify_id_valid(C.int(fd), C.uint64_t(id))
886 if retCode == 0 {
887 break
888 }
889
890 if errno == syscall.EINTR {
891 continue
892 }
893
894 if errno == syscall.ENOENT {
895 return errno
896 }
897
898 return errRc(retCode)
899 }
900
901 return nil
902 }