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

libxdp: Fix xdp prog memory leak in xsk_setup_xdp_prog #376

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

ricmli
Copy link
Contributor

@ricmli ricmli commented Nov 21, 2023

In the xsk_setup_xdp_prog function, the xsk structure temporarily takes ownership of an xdp_prog and stores it in ctx->xdp_prog. However, the allocated xdp_prog is not freed in xsk_destroy_xsk_struct, leading to a memory leak. This commit addresses the issue by adding a call to xdp_program__close to properly release the allocated xdp_prog memory.

In the xsk_setup_xdp_prog function, the xsk structure
temporarily takes ownership of an xdp_prog and stores it in
ctx->xdp_prog. However, the allocated memory is not freed
in xsk_destroy_xsk_struct, leading to a memory leak. This
commit addresses the issue by adding a call to
xdp_program_close to properly release the allocated
xdp_prog memory.

Signed-off-by: Ric Li <ming3.li@intel.com>
@ricmli
Copy link
Contributor Author

ricmli commented Nov 21, 2023

Reproduction Steps:

We have two processes involved: Process 1, a privileged process, and Process 2, an unprivileged process.

  • Process 1:

    • Loads and attaches our xdp object file using xdp_program__open_file and xdp_program__attach, or takes no action.
    • Calls xsk_setup_xdp_prog to obtain the xsks_map_fd.
    • When exit, detach and close the prog we opened, also detach and close the multiprog.
  • Process 2:

    • Follows the standard AF_XDP routine, except for setting the XSK_LIBXDP_FLAGS__INHIBIT_PROG_LOAD, obtains the xsks_map_fd from Process 1 via a unix domain socket and updates the socket fd to xsks_map.

Example Output from AddressSanitizer for Process 1:

=================================================================
==702507==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 208 byte(s) in 2 object(s) allocated from:
    #0 0x7f85fa8b4a57 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    #1 0x7f85fb391a5b in xdp_program__new /home/media/ric/ricmli/xdp-tools/lib/libxdp/libxdp.c:1112
    #2 0x7f85fb391a5b in xdp_program__create_from_obj /home/media/ric/ricmli/xdp-tools/lib/libxdp/libxdp.c:1174

...

SUMMARY: AddressSanitizer: 10688 byte(s) leaked in 40 allocation(s).

Copy link
Member

@tohojo tohojo left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! :)

@tohojo tohojo merged commit cae9c91 into xdp-project:master Nov 21, 2023
5 checks passed
@ricmli ricmli deleted the xdp_prog_leak branch November 23, 2023 03:30
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.

2 participants