Repository navigation
Conversation
👷 Deploy request for tetragon pending review.Visit the deploys page to approve it
|
b00bb3e to
7f73177
Compare
|
could we test I reproduced it for unset, deleted, and evicted entries in BPF test. |
Thank you very much for the report, I've opened a follow-up PR here. |
7f73177 to
efcb068
Compare
kkourt
left a comment
There was a problem hiding this comment.
Thanks! Overall, LGTM.
Please find some comments below.
| __uint(max_entries, 1); // will be resized by agent when needed | ||
| __type(key, struct state_key); | ||
| __type(value, struct state_value); | ||
| } tg_scoped_state_map SEC(".maps"); |
There was a problem hiding this comment.
There was a problem hiding this comment.
Do we cleanup entries keys for processes that exit or do we rely on LRU? I'm guessing the former but it would be nice to clarify in a commit message (maybe I missed it).
There was a problem hiding this comment.
NO_PREALLOC doesn't work for normal LRU's. Though we kind of have NO_PREALLOC, since as long as the feature is not used, the map's size is 1.
Do we cleanup entries keys for processes that exit or do we rely on LRU? I
No there's no explicit cleanup. Today's idea is that either the the user deletes the key again or it is evicted at some point.
I also wonder if this is a map that would make sense to move into task storage, see, for example, #5830. CC: Fede
TIL. Thanks for the pointer! This solves the lifetime and size question. There's even an option for cgroups later on. Let me re-write the feature to use this.
After thinking more and trying this out, I noticed that task storage doesn't support atomic updates of values since it doesn't have a update map helper.
This is a problem when we extend the value into being a byte array to hold strings and we don't update the task we are currently hooking but the parent task (i.e. the process). then we can have multiple writers for the same value.
So I think we have to go with the current LRU approach for now.
There was a problem hiding this comment.
Is there a way to detect LRU evictions? Otherwise, policies can silently misbehave and there is no way for the user to realize it.
With a (normal) hashmap, we can at least figure out when we have ran out of space and notify the user (or even unload the policy), but the replacement policy is not obvious.
There are some things we could do (e.g., have the ktime in the value and delete stale PIDs, use a resizable hash map) but none seems ideal.
I kinda favor the fail-fast approach where we use a hashmap and if it overflows, we notify user-space to unload the policy but I can see benefits to both approaches.
In any case, if we end up with an LRU map we should document the implications this has (maybe we already do). It's already there! thanks!
There was a problem hiding this comment.
The AI suggested to to (manual) auto-cleanup by hooking on sched_process_free , similar to bpf_cgroup_rmdir.c. This is essentially the manual hooks that we (newly) suggest to the user but automated. I worry about the cost of hooking into a kernel function that executes on every thread exit, but we can implement it and see.
In the latest push (this diff) I've added a policy test commit and change the map to a hard-failing hashmap with the suggestion on where to hook to clean this up (at least for kprobes).
| 0x0f, 0x00, 0x00, 0x00, // Action = "UpdateState" | ||
| 0x00, 0x00, 0x00, 0x00, // State operator = "Set" | ||
| 0x03, 0x00, 0x00, 0x00, // State ID = 3 | ||
| 0x00, 0x00, 0x00, 0x00, // Scope = System |
There was a problem hiding this comment.
unrelated: do we feel that these tests are actually helpful?
There was a problem hiding this comment.
I think that they definitely are more of a change detector test. There's some value in seeing/needing to fix the tests to help understand how the bytes are laid out. On the other hand, I mainly use LLMs to fixup the tests because they require a lot of offset changes when you add a new selector.
Interested what others feel like.
efcb068 to
f4bba26
Compare
kkourt
left a comment
There was a problem hiding this comment.
Left a comment on the LRU map for your consideration. In any case, LGTM! Thanks!
Make some space for a more complex state handling by moving current implementation into state.h. There are no functional changes in this commit. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Add a Scope field in the State declaration of a TracingPolicy.
This will be implemented in the following commits.
Use-case:
This feature allows the user to taint not only the whole system but also
single processes and threads. This supports the need for policies like:
all processes that first match probe A are now subject to probe B.
Note that we protect against PID/TID reuse here by also taking the
start_time into account.
```
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "canary-block-connect"
spec:
podSelector:
matchLabels:
app: "canary-demo"
state:
- name: "canary"
scope: System/Process/Thread <-- new
```
Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Rename policyStateMap to policyStateMaps and return a slice of maps. In future commits the slice will contain an additional map. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Refactor the state identification into StateInfo. This only contains the previously existing ID. In the next commits, we'll include the scope of the state. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
This change does NOT implement the feature yet. We only add the needed map, define the constants in both eBPF and Go, and parse the new scope field so that it can be used in the next commit by the kernel.go parser to pass the new scope field. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
This commit pushes the scope setting into the selector buffer, where the eBPF code parses it and implements the scoped state. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Add a change detector test for the encoded scoped state selector and action. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Add documentation about the new `scope` variable. Also document that Process and Thread scoped states are not deleted when their process or thread exits. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
The validation can be applied to other map size options as well. So rename it to reflect that. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Previously, when a policy used scoped states, the map always had 32768 entries. Add a flag to configure the scoped state map size. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
In addition to the agent-global flag, add a per-policy option to set the size of the scoped state map. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
Add policytests for Process and Thread scoped states. To run commands on different threads, test-helper now runs all commands on the same thread and runs commands with a "thread " prefix on another thread. Signed-off-by: Leonard Cohnen <leonard.cohnen@gmail.com>
f4bba26 to
3f132cd
Compare
Description
Some states only make sense if they are scoped to a specific Thread or Process. This allow you to implement: All Processes that first match probe A are now subject to probe B.
AIL: 3
Changelog
Review notes:
I've verified that the current commit slicing is bisectable to comply without contributing rules.
Let me know if I should split the first commit into it's own PR. Put a refactor without the motivation of the larger change seems a bit out of place.
The main architecture question is how we want to handle the amount of states overflowing our map. This PR uses an LRU map for the Process/Thread states only. This guarantees persistence for the system-wide states while handling state leakage (i.e. states that are not deleted by the policy) in a graceful way.
We could decide to isolate noisy Processes/Threads from each other even more by doing some map in map magic.