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

Fix closing native program / maps handles in system worker thread. #2500

Merged
merged 10 commits into from
May 26, 2023

Conversation

saxena-anurag
Copy link
Contributor

@saxena-anurag saxena-anurag commented May 22, 2023

Fixes #2457

Description

Earlier fix done in PR #2395 was incomplete. The previous fix queued a workitem to close the program and map handles in the case of native module load failure. Closing the handles in system worker thread fails with INVALID_HANDLE as the handle is created for type UserMode, and the worker thread is running in system context.

The fix is to attach the current process to the worker thread to be able to close the handles. The new fix does the following:

  1. take reference on the current process, and pass process info to the worker thread.
  2. worker thread attaches process, closes handles, detaches process and release process reference.

Testing

Added new kernel tests.

Documentation

NA

@codecov
Copy link

codecov bot commented May 22, 2023

Codecov Report

Merging #2500 (e2ce9de) into main (667a948) will increase coverage by 0.04%.
The diff coverage is 95.38%.

@@            Coverage Diff             @@
##             main    #2500      +/-   ##
==========================================
+ Coverage   84.06%   84.11%   +0.04%     
==========================================
  Files         155      157       +2     
  Lines       28847    29037     +190     
==========================================
+ Hits        24251    24425     +174     
- Misses       4596     4612      +16     
Impacted Files Coverage Δ
libs/platform/user/ebpf_platform_user.cpp 84.84% <85.71%> (+0.44%) ⬆️
libs/execution_context/ebpf_native.c 90.72% <100.00%> (+0.14%) ⬆️
libs/execution_context/ebpf_program.c 84.72% <100.00%> (+0.02%) ⬆️
tests/api_test/api_test.cpp 98.26% <100.00%> (+0.09%) ⬆️

... and 8 files with indirect coverage changes

@saxena-anurag saxena-anurag changed the title DRAFT Fix closing native program / maps handles in system worker thread. May 23, 2023
@saxena-anurag saxena-anurag marked this pull request as ready for review May 23, 2023 17:00
@@ -15,6 +15,11 @@ static uint32_t _ebpf_platform_maximum_processor_count = 0;
extern DEVICE_OBJECT*
ebpf_driver_get_device_object();

typedef struct _ebpf_process_state
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typedef KAPC_STATE ebpf_process_state_t ?

return (intptr_t)process;
}

_Ret_maybenull_ ebpf_process_state_t*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? what is the need for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since ebpf_platform.h only contains:
typedef struct _ebpf_process_state ebpf_process_state_t;
the file which includes this does not know the size of the struct -- so sizeof(ebpf_process_state_t) will give compiler error.

ebpf_platform_detach_process(handle_info->process_state);

// Release the reference on the process object.
ebpf_platform_dereference_process(handle_info->process_handle);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed given line 1319 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, 1319 line is for the cleanup path which is not called in this path.

@saxena-anurag saxena-anurag added the bug Something isn't working label May 24, 2023
libs/execution_context/ebpf_native.c Outdated Show resolved Hide resolved
libs/execution_context/ebpf_native.c Outdated Show resolved Hide resolved
dthaler
dthaler previously approved these changes May 24, 2023
@@ -1408,3 +1413,37 @@ ebpf_utf8_string_to_unicode(_In_ const ebpf_utf8_string_t* input, _Outptr_ wchar
ebpf_free(unicode_string);
return retval;
}

_Ret_maybenull_ ebpf_process_state_t*
ebpf_allocate_process_state()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check Fault injection needed for all the new functions introduced?

I believe it is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is adding the following APIs:

ebpf_allocate_process_state
ebpf_platform_reference_process
ebpf_platform_dereference_process
ebpf_platform_attach_process
ebpf_platform_detach_process

Only one of these APIs is expected to fail -- ebpf_allocate_process_state. And since that API calls ebpf_allocate(), it is already covered for fault injection.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking it. Please add a comment for ebpf_allocate_process_state with
Fault injection as skipped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

shpalani
shpalani previously approved these changes May 25, 2023
@saxena-anurag saxena-anurag added this pull request to the merge queue May 25, 2023
@saxena-anurag saxena-anurag removed this pull request from the merge queue due to a manual request May 25, 2023
@dthaler dthaler added this pull request to the merge queue May 26, 2023
Merged via the queue into microsoft:main with commit c471719 May 26, 2023
68 checks passed
@saxena-anurag saxena-anurag deleted the user/anusa/issue_2457 branch May 26, 2023 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bpf_object__load hangs indefinitely if a native driver fails to load
5 participants