1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! Abstract syntax and semantics of a subset of cBPF accepted by seccomp.
use vstd::prelude::*;
// Syntax
verus! {
/// Operand for some cBPF instructions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Structural)]
pub enum Src { K(u32), X }
/// Return constant or register.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Structural)]
pub enum RetVal { K(u32), A }
/// Supported ALU ops in a cBPF program.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Structural)]
pub enum AluOp {
Add = 0x00, Sub = 0x10, Mul = 0x20, Div = 0x30, Or = 0x40,
And = 0x50, Lsh = 0x60, Rsh = 0x70, Xor = 0xa0,
}
/// Comparison operator of a conditional jump in cBPF.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Structural)]
pub enum JmpOp { Eq = 0x10, Gt = 0x20, Ge = 0x30, Set = 0x40 }
/// An abstract representation of cBPF instructions,
/// restricted to the instructions accepted by
/// [`seccomp_check_filter()`](https://github.com/torvalds/linux/blob/40288c9206c17eb66a603262e06a58d300d0f279/kernel/seccomp.c#L286-L343).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Structural)]
pub enum Instr {
/// `BPF_LD | BPF_W | BPF_ABS`: `A = *(u32 *)((char *)&data + k)`.
LdAbs(u32),
/// `BPF_LD | BPF_W | BPF_LEN`: `A = data.len()`.
LdLen,
/// `BPF_LD | BPF_IMM`: `A = k`.
LdImm(u32),
/// `BPF_LD | BPF_MEM`: `A = M[k]`.
LdMem(u32),
/// `BPF_LDX | BPF_W | BPF_LEN`: `X = data.len()`.
LdxLen,
/// `BPF_LDX | BPF_IMM`: `X = k`.
LdxImm(u32),
/// `BPF_LDX | BPF_MEM`: `X = M[k]`.
LdxMem(u32),
/// `BPF_ST`: `M[k] = A`.
St(u32),
/// `BPF_STX`: `M[k] = X`.
Stx(u32),
/// `BPF_ALU | op | src`: `A = A op src`.
Alu(AluOp, Src),
/// `BPF_ALU | BPF_NEG`: `A = -A`.
Neg,
/// `BPF_JMP | BPF_JA`: skip `k` instructions.
Ja(u32),
/// `BPF_JMP | op | src`: skip `jt` instructions if `A op src` holds, else `jf`.
Jmp { op: JmpOp, src: Src, jt: u8, jf: u8 },
/// `BPF_RET | rval`: return `rval`.
Ret(RetVal),
/// `BPF_MISC | BPF_TAX`: `X = A`.
Tax,
/// `BPF_MISC | BPF_TXA`: `A = X`.
Txa,
}
/// An abstract representation of a cBPF program.
#[derive(Debug, Clone, PartialEq, Eq)]
// Verus does not yet model non-Copy Clone derives.
#[verifier::external_derive(Clone)]
pub struct Program {
pub instrs: Vec<Instr>,
}
impl Instr {
/// Well-formed instructions.
pub open spec fn wf(self, pc: nat, max_pc: nat) -> bool {
match self {
Instr::LdAbs(k) => k % 4 == 0,
// Check for division by zero.
Instr::Alu(AluOp::Div, Src::K(k)) => k != 0,
Instr::Alu(AluOp::Lsh, Src::K(k)) => k < 32,
Instr::Alu(AluOp::Rsh, Src::K(k)) => k < 32,
// Check for invalid memory addresses.
Instr::LdMem(k) => k < Program::MEM_WORDS,
Instr::LdxMem(k) => k < Program::MEM_WORDS,
Instr::St(k) => k < Program::MEM_WORDS,
Instr::Stx(k) => k < Program::MEM_WORDS,
// Jump targets stay inside the program.
Instr::Ja(k) => pc + 1 + k < max_pc,
Instr::Jmp { jt, jf, .. } => pc + 1 + jt < max_pc && pc + 1 + jf < max_pc,
_ => true,
}
}
}
impl Program {
/// `BPF_MEMWORDS` in `linux/bpf_common.h`.
pub spec const MEM_WORDS: u32 = 16;
/// Structural instruction validity, without input-buffer bounds, Linux installation
/// limits, or scratch-memory initialization analysis.
pub open spec fn wf(self) -> bool {
&&& self.instrs@.last() is Ret
&&& forall |pc: int| #![trigger self.instrs@[pc]]
0 <= pc < self.instrs@.len() ==> self.instrs@[pc].wf(pc as nat, self.instrs@.len())
}
}
} // verus!
// Semantics
verus! {
/// Final result of executing a cBPF program.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Structural)]
pub enum Outcome {
Return(u32),
RuntimeError,
}
/// Abstract cBPF machine state used in proofs.
pub struct MachineState {
pub pc: nat,
pub a: u32,
pub x: u32,
pub mem: Seq<Option<u32>>,
}
impl Src {
pub open spec fn eval(self, st: MachineState) -> u32 {
match self {
Src::K(k) => k,
Src::X => st.x,
}
}
}
impl RetVal {
pub open spec fn eval(self, st: MachineState) -> u32 {
match self {
RetVal::K(k) => k,
RetVal::A => st.a,
}
}
}
impl AluOp {
/// Applies the ALU operator to 32-bit operands.
pub open spec fn eval(self, lhs: u32, rhs: u32) -> u32 {
match self {
AluOp::Add => (lhs + rhs) as u32,
AluOp::Sub => (lhs - rhs) as u32,
AluOp::Mul => (lhs * rhs) as u32,
AluOp::Div => lhs / rhs,
AluOp::Or => lhs | rhs,
AluOp::And => lhs & rhs,
AluOp::Xor => lhs ^ rhs,
AluOp::Lsh => lhs << (rhs % 32),
AluOp::Rsh => lhs >> (rhs % 32),
}
}
}
impl JmpOp {
/// Evaluates an unsigned comparison.
pub open spec fn eval(self, lhs: u32, rhs: u32) -> bool {
match self {
JmpOp::Eq => lhs == rhs,
JmpOp::Gt => lhs > rhs,
JmpOp::Ge => lhs >= rhs,
JmpOp::Set => (lhs & rhs) != 0,
}
}
}
impl MachineState {
/// `bpf_convert_filter()` zeroes A and X before the first cBPF instruction:
/// <https://github.com/torvalds/linux/blob/40288c9206c17eb66a603262e06a58d300d0f279/net/core/filter.c#L613-L617>.
/// Scratch memory is uninitialized stack.
pub open spec fn init() -> Self {
MachineState {
pc: 0,
a: 0,
x: 0,
mem: Seq::new(Program::MEM_WORDS as nat, |_: int| None),
}
}
pub open spec fn next_a(self, a: u32) -> Self {
MachineState {
pc: self.pc + 1,
a,
..self
}
}
pub open spec fn next_x(self, x: u32) -> Self {
MachineState {
pc: self.pc + 1,
x,
..self
}
}
pub open spec fn next_mem(self, k: int, v: u32) -> Self
recommends 0 <= k < self.mem.len(),
{
MachineState {
pc: self.pc + 1,
mem: self.mem.update(k, Some(v)),
..self
}
}
/// Advances past the current instruction and then skip `n` instructions.
pub open spec fn jump(self, n: nat) -> Self {
MachineState {
pc: self.pc + 1 + n,
..self
}
}
}
impl Instr {
/// Executes exactly one instruction.
///
/// `Ok(st)` means execution continues in `st`.
/// `Err(outcome)` means execution terminates with `outcome`.
///
/// `data` is the 64-byte image of `struct seccomp_data` in little endian.
pub open spec fn step(self, data: &[u8], st: MachineState) -> Result<MachineState, Outcome> {
match self {
Instr::LdAbs(k) => {
if k + 4 <= data@.len() {
// Load the 32-bit word at byte offset `off` of `data`, the in-memory image of
// `struct seccomp_data`, in *little endian*.
// Unlike socket filters, seccomp context loads are not byte-swapped:
// `seccomp_check_filter()` rewrites `BPF_LD | BPF_W | BPF_ABS` into the
// kernel-internal `BPF_LDX | BPF_W | BPF_ABS`
// (<https://github.com/torvalds/linux/blob/40288c9206c17eb66a603262e06a58d300d0f279/kernel/seccomp.c#L287-L288>).
let b0 = data@[k as int] as u32;
let b1 = data@[k + 1] as u32;
let b2 = data@[k + 2] as u32;
let b3 = data@[k + 3] as u32;
Ok(st.next_a(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)))
} else {
// Rejected statically by `seccomp_check_filter()`.
Err(Outcome::RuntimeError)
}
}
Instr::LdLen => Ok(st.next_a(data.len() as u32)),
Instr::LdImm(k) => Ok(st.next_a(k)),
Instr::LdMem(k) => {
if k < st.mem.len() {
match st.mem[k as int] {
Some(v) => Ok(st.next_a(v)),
// `check_load_and_stores()`.
None => Err(Outcome::RuntimeError),
}
} else {
Err(Outcome::RuntimeError)
}
}
Instr::LdxLen => Ok(st.next_x(data@.len() as u32)),
Instr::LdxImm(k) => Ok(st.next_x(k)),
Instr::LdxMem(k) => {
if k < st.mem.len() {
match st.mem[k as int] {
Some(v) => Ok(st.next_x(v)),
None => Err(Outcome::RuntimeError),
}
} else {
Err(Outcome::RuntimeError)
}
}
Instr::St(k) => {
if k < st.mem.len() {
Ok(st.next_mem(k as int, st.a))
} else {
Err(Outcome::RuntimeError)
}
}
Instr::Stx(k) => {
if k < st.mem.len() {
Ok(st.next_mem(k as int, st.x))
} else {
Err(Outcome::RuntimeError)
}
}
// [`bpf_convert_filter()`](https://github.com/torvalds/linux/blob/40288c9206c17eb66a603262e06a58d300d0f279/net/core/filter.c#L693-L702).
Instr::Alu(op, src) => {
let rhs = src.eval(st);
if op is Div && rhs == 0 {
Err(Outcome::Return(0))
} else {
Ok(st.next_a(op.eval(st.a, rhs)))
}
}
Instr::Neg => Ok(st.next_a(-st.a as u32)),
Instr::Ja(k) => Ok(st.jump(k as nat)),
Instr::Jmp { op, src, jt, jf } => {
let rhs = src.eval(st);
let skip = if op.eval(st.a, rhs) { jt as nat } else { jf as nat };
Ok(st.jump(skip))
}
Instr::Ret(rval) => Err(Outcome::Return(rval.eval(st))),
Instr::Tax => Ok(st.next_x(st.a)),
Instr::Txa => Ok(st.next_a(st.x)),
}
}
}
impl Program {
/// Executes from an arbitrary machine state.
pub open spec fn eval_from(self, data: &[u8], st: MachineState) -> Outcome
decreases self.instrs@.len() - st.pc when self.wf()
{
if st.pc >= self.instrs@.len() {
Outcome::RuntimeError
} else {
match self.instrs@[st.pc as int].step(data, st) {
Ok(next) => self.eval_from(data, next),
Err(outcome) => outcome,
}
}
}
/// Runs the filter on one `struct seccomp_data` (in little endian).
pub open spec fn eval(self, data: &[u8]) -> Outcome
recommends self.wf()
{
self.eval_from(data, MachineState::init())
}
}
} // verus!