mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

The BPF filter needs libbpf/BPF-skeleton support and root privilege. Add error messages to help users understand the problem easily. When it's not build with BPF support (make BUILD_BPF_SKEL=0). $ sudo perf record -e cycles --filter "pid != 0" true Error: BPF filter is requested but perf is not built with BPF. Please make sure to build with libbpf and BPF skeleton. Usage: perf record [<options>] [<command>] or: perf record [<options>] -- <command> [<options>] --filter <filter> event filter When it supports BPF but runs without root or CAP_BPF. Note that it also checks pinned BPF filters. $ perf record -e cycles --filter "pid != 0" -o /dev/null true Error: BPF filter only works for users with the CAP_BPF capability! Please run 'perf record --setup-filter pin' as root first. Usage: perf record [<options>] [<command>] or: perf record [<options>] -- <command> [<options>] --filter <filter> event filter Reviewed-by: Ian Rogers <irogers@google.com> Link: https://lore.kernel.org/r/20250604174835.1852481-1-namhyung@kernel.org Signed-off-by: Namhyung Kim <namhyung@kernel.org>
49 lines
1.2 KiB
C
49 lines
1.2 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, bool *used_root)
|
|
{
|
|
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;
|
|
|
|
*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 &&
|
|
header.version == _LINUX_CAPABILITY_VERSION_1)
|
|
continue;
|
|
|
|
pr_debug2("capget syscall failed (%s - %d) fall back on root check\n",
|
|
strerror(errno), errno);
|
|
*used_root = true;
|
|
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;
|
|
}
|