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

Simulate NMR API failures: Fix memory leaks and segmentation fault #2253

Merged
merged 3 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion libs/api/ebpf_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1753,11 +1753,17 @@ _initialize_ebpf_object_from_native_file(
}

Exit:
ebpf_free(program);
if (result != EBPF_SUCCESS) {
if (program && object.programs.size() == 0) {
shpalani marked this conversation as resolved.
Show resolved Hide resolved
ebpf_free(program->section_name);
ebpf_free(program->program_name);
}

clean_up_ebpf_programs(object.programs);
clean_up_ebpf_maps(object.maps);
}

ebpf_free(program);
ebpf_free_sections(infos);
EBPF_RETURN_RESULT(result);
}
Expand Down
17 changes: 17 additions & 0 deletions libs/api_common/store_helper_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,15 @@ ebpf_store_load_program_information(
Exit:
if (result != EBPF_SUCCESS) {
ebpf_free(*program_info);

// Deallocate the dynamic memory in the program_info_array vector.
if (program_info_array.size() > 0) {
for (auto program_data : program_info_array) {
ebpf_program_info_free(program_data);
}
}
}

if (program_data_key) {
close_registry_key(program_data_key);
}
Expand Down Expand Up @@ -569,6 +577,15 @@ ebpf_store_load_section_information(
Exit:
if (result != EBPF_SUCCESS) {
ebpf_free(*section_info);
// Deallocate the dynamic memory in the section_info_array vector.
if (section_info_array.size() > 0) {
for (auto section_data : section_info_array) {
ebpf_free(section_data->program_type);
shpalani marked this conversation as resolved.
Show resolved Hide resolved
ebpf_free(section_data->attach_type);
ebpf_free(const_cast<char*>(section_data->section_prefix));
ebpf_free(section_data);
}
}
}
if (section_data_key) {
close_registry_key(section_data_key);
Expand Down
1 change: 1 addition & 0 deletions libs/execution_context/ebpf_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ ebpf_native_terminate()
// ebpf_provider_unload is blocking call until all the
// native modules have been detached.
ebpf_provider_unload(_ebpf_native_provider);
_ebpf_native_provider = NULL;

// All native modules should be cleaned up by now.
ebpf_assert(!_ebpf_native_client_table || ebpf_hash_table_key_count(_ebpf_native_client_table) == 0);
Expand Down
5 changes: 4 additions & 1 deletion netebpfext/user/fwp_um.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#include "ebpf_fault_injection.h"
#include "fwp_um.h"
#include "net_ebpf_ext_sock_addr.h"
#include "netebpfext_platform.h"
Expand All @@ -21,6 +22,8 @@ DEFINE_GUID(EBPF_DEFAULT_SUBLAYER, 0x7c7b3fb9, 0x3331, 0x436a, 0x98, 0xe1, 0xb9,

std::unique_ptr<_fwp_engine> _fwp_engine::_engine;

static bool fault_injection_enabled = ebpf_fault_injection_is_enabled();

// Attempt to classify a test packet at a given WFP layer on a given interface index.
// This is used to test the xdp hook.
FWP_ACTION_TYPE
Expand Down Expand Up @@ -269,7 +272,7 @@ _fwp_engine::test_cgroup_inet4_connect(_In_ fwp_classify_parameters_t* parameter

action = test_callout(
FWPS_LAYER_ALE_CONNECT_REDIRECT_V4, FWPM_LAYER_ALE_CONNECT_REDIRECT_V4, EBPF_DEFAULT_SUBLAYER, incoming_value);
ebpf_assert(action == FWP_ACTION_PERMIT);
ebpf_assert(action == FWP_ACTION_PERMIT || !fault_injection_enabled);
shpalani marked this conversation as resolved.
Show resolved Hide resolved

if (_fwp_um_connect_request != nullptr) {
redirected =
Expand Down