mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-11 04:32:29 -04:00
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>
47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Capability utilities
|
|
*/
|
|
|
|
#include "cap.h"
|
|
#include "debug.h"
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <sys/syscall.h>
|
|
#include <unistd.h>
|
|
|
|
#define MAX_LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
|
|
|
|
bool perf_cap__capable(int cap)
|
|
{
|
|
struct __user_cap_header_struct header = {
|
|
.version = _LINUX_CAPABILITY_VERSION_3,
|
|
.pid = 0,
|
|
};
|
|
struct __user_cap_data_struct data[MAX_LINUX_CAPABILITY_U32S] = {};
|
|
__u32 cap_val;
|
|
|
|
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 &&
|
|
header.version == _LINUX_CAPABILITY_VERSION_1)
|
|
continue;
|
|
|
|
pr_debug2("capget syscall failed (%m) fall back on root check\n");
|
|
return geteuid() == 0;
|
|
}
|
|
|
|
/* Extract the relevant capability bit. */
|
|
if (cap >= 32) {
|
|
if (header.version == _LINUX_CAPABILITY_VERSION_3) {
|
|
cap_val = data[1].effective;
|
|
} else {
|
|
/* Capability beyond 32 is requested but only 32 are supported. */
|
|
return false;
|
|
}
|
|
} else {
|
|
cap_val = data[0].effective;
|
|
}
|
|
return (cap_val & (1 << (cap & 0x1f))) != 0;
|
|
}
|