Skip to content

Commit

Permalink
tests/ubpf_vm.rs: Add regression test for check_mem() addition overflow
Browse files Browse the repository at this point in the history
Add a test to make sure we don't overflow on additions between addr and
len in check_mem().

Link: #94
Signed-off-by: Quentin Monnet <[email protected]>
Signed-off-by: Richard Smith <[email protected]>
  • Loading branch information
qmonnet committed Jan 5, 2024
1 parent 56669f8 commit 36bf0d6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/ubpf_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,24 @@ fn test_vm_stdw() {
assert_eq!(vm.execute_program(mem).unwrap(), 0x44332211);
}

// If this case is not handled properly in check_mem(), then we may overflow when adding the
// context address and the offset, and make the thread panic with "attempt to add with overflow".
// Check that we panic with the expected out-of-bounds error.
#[test]
#[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
fn test_vm_stdw_add_overflow() {
let prog = assemble("
stdw [r2-0x1], 0x44332211
ldxw r0, [r1+2]
exit").unwrap();
let mem = &mut [
0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xcc, 0xdd
];
let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(&prog), 0x00, 0x10).unwrap();
_ = vm.execute_program(mem).unwrap();
}

#[test]
fn test_vm_sth() {
let prog = assemble("
Expand Down

0 comments on commit 36bf0d6

Please sign in to comment.