| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package seccomp |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"os" |
| 13 |
"runtime" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"syscall" |
| 17 |
"unsafe" |
| 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 |
import "C" |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
type VersionError struct { |
| 49 |
op string |
| 50 |
major, minor, micro uint |
| 51 |
curAPI, minAPI uint |
| 52 |
} |
| 53 |
|
| 54 |
func init() { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
_, _ = getAPI() |
| 60 |
} |
| 61 |
|
| 62 |
func (e VersionError) Error() string { |
| 63 |
if e.minAPI != 0 { |
| 64 |
return fmt.Sprintf("%s requires libseccomp >= %d.%d.%d and API level >= %d "+ |
| 65 |
"(current version: %d.%d.%d, API level: %d)", |
| 66 |
e.op, e.major, e.minor, e.micro, e.minAPI, |
| 67 |
verMajor, verMinor, verMicro, e.curAPI) |
| 68 |
} |
| 69 |
return fmt.Sprintf("%s requires libseccomp >= %d.%d.%d (current version: %d.%d.%d)", |
| 70 |
e.op, e.major, e.minor, e.micro, verMajor, verMinor, verMicro) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
type ScmpArch uint |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
type ScmpAction uint |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
type ScmpCompareOp uint |
| 84 |
|
| 85 |
|
| 86 |
type ScmpCondition struct { |
| 87 |
Argument uint `json:"argument,omitempty"` |
| 88 |
Op ScmpCompareOp `json:"operator,omitempty"` |
| 89 |
Operand1 uint64 `json:"operand_one,omitempty"` |
| 90 |
Operand2 uint64 `json:"operand_two,omitempty"` |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type ScmpSyscall int32 |
| 97 |
|
| 98 |
|
| 99 |
type ScmpFd int32 |
| 100 |
|
| 101 |
|
| 102 |
type ScmpNotifData struct { |
| 103 |
|
| 104 |
Syscall ScmpSyscall `json:"syscall,omitempty"` |
| 105 |
|
| 106 |
Arch ScmpArch `json:"arch,omitempty"` |
| 107 |
|
| 108 |
InstrPointer uint64 `json:"instr_pointer,omitempty"` |
| 109 |
|
| 110 |
Args []uint64 `json:"args,omitempty"` |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
type ScmpNotifReq struct { |
| 116 |
|
| 117 |
ID uint64 `json:"id,omitempty"` |
| 118 |
|
| 119 |
Pid uint32 `json:"pid,omitempty"` |
| 120 |
|
| 121 |
Flags uint32 `json:"flags,omitempty"` |
| 122 |
|
| 123 |
Data ScmpNotifData `json:"data,omitempty"` |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
type ScmpNotifResp struct { |
| 129 |
|
| 130 |
ID uint64 `json:"id,omitempty"` |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
Error int32 `json:"error,omitempty"` |
| 136 |
|
| 137 |
|
| 138 |
Val uint64 `json:"val,omitempty"` |
| 139 |
|
| 140 |
Flags uint32 `json:"flags,omitempty"` |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
const ( |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
ArchInvalid ScmpArch = iota |
| 153 |
|
| 154 |
ArchNative |
| 155 |
|
| 156 |
ArchX86 |
| 157 |
|
| 158 |
ArchAMD64 |
| 159 |
|
| 160 |
ArchX32 |
| 161 |
|
| 162 |
ArchARM |
| 163 |
|
| 164 |
ArchARM64 |
| 165 |
|
| 166 |
ArchMIPS |
| 167 |
|
| 168 |
ArchMIPS64 |
| 169 |
|
| 170 |
ArchMIPS64N32 |
| 171 |
|
| 172 |
ArchMIPSEL |
| 173 |
|
| 174 |
ArchMIPSEL64 |
| 175 |
|
| 176 |
|
| 177 |
ArchMIPSEL64N32 |
| 178 |
|
| 179 |
ArchPPC |
| 180 |
|
| 181 |
ArchPPC64 |
| 182 |
|
| 183 |
ArchPPC64LE |
| 184 |
|
| 185 |
ArchS390 |
| 186 |
|
| 187 |
ArchS390X |
| 188 |
|
| 189 |
ArchPARISC |
| 190 |
|
| 191 |
ArchPARISC64 |
| 192 |
|
| 193 |
ArchRISCV64 |
| 194 |
|
| 195 |
ArchLOONGARCH64 |
| 196 |
|
| 197 |
ArchM68K |
| 198 |
|
| 199 |
ArchSH |
| 200 |
|
| 201 |
ArchSHEB |
| 202 |
) |
| 203 |
|
| 204 |
const ( |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
ActInvalid ScmpAction = iota |
| 210 |
|
| 211 |
|
| 212 |
ActKillThread |
| 213 |
|
| 214 |
ActTrap |
| 215 |
|
| 216 |
|
| 217 |
ActNotify |
| 218 |
|
| 219 |
|
| 220 |
ActErrno |
| 221 |
|
| 222 |
|
| 223 |
ActTrace |
| 224 |
|
| 225 |
ActAllow |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
ActLog |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ActKillProcess |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
ActKill = ActKillThread |
| 240 |
) |
| 241 |
|
| 242 |
const ( |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
CompareInvalid ScmpCompareOp = iota |
| 250 |
|
| 251 |
|
| 252 |
CompareNotEqual |
| 253 |
|
| 254 |
CompareLess |
| 255 |
|
| 256 |
|
| 257 |
CompareLessOrEqual |
| 258 |
|
| 259 |
CompareEqual |
| 260 |
|
| 261 |
|
| 262 |
CompareGreaterEqual |
| 263 |
|
| 264 |
|
| 265 |
CompareGreater |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
CompareMaskedEqual |
| 270 |
) |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
var ErrSyscallDoesNotExist = errors.New("could not resolve syscall name") |
| 275 |
|
| 276 |
const ( |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
NotifRespFlagContinue uint32 = 1 |
| 283 |
) |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
func GetArchFromString(arch string) (ScmpArch, error) { |
| 290 |
if err := ensureSupportedVersion(); err != nil { |
| 291 |
return ArchInvalid, err |
| 292 |
} |
| 293 |
|
| 294 |
switch strings.ToLower(arch) { |
| 295 |
case "x86": |
| 296 |
return ArchX86, nil |
| 297 |
case "amd64", "x86-64", "x86_64", "x64": |
| 298 |
return ArchAMD64, nil |
| 299 |
case "x32": |
| 300 |
return ArchX32, nil |
| 301 |
case "arm": |
| 302 |
return ArchARM, nil |
| 303 |
case "arm64", "aarch64": |
| 304 |
return ArchARM64, nil |
| 305 |
case "mips": |
| 306 |
return ArchMIPS, nil |
| 307 |
case "mips64": |
| 308 |
return ArchMIPS64, nil |
| 309 |
case "mips64n32": |
| 310 |
return ArchMIPS64N32, nil |
| 311 |
case "mipsel": |
| 312 |
return ArchMIPSEL, nil |
| 313 |
case "mipsel64": |
| 314 |
return ArchMIPSEL64, nil |
| 315 |
case "mipsel64n32": |
| 316 |
return ArchMIPSEL64N32, nil |
| 317 |
case "ppc": |
| 318 |
return ArchPPC, nil |
| 319 |
case "ppc64": |
| 320 |
return ArchPPC64, nil |
| 321 |
case "ppc64le": |
| 322 |
return ArchPPC64LE, nil |
| 323 |
case "s390": |
| 324 |
return ArchS390, nil |
| 325 |
case "s390x": |
| 326 |
return ArchS390X, nil |
| 327 |
case "parisc": |
| 328 |
return ArchPARISC, nil |
| 329 |
case "parisc64": |
| 330 |
return ArchPARISC64, nil |
| 331 |
case "riscv64": |
| 332 |
return ArchRISCV64, nil |
| 333 |
case "loong64", "loongarch64": |
| 334 |
return ArchLOONGARCH64, nil |
| 335 |
case "m68k": |
| 336 |
return ArchM68K, nil |
| 337 |
case "sh": |
| 338 |
return ArchSH, nil |
| 339 |
case "sheb": |
| 340 |
return ArchSHEB, nil |
| 341 |
default: |
| 342 |
return ArchInvalid, fmt.Errorf("cannot convert unrecognized string %q", arch) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
func (a ScmpArch) String() string { |
| 348 |
switch a { |
| 349 |
case ArchX86: |
| 350 |
return "x86" |
| 351 |
case ArchAMD64: |
| 352 |
return "amd64" |
| 353 |
case ArchX32: |
| 354 |
return "x32" |
| 355 |
case ArchARM: |
| 356 |
return "arm" |
| 357 |
case ArchARM64: |
| 358 |
return "arm64" |
| 359 |
case ArchMIPS: |
| 360 |
return "mips" |
| 361 |
case ArchMIPS64: |
| 362 |
return "mips64" |
| 363 |
case ArchMIPS64N32: |
| 364 |
return "mips64n32" |
| 365 |
case ArchMIPSEL: |
| 366 |
return "mipsel" |
| 367 |
case ArchMIPSEL64: |
| 368 |
return "mipsel64" |
| 369 |
case ArchMIPSEL64N32: |
| 370 |
return "mipsel64n32" |
| 371 |
case ArchPPC: |
| 372 |
return "ppc" |
| 373 |
case ArchPPC64: |
| 374 |
return "ppc64" |
| 375 |
case ArchPPC64LE: |
| 376 |
return "ppc64le" |
| 377 |
case ArchS390: |
| 378 |
return "s390" |
| 379 |
case ArchS390X: |
| 380 |
return "s390x" |
| 381 |
case ArchPARISC: |
| 382 |
return "parisc" |
| 383 |
case ArchPARISC64: |
| 384 |
return "parisc64" |
| 385 |
case ArchRISCV64: |
| 386 |
return "riscv64" |
| 387 |
case ArchLOONGARCH64: |
| 388 |
return "loong64" |
| 389 |
case ArchM68K: |
| 390 |
return "m68k" |
| 391 |
case ArchSH: |
| 392 |
return "sh" |
| 393 |
case ArchSHEB: |
| 394 |
return "sheb" |
| 395 |
case ArchNative: |
| 396 |
return "native" |
| 397 |
case ArchInvalid: |
| 398 |
return "Invalid architecture" |
| 399 |
default: |
| 400 |
return fmt.Sprintf("Unknown architecture %#x", uint(a)) |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
func (a ScmpCompareOp) String() string { |
| 406 |
switch a { |
| 407 |
case CompareNotEqual: |
| 408 |
return "Not equal" |
| 409 |
case CompareLess: |
| 410 |
return "Less than" |
| 411 |
case CompareLessOrEqual: |
| 412 |
return "Less than or equal to" |
| 413 |
case CompareEqual: |
| 414 |
return "Equal" |
| 415 |
case CompareGreaterEqual: |
| 416 |
return "Greater than or equal to" |
| 417 |
case CompareGreater: |
| 418 |
return "Greater than" |
| 419 |
case CompareMaskedEqual: |
| 420 |
return "Masked equality" |
| 421 |
case CompareInvalid: |
| 422 |
return "Invalid comparison operator" |
| 423 |
default: |
| 424 |
return fmt.Sprintf("Unrecognized comparison operator %#x", uint(a)) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
func (a ScmpAction) String() string { |
| 430 |
switch a & 0xFFFF { |
| 431 |
case ActKillThread: |
| 432 |
return "Action: Kill thread" |
| 433 |
case ActKillProcess: |
| 434 |
return "Action: Kill process" |
| 435 |
case ActTrap: |
| 436 |
return "Action: Send SIGSYS" |
| 437 |
case ActErrno: |
| 438 |
return fmt.Sprintf("Action: Return error code %d", (a >> 16)) |
| 439 |
case ActTrace: |
| 440 |
return fmt.Sprintf("Action: Notify tracing processes with code %d", |
| 441 |
(a >> 16)) |
| 442 |
case ActNotify: |
| 443 |
return "Action: Notify userspace" |
| 444 |
case ActLog: |
| 445 |
return "Action: Log system call" |
| 446 |
case ActAllow: |
| 447 |
return "Action: Allow system call" |
| 448 |
default: |
| 449 |
return fmt.Sprintf("Unrecognized Action %#x", uint(a)) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
func (a ScmpAction) SetReturnCode(code int16) ScmpAction { |
| 458 |
aTmp := a & 0x0000FFFF |
| 459 |
if aTmp == ActErrno || aTmp == ActTrace { |
| 460 |
return (aTmp | (ScmpAction(code)&0xFFFF)<<16) |
| 461 |
} |
| 462 |
return a |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
func (a ScmpAction) GetReturnCode() int16 { |
| 467 |
return int16(a >> 16) |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
func GetLibraryVersion() (major, minor, micro uint) { |
| 476 |
return verMajor, verMinor, verMicro |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
func GetAPI() (uint, error) { |
| 485 |
return getAPI() |
| 486 |
} |
| 487 |
|
| •488 |
func GetApi() (uint, error) { |
| 489 |
return getAPI() |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
func SetAPI(api uint) error { |
| 499 |
return setAPI(api) |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
func (s ScmpSyscall) GetName() (string, error) { |
| 508 |
return s.GetNameByArch(ArchNative) |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
func (s ScmpSyscall) GetNameByArch(arch ScmpArch) (string, error) { |
| 518 |
if err := sanitizeArch(arch); err != nil { |
| 519 |
return "", err |
| 520 |
} |
| 521 |
|
| 522 |
cString := C.seccomp_syscall_resolve_num_arch(arch.toNative(), C.int(s)) |
| 523 |
if cString == nil { |
| 524 |
return "", ErrSyscallDoesNotExist |
| 525 |
} |
| 526 |
defer C.free(unsafe.Pointer(cString)) |
| 527 |
|
| 528 |
finalStr := C.GoString(cString) |
| 529 |
return finalStr, nil |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
func GetSyscallFromName(name string) (ScmpSyscall, error) { |
| 538 |
if err := ensureSupportedVersion(); err != nil { |
| 539 |
return 0, err |
| 540 |
} |
| 541 |
|
| 542 |
cString := C.CString(name) |
| 543 |
defer C.free(unsafe.Pointer(cString)) |
| 544 |
|
| 545 |
result := C.seccomp_syscall_resolve_name(cString) |
| 546 |
if result == scmpError { |
| 547 |
return 0, ErrSyscallDoesNotExist |
| 548 |
} |
| 549 |
|
| 550 |
return ScmpSyscall(result), nil |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
func GetSyscallFromNameByArch(name string, arch ScmpArch) (ScmpSyscall, error) { |
| 559 |
if err := ensureSupportedVersion(); err != nil { |
| 560 |
return 0, err |
| 561 |
} |
| 562 |
if err := sanitizeArch(arch); err != nil { |
| 563 |
return 0, err |
| 564 |
} |
| 565 |
|
| 566 |
cString := C.CString(name) |
| 567 |
defer C.free(unsafe.Pointer(cString)) |
| 568 |
|
| 569 |
result := C.seccomp_syscall_resolve_name_arch(arch.toNative(), cString) |
| 570 |
if result == scmpError { |
| 571 |
return 0, ErrSyscallDoesNotExist |
| 572 |
} |
| 573 |
|
| 574 |
return ScmpSyscall(result), nil |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
func MakeCondition(arg uint, comparison ScmpCompareOp, values ...uint64) (ScmpCondition, error) { |
| 591 |
var condStruct ScmpCondition |
| 592 |
|
| 593 |
if err := ensureSupportedVersion(); err != nil { |
| 594 |
return condStruct, err |
| 595 |
} |
| 596 |
|
| 597 |
if err := sanitizeCompareOp(comparison); err != nil { |
| 598 |
return condStruct, err |
| 599 |
} else if arg > 5 { |
| 600 |
return condStruct, fmt.Errorf("syscalls only have up to 6 arguments (%d given)", arg) |
| 601 |
} else if len(values) > 2 { |
| 602 |
return condStruct, fmt.Errorf("conditions can have at most 2 arguments (%d given)", len(values)) |
| 603 |
} else if len(values) == 0 { |
| 604 |
return condStruct, errors.New("must provide at least one value to compare against") |
| 605 |
} |
| 606 |
|
| 607 |
condStruct.Argument = arg |
| 608 |
condStruct.Op = comparison |
| 609 |
condStruct.Operand1 = values[0] |
| 610 |
if len(values) == 2 { |
| 611 |
condStruct.Operand2 = values[1] |
| 612 |
} else { |
| 613 |
condStruct.Operand2 = 0 |
| 614 |
} |
| 615 |
|
| 616 |
return condStruct, nil |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
func GetNativeArch() (ScmpArch, error) { |
| 624 |
if err := ensureSupportedVersion(); err != nil { |
| 625 |
return ArchInvalid, err |
| 626 |
} |
| 627 |
|
| 628 |
arch := C.seccomp_arch_native() |
| 629 |
|
| 630 |
return archFromNative(arch) |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
type ScmpFilter struct { |
| 639 |
filterCtx C.scmp_filter_ctx |
| 640 |
valid bool |
| 641 |
lock sync.Mutex |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
func NewFilter(defaultAction ScmpAction) (*ScmpFilter, error) { |
| 649 |
if err := ensureSupportedVersion(); err != nil { |
| 650 |
return nil, err |
| 651 |
} |
| 652 |
|
| 653 |
if err := sanitizeAction(defaultAction); err != nil { |
| 654 |
return nil, err |
| 655 |
} |
| 656 |
|
| 657 |
fPtr := C.seccomp_init(defaultAction.toNative()) |
| 658 |
if fPtr == nil { |
| 659 |
return nil, errors.New("could not create filter") |
| 660 |
} |
| 661 |
|
| 662 |
filter := new(ScmpFilter) |
| 663 |
filter.filterCtx = fPtr |
| 664 |
filter.valid = true |
| 665 |
runtime.SetFinalizer(filter, filterFinalizer) |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
if err := filter.setFilterAttr(filterAttrTsync, 0x1); err != nil && err != syscall.ENOTSUP { |
| 670 |
filter.Release() |
| 671 |
return nil, fmt.Errorf("could not create filter: error setting tsync bit: %w", err) |
| 672 |
} |
| 673 |
|
| 674 |
return filter, nil |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
func (f *ScmpFilter) IsValid() bool { |
| 681 |
f.lock.Lock() |
| 682 |
defer f.lock.Unlock() |
| 683 |
|
| 684 |
return f.valid |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
func (f *ScmpFilter) Reset(defaultAction ScmpAction) error { |
| 691 |
f.lock.Lock() |
| 692 |
defer f.lock.Unlock() |
| 693 |
|
| 694 |
if err := sanitizeAction(defaultAction); err != nil { |
| 695 |
return err |
| 696 |
} else if !f.valid { |
| 697 |
return errBadFilter |
| 698 |
} |
| 699 |
|
| 700 |
if retCode := C.seccomp_reset(f.filterCtx, defaultAction.toNative()); retCode != 0 { |
| 701 |
return errRc(retCode) |
| 702 |
} |
| 703 |
|
| 704 |
return nil |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
func (f *ScmpFilter) Release() { |
| 714 |
f.lock.Lock() |
| 715 |
defer f.lock.Unlock() |
| 716 |
|
| 717 |
if !f.valid { |
| 718 |
return |
| 719 |
} |
| 720 |
|
| 721 |
f.valid = false |
| 722 |
C.seccomp_release(f.filterCtx) |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
func (f *ScmpFilter) Merge(src *ScmpFilter) error { |
| 736 |
f.lock.Lock() |
| 737 |
defer f.lock.Unlock() |
| 738 |
|
| 739 |
src.lock.Lock() |
| 740 |
defer src.lock.Unlock() |
| 741 |
|
| 742 |
if !src.valid || !f.valid { |
| 743 |
return errors.New("one or more of the filter contexts is invalid or uninitialized") |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
if retCode := C.seccomp_merge(f.filterCtx, src.filterCtx); retCode != 0 { |
| 748 |
e := errRc(retCode) |
| 749 |
if e == syscall.EINVAL { |
| 750 |
return fmt.Errorf("filters could not be merged due to a mismatch in attributes or invalid filter: %w", e) |
| 751 |
} |
| 752 |
return e |
| 753 |
} |
| 754 |
|
| 755 |
src.valid = false |
| 756 |
|
| 757 |
return nil |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
func (f *ScmpFilter) IsArchPresent(arch ScmpArch) (bool, error) { |
| 772 |
f.lock.Lock() |
| 773 |
defer f.lock.Unlock() |
| 774 |
|
| 775 |
if err := sanitizeArch(arch); err != nil { |
| 776 |
return false, err |
| 777 |
} else if !f.valid { |
| 778 |
return false, errBadFilter |
| 779 |
} |
| 780 |
|
| 781 |
if retCode := C.seccomp_arch_exist(f.filterCtx, arch.toNative()); retCode != 0 { |
| 782 |
e := errRc(retCode) |
| 783 |
if e == syscall.EEXIST { |
| 784 |
|
| 785 |
return false, nil |
| 786 |
} |
| 787 |
return false, e |
| 788 |
} |
| 789 |
|
| 790 |
return true, nil |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
func (f *ScmpFilter) AddArch(arch ScmpArch) error { |
| 798 |
f.lock.Lock() |
| 799 |
defer f.lock.Unlock() |
| 800 |
|
| 801 |
if err := sanitizeArch(arch); err != nil { |
| 802 |
return err |
| 803 |
} else if !f.valid { |
| 804 |
return errBadFilter |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
if retCode := C.seccomp_arch_add(f.filterCtx, arch.toNative()); retCode != 0 { |
| 811 |
if e := errRc(retCode); e != syscall.EEXIST { |
| 812 |
return e |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
return nil |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
func (f *ScmpFilter) RemoveArch(arch ScmpArch) error { |
| 824 |
f.lock.Lock() |
| 825 |
defer f.lock.Unlock() |
| 826 |
|
| 827 |
if err := sanitizeArch(arch); err != nil { |
| 828 |
return err |
| 829 |
} else if !f.valid { |
| 830 |
return errBadFilter |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if retCode := C.seccomp_arch_remove(f.filterCtx, arch.toNative()); retCode != 0 { |
| 837 |
if e := errRc(retCode); e != syscall.EEXIST { |
| 838 |
return e |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
return nil |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
func (f *ScmpFilter) Precompute() error { |
| 851 |
f.lock.Lock() |
| 852 |
defer f.lock.Unlock() |
| 853 |
|
| 854 |
if !f.valid { |
| 855 |
return errBadFilter |
| 856 |
} |
| 857 |
|
| 858 |
if retCode := C.seccomp_precompute(f.filterCtx); retCode != 0 { |
| 859 |
return errRc(retCode) |
| 860 |
} |
| 861 |
|
| 862 |
return nil |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
func (f *ScmpFilter) Load() error { |
| 868 |
f.lock.Lock() |
| 869 |
defer f.lock.Unlock() |
| 870 |
|
| 871 |
if !f.valid { |
| 872 |
return errBadFilter |
| 873 |
} |
| 874 |
|
| 875 |
if retCode := C.seccomp_load(f.filterCtx); retCode != 0 { |
| 876 |
return errRc(retCode) |
| 877 |
} |
| 878 |
|
| 879 |
return nil |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
func (f *ScmpFilter) GetDefaultAction() (ScmpAction, error) { |
| 886 |
action, err := f.getFilterAttr(filterAttrActDefault) |
| 887 |
if err != nil { |
| 888 |
return 0x0, err |
| 889 |
} |
| 890 |
|
| 891 |
return actionFromNative(action) |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
func (f *ScmpFilter) GetBadArchAction() (ScmpAction, error) { |
| 898 |
action, err := f.getFilterAttr(filterAttrActBadArch) |
| 899 |
if err != nil { |
| 900 |
return 0x0, err |
| 901 |
} |
| 902 |
|
| 903 |
return actionFromNative(action) |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
func (f *ScmpFilter) GetNoNewPrivsBit() (bool, error) { |
| 914 |
noNewPrivs, err := f.getFilterAttr(filterAttrNNP) |
| 915 |
if err != nil { |
| 916 |
return false, err |
| 917 |
} |
| 918 |
|
| 919 |
if noNewPrivs == 0 { |
| 920 |
return false, nil |
| 921 |
} |
| 922 |
|
| 923 |
return true, nil |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
func (f *ScmpFilter) GetLogBit() (bool, error) { |
| 933 |
log, err := f.getFilterAttr(filterAttrLog) |
| 934 |
if err != nil { |
| 935 |
if e := checkAPI("GetLogBit", 3, 2, 4, 0); e != nil { |
| 936 |
err = e |
| 937 |
} |
| 938 |
|
| 939 |
return false, err |
| 940 |
} |
| 941 |
|
| 942 |
if log == 0 { |
| 943 |
return false, nil |
| 944 |
} |
| 945 |
|
| 946 |
return true, nil |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
func (f *ScmpFilter) GetSSB() (bool, error) { |
| 956 |
ssb, err := f.getFilterAttr(filterAttrSSB) |
| 957 |
if err != nil { |
| 958 |
if e := checkAPI("GetSSB", 4, 2, 5, 0); e != nil { |
| 959 |
err = e |
| 960 |
} |
| 961 |
|
| 962 |
return false, err |
| 963 |
} |
| 964 |
|
| 965 |
if ssb == 0 { |
| 966 |
return false, nil |
| 967 |
} |
| 968 |
|
| 969 |
return true, nil |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
func (f *ScmpFilter) GetOptimize() (int, error) { |
| 976 |
level, err := f.getFilterAttr(filterAttrOptimize) |
| 977 |
if err != nil { |
| 978 |
if e := checkAPI("GetOptimize", 4, 2, 5, 0); e != nil { |
| 979 |
err = e |
| 980 |
} |
| 981 |
|
| 982 |
return 0, err |
| 983 |
} |
| 984 |
|
| 985 |
return int(level), nil |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
func (f *ScmpFilter) GetRawRC() (bool, error) { |
| 992 |
rawrc, err := f.getFilterAttr(filterAttrRawRC) |
| 993 |
if err != nil { |
| 994 |
if e := checkAPI("GetRawRC", 4, 2, 5, 0); e != nil { |
| 995 |
err = e |
| 996 |
} |
| 997 |
|
| 998 |
return false, err |
| 999 |
} |
| 1000 |
|
| 1001 |
if rawrc == 0 { |
| 1002 |
return false, nil |
| 1003 |
} |
| 1004 |
|
| 1005 |
return true, nil |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
func (f *ScmpFilter) GetWaitKill() (bool, error) { |
| 1012 |
val, err := f.getFilterAttr(filterAttrWaitKill) |
| 1013 |
if err != nil { |
| 1014 |
if e := checkAPI("GetWaitKill", 7, 2, 6, 0); e != nil { |
| 1015 |
err = e |
| 1016 |
} |
| 1017 |
|
| 1018 |
return false, err |
| 1019 |
} |
| 1020 |
if val == 0 { |
| 1021 |
return false, nil |
| 1022 |
} |
| 1023 |
|
| 1024 |
return true, nil |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
func (f *ScmpFilter) SetBadArchAction(action ScmpAction) error { |
| 1031 |
if err := sanitizeAction(action); err != nil { |
| 1032 |
return err |
| 1033 |
} |
| 1034 |
|
| 1035 |
return f.setFilterAttr(filterAttrActBadArch, action.toNative()) |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
func (f *ScmpFilter) SetNoNewPrivsBit(state bool) error { |
| 1044 |
var toSet C.uint32_t = 0x0 |
| 1045 |
|
| 1046 |
if state { |
| 1047 |
toSet = 0x1 |
| 1048 |
} |
| 1049 |
|
| 1050 |
return f.setFilterAttr(filterAttrNNP, toSet) |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
func (f *ScmpFilter) SetLogBit(state bool) error { |
| 1058 |
var toSet C.uint32_t = 0x0 |
| 1059 |
|
| 1060 |
if state { |
| 1061 |
toSet = 0x1 |
| 1062 |
} |
| 1063 |
|
| 1064 |
err := f.setFilterAttr(filterAttrLog, toSet) |
| 1065 |
if err != nil { |
| 1066 |
if e := checkAPI("SetLogBit", 3, 2, 4, 0); e != nil { |
| 1067 |
err = e |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
return err |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
func (f *ScmpFilter) SetSSB(state bool) error { |
| 1079 |
var toSet C.uint32_t = 0x0 |
| 1080 |
|
| 1081 |
if state { |
| 1082 |
toSet = 0x1 |
| 1083 |
} |
| 1084 |
|
| 1085 |
err := f.setFilterAttr(filterAttrSSB, toSet) |
| 1086 |
if err != nil { |
| 1087 |
if e := checkAPI("SetSSB", 4, 2, 5, 0); e != nil { |
| 1088 |
err = e |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
return err |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
func (f *ScmpFilter) SetOptimize(level int) error { |
| 1108 |
cLevel := C.uint32_t(level) |
| 1109 |
|
| 1110 |
err := f.setFilterAttr(filterAttrOptimize, cLevel) |
| 1111 |
if err != nil { |
| 1112 |
if e := checkAPI("SetOptimize", 4, 2, 5, 0); e != nil { |
| 1113 |
err = e |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
return err |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
func (f *ScmpFilter) SetRawRC(state bool) error { |
| 1123 |
var toSet C.uint32_t = 0x0 |
| 1124 |
|
| 1125 |
if state { |
| 1126 |
toSet = 0x1 |
| 1127 |
} |
| 1128 |
|
| 1129 |
err := f.setFilterAttr(filterAttrRawRC, toSet) |
| 1130 |
if err != nil { |
| 1131 |
if e := checkAPI("SetRawRC", 4, 2, 5, 0); e != nil { |
| 1132 |
err = e |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
return err |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
func (f *ScmpFilter) SetWaitKill(state bool) error { |
| 1142 |
var toSet C.uint32_t = 0x0 |
| 1143 |
|
| 1144 |
if state { |
| 1145 |
toSet = 0x1 |
| 1146 |
} |
| 1147 |
|
| 1148 |
err := f.setFilterAttr(filterAttrWaitKill, toSet) |
| 1149 |
if err != nil { |
| 1150 |
if e := checkAPI("SetWaitKill", 7, 2, 6, 0); e != nil { |
| 1151 |
err = e |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
return err |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
func (f *ScmpFilter) SetSyscallPriority(call ScmpSyscall, priority uint8) error { |
| 1164 |
f.lock.Lock() |
| 1165 |
defer f.lock.Unlock() |
| 1166 |
|
| 1167 |
if !f.valid { |
| 1168 |
return errBadFilter |
| 1169 |
} |
| 1170 |
|
| 1171 |
if retCode := C.seccomp_syscall_priority(f.filterCtx, C.int(call), |
| 1172 |
C.uint8_t(priority)); retCode != 0 { |
| 1173 |
return errRc(retCode) |
| 1174 |
} |
| 1175 |
|
| 1176 |
return nil |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
func (f *ScmpFilter) AddRule(call ScmpSyscall, action ScmpAction) error { |
| 1184 |
return f.addRuleGeneric(call, action, false, nil) |
| 1185 |
} |
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
func (f *ScmpFilter) AddRuleExact(call ScmpSyscall, action ScmpAction) error { |
| 1196 |
return f.addRuleGeneric(call, action, true, nil) |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
func (f *ScmpFilter) AddRuleConditional(call ScmpSyscall, action ScmpAction, conds []ScmpCondition) error { |
| 1203 |
return f.addRuleGeneric(call, action, false, conds) |
| 1204 |
} |
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
func (f *ScmpFilter) AddRuleConditionalExact(call ScmpSyscall, action ScmpAction, conds []ScmpCondition) error { |
| 1214 |
return f.addRuleGeneric(call, action, true, conds) |
| 1215 |
} |
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
func (f *ScmpFilter) ExportPFC(file *os.File) error { |
| 1222 |
f.lock.Lock() |
| 1223 |
defer f.lock.Unlock() |
| 1224 |
|
| 1225 |
fd := file.Fd() |
| 1226 |
|
| 1227 |
if !f.valid { |
| 1228 |
return errBadFilter |
| 1229 |
} |
| 1230 |
|
| 1231 |
if retCode := C.seccomp_export_pfc(f.filterCtx, C.int(fd)); retCode != 0 { |
| 1232 |
return errRc(retCode) |
| 1233 |
} |
| 1234 |
|
| 1235 |
return nil |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
func (f *ScmpFilter) ExportBPF(file *os.File) error { |
| 1243 |
f.lock.Lock() |
| 1244 |
defer f.lock.Unlock() |
| 1245 |
|
| 1246 |
fd := file.Fd() |
| 1247 |
|
| 1248 |
if !f.valid { |
| 1249 |
return errBadFilter |
| 1250 |
} |
| 1251 |
|
| 1252 |
if retCode := C.seccomp_export_bpf(f.filterCtx, C.int(fd)); retCode != 0 { |
| 1253 |
return errRc(retCode) |
| 1254 |
} |
| 1255 |
|
| 1256 |
return nil |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
func (f *ScmpFilter) ExportBPFMem() ([]byte, error) { |
| 1262 |
f.lock.Lock() |
| 1263 |
defer f.lock.Unlock() |
| 1264 |
|
| 1265 |
if !f.valid { |
| 1266 |
return nil, errBadFilter |
| 1267 |
} |
| 1268 |
|
| 1269 |
var len C.size_t |
| 1270 |
|
| 1271 |
if retCode := C.seccomp_export_bpf_mem(f.filterCtx, unsafe.Pointer(nil), &len); retCode < 0 { |
| 1272 |
return nil, errRc(retCode) |
| 1273 |
} |
| 1274 |
|
| 1275 |
buf := make([]byte, int(len)) |
| 1276 |
if retCode := C.seccomp_export_bpf_mem(f.filterCtx, unsafe.Pointer(&buf[0]), &len); retCode < 0 { |
| 1277 |
return nil, errRc(retCode) |
| 1278 |
} |
| 1279 |
|
| 1280 |
return buf, nil |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
func (f *ScmpFilter) GetNotifFd() (ScmpFd, error) { |
| 1291 |
return f.getNotifFd() |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
func NotifReceive(fd ScmpFd) (*ScmpNotifReq, error) { |
| 1300 |
return notifReceive(fd) |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
func NotifRespond(fd ScmpFd, scmpResp *ScmpNotifResp) error { |
| 1306 |
return notifRespond(fd, scmpResp) |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
func NotifIDValid(fd ScmpFd, id uint64) error { |
| 1313 |
return notifIDValid(fd, id) |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
func (f *ScmpFilter) TransactionStart() error { |
| 1324 |
f.lock.Lock() |
| 1325 |
defer f.lock.Unlock() |
| 1326 |
|
| 1327 |
if !f.valid { |
| 1328 |
return errBadFilter |
| 1329 |
} |
| 1330 |
|
| 1331 |
if retCode := C.seccomp_transaction_start(f.filterCtx); retCode < 0 { |
| 1332 |
return errRc(retCode) |
| 1333 |
} |
| 1334 |
|
| 1335 |
return nil |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
func (f *ScmpFilter) TransactionReject() { |
| 1340 |
f.lock.Lock() |
| 1341 |
defer f.lock.Unlock() |
| 1342 |
|
| 1343 |
if !f.valid { |
| 1344 |
return |
| 1345 |
} |
| 1346 |
|
| 1347 |
C.seccomp_transaction_reject(f.filterCtx) |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
func (f *ScmpFilter) TransactionCommit() error { |
| 1352 |
f.lock.Lock() |
| 1353 |
defer f.lock.Unlock() |
| 1354 |
|
| 1355 |
if !f.valid { |
| 1356 |
return errBadFilter |
| 1357 |
} |
| 1358 |
|
| 1359 |
if retCode := C.seccomp_transaction_commit(f.filterCtx); retCode < 0 { |
| 1360 |
return errRc(retCode) |
| 1361 |
} |
| 1362 |
|
| 1363 |
return nil |
| 1364 |
} |