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(epbf): fix behavior of has_prefix() and add strncmp() #4394

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

oshaked1
Copy link
Contributor

@oshaked1 oshaked1 commented Nov 24, 2024

Closes #4395

There are 2 implementations for has_prefix(), one as a macro and one as a function (depending on the compiler version). They both behave differently in cases where no difference was found in n iterations - the macro returns 1 (true), which is an issue if the prefix is longer than n, while the function returns 0 (false) which is wrong if the prefix and string are identical. An additional check was added to has_prefix() to account for prefixes longer than n, while still returning true when the strings are equal.

Additionally, an strncmp() macro and function were added, because most uses of has_prefix() become clearer when using strncmp().

Only a single usage of has_prefix() (in filter_file_path()) cannot use strncmp() because the prefix length can only be determined at runtime which makes usage of an unrolled loop impossible. Instead, it must use has_prefix() which accounts for prefixes shorter than the string, unlike strncmp().

@@ -3140,7 +3140,7 @@ statfunc int capture_file_write(struct pt_regs *ctx, u32 event_id, bool is_buf)
// otherwise the capture will overwrite itself.
int pid = 0;
void *path_buf = get_path_str_cached(file);
if (path_buf != NULL && has_prefix("/dev/null", (char *) path_buf, 10)) {
if (path_buf != NULL && strncmp("/dev/null", (char *) path_buf, 9) == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, don't we want to compare 10 bytes to take into account null byte as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right I'll change it

There are 2 implementations for `has_prefix()`, one as a macro and one as a function (depending on the compiler version).
They both behave differently in cases where no difference was found in `n` iterations - the macro returns 1 (true), which is an issue if the prefix is longer than `n`, while the function returns 0 (false) which is wrong if the prefix and string are identical.
An additional check was added to `has_prefix()` to account for prefixes longer than `n`, while still returning true when the strings are equal.

Additionally, an `strncmp()` macro and function were added, because most uses of `has_prefix()` become clearer when using `strncmp()`.

Only a single usage of `has_prefix()` (in `filter_file_path()`) cannot use `strncmp()` because the prefix length can only be determined at runtime which makes usage of an unrolled loop impossible. Instead, it must use `has_prefix()` which accounts for prefixes shorter than the string, unlike `strncmp()`.
@oshaked1 oshaked1 force-pushed the has_prefix_to_strncmp branch from 2a66416 to 154325e Compare November 25, 2024 09:02
Copy link
Collaborator

@yanivagman yanivagman left a comment

Choose a reason for hiding this comment

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

LGTM

@yanivagman yanivagman merged commit aa615f3 into aquasecurity:main Nov 25, 2024
31 checks passed
@oshaked1 oshaked1 deleted the has_prefix_to_strncmp branch November 25, 2024 12:47
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.

has_prefix() behaves incorrectly
2 participants