Skip to content

Commit

Permalink
Fix memory issues in ebpfsvc
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Aug 15, 2023
1 parent 0b18e21 commit 24ba7c9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[submodule "external/ubpf"]
path = external/ubpf
url = https://github.com/iovisor/ubpf.git
branch = memory
[submodule "external/bpftool"]
path = external/bpftool
url = https://github.com/dthaler/bpftool-1.git
Expand Down
19 changes: 17 additions & 2 deletions ebpfsvc/rpc_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ ebpf_server_verify_and_load_program(
if (info->instruction_count == 0) {
return EBPF_INVALID_ARGUMENT;
}
*logs = nullptr;
*logs_size = 0;

// Set the handle of program being verified in thread-local storage.
set_program_under_verification(reinterpret_cast<ebpf_handle_t>(info->program_handle));

const char* ebpf_logs = nullptr;
uint32_t ebpf_logs_size = 0;
result = ebpf_verify_and_load_program(
&info->program_type,
reinterpret_cast<ebpf_handle_t>(info->program_handle),
Expand All @@ -36,8 +40,19 @@ ebpf_server_verify_and_load_program(
info->handle_map,
info->instruction_count,
reinterpret_cast<ebpf_inst*>(info->instructions),
const_cast<const char**>(logs),
logs_size);
&ebpf_logs,
&ebpf_logs_size);

if (ebpf_logs) {
// The ebpf_logs buffer was allocated by the ebpf allocator whereas we
// must return a string allocated by the MIDL allocator.
*logs = (char*)MIDL_user_allocate(ebpf_logs_size);
if (*logs) {
memcpy(*logs, ebpf_logs, ebpf_logs_size);
ebpf_free((void*)ebpf_logs);
*logs_size = ebpf_logs_size;
}
}

ebpf_clear_thread_local_storage();
return result;
Expand Down
2 changes: 1 addition & 1 deletion external/ubpf
Submodule ubpf updated 1 files
+5 −0 vm/ubpf_jit_arm64.c
2 changes: 1 addition & 1 deletion libs/service/verifier_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ verify_byte_code(
info.type = get_program_type_windows(*program_type);
} catch (std::runtime_error e) {
error << "error: " << e.what();
*error_message = allocate_string(error.str());
*error_message = allocate_string(error.str(), error_message_size);
return EBPF_VERIFICATION_FAILED;
}

Expand Down

0 comments on commit 24ba7c9

Please sign in to comment.