| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package seccomp |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"syscall" |
| 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 |
import "C" |
| 277 |
|
| 278 |
|
| 279 |
type scmpFilterAttr uint32 |
| 280 |
|
| 281 |
|
| 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 |
|
| 297 |
scmpError C.int = -1 |
| 298 |
|
| 299 |
archStart ScmpArch = ArchNative |
| 300 |
archEnd ScmpArch = ArchSHEB |
| 301 |
|
| 302 |
actionStart ScmpAction = ActKillThread |
| 303 |
actionEnd ScmpAction = ActKillProcess |
| 304 |
|
| 305 |
compareOpStart ScmpCompareOp = CompareNotEqual |
| 306 |
compareOpEnd ScmpCompareOp = CompareMaskedEqual |
| 307 |
) |
| 308 |
|
| 309 |
var ( |
| 310 |
|
| 311 |
errBadFilter = errors.New("filter is invalid or uninitialized") |
| 312 |
errDefAction = errors.New("requested action matches default action of filter") |
| 313 |
|
| 314 |
verMajor = uint(C.get_major_version()) |
| 315 |
verMinor = uint(C.get_minor_version()) |
| 316 |
verMicro = uint(C.get_micro_version()) |
| 317 |
) |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 370 |
|
| 371 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 417 |
|
| 418 |
|
| 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 |
|
| 436 |
|
| 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 |
|
| 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 |
|
| 484 |
|
| 485 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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) |
| 762 |
resp.flags = C.__u32(scmpResp.Flags) |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
func checkAPI(op string, minLevel uint, major, minor, micro uint) error { |
| 771 |
|
| 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 |
|
| 787 |
|
| 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 |
|
| 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 |
|
| 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 |
} |