Skip to content

mbset: fix followChildren ID leak (duplicate paths) - #5907

Open
abdullahhanif-001 wants to merge 4 commits into
cilium:mainfrom
abdullahhanif-001:fix/mbset-removeid-dup-path
Open

abdullahhanif-001 wants to merge 4 commits into
cilium:mainfrom
abdullahhanif-001:fix/mbset-removeid-dup-path

Conversation

@abdullahhanif-001

Copy link
Copy Markdown

Summary

Fixes #5744.

When matchBinaries contains duplicate paths (or small programs with a nil execve updater), RemoveID could fail early on ebpf.ErrKeyNotExist / skip ID release and leak one of the 64 followChildren mbset IDs.

Changes

  • pkg/mbset/mbset.go: treat ErrKeyNotExist as already-removed (continue); always delete(s.ids, id) when updater is nil
  • pkg/selectors/selectors.go: dedupe paths in WriteMatchBinariesPath
  • pkg/selectors/matchbinaries_paths_test.go: unit test for path dedupe (//go:build !windows)

Test plan

  • GOOS=linux CGO_ENABLED=0 go test -c ./pkg/selectors/ compiles with new test
  • GOOS=linux CGO_ENABLED=0 go test -c ./pkg/mbset/ compiles
  • CI: go test ./pkg/selectors/ -run TestWriteMatchBinariesPathDedupes

@FedeDP FedeDP 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.

So, i'll split the PR over 3 commits, one for each fix:

  • delete(s.ids, id) when update is nil
  • errors.Is(err, ebpf.ErrKeyNotExist)
  • pkg/selectors/selectors.go changes

This would make the PR much easier to review :)
Also it would be great if you could add a test for the issue you are trying to solve.
Thanks!

Comment thread pkg/mbset/mbset.go Resolved
@abdullahhanif-001

Copy link
Copy Markdown
Author

@FedeDP Thanks — that split makes sense.

I'll rework into three commits:

  1. delete(s.ids, id) when update == nil
  2. treat ebpf.ErrKeyNotExist as already-removed in the RemoveID path loop
  3. dedupe in WriteMatchBinariesPath

I'll also take your comment wording on the nil-updater path, and add a test that covers the duplicate-path / RemoveID leak (not only the write-side dedupe).

@abdullahhanif-001

Copy link
Copy Markdown
Author

@FedeDP Done — force-pushed as three commits:

  1. mbset: release ID when execve updater is nil (+ TestRemoveIDReleasesSlotWhenUpdaterNil)
  2. mbset: ignore ErrKeyNotExist when removing duplicate paths
  3. selectors: dedupe matchBinaries paths at write time (+ write-side dedupe unit test)

Also applied your comment wording on the nil-updater path. Happy to add an integration case on top of TestMatchBinariesFollowChildrenIDs if you want that exercised under kprobe_multi as well.

1 similar comment
@abdullahhanif-001

Copy link
Copy Markdown
Author

@FedeDP Done — force-pushed as three commits:

  1. mbset: release ID when execve updater is nil (+ TestRemoveIDReleasesSlotWhenUpdaterNil)
  2. mbset: ignore ErrKeyNotExist when removing duplicate paths
  3. selectors: dedupe matchBinaries paths at write time (+ write-side dedupe unit test)

Also applied your comment wording on the nil-updater path. Happy to add an integration case on top of TestMatchBinariesFollowChildrenIDs if you want that exercised under kprobe_multi as well.

@abdullahhanif-001
abdullahhanif-001 force-pushed the fix/mbset-removeid-dup-path branch from 90d470b to 4f675c3 Compare October 24, 2026 04:15
@netlify

netlify Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

👷 Deploy request for tetragon pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit bb5fe56

@FedeDP

FedeDP commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Thanks! Can you be a bit more descriptive in the 2nd and 3rd commit messages? Eg: which problem are you trying to solve and why!
Thank you very much :)

@abdullahhanif-001
abdullahhanif-001 force-pushed the fix/mbset-removeid-dup-path branch from 4f675c3 to ac6c935 Compare October 28, 2026 05:07
@abdullahhanif-001

Copy link
Copy Markdown
Author

@FedeDP Done — rewrote the 2nd and 3rd commit messages with the failure mode and why. Force-pushed.

Comment thread pkg/selectors/selectors.go Outdated
func (k *KernelSelectorState) WriteMatchBinariesPath(selectorID int, path string) {
var bytePath [processapi.BINARY_PATH_MAX_LEN]byte
copy(bytePath[:], path)
for _, existing := range k.matchBinariesPaths[selectorID] {

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.

Am i correct saying that, considering third commit, second one is defensive programming (ie: it should never happen)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah — with the write-side dedupe, the normal WriteMatchBinariesPath → RemoveID path shouldn't hit the same key twice anymore.

I'd still keep the ErrKeyNotExist handling though. RemoveID takes whatever path list the caller hands it; if something else builds a duplicate list (or an older policy still has one), failing the second Lookup used to abort the whole RemoveID and leave the ID allocated. Cheap to tolerate, and the ID leak is worse than a silent continue.

@FedeDP FedeDP 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.

Changes LGTM. Let's wait for another reviewer!

@FedeDP

FedeDP commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Can you fix the failing checks (checkpatch and golangci-lint) ? Thanks!

@abdullahhanif-001
abdullahhanif-001 force-pushed the fix/mbset-removeid-dup-path branch from ac6c935 to 62bbea5 Compare October 28, 2026 09:11
@abdullahhanif-001

Copy link
Copy Markdown
Author

@FedeDP Fixed the failing checks:

  • checkpatch: added Signed-off-by on all three commits
  • golangci-lint: switched the dedupe loop to slices.Contains
  • validate labels: added release-note/bug

Force-pushed. go test ./pkg/mbset still green on my side.

@abdullahhanif-001

Copy link
Copy Markdown
Author

@FedeDP One note: I don't have permission to set labels on the PR. Could you (or another maintainer) add release-note/bug? The Signed-off-by + slices.Contains fixes are already force-pushed.

@FedeDP FedeDP added kind/bug Something isn't working release-note/bug This PR fixes an issue in a previous release of Tetragon. labels Oct 28, 2026
Comment thread pkg/selectors/selectors.go Outdated
Comment on lines +238 to +240
if slices.Contains(k.matchBinariesPaths[selectorID], bytePath) {
return
}

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.

This is quadratic in the number of matchBinaries values. De-duping once with a map[string]struct{} in the ParseMatchBinary In/NoIn loop before calling WriteMatchBinariesPath would avoid this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch, moved the dedupe into ParseMatchBinary (map-based), WriteMatchBinariesPath is a plain append again. Test updated to TestParseMatchBinaryDedupesPaths.

Comment thread pkg/mbset/mbset.go Outdated
@@ -73,6 +73,8 @@ func (s *state) RemoveID(id uint32, paths [][processapi.BINARY_PATH_MAX_LEN]byte
defer s.mu.Unlock()

if update == nil {
// No execve_map updater (only set when large bpf progs are enabled): still release the ID slot.
delete(s.ids, id)

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.

Releasing the ID here leaves its bit set in tg_mbset_map and in the execve_map entries of running processes. The next followChildren selector to get this ID then matches the removed policy's binaries and their children, so a Sigkill policy, for example, can fire on completely unrelated binaries.

One way I think you could address this:

Drop this early return so step 1 always cleans tg_mbset_map, and have updatePids return an error when update == nil and some process still carries the bit. The ID is then released whenever the matched processes have exited, and kept allocated otherwise. TestRemoveIDReleasesSlotWhenUpdaterNil would need the pinned maps and become a privileged test.

Then have AllocID rescan execve_map for an ID held back this way once it runs out of free ones. This reclaims IDs as processes exit instead of leaving them allocated until restart.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right, thanks. Went with your suggestion: tg_mbset_map is always cleaned, and if any pid still carries the bit the ID is kept as stale and reclaimed later in AllocID. Also added an iter.Err() check, plus a separate commit fixing an ID leak when selector parsing fails (happy to split it out).

When the same binary path shows up twice in one matchBinaries selector,
RemoveID deletes the mbset entry on the first copy. The second Lookup
then returns ErrKeyNotExist. That used to abort the whole RemoveID and
leave the ID allocated, which burns a slot under the 64-bit
followChildren cap.

Treat ErrKeyNotExist as already-removed and keep walking the path list
so the ID still gets released.

Signed-off-by: Abdullah Hanif <318923962+abdullahhanif-001@users.noreply.github.com>
RemoveID used to bail out early when there's no execve_map updater
(small programs), and the previous version of this series turned that
into "just release the ID". That is wrong: the bit stays set in
tg_mbset_map and in the execve_map entries of running processes, so
the next followChildren selector that gets the ID matches the removed
policy's binaries and their children. With a Sigkill action that kills
unrelated stuff.

Drop the early return so tg_mbset_map is always cleaned up. Without an
updater we can't clear the bit from execve_map, so just check that no
process has it and fail otherwise. Same goes for the updater path when
the retries run out, and also if iterating execve_map fails (that used
to look like "no pids found" and released the ID).

When RemoveID fails after tg_mbset_map is clean, keep the ID allocated
and remember it as stale. Once AllocID runs out of free IDs it scans
execve_map once and releases the stale IDs nobody carries anymore. A
bit can only be inherited from a parent at that point, so if no process
has it, no new process will get it either.

Map opening goes through state.openMap so the tests can use private
maps. The tests create BPF maps, so they need to run privileged like
the rest of `make test`.

Signed-off-by: Abdullah Hanif <318923962+abdullahhanif-001@users.noreply.github.com>
When the same binary path shows up more than once in a matchBinaries
selector it ends up multiple times in matchBinariesPaths, and RemoveID
then hits the same mbset key twice (see previous commit).

Skip duplicates while walking the In/NotIn values. Doing it there with
a map keeps it linear, instead of searching the already written paths
for every new one in WriteMatchBinariesPath.

Signed-off-by: Abdullah Hanif <318923962+abdullahhanif-001@users.noreply.github.com>
ParseMatchBinary allocates the mbset ID before it looks at the values,
and the selector is only stored in the state at the very end. So if a
path is too long the ID is gone for good, CleanupKernelSelectorState
never sees it. Same thing one level up: createKernelSelectorState drops
the whole state when a later selector fails, together with the IDs the
earlier ones already took.

With only 64 IDs, a few broken policies are enough to make every
followChildren policy fail to load until restart.

Add mbset.ReleaseID for IDs that never made it to the mbset map and use
it in both error paths.

Signed-off-by: Abdullah Hanif <318923962+abdullahhanif-001@users.noreply.github.com>
@abdullahhanif-001
abdullahhanif-001 force-pushed the fix/mbset-removeid-dup-path branch from 62bbea5 to bb5fe56 Compare November 20, 2026 16:17

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

kind/bug Something isn't working release-note/bug This PR fixes an issue in a previous release of Tetragon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mbset: RemoveID returns early and leaks the followChildren ID (duplicate value, small programs)

3 participants