validate.bend checks
raw source on the hub · import bend-osdev-bpf@0.1.0.0/validate.bend as Validate
bend-osdev-bpf: validating a classic BPF program before it runs.
The rules of the BSD bpf(4) manual page and McCanne and Jacobson (1993): * the program is not empty, and has at most limit instructions (BPF_MAXINSNS, 4096, by default); * every jump lands on an instruction of the program (jumps only move forward: their offsets are unsigned), and a conditional jump's offsets fit their 8-bit fields; * no instruction but a return or a jump can be the last, so the program ends in a return; * scratch memory indices are below BPF_MEMWORDS (16); * no division or modulo by a constant zero; * optionally (BpfValidateOptions.require_mem_init, on by default, as Linux checks socket filters and seccomp filters alike): a scratch slot is read only after every path from the start to the read has written it, where the instruction after a return counts as reached from the return (as Linux counts it: observed on the running kernel). The opcodes themselves are checked when a program is decoded (bpf_decode); bpf_prepare decodes and validates in one step.
2 imports
import Base import ./insn.bend as I
Types
type BpfValidateOptions source · line 29 · raw
Data
the choices validation offers: the instruction limit, and whether every scratch read must follow a write on every path
BpfValidateOptions@max_insns:Nat -> @require_mem_init:Bool -> BpfValidateOptions
type BpfInitStep source · line 172 · raw
Data
BpfInitStep@ok:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @fall:U32 -> @ahead:List<&2, U32> -> BpfInitStep
Definitions
def bpf_maxinsns source · line 24 · raw
Nat
BPF_MAXINSNS: the usual instruction limit
def BpfValidateOptions.default source · line 33 · raw
BpfValidateOptions
what Linux checks: at most 4096 instructions, scratch written before read
def BpfValidateOptions.permissive source · line 37 · raw
BpfValidateOptions
the rules of bpf(4) alone: scratch memory may be read before it is written
def bpf_chk source · line 40 · raw
@ok:Bool -> @e:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_both source · line 47 · raw
@r:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @s:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_check.next source · line 55 · raw
@pc:Nat -> @r:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
an instruction that goes on to the next one must not be the last
def bpf_check.k_div source · line 58 · raw
@src:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSrc -> @pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_check.div source · line 65 · raw
@o:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfAluOp -> @src:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSrc -> @pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_check.mem source · line 74 · raw
@+k:U32 -> @+pc:Nat -> @r:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_check.jmp source · line 77 · raw
@+jt:U32 -> @+jf:U32 -> @+pc:Nat -> @+r:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_check.op source · line 81 · raw
@op:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp -> @+pc:Nat -> @+r:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
the checks of one instruction at index pc, with r instructions after it
def bpf_check.all source · line 104 · raw
@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @+pc:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
the instructions' own checks, structurally: the reference the proofs reason about (PROOF.bend: F_check ties bpf_check.go to it)
def bpf_check.go source · line 113 · raw
@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @+pc:Nat -> @ok:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @n:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
the same as a tail call: ok is the verdict so far, and n counts the instructions from pc on; the loop stops at the instruction after a failure
def bpf_count.go source · line 129 · raw
@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @n:Nat -> Nat
the number of instructions, as a tail call (PROOF.bend: F_count)
def bpf_count source · line 136 · raw
@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Nat
def bpf_check.len source · line 139 · raw
@+n:Nat -> @limit:Nat -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_mem.bit.go source · line 147 · raw
@small:Bool -> @k:U32 -> U32
def bpf_mem.bit source · line 154 · raw
@+k:U32 -> U32
def bpf_mem.meet.go source · line 158 · raw
@ahead:List<&2, U32> -> @n:Nat -> @+m:U32 -> @acc:List<&2, U32> -> List<&2, U32>
meet the n-th mask ahead with m (acc: the masks before it, last first)
def bpf_mem.meet source · line 169 · raw
@ahead:List<&2, U32> -> @n:Nat -> @+m:U32 -> List<&2, U32>
def bpf_mem.read source · line 175 · raw
@+pc:Nat -> @+now:U32 -> @+k:U32 -> @ahead:List<&2, U32> -> BpfInitStep
def bpf_mem.step source · line 179 · raw
@op:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp -> @+pc:Nat -> @+now:U32 -> @ahead:List<&2, U32> -> BpfInitStep
one instruction, given the slots written on every path to it
def bpf_mem.head source · line 196 · raw
@ahead:List<&2, U32> -> U32
def bpf_mem.tail source · line 203 · raw
@ahead:List<&2, U32> -> List<&2, U32>
def bpf_mem.go source · line 210 · raw
@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @+pc:Nat -> @st:BpfInitStep -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_mem.none source · line 226 · raw
@n:Nat -> @acc:List<&2, U32> -> List<&2, U32>
n masks of 65535: no jump reaches any instruction yet
def bpf_check.mem_init source · line 233 · raw
@need:Bool -> @n:Nat -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_validate.mem source · line 243 · raw
@v:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @need:Bool -> @n:Nat -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_validate.ops source · line 250 · raw
@v:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @+n:Nat -> @need:Bool -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
def bpf_validate_with source · line 259 · raw
@opts:BpfValidateOptions -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
Done{Unit{}} if the program may run, or the first rule it breaks: the length, then each instruction's own rules in order, then scratch memory
def bpf_validate source · line 266 · raw
@limit:Nat -> @+prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit>
the default options, with this instruction limit
def bpf_prepare.ok source · line 269 · raw
@v:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, Unit> -> @ops:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>
def bpf_prepare.go source · line 276 · raw
@opts:BpfValidateOptions -> @d:Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>
def bpf_prepare_with source · line 284 · raw
@opts:BpfValidateOptions -> @rs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfRaw> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>
decode a stored program and validate it: the instructions, ready to run
def bpf_prepare source · line 288 · raw
@limit:Nat -> @rs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfRaw> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp>>
the same, with the default options and this instruction limit