Skip to contentSkip to conversation

perf cap: Tidy up and improve capability testing

PATCH In review Ian Rogers 2 participants 6 messages Aug 6, 2024 – Oct 26, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH v3 1/1 perf cap: Tidy up and improve capability testing

Remove dependence on libcap. libcap is only used to query whether a capability is supported, which is just 1 capget system call.

If the capget system call fails, fall back on root permission checking. Previously if libcap fails then the permission is assumed not present which may be pessimistic/wrong.

Add a used_root out argument to perf_cap__capable to say whether the fall back root check was used. This allows the correct error message, "root" vs "users with the CAP_PERFMON or CAP_SYS_ADMIN capability", to be selected.

Tidy uses of perf_cap__capable so that tests aren't repeated if capget isn't supported.

---
v3: rebase resolving builtin-ftrace.c conflicts and incorporating
change in:
https://lore.kernel.org/lkml/[email protected]/
v2: fix syscall number and '>' should have been '>='.

---
 tools/perf/Makefile.config  | 11 -------
 tools/perf/builtin-ftrace.c | 28 ++++++++---------
 tools/perf/util/Build       |  2 +-
 tools/perf/util/cap.c       | 63 ++++++++++++++++++++++++++-----------
 tools/perf/util/cap.h       | 23 ++------------
 tools/perf/util/symbol.c    |  8 ++---
 tools/perf/util/util.c      | 12 +++++--
 7 files changed, 75 insertions(+), 72 deletions(-)
tools/perf/Makefile.config +0 -11
index fa679db61f62..4eb1fc897baf 100644
@@ -1031,17 +1031,6 @@ ifndef NO_LIBZSTD
   endif
 endif
 
-ifndef NO_LIBCAP
-  ifeq ($(feature-libcap), 1)
-    CFLAGS += -DHAVE_LIBCAP_SUPPORT
-    EXTLIBS += -lcap
-    $(call detected,CONFIG_LIBCAP)
-  else
-    $(warning No libcap found, disables capability support, please install libcap-devel/libcap-dev)
-    NO_LIBCAP := 1
-  endif
-endif
-
 ifndef NO_BACKTRACE
   ifeq ($(feature-backtrace), 1)
     CFLAGS += -DHAVE_BACKTRACE_SUPPORT
tools/perf/builtin-ftrace.c +14 -14
index a615c405d98f..88a87bf387d2 100644
@@ -63,20 +63,21 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
 	done = true;
 }
 
-static int check_ftrace_capable(void)
+static bool check_ftrace_capable(void)
 {
-	if (!(perf_cap__capable(CAP_PERFMON) ||
-	      perf_cap__capable(CAP_SYS_ADMIN))) {
-		pr_err("ftrace only works for %s!\n",
-#ifdef HAVE_LIBCAP_SUPPORT
-		"users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
-#else
-		"root"
-#endif
+	bool used_root;
+
+	if (perf_cap__capable(CAP_PERFMON, &used_root))
+		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"
 		);
-		return -1;
-	}
-	return 0;
+	return false;
 }
 
 static int __write_tracing_file(const char *name, const char *val, bool append)
@@ -1579,8 +1580,7 @@ int cmd_ftrace(int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	ret = check_ftrace_capable();
-	if (ret < 0)
+	if (!check_ftrace_capable())
 		return -1;
 
 	ret = perf_config(perf_ftrace_config, &ftrace);
tools/perf/util/Build +1 -1
index b24360c04aae..adbaf7748019 100644
@@ -221,7 +221,7 @@ perf-util-$(CONFIG_ZLIB) += zlib.o
 perf-util-$(CONFIG_LZMA) += lzma.o
 perf-util-$(CONFIG_ZSTD) += zstd.o
 
-perf-util-$(CONFIG_LIBCAP) += cap.o
+perf-util-y += cap.o
 
 perf-util-$(CONFIG_CXX_DEMANGLE) += demangle-cxx.o
 perf-util-y += demangle-ocaml.o
tools/perf/util/cap.c +44 -19

71 lines not on this page · raw message

tools/perf/util/cap.h +3 -20
index ae52878c0b2e..0c6a1ff55f07 100644
@@ -3,26 +3,6 @@
 #define __PERF_CAP_H
 
 #include <stdbool.h>
-#include <linux/capability.h>
-#include <linux/compiler.h>
-
-#ifdef HAVE_LIBCAP_SUPPORT
-
-#include <sys/capability.h>
-
-bool perf_cap__capable(cap_value_t cap);
-
-#else
-
-#include <unistd.h>
-#include <sys/types.h>
-
-static inline bool perf_cap__capable(int cap __maybe_unused)
-{
-	return geteuid() == 0;
-}
-
-#endif /* HAVE_LIBCAP_SUPPORT */
 
 /* For older systems */
 #ifndef CAP_SYSLOG
@@ -33,4 +13,7 @@ static inline bool perf_cap__capable(int cap __maybe_unused)
 #define CAP_PERFMON	38
 #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);
+
 #endif /* __PERF_CAP_H */
tools/perf/util/symbol.c +4 -4
index 19eb623e0826..a18927d792af 100644
@@ -2425,14 +2425,14 @@ 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);
 
 	if (fp != NULL) {
 		char line[8];
 
 		if (fgets(line, sizeof(line), fp) != NULL)
-			value = perf_cap__capable(CAP_SYSLOG) ?
-					(atoi(line) >= 2) :
-					(atoi(line) != 0);
+			value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0);
 
 		fclose(fp);
 	}
@@ -2440,7 +2440,7 @@ static bool symbol__read_kptr_restrict(void)
 	/* Per kernel/kallsyms.c:
 	 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
 	 */
-	if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG))
+	if (perf_event_paranoid() > 1 && !cap_syslog)
 		value = true;
 
 	return value;
tools/perf/util/util.c +9 -3
index 4f561e5e4162..9d55a13787ce 100644
@@ -325,9 +325,15 @@ int perf_event_paranoid(void)
 
 bool perf_event_paranoid_check(int max_level)
 {
-	return perf_cap__capable(CAP_SYS_ADMIN) ||
-			perf_cap__capable(CAP_PERFMON) ||
-			perf_event_paranoid() <= 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;
 }
 
 static int
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
author reviewer
#1 lore raw
Quoted 13 lines · Ian Rogers

Remove dependence on libcap. libcap is only used to query whether a capability is supported, which is just 1 capget system call.

If the capget system call fails, fall back on root permission checking. Previously if libcap fails then the permission is assumed not present which may be pessimistic/wrong.

Add a used_root out argument to perf_cap__capable to say whether the fall back root check was used. This allows the correct error message, "root" vs "users with the CAP_PERFMON or CAP_SYS_ADMIN capability", to be selected.

Quoted 4 lines

Tidy uses of perf_cap__capable so that tests aren't repeated if capget isn't supported.

Ping.

Thanks,
Ian

Quoted 270 lines

---
v3: rebase resolving builtin-ftrace.c conflicts and incorporating
change in:
https://lore.kernel.org/lkml/[email protected]/
v2: fix syscall number and '>' should have been '>='.

---
 tools/perf/Makefile.config  | 11 -------
 tools/perf/builtin-ftrace.c | 28 ++++++++---------
 tools/perf/util/Build       |  2 +-
 tools/perf/util/cap.c       | 63 ++++++++++++++++++++++++++-----------
 tools/perf/util/cap.h       | 23 ++------------
 tools/perf/util/symbol.c    |  8 ++---
 tools/perf/util/util.c      | 12 +++++--
 7 files changed, 75 insertions(+), 72 deletions(-)
tools/perf/Makefile.config +0 -11
index fa679db61f62..4eb1fc897baf 100644
@@ -1031,17 +1031,6 @@ ifndef NO_LIBZSTD
   endif
 endif
 
-ifndef NO_LIBCAP
-  ifeq ($(feature-libcap), 1)
-    CFLAGS += -DHAVE_LIBCAP_SUPPORT
-    EXTLIBS += -lcap
-    $(call detected,CONFIG_LIBCAP)
-  else
-    $(warning No libcap found, disables capability support, please install libcap-devel/libcap-dev)
-    NO_LIBCAP := 1
-  endif
-endif
-
 ifndef NO_BACKTRACE
   ifeq ($(feature-backtrace), 1)
     CFLAGS += -DHAVE_BACKTRACE_SUPPORT
tools/perf/builtin-ftrace.c +14 -14
index a615c405d98f..88a87bf387d2 100644
@@ -63,20 +63,21 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
        done = true;
 }
 
-static int check_ftrace_capable(void)
+static bool check_ftrace_capable(void)
 {
-       if (!(perf_cap__capable(CAP_PERFMON) ||
-             perf_cap__capable(CAP_SYS_ADMIN))) {
-               pr_err("ftrace only works for %s!\n",
-#ifdef HAVE_LIBCAP_SUPPORT
-               "users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
-#else
-               "root"
-#endif
+       bool used_root;
+
+       if (perf_cap__capable(CAP_PERFMON, &used_root))
+               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"
                );
-               return -1;
-       }
-       return 0;
+       return false;
 }
 
 static int __write_tracing_file(const char *name, const char *val, bool append)
@@ -1579,8 +1580,7 @@ int cmd_ftrace(int argc, const char **argv)
        signal(SIGCHLD, sig_handler);
        signal(SIGPIPE, sig_handler);
 
-       ret = check_ftrace_capable();
-       if (ret < 0)
+       if (!check_ftrace_capable())
                return -1;
 
        ret = perf_config(perf_ftrace_config, &ftrace);
tools/perf/util/Build +1 -1
index b24360c04aae..adbaf7748019 100644
@@ -221,7 +221,7 @@ perf-util-$(CONFIG_ZLIB) += zlib.o
 perf-util-$(CONFIG_LZMA) += lzma.o
 perf-util-$(CONFIG_ZSTD) += zstd.o
 
-perf-util-$(CONFIG_LIBCAP) += cap.o
+perf-util-y += cap.o
 
 perf-util-$(CONFIG_CXX_DEMANGLE) += demangle-cxx.o
 perf-util-y += demangle-ocaml.o
tools/perf/util/cap.c +44 -19
index c3ba841bbf37..7574a67651bc 100644
@@ -3,27 +3,52 @@
  * Capability utilities
  */
 
-#ifdef HAVE_LIBCAP_SUPPORT
-
 #include "cap.h"
-#include <stdbool.h>
-#include <sys/capability.h>
-
-bool perf_cap__capable(cap_value_t cap)
-{
-       cap_flag_value_t val;
-       cap_t caps = cap_get_proc();
+#include "debug.h"
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/capability.h>
+#include <sys/syscall.h>
 
-       if (!caps)
-               return false;
+#ifndef SYS_capget
+#define SYS_capget 90
+#endif
 
-       if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &val) != 0)
-               val = CAP_CLEAR;
+#define MAX_LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
 
-       if (cap_free(caps) != 0)
-               return false;
-
-       return val == CAP_SET;
+bool perf_cap__capable(int cap, bool *used_root)
+{
+       struct __user_cap_header_struct header = {
+               .version = _LINUX_CAPABILITY_VERSION_3,
+               .pid = getpid(),
+       };
+       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;
 }
-
-#endif  /* HAVE_LIBCAP_SUPPORT */
tools/perf/util/cap.h +3 -20
index ae52878c0b2e..0c6a1ff55f07 100644
@@ -3,26 +3,6 @@
 #define __PERF_CAP_H
 
 #include <stdbool.h>
-#include <linux/capability.h>
-#include <linux/compiler.h>
-
-#ifdef HAVE_LIBCAP_SUPPORT
-
-#include <sys/capability.h>
-
-bool perf_cap__capable(cap_value_t cap);
-
-#else
-
-#include <unistd.h>
-#include <sys/types.h>
-
-static inline bool perf_cap__capable(int cap __maybe_unused)
-{
-       return geteuid() == 0;
-}
-
-#endif /* HAVE_LIBCAP_SUPPORT */
 
 /* For older systems */
 #ifndef CAP_SYSLOG
@@ -33,4 +13,7 @@ static inline bool perf_cap__capable(int cap __maybe_unused)
 #define CAP_PERFMON    38
 #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);
+
 #endif /* __PERF_CAP_H */
tools/perf/util/symbol.c +4 -4
index 19eb623e0826..a18927d792af 100644
@@ -2425,14 +2425,14 @@ 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);
 
        if (fp != NULL) {
                char line[8];
 
                if (fgets(line, sizeof(line), fp) != NULL)
-                       value = perf_cap__capable(CAP_SYSLOG) ?
-                                       (atoi(line) >= 2) :
-                                       (atoi(line) != 0);
+                       value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0);
 
                fclose(fp);
        }
@@ -2440,7 +2440,7 @@ static bool symbol__read_kptr_restrict(void)
        /* Per kernel/kallsyms.c:
         * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
         */
-       if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG))
+       if (perf_event_paranoid() > 1 && !cap_syslog)
                value = true;
 
        return value;
tools/perf/util/util.c +9 -4
index 4f561e5e4162..9d55a13787ce 100644
@@ -325,9 +325,15 @@ int perf_event_paranoid(void)
 
 bool perf_event_paranoid_check(int max_level)
 {
-       return perf_cap__capable(CAP_SYS_ADMIN) ||
-                       perf_cap__capable(CAP_PERFMON) ||
-                       perf_event_paranoid() <= 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;
 }
 
 static int
--

2.46.0.rc2.264.g509ed76dc8-goog

reviewer
#2 lore raw
In reply to Ian Rogers
Quoted 12 lines · Ian Rogers

Remove dependence on libcap. libcap is only used to query whether a capability is supported, which is just 1 capget system call.

If the capget system call fails, fall back on root permission checking. Previously if libcap fails then the permission is assumed not present which may be pessimistic/wrong.

Add a used_root out argument to perf_cap__capable to say whether the fall back root check was used. This allows the correct error message, "root" vs "users with the CAP_PERFMON or CAP_SYS_ADMIN capability", to be selected.

Quoted 4 lines

Tidy uses of perf_cap__capable so that tests aren't repeated if capget isn't supported.

I find I need to revert this. E.g.

$ install-perf.sh
$ perf record -e intel_pt//k uname
WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
check /proc/sys/kernel/kptr_restrict and
/proc/sys/kernel/perf_event_paranoid.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Couldn't record kernel reference relocation symbol
Symbol resolution may be skewed if relocation was used (e.g. kexec). Check /proc/kallsyms permission or run as root.
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.061 MB perf.data ]

$ git revert e25ebda78e230283bf707ae3e9655270ff40a7f9
Auto-merging tools/perf/Makefile.config
Auto-merging tools/perf/builtin-ftrace.c
Auto-merging tools/perf/util/Build
Auto-merging tools/perf/util/symbol.c
[master 752707588a700] Revert "perf cap: Tidy up and improve capability
testing"
 7 files changed, 70 insertions(+), 73 deletions(-)
$ install-perf.sh
$ perf record -e intel_pt//k uname
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.065 MB perf.data ]
Quoted 267 lines

---
v3: rebase resolving builtin-ftrace.c conflicts and incorporating
change in:
https://lore.kernel.org/lkml/[email protected]/
v2: fix syscall number and '>' should have been '>='.

---
 tools/perf/Makefile.config  | 11 -------
 tools/perf/builtin-ftrace.c | 28 ++++++++---------
 tools/perf/util/Build       |  2 +-
 tools/perf/util/cap.c       | 63 ++++++++++++++++++++++++++-----------
 tools/perf/util/cap.h       | 23 ++------------
 tools/perf/util/symbol.c    |  8 ++---
 tools/perf/util/util.c      | 12 +++++--
 7 files changed, 75 insertions(+), 72 deletions(-)
tools/perf/Makefile.config +0 -11
index fa679db61f62..4eb1fc897baf 100644
@@ -1031,17 +1031,6 @@ ifndef NO_LIBZSTD
   endif
 endif
 
-ifndef NO_LIBCAP
-  ifeq ($(feature-libcap), 1)
-    CFLAGS += -DHAVE_LIBCAP_SUPPORT
-    EXTLIBS += -lcap
-    $(call detected,CONFIG_LIBCAP)
-  else
-    $(warning No libcap found, disables capability support, please install libcap-devel/libcap-dev)
-    NO_LIBCAP := 1
-  endif
-endif
-
 ifndef NO_BACKTRACE
   ifeq ($(feature-backtrace), 1)
     CFLAGS += -DHAVE_BACKTRACE_SUPPORT
tools/perf/builtin-ftrace.c +14 -14
index a615c405d98f..88a87bf387d2 100644
@@ -63,20 +63,21 @@ static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
 	done = true;
 }
 
-static int check_ftrace_capable(void)
+static bool check_ftrace_capable(void)
 {
-	if (!(perf_cap__capable(CAP_PERFMON) ||
-	      perf_cap__capable(CAP_SYS_ADMIN))) {
-		pr_err("ftrace only works for %s!\n",
-#ifdef HAVE_LIBCAP_SUPPORT
-		"users with the CAP_PERFMON or CAP_SYS_ADMIN capability"
-#else
-		"root"
-#endif
+	bool used_root;
+
+	if (perf_cap__capable(CAP_PERFMON, &used_root))
+		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"
 		);
-		return -1;
-	}
-	return 0;
+	return false;
 }
 
 static int __write_tracing_file(const char *name, const char *val, bool append)
@@ -1579,8 +1580,7 @@ int cmd_ftrace(int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	ret = check_ftrace_capable();
-	if (ret < 0)
+	if (!check_ftrace_capable())
 		return -1;
 
 	ret = perf_config(perf_ftrace_config, &ftrace);
tools/perf/util/Build +1 -1
index b24360c04aae..adbaf7748019 100644
@@ -221,7 +221,7 @@ perf-util-$(CONFIG_ZLIB) += zlib.o
 perf-util-$(CONFIG_LZMA) += lzma.o
 perf-util-$(CONFIG_ZSTD) += zstd.o
 
-perf-util-$(CONFIG_LIBCAP) += cap.o
+perf-util-y += cap.o
 
 perf-util-$(CONFIG_CXX_DEMANGLE) += demangle-cxx.o
 perf-util-y += demangle-ocaml.o
tools/perf/util/cap.c +44 -19
index c3ba841bbf37..7574a67651bc 100644
@@ -3,27 +3,52 @@
  * Capability utilities
  */
 
-#ifdef HAVE_LIBCAP_SUPPORT
-
 #include "cap.h"
-#include <stdbool.h>
-#include <sys/capability.h>
-
-bool perf_cap__capable(cap_value_t cap)
-{
-	cap_flag_value_t val;
-	cap_t caps = cap_get_proc();
+#include "debug.h"
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/capability.h>
+#include <sys/syscall.h>
 
-	if (!caps)
-		return false;
+#ifndef SYS_capget
+#define SYS_capget 90
+#endif
 
-	if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &val) != 0)
-		val = CAP_CLEAR;
+#define MAX_LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3
 
-	if (cap_free(caps) != 0)
-		return false;
-
-	return val == CAP_SET;
+bool perf_cap__capable(int cap, bool *used_root)
+{
+	struct __user_cap_header_struct header = {
+		.version = _LINUX_CAPABILITY_VERSION_3,
+		.pid = getpid(),
+	};
+	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;
 }
-
-#endif  /* HAVE_LIBCAP_SUPPORT */
tools/perf/util/cap.h +3 -20
index ae52878c0b2e..0c6a1ff55f07 100644
@@ -3,26 +3,6 @@
 #define __PERF_CAP_H
 
 #include <stdbool.h>
-#include <linux/capability.h>
-#include <linux/compiler.h>
-
-#ifdef HAVE_LIBCAP_SUPPORT
-
-#include <sys/capability.h>
-
-bool perf_cap__capable(cap_value_t cap);
-
-#else
-
-#include <unistd.h>
-#include <sys/types.h>
-
-static inline bool perf_cap__capable(int cap __maybe_unused)
-{
-	return geteuid() == 0;
-}
-
-#endif /* HAVE_LIBCAP_SUPPORT */
 
 /* For older systems */
 #ifndef CAP_SYSLOG
@@ -33,4 +13,7 @@ static inline bool perf_cap__capable(int cap __maybe_unused)
 #define CAP_PERFMON	38
 #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);
+
 #endif /* __PERF_CAP_H */
tools/perf/util/symbol.c +4 -4
index 19eb623e0826..a18927d792af 100644
@@ -2425,14 +2425,14 @@ 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);
 
 	if (fp != NULL) {
 		char line[8];
 
 		if (fgets(line, sizeof(line), fp) != NULL)
-			value = perf_cap__capable(CAP_SYSLOG) ?
-					(atoi(line) >= 2) :
-					(atoi(line) != 0);
+			value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0);
 
 		fclose(fp);
 	}
@@ -2440,7 +2440,7 @@ static bool symbol__read_kptr_restrict(void)
 	/* Per kernel/kallsyms.c:
 	 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
 	 */
-	if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG))
+	if (perf_event_paranoid() > 1 && !cap_syslog)
 		value = true;
 
 	return value;
tools/perf/util/util.c +9 -3
index 4f561e5e4162..9d55a13787ce 100644
@@ -325,9 +325,15 @@ int perf_event_paranoid(void)
 
 bool perf_event_paranoid_check(int max_level)
 {
-	return perf_cap__capable(CAP_SYS_ADMIN) ||
-			perf_cap__capable(CAP_PERFMON) ||
-			perf_event_paranoid() <= 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;
 }
 
 static int
author reviewer
#3 lore raw
Quoted 41 lines · Adrian Hunter
Quoted 12 lines · Ian Rogers

Remove dependence on libcap. libcap is only used to query whether a capability is supported, which is just 1 capget system call.

If the capget system call fails, fall back on root permission checking. Previously if libcap fails then the permission is assumed not present which may be pessimistic/wrong.

Add a used_root out argument to perf_cap__capable to say whether the fall back root check was used. This allows the correct error message, "root" vs "users with the CAP_PERFMON or CAP_SYS_ADMIN capability", to be selected.

Quoted 4 lines

Tidy uses of perf_cap__capable so that tests aren't repeated if capget isn't supported.

I find I need to revert this. E.g.

$ install-perf.sh
$ perf record -e intel_pt//k uname
WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
check /proc/sys/kernel/kptr_restrict and
/proc/sys/kernel/perf_event_paranoid.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Couldn't record kernel reference relocation symbol
Symbol resolution may be skewed if relocation was used (e.g. kexec). Check /proc/kallsyms permission or run as root.
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.061 MB perf.data ]

Quoted 13 lines
$ git revert e25ebda78e230283bf707ae3e9655270ff40a7f9
Auto-merging tools/perf/Makefile.config
Auto-merging tools/perf/builtin-ftrace.c
Auto-merging tools/perf/util/Build
Auto-merging tools/perf/util/symbol.c
[master 752707588a700] Revert "perf cap: Tidy up and improve capability
testing"
 7 files changed, 70 insertions(+), 73 deletions(-)
$ install-perf.sh
$ perf record -e intel_pt//k uname
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.065 MB perf.data ]

Sorry for this. I couldn't immediately see a code path that should impact perf record and the reproducer works for me without the revert. Running your command under gdb looking for calls to perf_cap__capable:

1)

```
#0  0x00005555557f7abe in symbol__read_kptr_restrict () at util/symbol.c:2429
#1  0x00005555557f759f in symbol__init (env=0x0) at util/symbol.c:2523
#2  0x00005555555e7ca3 in cmd_record (argc=1, argv=0x7fffffffe390) at
builtin-record.c:4114
#3  0x000055555569775f in run_builtin (p=0x5555560bda50
<commands+288>, argc=4,
   argv=0x7fffffffe390) at perf.c:351
#4  0x0000555555696e13 in handle_internal_command (argc=4,
argv=0x7fffffffe390) at perf.c:404
#5  0x000055555569762f in run_argv (argcp=0x7fffffffe1dc,
argv=0x7fffffffe1d0) at perf.c:448
#6  0x0000555555696a74 in main (argc=4, argv=0x7fffffffe390) at perf.c:562
```
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-record.c?h=perf-tools-next#n4108
```
/*
* Allow aliases to facilitate the lookup of symbols for address
* filters. Refer to auxtrace_parse_filters().
*/
symbol_conf.allow_aliases = true;

symbol__init(NULL);
```

I don't think this is the cause but it seems unfortunate we're enabling symbols in perf record unconditionally.

2)

```
#0  perf_cap__capable (cap=21, used_root=0x7fffffffbd87) at util/cap.c:22
#1  0x0000555555878236 in perf_event_paranoid_check (max_level=-1) at
util/util.c:330
#2  0x00005555559574ba in intel_pt_recording_options
(itr=0x55555610ada0, evlist=0x5555560f36f0,
   opts=0x5555560b4818 <record+320>) at arch/x86/util/intel-pt.c:619
#3  0x00005555558a46ad in auxtrace_record__options
(itr=0x55555610ada0, evlist=0x5555560f36f0,
   opts=0x5555560b4818 <record+320>) at util/auxtrace.c:619
#4  0x00005555555e8139 in cmd_record (argc=1, argv=0x7fffffffe390) at
builtin-record.c:4206
#5  0x000055555569775f in run_builtin (p=0x5555560bda50
<commands+288>, argc=4,
   argv=0x7fffffffe390) at perf.c:351
#6  0x0000555555696e13 in handle_internal_command (argc=4,
argv=0x7fffffffe390) at perf.c:404
#7  0x000055555569762f in run_argv (argcp=0x7fffffffe1dc,
argv=0x7fffffffe1d0) at perf.c:448
#8  0x0000555555696a74 in main (argc=4, argv=0x7fffffffe390) at perf.c:562
```

So this looks like the cause.

Would it be possible for you to trace (strace/gdb/perf trace) what's happening in perf_cap__capable? I suspect the system call capget and its interpretation are the source of the problem, but I can't eyeball an issue in:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/cap.c?h=perf-tools-next#n53

I'd prefer a fix to a revert but I'll leave that to others to decide.

Thanks,
Ian

reviewer
#4 lore raw
Quoted 56 lines · Ian Rogers
Quoted 41 lines · Adrian Hunter
Quoted 12 lines · Ian Rogers

Remove dependence on libcap. libcap is only used to query whether a capability is supported, which is just 1 capget system call.

If the capget system call fails, fall back on root permission checking. Previously if libcap fails then the permission is assumed not present which may be pessimistic/wrong.

Add a used_root out argument to perf_cap__capable to say whether the fall back root check was used. This allows the correct error message, "root" vs "users with the CAP_PERFMON or CAP_SYS_ADMIN capability", to be selected.

Quoted 4 lines

Tidy uses of perf_cap__capable so that tests aren't repeated if capget isn't supported.

I find I need to revert this. E.g.

$ install-perf.sh
$ perf record -e intel_pt//k uname
WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
check /proc/sys/kernel/kptr_restrict and
/proc/sys/kernel/perf_event_paranoid.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Couldn't record kernel reference relocation symbol
Symbol resolution may be skewed if relocation was used (e.g. kexec). Check /proc/kallsyms permission or run as root.
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.061 MB perf.data ]

Quoted 13 lines
$ git revert e25ebda78e230283bf707ae3e9655270ff40a7f9
Auto-merging tools/perf/Makefile.config
Auto-merging tools/perf/builtin-ftrace.c
Auto-merging tools/perf/util/Build
Auto-merging tools/perf/util/symbol.c
[master 752707588a700] Revert "perf cap: Tidy up and improve capability
testing"
 7 files changed, 70 insertions(+), 73 deletions(-)
$ install-perf.sh
$ perf record -e intel_pt//k uname
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.065 MB perf.data ]
Quoted 3 lines

Sorry for this. I couldn't immediately see a code path that should impact perf record and the reproducer works for me without the revert.

Sorry, I forgot to highlight that capabilities are being used as described here:

https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html

i.e. # setcap "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf

	# setcap -v "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf
	perf: OK
	# getcap perf
	perf = cap_sys_ptrace,cap_syslog,cap_perfmon+ep

instead of changing perf_paranoid, kptr_restrict etc

Quoted 65 lines

revert. Running your command under gdb looking for calls to perf_cap__capable:

1)

```
#0  0x00005555557f7abe in symbol__read_kptr_restrict () at util/symbol.c:2429
#1  0x00005555557f759f in symbol__init (env=0x0) at util/symbol.c:2523
#2  0x00005555555e7ca3 in cmd_record (argc=1, argv=0x7fffffffe390) at
builtin-record.c:4114
#3  0x000055555569775f in run_builtin (p=0x5555560bda50
<commands+288>, argc=4,
   argv=0x7fffffffe390) at perf.c:351
#4  0x0000555555696e13 in handle_internal_command (argc=4,
argv=0x7fffffffe390) at perf.c:404
#5  0x000055555569762f in run_argv (argcp=0x7fffffffe1dc,
argv=0x7fffffffe1d0) at perf.c:448
#6  0x0000555555696a74 in main (argc=4, argv=0x7fffffffe390) at perf.c:562
```
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-record.c?h=perf-tools-next#n4108
```
/*
* Allow aliases to facilitate the lookup of symbols for address
* filters. Refer to auxtrace_parse_filters().
*/
symbol_conf.allow_aliases = true;

symbol__init(NULL);
```

I don't think this is the cause but it seems unfortunate we're enabling symbols in perf record unconditionally.

2)

```
#0  perf_cap__capable (cap=21, used_root=0x7fffffffbd87) at util/cap.c:22
#1  0x0000555555878236 in perf_event_paranoid_check (max_level=-1) at
util/util.c:330
#2  0x00005555559574ba in intel_pt_recording_options
(itr=0x55555610ada0, evlist=0x5555560f36f0,
   opts=0x5555560b4818 <record+320>) at arch/x86/util/intel-pt.c:619
#3  0x00005555558a46ad in auxtrace_record__options
(itr=0x55555610ada0, evlist=0x5555560f36f0,
   opts=0x5555560b4818 <record+320>) at util/auxtrace.c:619
#4  0x00005555555e8139 in cmd_record (argc=1, argv=0x7fffffffe390) at
builtin-record.c:4206
#5  0x000055555569775f in run_builtin (p=0x5555560bda50
<commands+288>, argc=4,
   argv=0x7fffffffe390) at perf.c:351
#6  0x0000555555696e13 in handle_internal_command (argc=4,
argv=0x7fffffffe390) at perf.c:404
#7  0x000055555569762f in run_argv (argcp=0x7fffffffe1dc,
argv=0x7fffffffe1d0) at perf.c:448
#8  0x0000555555696a74 in main (argc=4, argv=0x7fffffffe390) at perf.c:562
```

So this looks like the cause.

Would it be possible for you to trace (strace/gdb/perf trace) what's happening in perf_cap__capable? I suspect the system call capget and its interpretation are the source of the problem, but I can't eyeball an issue in:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/cap.c?h=perf-tools-next#n53

I'd prefer a fix to a revert but I'll leave that to others to decide.

Thanks,
Ian

author reviewer
#5 lore raw
Quoted 67 lines · Adrian Hunter
Quoted 56 lines · Ian Rogers
Quoted 41 lines · Adrian Hunter
Quoted 12 lines · Ian Rogers

Remove dependence on libcap. libcap is only used to query whether a capability is supported, which is just 1 capget system call.

If the capget system call fails, fall back on root permission checking. Previously if libcap fails then the permission is assumed not present which may be pessimistic/wrong.

Add a used_root out argument to perf_cap__capable to say whether the fall back root check was used. This allows the correct error message, "root" vs "users with the CAP_PERFMON or CAP_SYS_ADMIN capability", to be selected.

Quoted 4 lines

Tidy uses of perf_cap__capable so that tests aren't repeated if capget isn't supported.

I find I need to revert this. E.g.

$ install-perf.sh
$ perf record -e intel_pt//k uname
WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
check /proc/sys/kernel/kptr_restrict and
/proc/sys/kernel/perf_event_paranoid.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Couldn't record kernel reference relocation symbol
Symbol resolution may be skewed if relocation was used (e.g. kexec). Check /proc/kallsyms permission or run as root.
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.061 MB perf.data ]

Quoted 13 lines
$ git revert e25ebda78e230283bf707ae3e9655270ff40a7f9
Auto-merging tools/perf/Makefile.config
Auto-merging tools/perf/builtin-ftrace.c
Auto-merging tools/perf/util/Build
Auto-merging tools/perf/util/symbol.c
[master 752707588a700] Revert "perf cap: Tidy up and improve capability
testing"
 7 files changed, 70 insertions(+), 73 deletions(-)
$ install-perf.sh
$ perf record -e intel_pt//k uname
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.065 MB perf.data ]
Quoted 3 lines

Sorry for this. I couldn't immediately see a code path that should impact perf record and the reproducer works for me without the revert.

Sorry, I forgot to highlight that capabilities are being used as described here:

https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html

Quoted 7 lines

i.e. # setcap "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf

        # setcap -v "cap_perfmon,cap_sys_ptrace,cap_syslog=ep" perf
        perf: OK
        # getcap perf
        perf = cap_sys_ptrace,cap_syslog,cap_perfmon+ep

instead of changing perf_paranoid, kptr_restrict etc

Thanks Adrian. I was able to reproduce something like the problem. Anyway, the issue appears to be include path related, I came up with:
https://lore.kernel.org/lkml/[email protected]/
Could you take a look and test for me. Again, sorry for the inconvenience.

Ian

Quoted 66 lines
Quoted 65 lines

revert. Running your command under gdb looking for calls to perf_cap__capable:

1)

```
#0  0x00005555557f7abe in symbol__read_kptr_restrict () at util/symbol.c:2429
#1  0x00005555557f759f in symbol__init (env=0x0) at util/symbol.c:2523
#2  0x00005555555e7ca3 in cmd_record (argc=1, argv=0x7fffffffe390) at
builtin-record.c:4114
#3  0x000055555569775f in run_builtin (p=0x5555560bda50
<commands+288>, argc=4,
   argv=0x7fffffffe390) at perf.c:351
#4  0x0000555555696e13 in handle_internal_command (argc=4,
argv=0x7fffffffe390) at perf.c:404
#5  0x000055555569762f in run_argv (argcp=0x7fffffffe1dc,
argv=0x7fffffffe1d0) at perf.c:448
#6  0x0000555555696a74 in main (argc=4, argv=0x7fffffffe390) at perf.c:562
```
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-record.c?h=perf-tools-next#n4108
```
/*
* Allow aliases to facilitate the lookup of symbols for address
* filters. Refer to auxtrace_parse_filters().
*/
symbol_conf.allow_aliases = true;

symbol__init(NULL);
```

I don't think this is the cause but it seems unfortunate we're enabling symbols in perf record unconditionally.

2)

```
#0  perf_cap__capable (cap=21, used_root=0x7fffffffbd87) at util/cap.c:22
#1  0x0000555555878236 in perf_event_paranoid_check (max_level=-1) at
util/util.c:330
#2  0x00005555559574ba in intel_pt_recording_options
(itr=0x55555610ada0, evlist=0x5555560f36f0,
   opts=0x5555560b4818 <record+320>) at arch/x86/util/intel-pt.c:619
#3  0x00005555558a46ad in auxtrace_record__options
(itr=0x55555610ada0, evlist=0x5555560f36f0,
   opts=0x5555560b4818 <record+320>) at util/auxtrace.c:619
#4  0x00005555555e8139 in cmd_record (argc=1, argv=0x7fffffffe390) at
builtin-record.c:4206
#5  0x000055555569775f in run_builtin (p=0x5555560bda50
<commands+288>, argc=4,
   argv=0x7fffffffe390) at perf.c:351
#6  0x0000555555696e13 in handle_internal_command (argc=4,
argv=0x7fffffffe390) at perf.c:404
#7  0x000055555569762f in run_argv (argcp=0x7fffffffe1dc,
argv=0x7fffffffe1d0) at perf.c:448
#8  0x0000555555696a74 in main (argc=4, argv=0x7fffffffe390) at perf.c:562
```

So this looks like the cause.

Would it be possible for you to trace (strace/gdb/perf trace) what's happening in perf_cap__capable? I suspect the system call capget and its interpretation are the source of the problem, but I can't eyeball an issue in:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/cap.c?h=perf-tools-next#n53

I'd prefer a fix to a revert but I'll leave that to others to decide.

Thanks,
Ian