Repository navigation
mbset: fix followChildren ID leak (duplicate paths) - #5907
abdullahhanif-001 wants to merge 4 commits into
Conversation
FedeDP
left a comment
There was a problem hiding this comment.
So, i'll split the PR over 3 commits, one for each fix:
delete(s.ids, id)when update is nilerrors.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!
|
@FedeDP Thanks — that split makes sense. I'll rework into three commits:
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). |
|
@FedeDP Done — force-pushed as three commits:
Also applied your comment wording on the nil-updater path. Happy to add an integration case on top of |
1 similar comment
|
@FedeDP Done — force-pushed as three commits:
Also applied your comment wording on the nil-updater path. Happy to add an integration case on top of |
90d470b to
4f675c3
Compare
👷 Deploy request for tetragon pending review.Visit the deploys page to approve it
|
|
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! |
4f675c3 to
ac6c935
Compare
|
@FedeDP Done — rewrote the 2nd and 3rd commit messages with the failure mode and why. Force-pushed. |
| 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] { |
There was a problem hiding this comment.
Am i correct saying that, considering third commit, second one is defensive programming (ie: it should never happen)?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Changes LGTM. Let's wait for another reviewer!
|
Can you fix the failing checks (checkpatch and golangci-lint) ? Thanks! |
ac6c935 to
62bbea5
Compare
|
@FedeDP Fixed the failing checks:
Force-pushed. |
|
@FedeDP One note: I don't have permission to set labels on the PR. Could you (or another maintainer) add |
| if slices.Contains(k.matchBinariesPaths[selectorID], bytePath) { | ||
| return | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good catch, moved the dedupe into ParseMatchBinary (map-based), WriteMatchBinariesPath is a plain append again. Test updated to TestParseMatchBinaryDedupesPaths.
| @@ -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) | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
62bbea5 to
bb5fe56
Compare
Summary
Fixes #5744.
When
matchBinariescontains duplicate paths (or small programs with a nil execve updater),RemoveIDcould fail early onebpf.ErrKeyNotExist/ skip ID release and leak one of the 64 followChildren mbset IDs.Changes
pkg/mbset/mbset.go: treatErrKeyNotExistas already-removed (continue); alwaysdelete(s.ids, id)when updater is nilpkg/selectors/selectors.go: dedupe paths inWriteMatchBinariesPathpkg/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 testGOOS=linux CGO_ENABLED=0 go test -c ./pkg/mbset/compilesgo test ./pkg/selectors/ -run TestWriteMatchBinariesPathDedupes