bpfvm 0.1.2

`bpfvm` is a small BPF VM implementation and cBPF token 'assembler'. It is intended for testing cBPF functionality before deployment, e.g. seccomp BPF filters.
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright © 2022, Steve Smith <tarkasteve@gmail.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

use libc::sock_filter;
use log::{error, debug, info};

use crate::errors::{Error, Result};
use crate::{BPFProg, RunData, bpf::BPF_A};

const MEMSIZE: usize = libc::BPF_MEMWORDS as usize;

pub struct BpfVM {
    pub pc: usize,
    pub acc: u32,
    pub idx: u32,
    pub mem: [u32; MEMSIZE],
    pub prog: Vec<sock_filter>,
}

fn fetch_u32(data: RunData, off: usize) -> Result<u32> {
    // Offsets are in bytes, data is in words
    let woff = off / 4;
    Ok(data[woff])
}

fn fetch_u16(_data: RunData, _off: usize) -> Result<u32> {
    // FIXME: Not supported by seccomp, implement later if needed
    Err(Error::UnsupportedDataOffset)
}

fn fetch_u8(_data: RunData, _off: usize) -> Result<u32> {
    // FIXME: Not supported by seccomp, implement later if needed
    Err(Error::UnsupportedDataOffset)
}

fn fetch_data(data: RunData, off: usize, size: u16) -> Result<u32> {
    match size as u32 {
        libc::BPF_W => fetch_u32(data, off),
        libc::BPF_H => fetch_u16(data, off),
        libc::BPF_B => fetch_u8(data, off),
        _ => return Err(Error::InvalidInstructionCode(size)),
    }
}

pub fn any_to_data<T: Sized>(p: &T) -> RunData {
    unsafe {
        ::std::slice::from_raw_parts((p as *const T) as *const u32, ::std::mem::size_of::<T>())
    }
}

impl BpfVM {
    pub fn new(prog: &BPFProg) -> Result<BpfVM> {
        if prog.len() > u16::MAX as usize {
            return Err(Error::ProgramTooLong(prog.len()));
        }

        Ok(BpfVM {
            pc: 0,
            acc: 0,
            idx: 0,
            mem: [0; MEMSIZE],
            prog: prog.clone(),
        })
    }

    pub fn reset(&mut self) -> Result<()> {
        self.pc = 0;
        self.acc = 0;
        self.idx = 0;
        self.mem = [0; MEMSIZE];
        Ok(())
    }

    fn fetch_src(&self, src: u16, curr: &sock_filter) -> Result<u32> {
        match src as u32 {
            libc::BPF_K => {
                info!("Src is K: 0x{:x}", curr.k);
                return Ok(curr.k);
            }
            libc::BPF_X => {
                info!("Src is IDX: 0x{:x}", self.idx);
                return Ok(self.idx);
            }
            BPF_A => {
                info!("Src is ACC: 0x{:x}", self.acc);
                return Ok(self.acc);
            }
            _ => return Err(Error::InvalidInstructionCode(curr.code)),
        }
    }

    pub fn execute(&mut self, data: RunData) -> Result<Option<u32>> {
        let curr = self.prog[self.pc];
        debug!("Executing line 0x{:x}: {:x?}", self.pc, curr);

        self.pc += 1;

        let inst = curr.code & 0x7; // Instruction ("class")
        let size = curr.code & 0x18; // Target size
        let mode = (curr.code & 0xe0) as u32; // Target
        let op = curr.code & 0xf0; // ALU/JMP op
        let src = curr.code & 0x08; // K or idx

        debug!("Executing instruction 0x{:x}", inst);
        match inst as u32 {
            libc::BPF_LD => {
                self.acc = match mode {
                    libc::BPF_IMM => curr.k,
                    libc::BPF_ABS => fetch_data(data, curr.k as usize, size)?,
                    libc::BPF_IND => fetch_data(data, (self.idx + curr.k) as usize, size)?,
                    libc::BPF_MEM => self.mem[curr.k as usize],
                    libc::BPF_LEN => data.len() as u32,
                    _ => return Err(Error::InvalidInstructionCode(curr.code)),
                };
                info!("Loaded value 0x{:x} into ACC", self.acc);
            }
            libc::BPF_LDX => {
                self.idx = match mode {
                    libc::BPF_IMM => curr.k,
                    libc::BPF_MEM => self.mem[curr.k as usize],
                    libc::BPF_LEN => data.len() as u32,
                    _ => return Err(Error::InvalidInstructionCode(curr.code)),
                };
                info!("Loaded value 0x{:x} into IDX", self.idx);
            }
            libc::BPF_ST => {
                self.mem[curr.k as usize] = self.acc;
            }
            libc::BPF_STX => {
                self.mem[curr.k as usize] = self.idx;
            }
            libc::BPF_ALU => {
                let sval = self.fetch_src(src, &curr)?;
                self.acc = match op as u32 {
                    libc::BPF_ADD => {
                        info!("Executing ADD with 0x{:x}", sval);
                        self.acc + sval
                    }
                    libc::BPF_SUB => {
                        info!("Executing SUB with 0x{:x}", sval);
                        self.acc - sval
                    }
                    libc::BPF_MUL => {
                        info!("Executing MUL with 0x{:x}", sval);
                        self.acc * sval
                    }
                    libc::BPF_DIV => {
                        info!("Executing DIV with 0x{:x}", sval);
                        self.acc / sval
                    }
                    libc::BPF_OR => {
                        info!("Executing OR with 0x{:x}", sval);
                        self.acc | sval
                    }
                    libc::BPF_AND => {
                        info!("Executing AND with 0x{:x}", sval);
                        self.acc & sval
                    }
                    libc::BPF_LSH => {
                        info!("Executing LSH with 0x{:x}", sval);
                        self.acc << sval
                    }
                    libc::BPF_RSH => {
                        info!("Executing RSH with 0x{:x}", sval);
                        self.acc >> sval
                    }
                    libc::BPF_MOD => {
                        info!("Executing MOD  with 0x{:x}", sval);
                        self.acc % sval
                    }
                    libc::BPF_XOR => {
                        info!("Executing XOR with 0x{:x}", sval);
                        self.acc ^ sval
                    }
                    libc::BPF_NEG => {
                        error!("NEG is not supported");
                        return Err(Error::UnsupportedInstruction(libc::BPF_NEG as u16));
                    }
                    _ => return Err(Error::UnknownInstruction(inst)),
                };
            }
            libc::BPF_JMP => {
                match op as u32 {
                    libc::BPF_JA => {
                        info!("JA with 0x{:x}", curr.k);
                        self.pc += curr.k as usize;
                    }
                    libc::BPF_JEQ => {
                        if self.acc == curr.k {
                            info!("JEQ: 0x{:x} == 0x{:x} -> 0x{:x}", self.acc, curr.k, curr.jt);
                            self.pc += curr.jt as usize;
                        } else {
                            info!("JEQ: 0x{:x} != 0x{:x} -> 0x{:x}", self.acc, curr.k, curr.jf);
                            self.pc += curr.jf as usize;
                        }
                    }
                    libc::BPF_JGT => {
                        if self.acc > curr.k {
                            info!("JGT: 0x{:x} > 0x{:x} -> 0x{:x}", self.acc, curr.k, curr.jt);
                            self.pc += curr.jt as usize;
                        } else {
                            info!(
                                "JGT: 0x{:x} ! > 0x{:x} -> 0x{:x}",
                                self.acc, curr.k, curr.jf
                            );
                            self.pc += curr.jf as usize;
                        }
                    }
                    libc::BPF_JGE => {
                        if self.acc >= curr.k {
                            info!("JGE: 0x{:x} >= 0x{:x} -> 0x{:x}", self.acc, curr.k, curr.jt);
                            self.pc += curr.jt as usize;
                        } else {
                            info!(
                                "JGE: 0x{:x} ! >= 0x{:x} -> 0x{:x}",
                                self.acc, curr.k, curr.jf
                            );
                            self.pc += curr.jf as usize;
                        }
                    }
                    libc::BPF_JSET => {
                        if (self.acc & curr.k) > 0 {
                            info!("JGE: 0x{:x} & 0x{:x} -> 0x{:x}", self.acc, curr.k, curr.jt);
                            self.pc += curr.jt as usize;
                        } else {
                            info!(
                                "JGE: 0x{:x} ! & 0x{:x} -> 0x{:x}",
                                self.acc, curr.k, curr.jf
                            );
                            self.pc += curr.jf as usize;
                        }
                    }
                    _ => return Err(Error::UnknownInstruction(inst)),
                };
            }
            libc::BPF_RET => {
                let rsrc = curr.code & 0x18;
                let sval = self.fetch_src(rsrc, &curr)?;
                info!("Executing RET with 0x{:x}", sval);
                return Ok(Some(sval));
            }
            libc::BPF_MISC => return Err(Error::UnsupportedInstruction(inst)),
            _ => return Err(Error::UnknownInstruction(inst)),
        }

        Ok(None)
    }

    pub fn run(&mut self, data: RunData) -> Result<u32> {
        info!("Starting VM");

        self.reset()?;

        while self.pc < self.prog.len() {
            if let Some(r) = self.execute(data)? {
                info!("execute() returned value 0x{:x}; terminating", r);
                return Ok(r);
            }
        }

        Ok(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use test_log;

    const WORDS: u32 = 4;

    fn bpf_stmt(code: u32, val: u32) -> sock_filter {
        sock_filter {
            code: code as u16,
            jt: 0,
            jf: 0,
            k: val,
        }
    }
    fn bpf_jmp(code: u32, k: u32, jt: u8, jf: u8) -> sock_filter {
        sock_filter {
            code: code as u16,
            jt,
            jf,
            k,
        }
    }

    #[test_log::test]
    fn test_ret() {
        let prog = vec![bpf_stmt(libc::BPF_RET | libc::BPF_K, 99)];
        let mut vm = BpfVM::new(&prog).unwrap();
        let data = vec![];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 99);
    }

    #[test_log::test]
    fn test_load_and_ret() {
        let prog = vec![
            bpf_stmt(libc::BPF_LD | libc::BPF_K, 99),
            bpf_stmt(libc::BPF_RET | BPF_A, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();
        let data = vec![];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 99);
    }

    #[test_log::test]
    fn test_load_data() {
        let prog = vec![
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 1*WORDS),
            bpf_stmt(libc::BPF_RET | BPF_A, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();
        let data = vec![0, 0xFFFFFFFF];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 0xFFFFFFFF);
    }

    #[test_log::test]
    fn test_alu_mask() {
        let prog = vec![
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 2*WORDS),
            bpf_stmt(libc::BPF_ALU | libc::BPF_AND | libc::BPF_K, 0xF0),
            bpf_stmt(libc::BPF_RET | BPF_A, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();

        let data = vec![0, 0, 0xFF, 0];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 0xF0);

        let data = vec![0, 0, 0x80, 0];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 0x80);
    }

    #[test_log::test]
    fn test_alu_mul() {
        let prog = vec![
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 2*WORDS),
            bpf_stmt(libc::BPF_ALU | libc::BPF_MUL | libc::BPF_K, 2),
            bpf_stmt(libc::BPF_RET | BPF_A, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();

        let data = vec![0, 0, 8, 0];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 16);
    }

    #[test_log::test]
    fn test_ld_ja_ret() {
        let prog = vec![
            bpf_stmt(libc::BPF_LD | libc::BPF_K, 99),
            bpf_stmt(libc::BPF_JMP | libc::BPF_JA, 1),
            // Should skip this one
            bpf_stmt(libc::BPF_LD | libc::BPF_K, 999),
            bpf_stmt(libc::BPF_RET | BPF_A, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();
        let data = vec![];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 99);
    }

    #[test_log::test]
    fn test_ld_gt_ret() {
        let prog = vec![
            bpf_stmt(libc::BPF_LD | libc::BPF_K, 99),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JGT, 98, 1, 0),
            // Should skip this one
            bpf_stmt(libc::BPF_LD | libc::BPF_K, 999),
            bpf_stmt(libc::BPF_RET | BPF_A, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();
        let data = vec![];
        let ret = vm.run(&data).unwrap();
        assert!(ret == 99);
    }

    #[test_log::test]
    fn test_seccomp_data_conv() {
        let sc_data = libc::seccomp_data {
            nr: 1,
            arch: 2,
            instruction_pointer: 3,
            args: [4, 5, 6, 7, 8, 9],
        };
        let data = any_to_data(&sc_data);

        let prog = vec![
            // NR
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 0*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 1, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 100),
            // arch
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 1*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 2, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 101),
            // inst_ptr
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 2*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 3, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 102),
            // args[0] = [0, 4]
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 3*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 0, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 103),
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 4*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 4, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 104),
            // args[0] = [0, 5]
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 5*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 0, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 105),
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 6*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 5, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 106),
            // args[0] = [0, 6]
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 7*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 0, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 107),
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 8*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 6, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 108),
            // args[0] = [0, 7]
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 9*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 0, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 109),
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 10*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 7, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 110),
            // args[0] = [0, 8]
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 11*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 0, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 111),
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 12*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 8, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 112),
            // args[0] = [0, 9]
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 13*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 0, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 113),
            bpf_stmt(libc::BPF_LD | libc::BPF_ABS | libc::BPF_W, 14*WORDS),
            bpf_jmp(libc::BPF_JMP | libc::BPF_JEQ, 9, 1, 0),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 114),
            bpf_stmt(libc::BPF_RET | libc::BPF_K, 0),
        ];
        let mut vm = BpfVM::new(&prog).unwrap();

        let ret = vm.run(&data).unwrap();
        assert!(ret == 0, "Failed, ret = 0x{:x}", ret);
    }
}