mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-03-22 07:27:12 +08:00
strerror() has thread safety issues, strerror_r() requires stack allocated buffers. Code in perf has already been using the "%m" formatting flag that is a widely support glibc extension to print the current errno's description. Expand the usage of this formatting flag and remove usage of strerror()/strerror_r(). Signed-off-by: Ian Rogers <irogers@google.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexandre Ghiti <alexghiti@rivosinc.com> Cc: Blake Jones <blakejones@google.com> Cc: Chun-Tse Shao <ctshao@google.com> Cc: Dmitriy Vyukov <dvyukov@google.com> Cc: Dr. David Alan Gilbert <linux@treblig.org> Cc: Haibo Xu <haibo1.xu@intel.com> Cc: Howard Chu <howardchu95@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Leo Yan <leo.yan@arm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephen Brennan <stephen.s.brennan@oracle.com> Cc: Thomas Falcon <thomas.falcon@intel.com> Cc: Yunseong Kim <ysk@kzalloc.com> Cc: Zhongqiu Han <quic_zhonhan@quicinc.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
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 (%m) fall back on root check\n");
|
|
*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;
|
|
}
|