Skip to content

Stateful Policies: allow scoping the state to the thread or the process - #6015

Open
3u13r wants to merge 12 commits into
cilium:mainfrom
3u13r:pr/3u13r/add-scopes-to-policy-state
Open

3u13r wants to merge 12 commits into
cilium:mainfrom
3u13r:pr/3u13r/add-scopes-to-policy-state

Conversation

@3u13r

@3u13r 3u13r commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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

Stateful Policies: allow scoping the state to the thread or the process

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.

@netlify

netlify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

👷 Deploy request for tetragon pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 3f132cd

@3u13r
3u13r force-pushed the pr/3u13r/add-scopes-to-policy-state branch 2 times, most recently from b00bb3e to 7f73177 Compare November 4, 2026 20:55
@3u13r
3u13r marked this pull request as ready for review November 4, 2026 23:12
@3u13r
3u13r requested review from a team and kkourt as code owners November 4, 2026 23:12
@immanuwell

Copy link
Copy Markdown
Member

could we test NotEqual with missing scoped state?
I mean, filter_state_loop() rejects lookup misses, but #5776 agreed they should match.
this predates the PR, but LRU eviction adds another case

I reproduced it for unset, deleted, and evicted entries in BPF test.
errors building the key should still reject

@3u13r

3u13r commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

could we test NotEqual with missing scoped state?

Thank you very much for the report, I've opened a follow-up PR here.
If you find more things, I think it's best to open an issue and tag e.g. me. This allows for better tracking/discussion.

@3u13r
3u13r force-pushed the pr/3u13r/add-scopes-to-policy-state branch from 7f73177 to efcb068 Compare November 7, 2026 03:44

@kkourt kkourt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Overall, LGTM.
Please find some comments below.

Comment thread bpf/process/state.h
__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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are we using NO_PREALLOC here? In general, I think it's nice to have an option to set the map into NO_PREALLOC (or even have it by default). See for example: #5891.

I also wonder if this is a map that would make sense to move into task storage, see, for example, #5830. CC: @FedeDP

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

@3u13r 3u13r Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@kkourt kkourt Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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!

@3u13r 3u13r Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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).

Comment thread pkg/selectors/kernel_test.go Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

unrelated: do we feel that these tests are actually helpful?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread bpf/process/state.h Resolved
@kkourt kkourt added the release-note/minor This PR introduces a minor user-visible change label Nov 11, 2026
@3u13r
3u13r force-pushed the pr/3u13r/add-scopes-to-policy-state branch from efcb068 to f4bba26 Compare November 13, 2026 00:26
@3u13r
3u13r requested a review from a team as a code owner November 13, 2026 00:26

@kkourt kkourt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left a comment on the LRU map for your consideration. In any case, LGTM! Thanks!

3u13r added 11 commits November 20, 2026 18:11
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>
@3u13r
3u13r force-pushed the pr/3u13r/add-scopes-to-policy-state branch from f4bba26 to 3f132cd Compare November 20, 2026 17:21
@3u13r
3u13r requested a review from kkourt November 20, 2026 17:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/minor This PR introduces a minor user-visible change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants