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

Follow-up for masks on shift operations #105

Merged
merged 2 commits into from
Mar 15, 2024
Merged
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
13 changes: 8 additions & 5 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ fn check_mem(addr: u64, len: usize, access_type: &str, insn_ptr: usize,
#[allow(cyclomatic_complexity)]
pub fn execute_program(prog_: Option<&[u8]>, mem: &[u8], mbuff: &[u8], helpers: &HashMap<u32, ebpf::Helper>) -> Result<u64, Error> {
const U32MAX: u64 = u32::MAX as u64;
const SHIFT_MASK_32: u32 = 0x1f;
const SHIFT_MASK_64: u64 = 0x3f;

let prog = match prog_ {
Expand Down Expand Up @@ -226,10 +225,12 @@ pub fn execute_program(prog_: Option<&[u8]>, mem: &[u8], mbuff: &[u8], helpers:
ebpf::OR32_REG => reg[_dst] = (reg[_dst] as u32 | reg[_src] as u32) as u64,
ebpf::AND32_IMM => reg[_dst] = (reg[_dst] as u32 & insn.imm as u32) as u64,
ebpf::AND32_REG => reg[_dst] = (reg[_dst] as u32 & reg[_src] as u32) as u64,
ebpf::LSH32_IMM => reg[_dst] = (reg[_dst] as u32).wrapping_shl(insn.imm as u32 & SHIFT_MASK_32) as u64,
ebpf::LSH32_REG => reg[_dst] = (reg[_dst] as u32).wrapping_shl(reg[_src] as u32 & SHIFT_MASK_32) as u64,
ebpf::RSH32_IMM => reg[_dst] = (reg[_dst] as u32).wrapping_shr(insn.imm as u32 & SHIFT_MASK_32) as u64,
ebpf::RSH32_REG => reg[_dst] = (reg[_dst] as u32).wrapping_shr(reg[_src] as u32 & SHIFT_MASK_32) as u64,
// As for the 64-bit version, we should mask the number of bits to shift with
// 0x1f, but .wrappping_shr() already takes care of it for us.
ebpf::LSH32_IMM => reg[_dst] = (reg[_dst] as u32).wrapping_shl(insn.imm as u32) as u64,
ebpf::LSH32_REG => reg[_dst] = (reg[_dst] as u32).wrapping_shl(reg[_src] as u32) as u64,
ebpf::RSH32_IMM => reg[_dst] = (reg[_dst] as u32).wrapping_shr(insn.imm as u32) as u64,
ebpf::RSH32_REG => reg[_dst] = (reg[_dst] as u32).wrapping_shr(reg[_src] as u32) as u64,
ebpf::NEG32 => { reg[_dst] = (reg[_dst] as i32).wrapping_neg() as u64; reg[_dst] &= U32MAX; },
ebpf::MOD32_IMM if insn.imm as u32 == 0 => (),
ebpf::MOD32_IMM => reg[_dst] = (reg[_dst] as u32 % insn.imm as u32) as u64,
Expand All @@ -239,6 +240,8 @@ pub fn execute_program(prog_: Option<&[u8]>, mem: &[u8], mbuff: &[u8], helpers:
ebpf::XOR32_REG => reg[_dst] = (reg[_dst] as u32 ^ reg[_src] as u32) as u64,
ebpf::MOV32_IMM => reg[_dst] = insn.imm as u32 as u64,
ebpf::MOV32_REG => reg[_dst] = (reg[_src] as u32) as u64,
// As for the 64-bit version, we should mask the number of bits to shift with
// 0x1f, but .wrappping_shr() already takes care of it for us.
ebpf::ARSH32_IMM => { reg[_dst] = (reg[_dst] as i32).wrapping_shr(insn.imm as u32) as u64; reg[_dst] &= U32MAX; },
ebpf::ARSH32_REG => { reg[_dst] = (reg[_dst] as i32).wrapping_shr(reg[_src] as u32) as u64; reg[_dst] &= U32MAX; },
ebpf::LE => {
Expand Down
46 changes: 46 additions & 0 deletions tests/ubpf_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,52 @@ fn test_vm_arsh_reg() {
assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
}

#[test]
fn test_vm_arsh_imm_overflow() {
let prog = assemble("
mov r0, 1
lsh r0, 63
arsh r0, 0xff20
exit").unwrap();
let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
assert_eq!(vm.execute_program().unwrap(), 0xffffffff80000000);
}

#[test]
fn test_vm_arsh_reg_overflow() {
let prog = assemble("
mov r0, 1
lsh r0, 63
mov r1, 0xff04
arsh r0, r1
exit").unwrap();
let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
assert_eq!(vm.execute_program().unwrap(), 0xf800000000000000);
}

#[test]
fn test_vm_arsh32_imm_overflow() {
let prog = assemble("
mov32 r0, 1
lsh32 r0, 31
arsh32 r0, 0xff10
exit").unwrap();
let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
}

#[test]
fn test_vm_arsh32_reg_overflow() {
let prog = assemble("
mov32 r0, 1
lsh32 r0, 31
mov32 r1, 32
arsh32 r0, r1
exit").unwrap();
let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
assert_eq!(vm.execute_program().unwrap(), 0x80000000);
}

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