From f7294844b20df5ecb32b47ad74b000840db63de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Thu, 12 Sep 2024 16:21:00 +0200 Subject: [PATCH 1/3] Fixes EbpfError::ExecutionOverrun in interpreter. --- src/interpreter.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index e230a8df..4dd6fa23 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -160,10 +160,10 @@ impl<'a, 'b, C: ContextObject> Interpreter<'a, 'b, C> { let config = &self.executable.get_config(); self.vm.due_insn_count += 1; - let mut next_pc = self.reg[11] + 1; - if next_pc as usize * ebpf::INSN_SIZE > self.program.len() { + if self.reg[11] as usize * ebpf::INSN_SIZE >= self.program.len() { throw_error!(self, EbpfError::ExecutionOverrun); } + let mut next_pc = self.reg[11] + 1; let mut insn = ebpf::get_insn_unchecked(self.program, self.reg[11] as usize); let dst = insn.dst as usize; let src = insn.src as usize; From 6c82b1339e7f380baef8da911141566ce546e0dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Thu, 12 Sep 2024 16:21:17 +0200 Subject: [PATCH 2/3] Fixes EbpfError::ExceededMaxInstructions in interpreter. --- src/interpreter.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/interpreter.rs b/src/interpreter.rs index 4dd6fa23..19aec1ab 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -159,6 +159,9 @@ impl<'a, 'b, C: ContextObject> Interpreter<'a, 'b, C> { pub fn step(&mut self) -> bool { let config = &self.executable.get_config(); + if config.enable_instruction_meter && self.vm.due_insn_count >= self.vm.previous_instruction_meter { + throw_error!(self, EbpfError::ExceededMaxInstructions); + } self.vm.due_insn_count += 1; if self.reg[11] as usize * ebpf::INSN_SIZE >= self.program.len() { throw_error!(self, EbpfError::ExecutionOverrun); From a2466f22b0a4110ee7d335e3013a4e968aaeaacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Thu, 12 Sep 2024 16:24:25 +0200 Subject: [PATCH 3/3] Adds test coverage. --- tests/execution.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/execution.rs b/tests/execution.rs index 424005b1..fae306d2 100644 --- a/tests/execution.rs +++ b/tests/execution.rs @@ -3499,6 +3499,15 @@ fn test_execution_overrun() { TestContextObject::new(1), ProgramResult::Err(EbpfError::ExceededMaxInstructions), ); + test_interpreter_and_jit_asm!( + " + add r1, 0", + config.clone(), + [], + (), + TestContextObject::new(0), + ProgramResult::Err(EbpfError::ExceededMaxInstructions), + ); } #[test]