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: clock time detection #4513

Merged
merged 2 commits into from
Jan 17, 2025

Conversation

rscampos
Copy link
Collaborator

@rscampos rscampos commented Jan 16, 2025

1. Explain what the PR does

23d8ea1 chore(go.mod): bump libbpfgo
16d1e19 fix: clock time detection

16d1e19 fix: clock time detection

- The clock time detection was rework since the libbpfgo func
BPFHelperIsSupported changed;
- Unless it is explicitly marked as unsupported (supported=false),
it will default to BOOTTIME.
```st case the helper is not supported so the monotonic is set.

2. Explain how to test it

3. Other comments

fix: #4505

Most of the change is located in libbpfgo, but I've inverted the order of the checking since most of the time we support boottime.

What changed and why in the libbpfgo: https://github.com/aquasecurity/libbpfgo/blob/45a155ff1a362156b5c7d1b75bc3db5f8cb5e526/libbpfgo.go#L104-L116

@rscampos rscampos requested a review from geyslan January 16, 2025 18:50
@rscampos rscampos self-assigned this Jan 16, 2025
@rscampos rscampos marked this pull request as ready for review January 16, 2025 18:51
@rscampos rscampos changed the title fix: Fix clock time detection fix: clock time detection Jan 16, 2025
Comment on lines 399 to 400
// Use monotonic time if the helper isn't supported.
usedClockID = traceetime.CLOCK_MONOTONIC
Copy link
Member

Choose a reason for hiding this comment

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

What if BPFHelperIsSupported returns supported == false and errno == EPERM? Shouldn't we set it as monotonic in this case? As this wrapped helper is kinda "returning wrong errno", what about just logger.Debug the errno, I mean... we're under caps EBPF, so EPERM shouldn't be returned if the libbpf helper were returning a right errno, am I following?

If we can rely on the supported value, we should stick with it and log the errno just for the sake of debugging.

P.S.: the link of the latest libbpfgo commit related to this fix as comments would be a must.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yaniv and I discussed this today, we should treat any error as making the supported essentially indeterminable. Therefore we should short circuit and return at that point, I think with a WARN log.

Copy link
Member

Choose a reason for hiding this comment

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

But supported will only be 1 if really supported. The errno in that case, if set is only garbage.

I guess that the order of checking would always be 1. the real return (libbpf takes care of it correctly) and 2. in the case of error, errno should be complementary information (wrong or not).

Copy link
Collaborator

@NDStrahilevitz NDStrahilevitz Jan 16, 2025

Choose a reason for hiding this comment

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

I am assuming that the default return of supported is false. So if there is an error it will be false. And in any case an error should indicate that we don't actually know the support check worked. That is why the case of err!=nil is its own thing, and should stay with BOOT for the heuristic reason mentioned.

Copy link
Collaborator

@NDStrahilevitz NDStrahilevitz left a comment

Choose a reason for hiding this comment

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

Copying from my comment to geyslan:

Yaniv and I discussed this today, we should treat any error as making the supported essentially indeterminable. Therefore we should short circuit and return at that point, I think with a WARN log. This is because it is more likely these days for a kernel to run with BOOT support. Then if we know there is no error, we can treat the !supported case and move to MONOTONIC.

@rscampos rscampos force-pushed the fix_clock_time_detection branch from 9a14480 to 0e12925 Compare January 17, 2025 10:34
@rscampos
Copy link
Collaborator Author

Thank you for the discussion, everyone. @geyslan and I had an offline discussion and revised the logic (in libbpfgo and Tracee) after identifying some issues with libbpf. I'll provide a detailed explanation of the issue, but the following is the logic explanation change.

Since this feature probe in Tracee runs with elevated capabilities, it can rely on the supported flag. The logic has been slightly adjusted from the previous version: unless it is explicitly marked as unsupported (supported=false), it will default to BOOTTIME. Any errors are logged as warnings in debug mode, regardless of whether the feature is supported or !supported.

@rscampos rscampos force-pushed the fix_clock_time_detection branch 7 times, most recently from 5f66fec to 277ef87 Compare January 17, 2025 15:26
- The clock time detection was rework since the libbpfgo func
BPFHelperIsSupported changed;
- Unless it is explicitly marked as unsupported (supported=false),
it will default to BOOTTIME.
@rscampos rscampos force-pushed the fix_clock_time_detection branch from 277ef87 to 23d8ea1 Compare January 17, 2025 15:43
Copy link
Member

@geyslan geyslan left a comment

Choose a reason for hiding this comment

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

LGTM

This comment was marked as resolved.

@rscampos rscampos merged commit aeca328 into aquasecurity:main Jan 17, 2025
41 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.

Reported events timestamp is wrong
3 participants