| 1 |
// Public API specification for libseccomp Go bindings |
| 2 |
// Contains public API for the bindings |
| 3 |
|
| 4 |
// Package seccomp provides bindings for libseccomp, a library wrapping the Linux |
| 5 |
// seccomp syscall. Seccomp enables an application to restrict system call use |
| 6 |
// for itself and its children. |
| 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 |
#include <errno.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <seccomp.h> |
| 24 |
|
| 25 |
// The following functions were added in libseccomp v2.6.0. |
| 26 |
#if SCMP_VER_MAJOR == 2 && SCMP_VER_MINOR < 6 |
| 27 |
int seccomp_precompute(scmp_filter_ctx ctx) { |
| 28 |
return -EOPNOTSUPP; |
| 29 |
} |
| 30 |
int seccomp_export_bpf_mem(const scmp_filter_ctx ctx, void *buf, size_t *len) { |
| 31 |
return -EOPNOTSUPP; |
| 32 |
} |
| 33 |
int seccomp_transaction_start(const scmp_filter_ctx ctx) { |
| 34 |
return -EOPNOTSUPP; |
| 35 |
} |
| 36 |
int seccomp_transaction_commit(const scmp_filter_ctx ctx) { |
| 37 |
return -EOPNOTSUPP; |
| 38 |
} |
| 39 |
void seccomp_transaction_reject(const scmp_filter_ctx ctx) {} |
| 40 |
#endif |
| 41 |
*/ |
| 42 |
import "C" |
| 43 |
|
| 44 |
// Exported types |
| 45 |
|
| 46 |
// VersionError represents an error when either the system libseccomp version |
| 47 |
// or the kernel version is too old to perform the operation requested. |
| 48 |
type VersionError struct { |
| 49 |
op string // operation that failed or would fail |
| 50 |
major, minor, micro uint // minimally required libseccomp version |
| 51 |
curAPI, minAPI uint // current and minimally required API versions |
| 52 |
} |
| 53 |
|
| 54 |
func init() { |
| 55 |
// This forces the cgo libseccomp to initialize its internal API support state, |
| 56 |
// which is necessary on older versions of libseccomp (< 2.5.0) in order to work |
| 57 |
// correctly. |
| 58 |
// TODO: remove once libseccomp < v2.5.0 is not supported. |
| 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 |
// ScmpArch represents a CPU architecture. Seccomp can restrict syscalls on a |
| 74 |
// per-architecture basis. |
| 75 |
type ScmpArch uint |
| 76 |
|
| 77 |
// ScmpAction represents an action to be taken on a filter rule match in |
| 78 |
// libseccomp |
| 79 |
type ScmpAction uint |
| 80 |
|
| 81 |
// ScmpCompareOp represents a comparison operator which can be used in a filter |
| 82 |
// rule |
| 83 |
type ScmpCompareOp uint |
| 84 |
|
| 85 |
// ScmpCondition represents a rule in a libseccomp filter context |
| 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 |
// Seccomp userspace notification structures associated with filters that use the ActNotify action. |
| 94 |
|
| 95 |
// ScmpSyscall identifies a Linux System Call by its number. |
| 96 |
type ScmpSyscall int32 |
| 97 |
|
| 98 |
// ScmpFd represents a file-descriptor used for seccomp userspace notifications. |
| 99 |
type ScmpFd int32 |
| 100 |
|
| 101 |
// ScmpNotifData describes the system call context that triggered a notification. |
| 102 |
type ScmpNotifData struct { |
| 103 |
// Syscall is the syscall number. |
| 104 |
Syscall ScmpSyscall `json:"syscall,omitempty"` |
| 105 |
// Arch is the filter architecture. |
| 106 |
Arch ScmpArch `json:"arch,omitempty"` |
| 107 |
// InstrPointer is the address of the instruction that triggered a notification. |
| 108 |
InstrPointer uint64 `json:"instr_pointer,omitempty"` |
| 109 |
// Args are the arguments (up to 6) for the syscall. |
| 110 |
Args []uint64 `json:"args,omitempty"` |
| 111 |
} |
| 112 |
|
| 113 |
// ScmpNotifReq represents a seccomp userspace notification. See NotifReceive() for |
| 114 |
// info on how to pull such a notification. |
| 115 |
type ScmpNotifReq struct { |
| 116 |
// ID is the notification ID. |
| 117 |
ID uint64 `json:"id,omitempty"` |
| 118 |
// Pid is the process that triggered the notification event. |
| 119 |
Pid uint32 `json:"pid,omitempty"` |
| 120 |
// Flags is filter flags (see seccomp(2)). |
| 121 |
Flags uint32 `json:"flags,omitempty"` |
| 122 |
// Data is system call context that triggered the notification. |
| 123 |
Data ScmpNotifData `json:"data,omitempty"` |
| 124 |
} |
| 125 |
|
| 126 |
// ScmpNotifResp represents a seccomp userspace notification response. See NotifRespond() |
| 127 |
// for info on how to push such a response. |
| 128 |
type ScmpNotifResp struct { |
| 129 |
// ID is the notification ID (must match the corresponding ScmpNotifReq ID). |
| 130 |
ID uint64 `json:"id,omitempty"` |
| 131 |
// Error must be 0 if no error occurred, or an error constant from |
| 132 |
// package syscall (e.g., syscall.EPERM, etc). In the latter case, it |
| 133 |
// is used as an error return from the syscall that created the |
| 134 |
// notification. |
| 135 |
Error int32 `json:"error,omitempty"` |
| 136 |
// Val is a return value for the syscall that created the notification. |
| 137 |
// Only relevant if Error is 0. |
| 138 |
Val uint64 `json:"val,omitempty"` |
| 139 |
// Flags is userspace notification response flag (e.g., NotifRespFlagContinue). |
| 140 |
Flags uint32 `json:"flags,omitempty"` |
| 141 |
} |
| 142 |
|
| 143 |
// Exported Constants |
| 144 |
|
| 145 |
const ( |
| 146 |
// Valid architectures recognized by libseccomp |
| 147 |
// PowerPC and S390(x) architectures are unavailable below library version |
| 148 |
// v2.3.0 and will returns errors if used with incompatible libraries |
| 149 |
|
| 150 |
// ArchInvalid is a placeholder to ensure uninitialized ScmpArch |
| 151 |
// variables are invalid |
| 152 |
ArchInvalid ScmpArch = iota |
| 153 |
// ArchNative is the native architecture of the kernel |
| 154 |
ArchNative |
| 155 |
// ArchX86 represents 32-bit x86 syscalls |
| 156 |
ArchX86 |
| 157 |
// ArchAMD64 represents 64-bit x86-64 syscalls |
| 158 |
ArchAMD64 |
| 159 |
// ArchX32 represents 64-bit x86-64 syscalls (32-bit pointers) |
| 160 |
ArchX32 |
| 161 |
// ArchARM represents 32-bit ARM syscalls |
| 162 |
ArchARM |
| 163 |
// ArchARM64 represents 64-bit ARM syscalls |
| 164 |
ArchARM64 |
| 165 |
// ArchMIPS represents 32-bit MIPS syscalls |
| 166 |
ArchMIPS |
| 167 |
// ArchMIPS64 represents 64-bit MIPS syscalls |
| 168 |
ArchMIPS64 |
| 169 |
// ArchMIPS64N32 represents 64-bit MIPS syscalls (32-bit pointers) |
| 170 |
ArchMIPS64N32 |
| 171 |
// ArchMIPSEL represents 32-bit MIPS syscalls (little endian) |
| 172 |
ArchMIPSEL |
| 173 |
// ArchMIPSEL64 represents 64-bit MIPS syscalls (little endian) |
| 174 |
ArchMIPSEL64 |
| 175 |
// ArchMIPSEL64N32 represents 64-bit MIPS syscalls (little endian, |
| 176 |
// 32-bit pointers) |
| 177 |
ArchMIPSEL64N32 |
| 178 |
// ArchPPC represents 32-bit POWERPC syscalls |
| 179 |
ArchPPC |
| 180 |
// ArchPPC64 represents 64-bit POWER syscalls (big endian) |
| 181 |
ArchPPC64 |
| 182 |
// ArchPPC64LE represents 64-bit POWER syscalls (little endian) |
| 183 |
ArchPPC64LE |
| 184 |
// ArchS390 represents 31-bit System z/390 syscalls |
| 185 |
ArchS390 |
| 186 |
// ArchS390X represents 64-bit System z/390 syscalls |
| 187 |
ArchS390X |
| 188 |
// ArchPARISC represents 32-bit PA-RISC |
| 189 |
ArchPARISC |
| 190 |
// ArchPARISC64 represents 64-bit PA-RISC |
| 191 |
ArchPARISC64 |
| 192 |
// ArchRISCV64 represents RISCV64 |
| 193 |
ArchRISCV64 |
| 194 |
// ArchLOONGARCH64 represents 64-bit LoongArch. |
| 195 |
ArchLOONGARCH64 |
| 196 |
// ArchM68K represents 32-bit Motorola 68000. |
| 197 |
ArchM68K |
| 198 |
// ArchSH represents SuperH. |
| 199 |
ArchSH |
| 200 |
// ArchSHEB represents Big-endian SuperH. |
| 201 |
ArchSHEB |
| 202 |
) |
| 203 |
|
| 204 |
const ( |
| 205 |
// Supported actions on filter match |
| 206 |
|
| 207 |
// ActInvalid is a placeholder to ensure uninitialized ScmpAction |
| 208 |
// variables are invalid |
| 209 |
ActInvalid ScmpAction = iota |
| 210 |
// ActKillThread kills the thread that violated the rule. |
| 211 |
// All other threads from the same thread group will continue to execute. |
| 212 |
ActKillThread |
| 213 |
// ActTrap throws SIGSYS |
| 214 |
ActTrap |
| 215 |
// ActNotify triggers a userspace notification. This action is only usable when |
| 216 |
// libseccomp API level 6 or higher is supported. |
| 217 |
ActNotify |
| 218 |
// ActErrno causes the syscall to return a negative error code. This |
| 219 |
// code can be set with the SetReturnCode method |
| 220 |
ActErrno |
| 221 |
// ActTrace causes the syscall to notify tracing processes with the |
| 222 |
// given error code. This code can be set with the SetReturnCode method |
| 223 |
ActTrace |
| 224 |
// ActAllow permits the syscall to continue execution |
| 225 |
ActAllow |
| 226 |
// ActLog permits the syscall to continue execution after logging it. |
| 227 |
// This action is only usable when libseccomp API level 3 or higher is |
| 228 |
// supported. |
| 229 |
ActLog |
| 230 |
// ActKillProcess kills the process that violated the rule. |
| 231 |
// All threads in the thread group are also terminated. |
| 232 |
// This action is only usable when libseccomp API level 3 or higher is |
| 233 |
// supported. |
| 234 |
ActKillProcess |
| 235 |
// ActKill kills the thread that violated the rule. |
| 236 |
// All other threads from the same thread group will continue to execute. |
| 237 |
// |
| 238 |
// Deprecated: use ActKillThread |
| 239 |
ActKill = ActKillThread |
| 240 |
) |
| 241 |
|
| 242 |
const ( |
| 243 |
// These are comparison operators used in conditional seccomp rules |
| 244 |
// They are used to compare the value of a single argument of a syscall |
| 245 |
// against a user-defined constant |
| 246 |
|
| 247 |
// CompareInvalid is a placeholder to ensure uninitialized ScmpCompareOp |
| 248 |
// variables are invalid |
| 249 |
CompareInvalid ScmpCompareOp = iota |
| 250 |
// CompareNotEqual returns true if the argument is not equal to the |
| 251 |
// given value |
| 252 |
CompareNotEqual |
| 253 |
// CompareLess returns true if the argument is less than the given value |
| 254 |
CompareLess |
| 255 |
// CompareLessOrEqual returns true if the argument is less than or equal |
| 256 |
// to the given value |
| 257 |
CompareLessOrEqual |
| 258 |
// CompareEqual returns true if the argument is equal to the given value |
| 259 |
CompareEqual |
| 260 |
// CompareGreaterEqual returns true if the argument is greater than or |
| 261 |
// equal to the given value |
| 262 |
CompareGreaterEqual |
| 263 |
// CompareGreater returns true if the argument is greater than the given |
| 264 |
// value |
| 265 |
CompareGreater |
| 266 |
// CompareMaskedEqual returns true if the masked argument value is |
| 267 |
// equal to the masked datum value. Mask is the first argument, and |
| 268 |
// datum is the second one. |
| 269 |
CompareMaskedEqual |
| 270 |
) |
| 271 |
|
| 272 |
// ErrSyscallDoesNotExist represents an error condition where |
| 273 |
// libseccomp is unable to resolve the syscall. |
| 274 |
var ErrSyscallDoesNotExist = errors.New("could not resolve syscall name") |
| 275 |
|
| 276 |
const ( |
| 277 |
// Userspace notification response flags |
| 278 |
|
| 279 |
// NotifRespFlagContinue tells the kernel to continue executing the system |
| 280 |
// call that triggered the notification. Must only be used when the notification |
| 281 |
// response's error is 0. |
| 282 |
NotifRespFlagContinue uint32 = 1 |
| 283 |
) |
| 284 |
|
| 285 |
// Helpers for types |
| 286 |
|
| 287 |
// GetArchFromString returns an ScmpArch constant from a string representing an |
| 288 |
// architecture |
| 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 |
// String returns a string representation of an architecture constant |
| 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 |
// String returns a string representation of a comparison operator constant |
| 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 |
// String returns a string representation of a seccomp match action |
| 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 |
// SetReturnCode adds a return code to a supporting ScmpAction, clearing any |
| 454 |
// existing code Only valid on ActErrno and ActTrace. Takes no action otherwise. |
| 455 |
// Accepts 16-bit return code as argument. |
| 456 |
// Returns a valid ScmpAction of the original type with the new error code set. |
| 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 |
// GetReturnCode returns the return code of an ScmpAction |
| 466 |
func (a ScmpAction) GetReturnCode() int16 { |
| 467 |
return int16(a >> 16) |
| 468 |
} |
| 469 |
|
| 470 |
// General utility functions |
| 471 |
|
| 472 |
// GetLibraryVersion returns the version of the library the bindings are built |
| 473 |
// against. |
| 474 |
// The version is formatted as follows: Major.Minor.Micro |
| 475 |
func GetLibraryVersion() (major, minor, micro uint) { |
| 476 |
return verMajor, verMinor, verMicro |
| 477 |
} |
| 478 |
|
| 479 |
// GetAPI returns the API level supported by the system. |
| 480 |
// Returns a positive int containing the API level, or 0 with an error if the |
| 481 |
// API level could not be detected due to the library being older than v2.4.0. |
| 482 |
// See the seccomp_api_get(3) man page for details on available API levels: |
| 483 |
// https://github.com/seccomp/libseccomp/blob/main/doc/man/man3/seccomp_api_get.3 |
| 484 |
func GetAPI() (uint, error) { |
| 485 |
return getAPI() |
| 486 |
} |
| 487 |
|
| 488 |
// SetAPI forcibly sets the API level. General use of this function is strongly |
| 489 |
// discouraged. |
| 490 |
// Returns an error if the API level could not be set. An error is always |
| 491 |
// returned if the library is older than v2.4.0 |
| 492 |
// See the seccomp_api_get(3) man page for details on available API levels: |
| 493 |
// https://github.com/seccomp/libseccomp/blob/main/doc/man/man3/seccomp_api_get.3 |
| 494 |
func SetAPI(api uint) error { |
| 495 |
return setAPI(api) |
| 496 |
} |
| 497 |
|
| 498 |
// Syscall functions |
| 499 |
|
| 500 |
// GetName retrieves the name of a syscall from its number. |
| 501 |
// Acts on any syscall number. |
| 502 |
// Returns either a string containing the name of the syscall, or an error. |
| 503 |
func (s ScmpSyscall) GetName() (string, error) { |
| 504 |
return s.GetNameByArch(ArchNative) |
| 505 |
} |
| 506 |
|
| 507 |
// GetNameByArch retrieves the name of a syscall from its number for a given |
| 508 |
// architecture. |
| 509 |
// Acts on any syscall number. |
| 510 |
// Accepts a valid architecture constant. |
| 511 |
// Returns either a string containing the name of the syscall, or an error. |
| 512 |
// if the syscall is unrecognized or an issue occurred. |
| 513 |
func (s ScmpSyscall) GetNameByArch(arch ScmpArch) (string, error) { |
| 514 |
if err := sanitizeArch(arch); err != nil { |
| 515 |
return "", err |
| 516 |
} |
| 517 |
|
| 518 |
cString := C.seccomp_syscall_resolve_num_arch(arch.toNative(), C.int(s)) |
| 519 |
if cString == nil { |
| 520 |
return "", ErrSyscallDoesNotExist |
| 521 |
} |
| 522 |
defer C.free(unsafe.Pointer(cString)) |
| 523 |
|
| 524 |
finalStr := C.GoString(cString) |
| 525 |
return finalStr, nil |
| 526 |
} |
| 527 |
|
| 528 |
// GetSyscallFromName returns the number of a syscall by name on the kernel's |
| 529 |
// native architecture. |
| 530 |
// Accepts a string containing the name of a syscall. |
| 531 |
// Returns the number of the syscall, or an error if no syscall with that name |
| 532 |
// was found. |
| 533 |
func GetSyscallFromName(name string) (ScmpSyscall, error) { |
| 534 |
if err := ensureSupportedVersion(); err != nil { |
| 535 |
return 0, err |
| 536 |
} |
| 537 |
|
| 538 |
cString := C.CString(name) |
| 539 |
defer C.free(unsafe.Pointer(cString)) |
| 540 |
|
| 541 |
result := C.seccomp_syscall_resolve_name(cString) |
| 542 |
if result == scmpError { |
| 543 |
return 0, ErrSyscallDoesNotExist |
| 544 |
} |
| 545 |
|
| 546 |
return ScmpSyscall(result), nil |
| 547 |
} |
| 548 |
|
| 549 |
// GetSyscallFromNameByArch returns the number of a syscall by name for a given |
| 550 |
// architecture's ABI. |
| 551 |
// Accepts the name of a syscall and an architecture constant. |
| 552 |
// Returns the number of the syscall, or an error if an invalid architecture is |
| 553 |
// passed or a syscall with that name was not found. |
| 554 |
func GetSyscallFromNameByArch(name string, arch ScmpArch) (ScmpSyscall, error) { |
| 555 |
if err := ensureSupportedVersion(); err != nil { |
| 556 |
return 0, err |
| 557 |
} |
| 558 |
if err := sanitizeArch(arch); err != nil { |
| 559 |
return 0, err |
| 560 |
} |
| 561 |
|
| 562 |
cString := C.CString(name) |
| 563 |
defer C.free(unsafe.Pointer(cString)) |
| 564 |
|
| 565 |
result := C.seccomp_syscall_resolve_name_arch(arch.toNative(), cString) |
| 566 |
if result == scmpError { |
| 567 |
return 0, ErrSyscallDoesNotExist |
| 568 |
} |
| 569 |
|
| 570 |
return ScmpSyscall(result), nil |
| 571 |
} |
| 572 |
|
| 573 |
// MakeCondition creates and returns a new condition to attach to a filter rule. |
| 574 |
// Associated rules will only match if this condition is true. |
| 575 |
// Accepts the number the argument we are checking, and a comparison operator |
| 576 |
// and value to compare to. |
| 577 |
// The rule will match if argument $arg (zero-indexed) of the syscall is |
| 578 |
// $COMPARE_OP the provided comparison value. |
| 579 |
// Some comparison operators accept two values. Masked equals, for example, |
| 580 |
// will mask $arg of the syscall with the second value provided (via bitwise |
| 581 |
// AND) and then compare against the first value provided. |
| 582 |
// For example, in the less than or equal case, if the syscall argument was |
| 583 |
// 0 and the value provided was 1, the condition would match, as 0 is less |
| 584 |
// than or equal to 1. |
| 585 |
// Return either an error on bad argument or a valid ScmpCondition struct. |
| 586 |
func MakeCondition(arg uint, comparison ScmpCompareOp, values ...uint64) (ScmpCondition, error) { |
| 587 |
var condStruct ScmpCondition |
| 588 |
|
| 589 |
if err := ensureSupportedVersion(); err != nil { |
| 590 |
return condStruct, err |
| 591 |
} |
| 592 |
|
| 593 |
if err := sanitizeCompareOp(comparison); err != nil { |
| 594 |
return condStruct, err |
| 595 |
} else if arg > 5 { |
| 596 |
return condStruct, fmt.Errorf("syscalls only have up to 6 arguments (%d given)", arg) |
| 597 |
} else if len(values) > 2 { |
| 598 |
return condStruct, fmt.Errorf("conditions can have at most 2 arguments (%d given)", len(values)) |
| 599 |
} else if len(values) == 0 { |
| 600 |
return condStruct, errors.New("must provide at least one value to compare against") |
| 601 |
} |
| 602 |
|
| 603 |
condStruct.Argument = arg |
| 604 |
condStruct.Op = comparison |
| 605 |
condStruct.Operand1 = values[0] |
| 606 |
if len(values) == 2 { |
| 607 |
condStruct.Operand2 = values[1] |
| 608 |
} else { |
| 609 |
condStruct.Operand2 = 0 // Unused |
| 610 |
} |
| 611 |
|
| 612 |
return condStruct, nil |
| 613 |
} |
| 614 |
|
| 615 |
// Utility Functions |
| 616 |
|
| 617 |
// GetNativeArch returns architecture token representing the native kernel |
| 618 |
// architecture |
| 619 |
func GetNativeArch() (ScmpArch, error) { |
| 620 |
if err := ensureSupportedVersion(); err != nil { |
| 621 |
return ArchInvalid, err |
| 622 |
} |
| 623 |
|
| 624 |
arch := C.seccomp_arch_native() |
| 625 |
|
| 626 |
return archFromNative(arch) |
| 627 |
} |
| 628 |
|
| 629 |
// Public Filter API |
| 630 |
|
| 631 |
// ScmpFilter represents a filter context in libseccomp. |
| 632 |
// A filter context is initially empty. Rules can be added to it, and it can |
| 633 |
// then be loaded into the kernel. |
| 634 |
type ScmpFilter struct { |
| 635 |
filterCtx C.scmp_filter_ctx |
| 636 |
valid bool |
| 637 |
lock sync.Mutex |
| 638 |
} |
| 639 |
|
| 640 |
// NewFilter creates and returns a new filter context. Accepts a default action to be |
| 641 |
// taken for syscalls which match no rules in the filter. |
| 642 |
// Returns a reference to a valid filter context, or nil and an error |
| 643 |
// if the filter context could not be created or an invalid default action was given. |
| 644 |
func NewFilter(defaultAction ScmpAction) (*ScmpFilter, error) { |
| 645 |
if err := ensureSupportedVersion(); err != nil { |
| 646 |
return nil, err |
| 647 |
} |
| 648 |
|
| 649 |
if err := sanitizeAction(defaultAction); err != nil { |
| 650 |
return nil, err |
| 651 |
} |
| 652 |
|
| 653 |
fPtr := C.seccomp_init(defaultAction.toNative()) |
| 654 |
if fPtr == nil { |
| 655 |
return nil, errors.New("could not create filter") |
| 656 |
} |
| 657 |
|
| 658 |
filter := new(ScmpFilter) |
| 659 |
filter.filterCtx = fPtr |
| 660 |
filter.valid = true |
| 661 |
runtime.SetFinalizer(filter, filterFinalizer) |
| 662 |
|
| 663 |
// Enable TSync so all goroutines will receive the same rules. |
| 664 |
// If the kernel does not support TSYNC, allow us to continue without error. |
| 665 |
if err := filter.setFilterAttr(filterAttrTsync, 0x1); err != nil && err != syscall.ENOTSUP { |
| 666 |
filter.Release() |
| 667 |
return nil, fmt.Errorf("could not create filter: error setting tsync bit: %w", err) |
| 668 |
} |
| 669 |
|
| 670 |
return filter, nil |
| 671 |
} |
| 672 |
|
| 673 |
// IsValid determines whether a filter context is valid to use. |
| 674 |
// Some operations (Release and Merge) render filter contexts invalid and |
| 675 |
// consequently prevent further use. |
| 676 |
func (f *ScmpFilter) IsValid() bool { |
| 677 |
f.lock.Lock() |
| 678 |
defer f.lock.Unlock() |
| 679 |
|
| 680 |
return f.valid |
| 681 |
} |
| 682 |
|
| 683 |
// Reset resets a filter context, removing all its existing state. |
| 684 |
// Accepts a new default action to be taken for syscalls which do not match. |
| 685 |
// Returns an error if the filter or action provided are invalid. |
| 686 |
func (f *ScmpFilter) Reset(defaultAction ScmpAction) error { |
| 687 |
f.lock.Lock() |
| 688 |
defer f.lock.Unlock() |
| 689 |
|
| 690 |
if err := sanitizeAction(defaultAction); err != nil { |
| 691 |
return err |
| 692 |
} else if !f.valid { |
| 693 |
return errBadFilter |
| 694 |
} |
| 695 |
|
| 696 |
if retCode := C.seccomp_reset(f.filterCtx, defaultAction.toNative()); retCode != 0 { |
| 697 |
return errRc(retCode) |
| 698 |
} |
| 699 |
|
| 700 |
return nil |
| 701 |
} |
| 702 |
|
| 703 |
// Release releases a filter context, freeing its memory. Should be called after |
| 704 |
// loading into the kernel, when the filter is no longer needed. |
| 705 |
// After calling this function, the given filter is no longer valid and cannot |
| 706 |
// be used. |
| 707 |
// Release() will be invoked automatically when a filter context is garbage |
| 708 |
// collected, but can also be called manually to free memory. |
| 709 |
func (f *ScmpFilter) Release() { |
| 710 |
f.lock.Lock() |
| 711 |
defer f.lock.Unlock() |
| 712 |
|
| 713 |
if !f.valid { |
| 714 |
return |
| 715 |
} |
| 716 |
|
| 717 |
f.valid = false |
| 718 |
C.seccomp_release(f.filterCtx) |
| 719 |
} |
| 720 |
|
| 721 |
// Merge merges two filter contexts. |
| 722 |
// The source filter src will be released as part of the process, and will no |
| 723 |
// longer be usable or valid after this call. |
| 724 |
// To be merged, filters must NOT share any architectures, and all their |
| 725 |
// attributes (Default Action, Bad Arch Action, and No New Privs bools) |
| 726 |
// must match. |
| 727 |
// The filter src will be merged into the filter this is called on. |
| 728 |
// The architectures of the src filter not present in the destination, and all |
| 729 |
// associated rules, will be added to the destination. |
| 730 |
// Returns an error if merging the filters failed. |
| 731 |
func (f *ScmpFilter) Merge(src *ScmpFilter) error { |
| 732 |
f.lock.Lock() |
| 733 |
defer f.lock.Unlock() |
| 734 |
|
| 735 |
src.lock.Lock() |
| 736 |
defer src.lock.Unlock() |
| 737 |
|
| 738 |
if !src.valid || !f.valid { |
| 739 |
return errors.New("one or more of the filter contexts is invalid or uninitialized") |
| 740 |
} |
| 741 |
|
| 742 |
// Merge the filters |
| 743 |
if retCode := C.seccomp_merge(f.filterCtx, src.filterCtx); retCode != 0 { |
| 744 |
e := errRc(retCode) |
| 745 |
if e == syscall.EINVAL { |
| 746 |
return fmt.Errorf("filters could not be merged due to a mismatch in attributes or invalid filter: %w", e) |
| 747 |
} |
| 748 |
return e |
| 749 |
} |
| 750 |
|
| 751 |
src.valid = false |
| 752 |
|
| 753 |
return nil |
| 754 |
} |
| 755 |
|
| 756 |
// IsArchPresent checks if an architecture is present in a filter. |
| 757 |
// If a filter contains an architecture, it uses its default action for |
| 758 |
// syscalls which do not match rules in it, and its rules can match syscalls |
| 759 |
// for that ABI. |
| 760 |
// If a filter does not contain an architecture, all syscalls made to that |
| 761 |
// kernel ABI will fail with the filter's default Bad Architecture Action |
| 762 |
// (by default, killing the process). |
| 763 |
// Accepts an architecture constant. |
| 764 |
// Returns true if the architecture is present in the filter, false otherwise, |
| 765 |
// and an error on an invalid filter context, architecture constant, or an |
| 766 |
// issue with the call to libseccomp. |
| 767 |
func (f *ScmpFilter) IsArchPresent(arch ScmpArch) (bool, error) { |
| 768 |
f.lock.Lock() |
| 769 |
defer f.lock.Unlock() |
| 770 |
|
| 771 |
if err := sanitizeArch(arch); err != nil { |
| 772 |
return false, err |
| 773 |
} else if !f.valid { |
| 774 |
return false, errBadFilter |
| 775 |
} |
| 776 |
|
| 777 |
if retCode := C.seccomp_arch_exist(f.filterCtx, arch.toNative()); retCode != 0 { |
| 778 |
e := errRc(retCode) |
| 779 |
if e == syscall.EEXIST { |
| 780 |
// -EEXIST is "arch not present" |
| 781 |
return false, nil |
| 782 |
} |
| 783 |
return false, e |
| 784 |
} |
| 785 |
|
| 786 |
return true, nil |
| 787 |
} |
| 788 |
|
| 789 |
// AddArch adds an architecture to the filter. |
| 790 |
// Accepts an architecture constant. |
| 791 |
// Returns an error on invalid filter context or architecture token, or an |
| 792 |
// issue with the call to libseccomp. |
| 793 |
func (f *ScmpFilter) AddArch(arch ScmpArch) error { |
| 794 |
f.lock.Lock() |
| 795 |
defer f.lock.Unlock() |
| 796 |
|
| 797 |
if err := sanitizeArch(arch); err != nil { |
| 798 |
return err |
| 799 |
} else if !f.valid { |
| 800 |
return errBadFilter |
| 801 |
} |
| 802 |
|
| 803 |
// Libseccomp returns -EEXIST if the specified architecture is already |
| 804 |
// present. Succeed silently in this case, as it's not fatal, and the |
| 805 |
// architecture is present already. |
| 806 |
if retCode := C.seccomp_arch_add(f.filterCtx, arch.toNative()); retCode != 0 { |
| 807 |
if e := errRc(retCode); e != syscall.EEXIST { |
| 808 |
return e |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
return nil |
| 813 |
} |
| 814 |
|
| 815 |
// RemoveArch removes an architecture from the filter. |
| 816 |
// Accepts an architecture constant. |
| 817 |
// Returns an error on invalid filter context or architecture token, or an |
| 818 |
// issue with the call to libseccomp. |
| 819 |
func (f *ScmpFilter) RemoveArch(arch ScmpArch) error { |
| 820 |
f.lock.Lock() |
| 821 |
defer f.lock.Unlock() |
| 822 |
|
| 823 |
if err := sanitizeArch(arch); err != nil { |
| 824 |
return err |
| 825 |
} else if !f.valid { |
| 826 |
return errBadFilter |
| 827 |
} |
| 828 |
|
| 829 |
// Similar to AddArch, -EEXIST is returned if the arch is not present |
| 830 |
// Succeed silently in that case, this is not fatal and the architecture |
| 831 |
// is not present in the filter after RemoveArch |
| 832 |
if retCode := C.seccomp_arch_remove(f.filterCtx, arch.toNative()); retCode != 0 { |
| 833 |
if e := errRc(retCode); e != syscall.EEXIST { |
| 834 |
return e |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
return nil |
| 839 |
} |
| 840 |
|
| 841 |
// Precompute precomputes the seccomp filter for later use by [Load] and |
| 842 |
// similar functions. Not only does this improve performance of [Load], |
| 843 |
// it also ensures that the seccomp filter can be loaded in an |
| 844 |
// async-signal-safe manner if no changes have been made to the filter |
| 845 |
// since it was precomputed. |
| 846 |
func (f *ScmpFilter) Precompute() error { |
| 847 |
f.lock.Lock() |
| 848 |
defer f.lock.Unlock() |
| 849 |
|
| 850 |
if !f.valid { |
| 851 |
return errBadFilter |
| 852 |
} |
| 853 |
|
| 854 |
if retCode := C.seccomp_precompute(f.filterCtx); retCode != 0 { |
| 855 |
return errRc(retCode) |
| 856 |
} |
| 857 |
|
| 858 |
return nil |
| 859 |
} |
| 860 |
|
| 861 |
// Load loads a filter context into the kernel. |
| 862 |
// Returns an error if the filter context is invalid or the syscall failed. |
| 863 |
func (f *ScmpFilter) Load() error { |
| 864 |
f.lock.Lock() |
| 865 |
defer f.lock.Unlock() |
| 866 |
|
| 867 |
if !f.valid { |
| 868 |
return errBadFilter |
| 869 |
} |
| 870 |
|
| 871 |
if retCode := C.seccomp_load(f.filterCtx); retCode != 0 { |
| 872 |
return errRc(retCode) |
| 873 |
} |
| 874 |
|
| 875 |
return nil |
| 876 |
} |
| 877 |
|
| 878 |
// GetDefaultAction returns the default action taken on a syscall which does not |
| 879 |
// match a rule in the filter, or an error if an issue was encountered |
| 880 |
// retrieving the value. |
| 881 |
func (f *ScmpFilter) GetDefaultAction() (ScmpAction, error) { |
| 882 |
action, err := f.getFilterAttr(filterAttrActDefault) |
| 883 |
if err != nil { |
| 884 |
return 0x0, err |
| 885 |
} |
| 886 |
|
| 887 |
return actionFromNative(action) |
| 888 |
} |
| 889 |
|
| 890 |
// GetBadArchAction returns the default action taken on a syscall for an |
| 891 |
// architecture not in the filter, or an error if an issue was encountered |
| 892 |
// retrieving the value. |
| 893 |
func (f *ScmpFilter) GetBadArchAction() (ScmpAction, error) { |
| 894 |
action, err := f.getFilterAttr(filterAttrActBadArch) |
| 895 |
if err != nil { |
| 896 |
return 0x0, err |
| 897 |
} |
| 898 |
|
| 899 |
return actionFromNative(action) |
| 900 |
} |
| 901 |
|
| 902 |
// GetNoNewPrivsBit returns the current state the No New Privileges bit will be set |
| 903 |
// to on the filter being loaded, or an error if an issue was encountered |
| 904 |
// retrieving the value. |
| 905 |
// The No New Privileges bit tells the kernel that new processes run with exec() |
| 906 |
// cannot gain more privileges than the process that ran exec(). |
| 907 |
// For example, a process with No New Privileges set would be unable to exec |
| 908 |
// setuid/setgid executables. |
| 909 |
func (f *ScmpFilter) GetNoNewPrivsBit() (bool, error) { |
| 910 |
noNewPrivs, err := f.getFilterAttr(filterAttrNNP) |
| 911 |
if err != nil { |
| 912 |
return false, err |
| 913 |
} |
| 914 |
|
| 915 |
if noNewPrivs == 0 { |
| 916 |
return false, nil |
| 917 |
} |
| 918 |
|
| 919 |
return true, nil |
| 920 |
} |
| 921 |
|
| 922 |
// GetLogBit returns the current state the Log bit will be set to on the filter |
| 923 |
// being loaded, or an error if an issue was encountered retrieving the value. |
| 924 |
// The Log bit tells the kernel that all actions taken by the filter, with the |
| 925 |
// exception of ActAllow, should be logged. |
| 926 |
// The Log bit is only usable when libseccomp API level 3 or higher is |
| 927 |
// supported. |
| 928 |
func (f *ScmpFilter) GetLogBit() (bool, error) { |
| 929 |
log, err := f.getFilterAttr(filterAttrLog) |
| 930 |
if err != nil { |
| 931 |
if e := checkAPI("GetLogBit", 3, 2, 4, 0); e != nil { |
| 932 |
err = e |
| 933 |
} |
| 934 |
|
| 935 |
return false, err |
| 936 |
} |
| 937 |
|
| 938 |
if log == 0 { |
| 939 |
return false, nil |
| 940 |
} |
| 941 |
|
| 942 |
return true, nil |
| 943 |
} |
| 944 |
|
| 945 |
// GetSSB returns the current state the SSB bit will be set to on the filter |
| 946 |
// being loaded, or an error if an issue was encountered retrieving the value. |
| 947 |
// The SSB bit tells the kernel that a seccomp user is not interested in enabling |
| 948 |
// Speculative Store Bypass mitigation. |
| 949 |
// The SSB bit is only usable when libseccomp API level 4 or higher is |
| 950 |
// supported. |
| 951 |
func (f *ScmpFilter) GetSSB() (bool, error) { |
| 952 |
ssb, err := f.getFilterAttr(filterAttrSSB) |
| 953 |
if err != nil { |
| 954 |
if e := checkAPI("GetSSB", 4, 2, 5, 0); e != nil { |
| 955 |
err = e |
| 956 |
} |
| 957 |
|
| 958 |
return false, err |
| 959 |
} |
| 960 |
|
| 961 |
if ssb == 0 { |
| 962 |
return false, nil |
| 963 |
} |
| 964 |
|
| 965 |
return true, nil |
| 966 |
} |
| 967 |
|
| 968 |
// GetOptimize returns the current optimization level of the filter, |
| 969 |
// or an error if an issue was encountered retrieving the value. |
| 970 |
// See SetOptimize for more details. |
| 971 |
func (f *ScmpFilter) GetOptimize() (int, error) { |
| 972 |
level, err := f.getFilterAttr(filterAttrOptimize) |
| 973 |
if err != nil { |
| 974 |
if e := checkAPI("GetOptimize", 4, 2, 5, 0); e != nil { |
| 975 |
err = e |
| 976 |
} |
| 977 |
|
| 978 |
return 0, err |
| 979 |
} |
| 980 |
|
| 981 |
return int(level), nil |
| 982 |
} |
| 983 |
|
| 984 |
// GetRawRC returns the current state of RawRC flag, or an error |
| 985 |
// if an issue was encountered retrieving the value. |
| 986 |
// See SetRawRC for more details. |
| 987 |
func (f *ScmpFilter) GetRawRC() (bool, error) { |
| 988 |
rawrc, err := f.getFilterAttr(filterAttrRawRC) |
| 989 |
if err != nil { |
| 990 |
if e := checkAPI("GetRawRC", 4, 2, 5, 0); e != nil { |
| 991 |
err = e |
| 992 |
} |
| 993 |
|
| 994 |
return false, err |
| 995 |
} |
| 996 |
|
| 997 |
if rawrc == 0 { |
| 998 |
return false, nil |
| 999 |
} |
| 1000 |
|
| 1001 |
return true, nil |
| 1002 |
} |
| 1003 |
|
| 1004 |
// GetWaitKill returns the current state of WaitKill flag, |
| 1005 |
// or an error if an issue was encountered retrieving the value. |
| 1006 |
// See SetWaitKill for more details. |
| 1007 |
func (f *ScmpFilter) GetWaitKill() (bool, error) { |
| 1008 |
val, err := f.getFilterAttr(filterAttrWaitKill) |
| 1009 |
if err != nil { |
| 1010 |
if e := checkAPI("GetWaitKill", 7, 2, 6, 0); e != nil { |
| 1011 |
err = e |
| 1012 |
} |
| 1013 |
|
| 1014 |
return false, err |
| 1015 |
} |
| 1016 |
if val == 0 { |
| 1017 |
return false, nil |
| 1018 |
} |
| 1019 |
|
| 1020 |
return true, nil |
| 1021 |
} |
| 1022 |
|
| 1023 |
// SetBadArchAction sets the default action taken on a syscall for an |
| 1024 |
// architecture not in the filter, or an error if an issue was encountered |
| 1025 |
// setting the value. |
| 1026 |
func (f *ScmpFilter) SetBadArchAction(action ScmpAction) error { |
| 1027 |
if err := sanitizeAction(action); err != nil { |
| 1028 |
return err |
| 1029 |
} |
| 1030 |
|
| 1031 |
return f.setFilterAttr(filterAttrActBadArch, action.toNative()) |
| 1032 |
} |
| 1033 |
|
| 1034 |
// SetNoNewPrivsBit sets the state of the No New Privileges bit, which will be |
| 1035 |
// applied on filter load, or an error if an issue was encountered setting the |
| 1036 |
// value. |
| 1037 |
// Filters with No New Privileges set to 0 can only be loaded if the process |
| 1038 |
// has the CAP_SYS_ADMIN capability. |
| 1039 |
func (f *ScmpFilter) SetNoNewPrivsBit(state bool) error { |
| 1040 |
var toSet C.uint32_t = 0x0 |
| 1041 |
|
| 1042 |
if state { |
| 1043 |
toSet = 0x1 |
| 1044 |
} |
| 1045 |
|
| 1046 |
return f.setFilterAttr(filterAttrNNP, toSet) |
| 1047 |
} |
| 1048 |
|
| 1049 |
// SetLogBit sets the state of the Log bit, which will be applied on filter |
| 1050 |
// load, or an error if an issue was encountered setting the value. |
| 1051 |
// The Log bit is only usable when libseccomp API level 3 or higher is |
| 1052 |
// supported. |
| 1053 |
func (f *ScmpFilter) SetLogBit(state bool) error { |
| 1054 |
var toSet C.uint32_t = 0x0 |
| 1055 |
|
| 1056 |
if state { |
| 1057 |
toSet = 0x1 |
| 1058 |
} |
| 1059 |
|
| 1060 |
err := f.setFilterAttr(filterAttrLog, toSet) |
| 1061 |
if err != nil { |
| 1062 |
if e := checkAPI("SetLogBit", 3, 2, 4, 0); e != nil { |
| 1063 |
err = e |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
return err |
| 1068 |
} |
| 1069 |
|
| 1070 |
// SetSSB sets the state of the SSB bit, which will be applied on filter |
| 1071 |
// load, or an error if an issue was encountered setting the value. |
| 1072 |
// The SSB bit is only usable when libseccomp API level 4 or higher is |
| 1073 |
// supported. |
| 1074 |
func (f *ScmpFilter) SetSSB(state bool) error { |
| 1075 |
var toSet C.uint32_t = 0x0 |
| 1076 |
|
| 1077 |
if state { |
| 1078 |
toSet = 0x1 |
| 1079 |
} |
| 1080 |
|
| 1081 |
err := f.setFilterAttr(filterAttrSSB, toSet) |
| 1082 |
if err != nil { |
| 1083 |
if e := checkAPI("SetSSB", 4, 2, 5, 0); e != nil { |
| 1084 |
err = e |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
return err |
| 1089 |
} |
| 1090 |
|
| 1091 |
// SetOptimize sets optimization level of the seccomp filter. By default |
| 1092 |
// libseccomp generates a set of sequential "if" statements for each rule in |
| 1093 |
// the filter. SetSyscallPriority can be used to prioritize the order for the |
| 1094 |
// default cause. The binary tree optimization sorts by syscall numbers and |
| 1095 |
// generates consistent O(log n) filter traversal for every rule in the filter. |
| 1096 |
// The binary tree may be advantageous for large filters. Note that |
| 1097 |
// SetSyscallPriority is ignored when level == 2. |
| 1098 |
// |
| 1099 |
// The different optimization levels are: |
| 1100 |
// 0: Reserved value, not currently used. |
| 1101 |
// 1: Rules sorted by priority and complexity (DEFAULT). |
| 1102 |
// 2: Binary tree sorted by syscall number. |
| 1103 |
func (f *ScmpFilter) SetOptimize(level int) error { |
| 1104 |
cLevel := C.uint32_t(level) |
| 1105 |
|
| 1106 |
err := f.setFilterAttr(filterAttrOptimize, cLevel) |
| 1107 |
if err != nil { |
| 1108 |
if e := checkAPI("SetOptimize", 4, 2, 5, 0); e != nil { |
| 1109 |
err = e |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
return err |
| 1114 |
} |
| 1115 |
|
| 1116 |
// SetRawRC sets whether libseccomp should pass system error codes back to the |
| 1117 |
// caller, instead of the default ECANCELED. Defaults to false. |
| 1118 |
func (f *ScmpFilter) SetRawRC(state bool) error { |
| 1119 |
var toSet C.uint32_t = 0x0 |
| 1120 |
|
| 1121 |
if state { |
| 1122 |
toSet = 0x1 |
| 1123 |
} |
| 1124 |
|
| 1125 |
err := f.setFilterAttr(filterAttrRawRC, toSet) |
| 1126 |
if err != nil { |
| 1127 |
if e := checkAPI("SetRawRC", 4, 2, 5, 0); e != nil { |
| 1128 |
err = e |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
return err |
| 1133 |
} |
| 1134 |
|
| 1135 |
// SetWaitKill sets whether libseccomp should request wait killable semantics |
| 1136 |
// when possible. Defaults to false. |
| 1137 |
func (f *ScmpFilter) SetWaitKill(state bool) error { |
| 1138 |
var toSet C.uint32_t = 0x0 |
| 1139 |
|
| 1140 |
if state { |
| 1141 |
toSet = 0x1 |
| 1142 |
} |
| 1143 |
|
| 1144 |
err := f.setFilterAttr(filterAttrWaitKill, toSet) |
| 1145 |
if err != nil { |
| 1146 |
if e := checkAPI("SetWaitKill", 7, 2, 6, 0); e != nil { |
| 1147 |
err = e |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
return err |
| 1152 |
} |
| 1153 |
|
| 1154 |
// SetSyscallPriority sets a syscall's priority. |
| 1155 |
// This provides a hint to the filter generator in libseccomp about the |
| 1156 |
// importance of this syscall. High-priority syscalls are placed |
| 1157 |
// first in the filter code, and incur less overhead (at the expense of |
| 1158 |
// lower-priority syscalls). |
| 1159 |
func (f *ScmpFilter) SetSyscallPriority(call ScmpSyscall, priority uint8) error { |
| 1160 |
f.lock.Lock() |
| 1161 |
defer f.lock.Unlock() |
| 1162 |
|
| 1163 |
if !f.valid { |
| 1164 |
return errBadFilter |
| 1165 |
} |
| 1166 |
|
| 1167 |
if retCode := C.seccomp_syscall_priority(f.filterCtx, C.int(call), |
| 1168 |
C.uint8_t(priority)); retCode != 0 { |
| 1169 |
return errRc(retCode) |
| 1170 |
} |
| 1171 |
|
| 1172 |
return nil |
| 1173 |
} |
| 1174 |
|
| 1175 |
// AddRule adds a single rule for an unconditional action on a syscall. |
| 1176 |
// Accepts the number of the syscall and the action to be taken on the call |
| 1177 |
// being made. |
| 1178 |
// Returns an error if an issue was encountered adding the rule. |
| 1179 |
func (f *ScmpFilter) AddRule(call ScmpSyscall, action ScmpAction) error { |
| 1180 |
return f.addRuleGeneric(call, action, false, nil) |
| 1181 |
} |
| 1182 |
|
| 1183 |
// AddRuleExact adds a single rule for an unconditional action on a syscall. |
| 1184 |
// Accepts the number of the syscall and the action to be taken on the call |
| 1185 |
// being made. |
| 1186 |
// No modifications will be made to the rule, and it will fail to add if it |
| 1187 |
// cannot be applied to the current architecture without modification. |
| 1188 |
// The rule will function exactly as described, but it may not function identically |
| 1189 |
// (or be able to be applied to) all architectures. |
| 1190 |
// Returns an error if an issue was encountered adding the rule. |
| 1191 |
func (f *ScmpFilter) AddRuleExact(call ScmpSyscall, action ScmpAction) error { |
| 1192 |
return f.addRuleGeneric(call, action, true, nil) |
| 1193 |
} |
| 1194 |
|
| 1195 |
// AddRuleConditional adds a single rule for a conditional action on a syscall. |
| 1196 |
// Returns an error if an issue was encountered adding the rule. |
| 1197 |
// All conditions must match for the rule to match. |
| 1198 |
func (f *ScmpFilter) AddRuleConditional(call ScmpSyscall, action ScmpAction, conds []ScmpCondition) error { |
| 1199 |
return f.addRuleGeneric(call, action, false, conds) |
| 1200 |
} |
| 1201 |
|
| 1202 |
// AddRuleConditionalExact adds a single rule for a conditional action on a |
| 1203 |
// syscall. |
| 1204 |
// No modifications will be made to the rule, and it will fail to add if it |
| 1205 |
// cannot be applied to the current architecture without modification. |
| 1206 |
// The rule will function exactly as described, but it may not function identically |
| 1207 |
// (or be able to be applied to) all architectures. |
| 1208 |
// Returns an error if an issue was encountered adding the rule. |
| 1209 |
func (f *ScmpFilter) AddRuleConditionalExact(call ScmpSyscall, action ScmpAction, conds []ScmpCondition) error { |
| 1210 |
return f.addRuleGeneric(call, action, true, conds) |
| 1211 |
} |
| 1212 |
|
| 1213 |
// ExportPFC output PFC-formatted, human-readable dump of a filter context's |
| 1214 |
// rules to a file. |
| 1215 |
// Accepts file to write to (must be open for writing). |
| 1216 |
// Returns an error if writing to the file fails. |
| 1217 |
func (f *ScmpFilter) ExportPFC(file *os.File) error { |
| 1218 |
f.lock.Lock() |
| 1219 |
defer f.lock.Unlock() |
| 1220 |
|
| 1221 |
fd := file.Fd() |
| 1222 |
|
| 1223 |
if !f.valid { |
| 1224 |
return errBadFilter |
| 1225 |
} |
| 1226 |
|
| 1227 |
if retCode := C.seccomp_export_pfc(f.filterCtx, C.int(fd)); retCode != 0 { |
| 1228 |
return errRc(retCode) |
| 1229 |
} |
| 1230 |
|
| 1231 |
return nil |
| 1232 |
} |
| 1233 |
|
| 1234 |
// ExportBPF outputs Berkeley Packet Filter-formatted, kernel-readable dump of a |
| 1235 |
// filter context's rules to a file. |
| 1236 |
// Accepts file to write to (must be open for writing). |
| 1237 |
// Returns an error if writing to the file fails. |
| 1238 |
func (f *ScmpFilter) ExportBPF(file *os.File) error { |
| 1239 |
f.lock.Lock() |
| 1240 |
defer f.lock.Unlock() |
| 1241 |
|
| 1242 |
fd := file.Fd() |
| 1243 |
|
| 1244 |
if !f.valid { |
| 1245 |
return errBadFilter |
| 1246 |
} |
| 1247 |
|
| 1248 |
if retCode := C.seccomp_export_bpf(f.filterCtx, C.int(fd)); retCode != 0 { |
| 1249 |
return errRc(retCode) |
| 1250 |
} |
| 1251 |
|
| 1252 |
return nil |
| 1253 |
} |
| 1254 |
|
| 1255 |
// ExportBPFMem is similar to [ExportBPF], except the data is written into |
| 1256 |
// a memory and returned as []byte. |
| 1257 |
func (f *ScmpFilter) ExportBPFMem() ([]byte, error) { |
| 1258 |
f.lock.Lock() |
| 1259 |
defer f.lock.Unlock() |
| 1260 |
|
| 1261 |
if !f.valid { |
| 1262 |
return nil, errBadFilter |
| 1263 |
} |
| 1264 |
|
| 1265 |
var len C.size_t |
| 1266 |
// Get the size required. |
| 1267 |
if retCode := C.seccomp_export_bpf_mem(f.filterCtx, unsafe.Pointer(nil), &len); retCode < 0 { |
| 1268 |
return nil, errRc(retCode) |
| 1269 |
} |
| 1270 |
// Get the data. |
| 1271 |
buf := make([]byte, int(len)) |
| 1272 |
if retCode := C.seccomp_export_bpf_mem(f.filterCtx, unsafe.Pointer(&buf[0]), &len); retCode < 0 { |
| 1273 |
return nil, errRc(retCode) |
| 1274 |
} |
| 1275 |
|
| 1276 |
return buf, nil |
| 1277 |
} |
| 1278 |
|
| 1279 |
// Userspace Notification API |
| 1280 |
|
| 1281 |
// GetNotifFd returns the userspace notification file descriptor associated with the given |
| 1282 |
// filter context. Such a file descriptor is only valid after the filter has been loaded |
| 1283 |
// and only when the filter uses the ActNotify action. The file descriptor can be used to |
| 1284 |
// retrieve and respond to notifications associated with the filter (see NotifReceive(), |
| 1285 |
// NotifRespond(), and NotifIDValid()). |
| 1286 |
func (f *ScmpFilter) GetNotifFd() (ScmpFd, error) { |
| 1287 |
return f.getNotifFd() |
| 1288 |
} |
| 1289 |
|
| 1290 |
// NotifReceive retrieves a seccomp userspace notification from a filter whose ActNotify |
| 1291 |
// action has triggered. The caller is expected to process the notification and return a |
| 1292 |
// response via NotifRespond(). Each invocation of this function returns one |
| 1293 |
// notification. As multiple notifications may be pending at any time, this function is |
| 1294 |
// normally called within a polling loop. |
| 1295 |
func NotifReceive(fd ScmpFd) (*ScmpNotifReq, error) { |
| 1296 |
return notifReceive(fd) |
| 1297 |
} |
| 1298 |
|
| 1299 |
// NotifRespond responds to a notification retrieved via NotifReceive(). The response Id |
| 1300 |
// must match that of the corresponding notification retrieved via NotifReceive(). |
| 1301 |
func NotifRespond(fd ScmpFd, scmpResp *ScmpNotifResp) error { |
| 1302 |
return notifRespond(fd, scmpResp) |
| 1303 |
} |
| 1304 |
|
| 1305 |
// NotifIDValid checks if a notification is still valid. An return value of nil means the |
| 1306 |
// notification is still valid. Otherwise the notification is not valid. This can be used |
| 1307 |
// to mitigate time-of-check-time-of-use (TOCTOU) attacks as described in seccomp_notify_id_valid(2). |
| 1308 |
func NotifIDValid(fd ScmpFd, id uint64) error { |
| 1309 |
return notifIDValid(fd, id) |
| 1310 |
} |
| 1311 |
|
| 1312 |
// TransactionStart starts a new seccomp filter transaction that the caller can |
| 1313 |
// use to perform any number of filter modifications which can then be |
| 1314 |
// committed to the filter using [TransactionCommit] or rejected using |
| 1315 |
// [TransactionReject]. It is important to note that transactions only affect |
| 1316 |
// the seccomp filter state while it is being managed by libseccomp; seccomp |
| 1317 |
// filters which have been loaded into the kernel can not be modified, only new |
| 1318 |
// seccomp filters can be added on top of the existing loaded filter stack. |
| 1319 |
func (f *ScmpFilter) TransactionStart() error { |
| 1320 |
f.lock.Lock() |
| 1321 |
defer f.lock.Unlock() |
| 1322 |
|
| 1323 |
if !f.valid { |
| 1324 |
return errBadFilter |
| 1325 |
} |
| 1326 |
|
| 1327 |
if retCode := C.seccomp_transaction_start(f.filterCtx); retCode < 0 { |
| 1328 |
return errRc(retCode) |
| 1329 |
} |
| 1330 |
|
| 1331 |
return nil |
| 1332 |
} |
| 1333 |
|
| 1334 |
// TransactionReject rejects a transaction started by [TransactionStart]. |
| 1335 |
func (f *ScmpFilter) TransactionReject() { |
| 1336 |
f.lock.Lock() |
| 1337 |
defer f.lock.Unlock() |
| 1338 |
|
| 1339 |
if !f.valid { |
| 1340 |
return |
| 1341 |
} |
| 1342 |
|
| 1343 |
C.seccomp_transaction_reject(f.filterCtx) |
| 1344 |
} |
| 1345 |
|
| 1346 |
// TransactionCommit commits a transaction started by [TransactionStart]. |
| 1347 |
func (f *ScmpFilter) TransactionCommit() error { |
| 1348 |
f.lock.Lock() |
| 1349 |
defer f.lock.Unlock() |
| 1350 |
|
| 1351 |
if !f.valid { |
| 1352 |
return errBadFilter |
| 1353 |
} |
| 1354 |
|
| 1355 |
if retCode := C.seccomp_transaction_commit(f.filterCtx); retCode < 0 { |
| 1356 |
return errRc(retCode) |
| 1357 |
} |
| 1358 |
|
| 1359 |
return nil |
| 1360 |
} |