Files
cubelinux-kernel/tools/perf/util/cap.c
T
Greg Kroah-Hartman 598cf27219 Linux 6.19.3
Link: https://lore.kernel.org/r/20260217200002.683975158@linuxfoundation.org
Tested-by: Florian Fainelli <florian.fainelli@broadcom.com>
Tested-by: Takeshi Ogasawara <takeshi.ogasawara@futuring-girl.com>
Tested-by: Peter Schneider <pschneider1968@googlemail.com>
Tested-by: Jon Hunter <jonathanh@nvidia.com>
Tested-by: Salvatore Bonaccorso <carnil@debian.org>
Tested-by: Brett A C Sheffield <bacs@librecast.net>
Tested-by: Mark Brown <broonie@kernel.org>
Tested-by: Luna Jernberg <droidbittin@gmail.com>
Tested-by: Ronald Warsow <rwarsow@gmx.de>
Tested-by: Justin M. Forbes <jforbes@fedoraproject.org>
Tested-by: Ron Economos <re@w6rz.net>
Tested-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2026-02-19 16:33:27 +01:00

50 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;
}