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(driver): correctly retrieve src ip+port from UDP recvmsg and recvfrom #1123

Merged
merged 5 commits into from May 29, 2023

Conversation

Andreagit97
Copy link
Member

@Andreagit97 Andreagit97 commented May 29, 2023

What type of PR is this?

/kind bug

Any specific area of the project related to this PR?

/area driver-kmod

/area driver-bpf

Does this PR require a change in the driver versions?

/version driver-API-version-patch

/version driver-SCHEMA-version-patch

What this PR does / why we need it:

This PR fixes a regression in the old drivers (bpf, kmod).
Some months ago I introduced this patch Andreagit97@1bc30f3 to obtain the correct tuple from the kernel in TCP connections. Unfortunately, this broke the tuple collection with UDP connections :/ The syscall involved are recvfrom, recvmsg

TL;DR; when collecting recvfrom or recvmsg with TCP connections we shouldn't rely on userspace structs to obtain information on the tuple, we need to check it directly in the kernel because userspace structs could be empty. On the other side with UDP, we always need to check the tuple in userspace structs because IP src and port src are never written in kernel structs, the network packet directly populates userspace structs https://github.com/torvalds/linux/blob/e338142b39cf40155054f95daa28d210d2ee1b2d/net/ipv4/udp.c#L1921

The fix is quite simple. As a first attempt, we try to read IP src and port src from the kernel, if the port is invalid (0) we fall back to userspace structs. Unfortunately with UDP connections, there could be cases in which also userspace struct will be empty because maybe the user is using recvfrom with src_addr==NULL, in this case, we cannot recover IP src and port src, or at least I didn't find a reliable way to do that :(

I added some tests to check the expected output. As you can notice there are still some GTEST_SKIP(), because the behavior is not ideal, but at least we fixed the regression. In the next future, we need to adapt these network syscalls between the 3 drivers.

Which issue(s) this PR fixes:

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

fix(driver): correctly retrieve src ip+port from UDP `recvmsg` and `recvfrom`

Signed-off-by: Andrea Terzolo <andrea.terzolo@polito.it>
Signed-off-by: Andrea Terzolo <andrea.terzolo@polito.it>
Signed-off-by: Andrea Terzolo <andrea.terzolo@polito.it>
Signed-off-by: Andrea Terzolo <andrea.terzolo@polito.it>
@Andreagit97
Copy link
Member Author

the fix is really small ~20 lines all the other stuff are comments and tests :)

@FedeDP
Copy link
Contributor

FedeDP commented May 29, 2023

/cc @LucaGuerra

@poiana poiana requested a review from LucaGuerra May 29, 2023 10:14
@FedeDP
Copy link
Contributor

FedeDP commented May 29, 2023

I'd also bump driver schema minor version since this impacts what we send to userspace.

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

/approve

@poiana
Copy link
Contributor

poiana commented May 29, 2023

LGTM label has been added.

Git tree hash: 95577cb45521e1ba515a0e31189d2ee8961b465b

Signed-off-by: Andrea Terzolo <andrea.terzolo@polito.it>
Co-authored-by: Federico Di Pierro <nierro92@gmail.com>
@Andreagit97
Copy link
Member Author

Andreagit97 commented May 29, 2023

I'd also bump driver schema minor version since this impacts what we send to userspace.

Done, thank you!

Copy link
Contributor

@LucaGuerra LucaGuerra 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 so much for this 🙏

@poiana
Copy link
Contributor

poiana commented May 29, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, FedeDP, LucaGuerra

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [Andreagit97,FedeDP,LucaGuerra]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit 1de9127 into falcosecurity:master May 29, 2023
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants