-
Notifications
You must be signed in to change notification settings - Fork 434
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
Conversation
a0f80bd
to
2a66416
Compare
pkg/ebpf/c/tracee.bpf.c
Outdated
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()`.
2a66416
to
154325e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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 inn
iterations - the macro returns 1 (true), which is an issue if the prefix is longer thann
, while the function returns 0 (false) which is wrong if the prefix and string are identical. An additional check was added tohas_prefix()
to account for prefixes longer thann
, while still returning true when the strings are equal.Additionally, an
strncmp()
macro and function were added, because most uses ofhas_prefix()
become clearer when usingstrncmp()
.Only a single usage of
has_prefix()
(infilter_file_path()
) cannot usestrncmp()
because the prefix length can only be determined at runtime which makes usage of an unrolled loop impossible. Instead, it must usehas_prefix()
which accounts for prefixes shorter than the string, unlikestrncmp()
.