Skip to content

Commit

Permalink
Make ALU operations check for invalid register values
Browse files Browse the repository at this point in the history
makeMemOp() already had a check but makeAluOp() didn't.

Fixes #505

Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Dec 23, 2023
1 parent 86e82a7 commit 94f4c61
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/asm_unmarshal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ struct Unmarshaller {
auto makeAluOp(size_t pc, ebpf_inst inst) -> Instruction {
if (inst.dst == R10_STACK_POINTER)
throw InvalidInstruction(pc, "Invalid target r10");
if (inst.dst > R10_STACK_POINTER || inst.src > R10_STACK_POINTER)
throw InvalidInstruction(pc, "Bad register");
return std::visit(overloaded{[&](Un::Op op) -> Instruction { return Un{.op = op, .dst = Reg{inst.dst}, .is64 = (inst.opcode & INST_CLS_MASK) == INST_CLS_ALU64}; },
[&](Bin::Op op) -> Instruction {
Bin res{
Expand Down
6 changes: 6 additions & 0 deletions src/test/test_marshal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ TEST_CASE("disasm_marshal_Mem", "[disasm][marshal]") {
TEST_CASE("fail unmarshal", "[disasm][marshal]") {
check_unmarshal_fail(ebpf_inst{.opcode = ((INST_MEM << 5) | INST_SIZE_B | INST_CLS_LDX), .dst = 11, .imm = 8},
"0: Bad register\n");
check_unmarshal_fail(ebpf_inst{.opcode = ((INST_MEM << 5) | INST_SIZE_B | INST_CLS_LDX), .dst = 1, .src = 11},
"0: Bad register\n");
check_unmarshal_fail(ebpf_inst{.opcode = (INST_ALU_OP_MOV | INST_SRC_IMM | INST_CLS_ALU), .dst = 11, .imm = 8},
"0: Bad register\n");
check_unmarshal_fail(ebpf_inst{.opcode = (INST_ALU_OP_MOV | INST_SRC_REG | INST_CLS_ALU), .dst = 1, .src = 11},
"0: Bad register\n");
check_unmarshal_fail(ebpf_inst{.opcode = ((INST_MEM << 5) | INST_SIZE_W | INST_CLS_LD)},
"0: plain LD\n");
check_unmarshal_fail(ebpf_inst{.opcode = INST_ALU_OP_END | INST_END_LE | INST_CLS_ALU, .dst = 1, .imm = 8}, "0: invalid endian immediate\n");
Expand Down

0 comments on commit 94f4c61

Please sign in to comment.