Переглянути джерело

tests/ubpf_vm.rs: Add regression test for check_mem() addition overflow

Add a test to make sure we don't overflow on additions between addr and
len in check_mem().

Link: https://github.com/qmonnet/rbpf/issues/94
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Richard Smith <ret2happy@126.com>
Quentin Monnet 1 рік тому
батько
коміт
36bf0d646c
1 змінених файлів з 18 додано та 0 видалено
  1. 18 0
      tests/ubpf_vm.rs

+ 18 - 0
tests/ubpf_vm.rs

@@ -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("