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

Export functions necessary for ebpf-go port #4116

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

lmb
Copy link
Collaborator

@lmb lmb commented Jan 6, 2025

This PR exports various functions required to make the ebpf-go port work. They fall into the following categories:

  • Functions dealing with the osfhandle machinery. There is no easy / reliable way to access the correct ucrt function from Go without re-exporting.
  • Functions which mirror existing functionality but return fd_t in some fashion, instead of operating on libbpf objects. This is necessary because the library has different semantics than libbpf.
  • Functions to translate between GUID and libbpf enums. These are necessary because the data types in the Go library can not accommodate GUIDs. Hence we need use enums and transparently map to GUID.

Commit messages below

Export ebpf_close_fd and ebpf_dup_fd

Export functions used to manipulate fd_t. This makes the use of the UCRT
_get_osfhandle an implementation detail which is abstracted away from users
of ebpfapi.dll. This also makes it easier to use the API via run-time
dynamic linking.

Allow attaching a link as an fd

Add a function which returns the link as an fd when attaching a program. The
existing functions are re-jigged to reduce code duplication.

Export functions to translate between GUID and enums

Allow users to resolve libbpf style enums to Windows GUIDs and vice versa.

@lmb lmb force-pushed the export-funcs branch 2 times, most recently from e455b71 to 16742ad Compare January 6, 2025 16:48
Export functions used to manipulate fd_t. This makes the use of the UCRT
_get_osfhandle an implementation detail which is abstracted away from
users of ebpfapi.dll. This also makes it easier to use the API via
run-time dynamic linking.
@lmb lmb force-pushed the export-funcs branch 4 times, most recently from 3dd21e0 to 1b2b5f1 Compare January 7, 2025 16:59
@@ -316,7 +316,7 @@ get_ebpf_program_type(bpf_prog_type_t bpf_program_type)
}

_Ret_maybenull_ const ebpf_attach_type_t*
get_ebpf_attach_type(bpf_attach_type_t bpf_attach_type) noexcept
ebpf_get_ebpf_attach_type(bpf_attach_type_t bpf_attach_type) noexcept
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this function is subject to a use after free: the returned ebpf_attach_type* is directly returned from _windows_section_definitions. That vector does get cleared when the DLL unloads. So something like:

  1. Load DLL
  2. Lookup an attach type
  3. Unload DLL
  4. Use attach type

would cause the use after free. Should I change this function to take an ebpf_attach_type_t* as an argument and copy into that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@saxena-anurag what do you think?

@lmb lmb force-pushed the export-funcs branch 2 times, most recently from de8e85e to 0463b70 Compare January 8, 2025 14:43
* @see ebpf_program_attach_by_fd
*/
_Must_inspect_result_ ebpf_result_t
ebpf_program_attach_as_fd(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not a great name, ebpf_program_attach_as_fd which already exists would make more sense imo. Open to bikeshedding.

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe ebpf_program_attach_by_fd_and_get_fd?
It's wordy but the "by fd" in 413 refers to the first input, which still applies in this prototype

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ebpf_program_attach_fds? I have a suspicion that there might be more cases like this in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, I'm currently working on ebpf_object_load_native_fds.

Copy link
Collaborator

Choose a reason for hiding this comment

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

To me, "attach fds" and "load ... fds" sound like they attach or load multiple fds, which is not the case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can we break API and rename _by_fd to _from_fd or so?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Or ebpf_program_attach_by_fds?

lmb added 2 commits January 8, 2025 08:13
Add a function which returns the link as an fd when attaching a program.
The existing functions are re-jigged to reduce code duplication.
Allow users to resolve libbpf style enums to Windows GUIDs and vice
versa.
ebpfapi/Source.def Outdated Show resolved Hide resolved
ebpfapi/Source.def Outdated Show resolved Hide resolved
ebpfapi/Source.def Show resolved Hide resolved
* @see ebpf_program_attach_by_fd
*/
_Must_inspect_result_ ebpf_result_t
ebpf_program_attach_as_fd(
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe ebpf_program_attach_by_fd_and_get_fd?
It's wordy but the "by fd" in 413 refers to the first input, which still applies in this prototype

include/ebpf_api.h Outdated Show resolved Hide resolved
include/ebpf_api.h Outdated Show resolved Hide resolved
include/ebpf_api.h Outdated Show resolved Hide resolved
include/ebpf_api.h Outdated Show resolved Hide resolved
include/ebpf_api.h Outdated Show resolved Hide resolved
include/ebpf_api.h Outdated Show resolved Hide resolved
libs/api/ebpf_api.cpp Outdated Show resolved Hide resolved
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.

3 participants