-
Notifications
You must be signed in to change notification settings - Fork 376
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
Actions: add rateLimitScope #1962
Merged
kevsecurity
merged 1 commit into
main
from
pr/kevsecurity/apply-rate-limit-to-all-threads
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1865,9 +1865,13 @@ do_action_signal(int signal) | |
*/ | ||
#define KEY_BYTES_PER_ARG 40 | ||
|
||
/* Rate limit scope. */ | ||
#define ACTION_RATE_LIMIT_SCOPE_THREAD 0 | ||
#define ACTION_RATE_LIMIT_SCOPE_PROCESS 1 | ||
#define ACTION_RATE_LIMIT_SCOPE_GLOBAL 2 | ||
|
||
struct ratelimit_key { | ||
__u64 func_id; | ||
__u64 retprobe_id; | ||
__u64 action; | ||
__u64 tid; | ||
__u8 data[MAX_POSSIBLE_ARGS * KEY_BYTES_PER_ARG]; | ||
|
@@ -1904,7 +1908,7 @@ struct { | |
|
||
#ifdef __LARGE_BPF_PROG | ||
static inline __attribute__((always_inline)) bool | ||
rate_limit(__u64 ratelimit_interval, struct msg_generic_kprobe *e) | ||
rate_limit(__u64 ratelimit_interval, __u64 ratelimit_scope, struct msg_generic_kprobe *e) | ||
{ | ||
__u64 curr_time = ktime_get_ns(); | ||
__u64 *last_repeat_entry; | ||
|
@@ -1926,9 +1930,20 @@ rate_limit(__u64 ratelimit_interval, struct msg_generic_kprobe *e) | |
ro_heap = map_lookup_elem(&ratelimit_ro_heap, &zero); | ||
|
||
key->func_id = e->func_id; | ||
key->retprobe_id = e->retprobe_id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this was never used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah was overkill. |
||
key->action = e->action; | ||
key->tid = e->tid; | ||
switch (ratelimit_scope) { | ||
case ACTION_RATE_LIMIT_SCOPE_THREAD: | ||
key->tid = e->tid; | ||
break; | ||
case ACTION_RATE_LIMIT_SCOPE_PROCESS: | ||
key->tid = e->current.pid; | ||
break; | ||
case ACTION_RATE_LIMIT_SCOPE_GLOBAL: | ||
key->tid = 0; | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
// Clean the heap | ||
probe_read(key->data, MAX_POSSIBLE_ARGS * KEY_BYTES_PER_ARG, ro_heap); | ||
|
@@ -2093,8 +2108,9 @@ do_action(void *ctx, __u32 i, struct msg_generic_kprobe *e, | |
break; | ||
case ACTION_POST: { | ||
__u64 ratelimit_interval __maybe_unused = actions->act[++i]; | ||
__u64 ratelimit_scope __maybe_unused = actions->act[++i]; | ||
#ifdef __LARGE_BPF_PROG | ||
if (rate_limit(ratelimit_interval, e)) | ||
if (rate_limit(ratelimit_interval, ratelimit_scope, e)) | ||
*post = false; | ||
#endif /* __LARGE_BPF_PROG */ | ||
__u32 stack_trace = actions->act[++i]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
would it make sense to memset zero those returned heap entries? (didn't check all the logic)
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.
I overwrite the heap with a read only heap that is all zero, using a probe_read. It should cost about the same as a memset but doesn't cost me the instruction / complexity count.