perf cap: Remove used_root parameter and simplify capability checks

Refactor perf_cap__capable() to completely remove the used_root out-parameter
as requested by the maintainer. Relying on an explicit used_root boolean
poisoned sequential capability checks (e.g. failing CAP_SYS_ADMIN checks
poisoning the flag for subsequent CAP_PERFMON evaluations for unprivileged
users) and created redundant complexity across check_ftrace_capable(),
symbol__read_kptr_restrict(), and perf_event_paranoid_check().

Streamline the capability API to perform a pure true/false boolean
evaluation. The function checks the Effective set using SYS_capget; if
the syscall is missing or fails on legacy kernels, it cleanly falls back
to checking EUID == 0. This perfectly preserves modern capability-aware host
sessions, guarantees transparent fallback for older kernels, and correctly
rejects privileged operations for containerized root processes that have
explicitly dropped their capability bounding and permitted sets.

Fixes: e25ebda78e ("perf cap: Tidy up and improve capability testing")
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Assisted-by: Antigravity:gemini-3.5-flash
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers
2026-07-22 22:06:40 -07:00
committed by Namhyung Kim
parent ec99be8a31
commit 87ec3437f3
6 changed files with 18 additions and 39 deletions

View File

@@ -72,18 +72,11 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
static bool check_ftrace_capable(void)
{
bool used_root;
if (perf_cap__capable(CAP_PERFMON, &used_root))
if (perf_cap__capable(CAP_PERFMON) ||
perf_cap__capable(CAP_SYS_ADMIN))
return true;
if (!used_root && perf_cap__capable(CAP_SYS_ADMIN, &used_root))
return true;
pr_err("ftrace only works for %s!\n",
used_root ? "root"
: "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
);
pr_err("ftrace only works for users with the CAP_PERFMON or CAP_SYS_ADMIN capability!\n");
return false;
}

View File

@@ -629,24 +629,20 @@ struct perf_bpf_filter_expr *perf_bpf_filter_expr__new(enum perf_bpf_filter_term
static bool check_bpf_filter_capable(void)
{
bool used_root;
int fd;
if (perf_cap__capable(CAP_BPF, &used_root))
if (perf_cap__capable(CAP_BPF))
return true;
if (!used_root) {
/* Check if root already pinned the filter programs and maps */
int fd = get_pinned_fd("filters");
if (fd >= 0) {
close(fd);
return true;
}
/* Check if root already pinned the filter programs and maps */
fd = get_pinned_fd("filters");
if (fd >= 0) {
close(fd);
return true;
}
pr_err("Error: BPF filter only works for %s!\n"
"\tPlease run 'perf record --setup-filter pin' as root first.\n",
used_root ? "root" : "users with the CAP_BPF capability");
pr_err("Error: BPF filter only works for users with the CAP_BPF capability!\n"
"\tPlease run 'perf record --setup-filter pin' as root first.\n");
return false;
}

View File

@@ -12,7 +12,7 @@
#define MAX_LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
bool perf_cap__capable(int cap, bool *used_root)
bool perf_cap__capable(int cap)
{
struct __user_cap_header_struct header = {
.version = _LINUX_CAPABILITY_VERSION_3,
@@ -21,7 +21,6 @@ bool perf_cap__capable(int cap, bool *used_root)
struct __user_cap_data_struct data[MAX_LINUX_CAPABILITY_U32S] = {};
__u32 cap_val;
*used_root = false;
while (syscall(SYS_capget, &header, &data[0]) == -1) {
/* Retry, first attempt has set the header.version correctly. */
if (errno == EINVAL && header.version != _LINUX_CAPABILITY_VERSION_3 &&
@@ -29,7 +28,6 @@ bool perf_cap__capable(int cap, bool *used_root)
continue;
pr_debug2("capget syscall failed (%m) fall back on root check\n");
*used_root = true;
return geteuid() == 0;
}

View File

@@ -18,7 +18,6 @@
#define CAP_BPF 39
#endif
/* Query if a capability is supported, used_root is set if the fallback root check was used. */
bool perf_cap__capable(int cap, bool *used_root);
bool perf_cap__capable(int cap);
#endif /* __PERF_CAP_H */

View File

@@ -2452,8 +2452,7 @@ static bool symbol__read_kptr_restrict(void)
{
bool value = false;
FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
bool used_root;
bool cap_syslog = perf_cap__capable(CAP_SYSLOG, &used_root);
bool cap_syslog = perf_cap__capable(CAP_SYSLOG);
if (fp != NULL) {
char line[8];

View File

@@ -378,15 +378,9 @@ int perf_event_paranoid(void)
bool perf_event_paranoid_check(int max_level)
{
bool used_root;
if (perf_cap__capable(CAP_SYS_ADMIN, &used_root))
return true;
if (!used_root && perf_cap__capable(CAP_PERFMON, &used_root))
return true;
return perf_event_paranoid() <= max_level;
return perf_cap__capable(CAP_SYS_ADMIN) ||
perf_cap__capable(CAP_PERFMON) ||
perf_event_paranoid() <= max_level;
}
int perf_tip(char **strp, const char *dirpath)