fix: prevent capability loss from UID elevation for non-root users#495
Open
xroche wants to merge 1 commit intoDataDog:mainfrom
Open
fix: prevent capability loss from UID elevation for non-root users#495xroche wants to merge 1 commit intoDataDog:mainfrom
xroche wants to merge 1 commit intoDataDog:mainfrom
Conversation
bf0294e to
4a3f974
Compare
When ddprof runs as a non-root user with file capabilities (e.g.
CAP_IPC_LOCK, CAP_PERFMON), the UID round-trip in open_proc_maps()
and open_proc_comm() permanently destroys all capabilities:
1. fopen("/proc/<pid>/maps") fails (target is non-dumpable)
2. stat() shows file owned by root (st_uid=0)
3. setresuid(0, 0, -1) sets real+effective to 0, saved stays 1000
4. Retry fopen still fails (needs CAP_SYS_PTRACE, not just UID 0)
5. setresuid(1000, 1000, -1) restores UIDs — all nonzero
6. Kernel clears ALL capabilities (Linux cap clearing rule)
7. Worker permanently loses IPC_LOCK + PERFMON → profiling breaks
The UID elevation only works when running as actual root (saved UID
stays 0, so capabilities are preserved on restore). For non-root
users, it is both useless and destructive.
Guard the UID elevation with is_root() so non-root users skip the
elevation attempt entirely, preserving their file capabilities.
Also adds test/user_override_caps-ut.cc to verify capability
preservation across the fixed code path.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
4a3f974 to
842e217
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #494
When ddprof runs as a non-root user with file capabilities (e.g.
cap_ipc_lock,cap_perfmon,cap_sys_ptrace=+ep), profiling targets that have their own file capabilities (e.g. nginx withcap_net_bind_service) permanently destroys all ddprof worker capabilities.The UID round-trip in
open_proc_maps()andopen_proc_comm()(e.g.1000→0→1000) triggers the Linux kernel's capability clearing rule: when all UIDs become nonzero after at least one was zero, ALL capabilities are cleared from permitted+effective+ambient sets.The fix
Guard the UID elevation with
is_root()so non-root users skip it entirely. This is safe because:/proc/<pid>/mapswithoutCAP_SYS_PTRACEregardless of UID) and destructive (clears all caps)Changes
src/dso_hdr.cc—open_proc_maps(): guard UID elevation withis_root()src/ddprof_process.cc—open_proc_comm(): same guardtest/user_override_caps-ut.cc— new test verifying capability preservationtest/CMakeLists.txt— register the new testTest plan
cmake -B build && cd build && make -j ddprof)CapEff ≠ 0demonstrates_capability_loss_bugtest can be run manually withsudo setcap 'cap_setuid,cap_setgid,cap_ipc_lock=+ep' ./build/test/user_override_caps-utto confirm the kernel behavior