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

Isolate eBPF store APIs into atomic km & um libraries. #2690

Merged
merged 63 commits into from
Aug 7, 2023

Conversation

gtrevi
Copy link
Collaborator

@gtrevi gtrevi commented Jul 19, 2023

Closes #2136

Description

This PR:

  • Creates two distinct and atomic KM & UM eBPF store library projects, ebpf_store_helper_km and ebpf_store_helper_um respectively, while maintaining a common global header, \include\ebpf_store_helper.h.
  • Consolidates all store API calls into the new libraries.
  • Fixes some bugs from code analysis.
  • Normalizes all related API naming.
  • Updates the MSI artifacts.
  • Updates the NuGet artifacts.

Testing

CI/CD

Documentation

eBpfExtensions.md

dthaler
dthaler previously approved these changes Aug 1, 2023
@gtrevi gtrevi enabled auto-merge August 2, 2023 20:25
dthaler
dthaler previously approved these changes Aug 3, 2023
*provider_key = NULL;

// Open (or create) root eBPF registry path.
#ifdef USER_MODE
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion define ebpf_store_root_key differently for user mode and kernel mode, then you dont have to differentiate in the ebpf_create_registry_key call.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The "sub-key" would also need to be part of the differentiation as well.

I refactored all the existing so that the ebpf_store_root_key and the new ebpf_store_root_sub_key variables are now atomically defined in their respective KM/UM ebpf_registry_helper libraries (and not in the projects that uses them, which was not ideal), this also further simplifies the code that uses the libraries.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd asked why they're different in the first place, and I still haven't heard any answer to that. Whatever the answer is, it should be documented in // comments at least, in the file(s).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

By looking to the code, it boils down to the different APIs used or KM/UM, i.e. ZwCreateKey and RegCreateKeyExA respectively. The first, takes a NULL (for OBJECT_ATTRIBUTES->RootDirectory) to use the given path as a fully qualified path, whereas the second builds the fully qualified path from predefined keys (https://learn.microsoft.com/en-us/windows/win32/sysinfo/predefined-keys):

But I'd still call out @saxena-anurag for the actual history of this implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think what Gianni mentioned was the reason to use #ifdef USER_MODE earlier -- to wrap 2 different registry APIs in a single common API (ebpf_create_registry_key())

Copy link
Collaborator

Choose a reason for hiding this comment

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

ZwCreateKey exists in user-mode too, so is not a reason to have a difference.

docs/eBpfExtensions.md Outdated Show resolved Hide resolved
docs/eBpfExtensions.md Outdated Show resolved Hide resolved
include/ebpf_store_helper.h Show resolved Hide resolved
libs/platform/CMakeLists.txt Outdated Show resolved Hide resolved
tools/export_program_info/CMakeLists.txt Outdated Show resolved Hide resolved
tests/sample/ext/drv/sample_ext.vcxproj Outdated Show resolved Hide resolved
tests/sample/ext/drv/CMakeLists.txt Outdated Show resolved Hide resolved
@gtrevi gtrevi requested a review from saxena-anurag August 5, 2023 05:09
docs/eBpfExtensions.md Outdated Show resolved Hide resolved
include/ebpf_store_helper.h Outdated Show resolved Hide resolved
@gtrevi gtrevi requested a review from dthaler August 7, 2023 15:59
Copy link
Collaborator

@dthaler dthaler left a comment

Choose a reason for hiding this comment

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

Approving since this is a great improvement, but I still don't understand the ZwCreateKey issue. I'll look into it myself in my PR work so can be dealt with separate from this PR.

#include "ebpf_store_helper.h"
#include "ebpf_windows.h"

#define IS_SUCCESS(x) (x == EBPF_SUCCESS)
Copy link
Contributor

Choose a reason for hiding this comment

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

non blocking comment: This macro was earlier defined as the UM and KM APIs dealt with different return types, but that is not the case now. So maybe this can be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure, that can eventually be replaced by its definition, if needed.

ebpf_close_registry_key(ebpf_store_key_t key);

_Must_inspect_result_ ebpf_result_t
ebpf_write_registry_value_binary(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: is it possible to merge this and the KM registry_helper.h into a single header file (but still have 2 different implementations / .c files)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

UM & KM have different functions (i.e. UM implements more helpers), so that'd require using preprocessor vars...

@gtrevi gtrevi added this pull request to the merge queue Aug 7, 2023
Merged via the queue into microsoft:main with commit 4cb79e5 Aug 7, 2023
@gtrevi gtrevi deleted the gtrevi/reg-api branch August 7, 2023 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Isolate eBPF Store APIs into a separate library.
4 participants