Skip to content

Commit

Permalink
Make undefined behavior check optional
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett committed May 20, 2024
1 parent 6007cad commit 1e43317
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions libfuzzer/libfuzz_harness.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, std::size_t size)
return -1;
}

ubpf_toggle_undefined_behavior_check(vm.get(), true);

char* error_message = nullptr;


if (ubpf_load(vm.get(), program_start, program_length, &error_message) != 0) {
// The program failed to load, due to a validation error.
// This is not interesting, as the fuzzer input is invalid.
Expand Down
12 changes: 12 additions & 0 deletions vm/inc/ubpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,18 @@ extern "C"
int
ubpf_set_instruction_limit(struct ubpf_vm* vm, uint32_t limit, uint32_t* previous_limit);


/**
* @brief Enable or disable undefined behavior checks. Undefined behavior includes
* reading from uninitialized memory or using uninitialized registers.
*
* @param[in] vm VM to enable or disable undefined behavior checks on.
* @param[in] enable Enable undefined behavior checks if true, disable if false.
* @return true if undefined behavior checks were previously enabled.
* @return false if undefined behavior checks were previously disabled.
*/
bool
ubpf_toggle_undefined_behavior_check(struct ubpf_vm* vm, bool enable);
#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions vm/ubpf_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct ubpf_vm
external_function_validate_t dispatcher_validate;

bool bounds_check_enabled;
bool undefined_behavior_check_enabled;
int (*error_printf)(FILE* stream, const char* format, ...);
struct ubpf_jit_result (*jit_translate)(struct ubpf_vm* vm, uint8_t* buffer, size_t* size, enum JitMode jit_mode);
bool (*jit_update_dispatcher)(
Expand Down
19 changes: 16 additions & 3 deletions vm/ubpf_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ ubpf_toggle_bounds_check(struct ubpf_vm* vm, bool enable)
return old;
}

bool
ubpf_toggle_undefined_behavior_check(struct ubpf_vm* vm, bool enable)
{
bool old = vm->undefined_behavior_check_enabled;
vm->undefined_behavior_check_enabled = enable;
return old;
}

void
ubpf_set_error_print(struct ubpf_vm* vm, int (*error_printf)(FILE* stream, const char* format, ...))
{
Expand Down Expand Up @@ -105,6 +113,7 @@ ubpf_create(void)
}

vm->bounds_check_enabled = true;
vm->undefined_behavior_check_enabled = false;
vm->error_printf = fprintf;

#if defined(__x86_64__) || defined(_M_X64)
Expand Down Expand Up @@ -385,7 +394,7 @@ static inline void
ubpf_mark_shadow_stack(
const struct ubpf_vm* vm, uint8_t* stack, uint64_t stack_length, uint8_t* shadow_stack, void* address, size_t size)
{
if (!vm->bounds_check_enabled) {
if (!vm->undefined_behavior_check_enabled) {
return;
}

Expand Down Expand Up @@ -426,7 +435,7 @@ static inline bool
ubpf_check_shadow_stack(
const struct ubpf_vm* vm, uint8_t* stack, uint64_t stack_length, uint8_t* shadow_stack, void* address, size_t size)
{
if (!vm->bounds_check_enabled) {
if (!vm->undefined_behavior_check_enabled) {
return true;
}

Expand Down Expand Up @@ -469,6 +478,10 @@ ubpf_check_shadow_stack(
static inline bool
ubpf_validate_shadow_register(const struct ubpf_vm* vm, uint16_t* shadow_registers, struct ebpf_inst inst)
{
if (!vm->undefined_behavior_check_enabled) {
return true;
}

bool src_register_required = false;
bool dst_register_required = false;
bool dst_register_initialized = false;
Expand Down Expand Up @@ -603,7 +616,7 @@ ubpf_exec_ex(
0,
};

if (vm->bounds_check_enabled) {
if (vm->undefined_behavior_check_enabled) {
shadow_stack = calloc(stack_length / 8, 1);
if (!shadow_stack) {
return_value = -1;
Expand Down

0 comments on commit 1e43317

Please sign in to comment.