~/bend-docscommunity

seccomp.bend checks

raw source on the hub · import bend-osdev-bpf@0.1.0.0/seccomp.bend as Seccomp

bend-osdev-bpf: seccomp filters (the seccomp(2) manual page).

A seccomp filter is a classic BPF program run over struct seccomp_data: int nr; __u32 arch; __u64 instruction_pointer; __u64 args[6]; 64 bytes, in the host's byte order, which is also the order of the filter's loads (little-endian on x86-64 and arm64). Its answer is a SECCOMP_RET_* value: the action in the high 16 bits, data in the low 16. With several filters installed, every one runs, the most recently installed first, and the answer is the first-seen action of the highest precedence, with its data: KILL_PROCESS, KILL_THREAD, TRAP, ERRNO, USER_NOTIF, TRACE, LOG, ALLOW, in decreasing order. An action value the manual page does not name is treated as KILL_PROCESS.

A seccomp filter loads only 32-bit words at aligned offsets inside seccomp_data (BPF_LD | BPF_W | BPF_ABS), and Linux refuses BPF_MOD in a seccomp filter (observed on the running kernel: the manual page does not list the instructions); seccomp_validate adds those rules to bpf_validate's. BPF_LEN loads give 64, the size of seccomp_data.

6 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B
import bend-osdev-word64@0.1.0.0/word64.bend as W
import ./insn.bend as I
import ./validate.bend as V
import ./bpf.bend as R

Types

type SeccompData source · line 28 · raw

Data

type SeccompAction source · line 76 · raw

Data

type SeccompLimits source · line 289 · raw

Data

the limits on one filter (BPF_MAXINSNS) and on all the filters of a thread together (MAX_INSNS_PER_PATH), where each filter already installed counts 4 instructions more than its length

Definitions

def SeccompData.field32 source · line 31 · raw

@order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @v:U32 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

def SeccompData.field64 source · line 38 · raw

@order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @v:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

def SeccompData.to_octets source · line 46 · raw

@+order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @d:SeccompData -> 0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets

the 64 bytes of struct seccomp_data, in the given byte order

def SeccompData.input source · line 51 · raw

@+order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @d:SeccompData -> 0x9fae2d11be141cc220d6a37e498b4020/bpf.BpfInput

def seccomp_order source · line 55 · raw

0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder

the byte order of x86-64 and arm64, the usual hosts

def seccomp_data_size source · line 58 · raw

U32

def seccomp_arch_x86_64 source · line 62 · raw

U32

AUDIT_ARCH_* values for seccomp_data.arch, and the x32 system call bit

def seccomp_arch_i386 source · line 65 · raw

U32

def seccomp_arch_aarch64 source · line 68 · raw

U32

def seccomp_x32_syscall_bit source · line 71 · raw

U32

def SeccompAction.pick source · line 86 · raw

@hit:Bool -> @a:SeccompAction -> @rest:SeccompAction -> SeccompAction

def SeccompAction.of_u32 source · line 95 · raw

@+v:U32 -> SeccompAction

the action of a SECCOMP_RET_* value: SECCOMP_RET_ACTION_FULL (0xffff0000) selects the action, SECCOMP_RET_DATA (0xffff) the data

def SeccompAction.to_u32 source · line 109 · raw

@a:SeccompAction -> U32

the SECCOMP_RET_* value of an action, with the low 16 bits of its data

def SeccompAction.clip source · line 129 · raw

@a:SeccompAction -> SeccompAction

the same action with its data cut to the 16 bits a return value holds

def SeccompAction.rank source · line 141 · raw

@a:SeccompAction -> Nat

the precedence: 0 for KILL_PROCESS, the most restrictive, to 7 for ALLOW

def SeccompAction.show source · line 160 · raw

@a:SeccompAction -> String

def seccomp_combine.pick source · line 179 · raw

@newer_wins:Bool -> @newer:SeccompAction -> @older:SeccompAction -> SeccompAction

def seccomp_combine source · line 188 · raw

@+newer:SeccompAction -> @+older:SeccompAction -> SeccompAction

the answer of two filters, given each one's: the higher precedence, and on a tie the newer filter's action (with its data)

def seccomp_check.word source · line 193 · raw

@+k:U32 -> @+pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

def seccomp_check.abs source · line 196 · raw

@size:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSize -> @+k:U32 -> @+pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

def seccomp_check.alu source · line 203 · raw

@o:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfAluOp -> @pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

def seccomp_check.op source · line 210 · raw

@op:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp -> @+pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

def seccomp_check.go source · line 224 · raw

@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @+pc:Nat -> @ok:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

ok is the verdict so far; the loop stops at the instruction after a failure

def seccomp_validate.go source · line 235 · raw

@v:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

def seccomp_validate_with source · line 244 · raw

@opts:0x9fae2d11be141cc220d6a37e498b4020/validate.BpfValidateOptions -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

bpf_validate's rules, only aligned 32-bit loads inside seccomp_data, and no BPF_MOD (checked once bpf_validate's rules hold)

def seccomp_validate source · line 248 · raw

@limit:Nat -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>

the default options (what Linux checks), with this instruction limit

def seccomp_run.fin source · line 253 · raw

@r:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, U32> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, SeccompAction>

def seccomp_run source · line 261 · raw

@+order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @d:SeccompData -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, SeccompAction>

one filter's action for a system call

def seccomp_eval.join source · line 264 · raw

@r:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, SeccompAction> -> @s:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, SeccompAction> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, SeccompAction>

def seccomp_eval source · line 277 · raw

@stack:List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> @+order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @+d:SeccompData -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, SeccompAction>

the action of a stack of filters, the most recently installed first; with no filter, ALLOW

def seccomp_limits source · line 292 · raw

SeccompLimits

def seccomp_path_len source · line 295 · raw

@stack:List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> Nat

def seccomp_install.fit source · line 302 · raw

@fits:Bool -> @total:Nat -> @ops:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @stack:List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>>

def seccomp_install.ok source · line 309 · raw

@v:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @path:Nat -> @+ops:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @+stack:List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>>

def seccomp_install.go source · line 317 · raw

@lim:SeccompLimits -> @d:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> @stack:List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>>

def seccomp_install source · line 328 · raw

@lim:SeccompLimits -> @stack:List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> @rs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfRaw> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>>

decode a stored filter, validate it for seccomp, and put it on top of the stack, as SECCOMP_SET_MODE_FILTER does