Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clarify signed & unsigned for jump insts #119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ pub fn execute_program(
insn_ptr = (insn_ptr as i16 + insn.off) as usize;
};

macro_rules! unsigned_u64 {
($imm:expr) => {
($imm as u32) as u64
};
}

match insn.opc {

// BPF_LD class
Expand Down Expand Up @@ -298,20 +304,22 @@ pub fn execute_program(

// BPF_JMP class
// TODO: check this actually works as expected for signed / unsigned ops
// J-EQ, J-NE, J-GT, J-GE, J-LT, J-LE: unsigned
// JS-GT, JS-GE, JS-LT, JS-LE: signed
ebpf::JA => do_jump(),
ebpf::JEQ_IMM => if reg[_dst] == insn.imm as u64 { do_jump(); },
ebpf::JEQ_IMM => if reg[_dst] == unsigned_u64!(insn.imm) { do_jump(); },
ebpf::JEQ_REG => if reg[_dst] == reg[_src] { do_jump(); },
ebpf::JGT_IMM => if reg[_dst] > insn.imm as u64 { do_jump(); },
ebpf::JGT_IMM => if reg[_dst] > unsigned_u64!(insn.imm) { do_jump(); },
ebpf::JGT_REG => if reg[_dst] > reg[_src] { do_jump(); },
ebpf::JGE_IMM => if reg[_dst] >= insn.imm as u64 { do_jump(); },
ebpf::JGE_IMM => if reg[_dst] >= unsigned_u64!(insn.imm) { do_jump(); },
ebpf::JGE_REG => if reg[_dst] >= reg[_src] { do_jump(); },
ebpf::JLT_IMM => if reg[_dst] < insn.imm as u64 { do_jump(); },
ebpf::JLT_IMM => if reg[_dst] < unsigned_u64!(insn.imm) { do_jump(); },
ebpf::JLT_REG => if reg[_dst] < reg[_src] { do_jump(); },
ebpf::JLE_IMM => if reg[_dst] <= insn.imm as u64 { do_jump(); },
ebpf::JLE_IMM => if reg[_dst] <= unsigned_u64!(insn.imm) { do_jump(); },
ebpf::JLE_REG => if reg[_dst] <= reg[_src] { do_jump(); },
ebpf::JSET_IMM => if reg[_dst] & insn.imm as u64 != 0 { do_jump(); },
ebpf::JSET_REG => if reg[_dst] & reg[_src] != 0 { do_jump(); },
ebpf::JNE_IMM => if reg[_dst] != insn.imm as u64 { do_jump(); },
ebpf::JNE_IMM => if reg[_dst] != unsigned_u64!(insn.imm) { do_jump(); },
ebpf::JNE_REG => if reg[_dst] != reg[_src] { do_jump(); },
ebpf::JSGT_IMM => if reg[_dst] as i64 > insn.imm as i64 { do_jump(); },
ebpf::JSGT_REG => if reg[_dst] as i64 > reg[_src] as i64 { do_jump(); },
Expand Down
32 changes: 32 additions & 0 deletions tests/ubpf_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,38 @@ fn test_vm_jset_imm() {
assert_eq!(vm.execute_program().unwrap(), 0x1);
}

#[test]
fn test_vm_jmp_unsigned_extend() {
use rbpf::ebpf::{Insn, BE, EXIT, JEQ_IMM, MOV32_IMM, LD_W_REG};

// the insn `jeq r2, 0x80000000, +2` will be rejected
assert!(assemble("jeq r2, 0x80000000, +2").is_err());
// the prog is as follows:
// ldxw r2, [r1]
// be32 r2
// jeq r2, 0x80000000, +2 # 0x80000000 should be interpreted as 0x0000000080000000 (unsigned)
// mov32 r0, 2
// exit
// mov32 r0, 1
// exit
// we build it manually to bypass the verifier in `assemble(...)`
let insns = vec![
Insn { opc: LD_W_REG, dst: 2, src: 1, off: 0, imm: 0 },
Insn { opc: BE, dst: 2, src: 0, off: 0, imm: 32 },
Insn { opc: JEQ_IMM, dst: 2, src: 0, off: 2, imm: 0x80000000u32 as i32 },
Insn { opc: MOV32_IMM, dst: 0, src: 0, off: 0, imm: 2 },
Insn { opc: EXIT, dst: 0, src: 0, off: 0, imm: 0 },
Insn { opc: MOV32_IMM, dst: 0, src: 0, off: 0, imm: 1 },
Insn { opc: EXIT, dst: 0, src: 0, off: 0, imm: 0 }
];
let prog = insns.iter().map(|x| x.to_array()).flatten().collect::<Vec<u8>>();
let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
let mut data = vec![
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
];
assert_eq!(vm.execute_program(&mut data).unwrap(), 1);
}

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