Skip to content

Commit

Permalink
Truncate all ALU32 except for bit-endianess (#474)
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <alanjo@microsoft.com>
  • Loading branch information
Alan-Jowett committed May 19, 2024
1 parent 307de7d commit 25425b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions vm/ubpf_jit_x86_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ translate(struct ubpf_vm* vm, struct jit_state* state, char** errmsg)
state->jit_status = UnknownInstruction;
*errmsg = ubpf_error("Unknown instruction at PC %d: opcode %02x", i, inst.opcode);
}

// If this is a ALU32 instruction, truncate the target register to 32 bits.
if (((inst.opcode & EBPF_CLS_MASK) == EBPF_CLS_ALU) &&
(inst.opcode & EBPF_ALU_OP_MASK) != 0xd0) {
emit_truncate_u32(state, dst);
}
}

if (state->jit_status != NoError) {
Expand Down
6 changes: 6 additions & 0 deletions vm/ubpf_jit_x86_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ emit_alu32_imm8(struct jit_state* state, int op, int src, int dst, int8_t imm)
emit1(state, imm);
}

static inline void
emit_truncate_u32(struct jit_state* state, int destination)
{
emit_alu32_imm32(state, 0x81, 4, destination, UINT32_MAX);
}

/* REX.W prefix and ModRM byte */
/* We use the MR encoding when there is a choice */
/* 'src' is often used as an opcode extension */
Expand Down
6 changes: 4 additions & 2 deletions vm/ubpf_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ ubpf_exec_ex(
}
break;
case EBPF_OP_JEQ32_REG:
if (u32(reg[inst.dst]) == reg[inst.src]) {
if (u32(reg[inst.dst]) == u32(reg[inst.src])) {
pc += inst.offset;
}
break;
Expand Down Expand Up @@ -1003,6 +1003,9 @@ ubpf_exec_ex(
// valid.
break;
}
if (((inst.opcode & EBPF_CLS_MASK) == EBPF_CLS_ALU) && (inst.opcode & EBPF_ALU_OP_MASK) != 0xd0) {
reg[inst.dst] &= UINT32_MAX;
}
}

cleanup:
Expand Down Expand Up @@ -1371,7 +1374,6 @@ ubpf_get_registers(const struct ubpf_vm* vm)
fprintf(stderr, "uBPF warning: registers are not exposed in release mode. Please recompile in debug mode\n");
return NULL;
}

#endif

typedef struct _ebpf_encoded_inst
Expand Down

0 comments on commit 25425b9

Please sign in to comment.