From nobody Sat Jul 25 16:18:39 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD91C3A0B24 for ; Thu, 16 Jul 2026 07:40:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784187638; cv=none; b=q4828oDMgxtH5ATYAA4XMPCWWtGper6XXXje+AxmqQvh7mohQH2kdxgr5kSBiuI662N6t6wMbeMFYv0iIP07cf/8tCCZczPmiT0zcGLPsza1MhaE2GWwr9jUSmrvzatML1b7vq6gxXXhW6/D3aobkJclgusSfjL9v0hyEyo52lE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784187638; c=relaxed/simple; bh=X32jELlMF94Xl0NmY7vypZygDvQes8I0SpcBi7a5U1w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r3xQ3rK2ztXbWC0Ka/ac2p2g2RCsuSQKgkGyNycGRcslleEMUlUh2E3TM7MOD3zCIRHl31XjeK+oZ+pEP9iLVvL2uaQTDcesJvdypk47Q8IO2t5nxLGWZW434uKB869HnU1gzLQl+DUFSR5o43Jj5LetkWXnXTjIJMj3DhVdhEY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bXcp7yiv; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bXcp7yiv" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c88aab7c1fcso15131749a12.3 for ; Thu, 16 Jul 2026 00:40:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784187632; x=1784792432; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=yE24h4tZ7G95OOIPOTLF1J84+Ri6r1nt8tkDTfpbhf0=; b=bXcp7yiv1899J+64f+8zwT1x8j3de+epXnrPXG/4WqAG1mQGbOimUmOwadxhfVl2Au M6si6OV48TdszTDbLjNIOy4zsRcV7Qbcq25eH8wknztpcXpG4G3bUuRKiz0bZqOcB9cF 0RYxLQ4dukUAOYMfmBbsTympLTTnZzc4B8FDgTE1Wf+BBwwkJvWEl9DxSTNYiPMp5JrQ sScKW7Pxlin9W7FaM5SxFioaDvN+XVPlNFvqVlfhULF1xkTl+OrcslDiRqAJpnckCtid EH+kdPJsO0CanIJe47yUejVeg9sda7eAAS/jGBmh1WDKf/dL5ri7vPU0pmFXVlKpEDtd tiTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784187632; x=1784792432; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=yE24h4tZ7G95OOIPOTLF1J84+Ri6r1nt8tkDTfpbhf0=; b=dPzBOSXoggK7yR2EmEYcMjOmc2Vl26aLa0+gQFYM5jTI6FyDlkNFdTn6m6tbI9OPSz z0IOIUomjr3zBKIpIGN6WLquO0Gyy4aqwwmvBrY8rwcs9ofcrWiwL0spuHGj3UM0Hkze 4KSYZ2QoTdGLPLTjo22xgDndtTQuiVfOCVCGk1iBL99HI2Hf1AlUTl4s2Q7e880nTIEs +lXT+4i0RR071URHg1VB6DZb/kL7rqA6RMgdz9ot/Ei/ig2oK1lqpVadsZyonLpNr/8a 9TWStDo+/+wHRCTafuqwnyqKCUjOzBhteXl4IX4vwChlpBGeCVGUQML84t4PGA4F90sA FWLA== X-Forwarded-Encrypted: i=1; AHgh+Rryfd/ILhOTaa/0tuqWlDjaM48cXLidPexNxadvEqD8Wfi0Nk5DSDSmSmjwPh24Cr5J+dIbczfKxaiiAKY=@vger.kernel.org X-Gm-Message-State: AOJu0Yw0DYs0lQINLgIYjuPCw8XIGAm06InwnsAksJUTbBhx5bKo05IZ pGpQS0wUCArj/YpQeWKVdUP4ChrkBjdVkAi4LSHz6jZ2qE5UKzqWkO7logE/yFg8GvW3nSfsMtO Ms+5tXfnhJA== X-Received: from dybrt21.prod.google.com ([2002:a05:7301:4715:b0:313:d566:5bbb]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:7106:b0:3c3:94bb:ceb3 with SMTP id adf61e73a8af0-3c394bbd03dmr690942637.10.1784187632131; Thu, 16 Jul 2026 00:40:32 -0700 (PDT) Date: Thu, 16 Jul 2026 00:40:28 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.55.0.141.g00534a21ce-goog Message-ID: <20260716074028.568999-1-irogers@google.com> Subject: [PATCH v2] perf cap: Remove used_root parameter and simplify capability checks From: Ian Rogers To: namhyung@kernel.org, acme@kernel.org Cc: abyssmystery@gmail.com, adrian.hunter@intel.com, irogers@google.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor perf_cap__capable() to completely remove the used_root out-paramet= er 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 =3D=3D 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: e25ebda78e23 ("perf cap: Tidy up and improve capability testing") Suggested-by: Namhyung Kim Assisted-by: Antigravity:gemini-3.5-flash Signed-off-by: Ian Rogers --- tools/perf/builtin-ftrace.c | 13 +++---------- tools/perf/util/bpf-filter.c | 22 +++++++++------------- tools/perf/util/cap.c | 4 +--- tools/perf/util/cap.h | 3 +-- tools/perf/util/symbol.c | 3 +-- tools/perf/util/util.c | 12 +++--------- 6 files changed, 18 insertions(+), 39 deletions(-) diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c index 9e4c5220d43c..f7126196b092 100644 --- a/tools/perf/builtin-ftrace.c +++ b/tools/perf/builtin-ftrace.c @@ -72,18 +72,11 @@ static void ftrace__workload_exec_failed_signal(int sig= no __maybe_unused, =20 static bool check_ftrace_capable(void) { - bool used_root; - - if (perf_cap__capable(CAP_PERFMON, &used_root)) - return true; - - if (!used_root && perf_cap__capable(CAP_SYS_ADMIN, &used_root)) + if (perf_cap__capable(CAP_PERFMON) || + perf_cap__capable(CAP_SYS_ADMIN)) return true; =20 - pr_err("ftrace only works for %s!\n", - used_root ? "root" - : "users with the CAP_PERFMON or CAP_SYS_ADMIN capability" - ); + pr_err("ftrace only works for users with the CAP_PERFMON or CAP_SYS_ADMIN= capability!\n"); return false; } =20 diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index 1a2e7b388d57..bcd81084e342 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -629,24 +629,20 @@ struct perf_bpf_filter_expr *perf_bpf_filter_expr__ne= w(enum perf_bpf_filter_term =20 static bool check_bpf_filter_capable(void) { - bool used_root; + int fd; =20 - if (perf_cap__capable(CAP_BPF, &used_root)) + if (perf_cap__capable(CAP_BPF)) return true; =20 - if (!used_root) { - /* Check if root already pinned the filter programs and maps */ - int fd =3D get_pinned_fd("filters"); - - if (fd >=3D 0) { - close(fd); - return true; - } + /* Check if root already pinned the filter programs and maps */ + fd =3D get_pinned_fd("filters"); + if (fd >=3D 0) { + close(fd); + return true; } =20 - pr_err("Error: BPF filter only works for %s!\n" - "\tPlease run 'perf record --setup-filter pin' as root first.\n", - used_root ? "root" : "users with the CAP_BPF capability"); + pr_err("Error: BPF filter only works for users with the CAP_BPF capabilit= y!\n" + "\tPlease run 'perf record --setup-filter pin' as root first.\n"); =20 return false; } diff --git a/tools/perf/util/cap.c b/tools/perf/util/cap.c index ac6d1d9a523d..272bd8255ff1 100644 --- a/tools/perf/util/cap.c +++ b/tools/perf/util/cap.c @@ -12,7 +12,7 @@ =20 #define MAX_LINUX_CAPABILITY_U32S _LINUX_CAPABILITY_U32S_3 =20 -bool perf_cap__capable(int cap, bool *used_root) +bool perf_cap__capable(int cap) { struct __user_cap_header_struct header =3D { .version =3D _LINUX_CAPABILITY_VERSION_3, @@ -21,7 +21,6 @@ bool perf_cap__capable(int cap, bool *used_root) struct __user_cap_data_struct data[MAX_LINUX_CAPABILITY_U32S] =3D {}; __u32 cap_val; =20 - *used_root =3D false; while (syscall(SYS_capget, &header, &data[0]) =3D=3D -1) { /* Retry, first attempt has set the header.version correctly. */ if (errno =3D=3D EINVAL && header.version !=3D _LINUX_CAPABILITY_VERSION= _3 && @@ -29,7 +28,6 @@ bool perf_cap__capable(int cap, bool *used_root) continue; =20 pr_debug2("capget syscall failed (%m) fall back on root check\n"); - *used_root =3D true; return geteuid() =3D=3D 0; } =20 diff --git a/tools/perf/util/cap.h b/tools/perf/util/cap.h index c1b8ac033ccc..bf09fb20c779 100644 --- a/tools/perf/util/cap.h +++ b/tools/perf/util/cap.h @@ -18,7 +18,6 @@ #define CAP_BPF 39 #endif =20 -/* Query if a capability is supported, used_root is set if the fallback ro= ot check was used. */ -bool perf_cap__capable(int cap, bool *used_root); +bool perf_cap__capable(int cap); =20 #endif /* __PERF_CAP_H */ diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index a562702b4841..94f9c8faedda 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -2452,8 +2452,7 @@ static bool symbol__read_kptr_restrict(void) { bool value =3D false; FILE *fp =3D fopen("/proc/sys/kernel/kptr_restrict", "r"); - bool used_root; - bool cap_syslog =3D perf_cap__capable(CAP_SYSLOG, &used_root); + bool cap_syslog =3D perf_cap__capable(CAP_SYSLOG); =20 if (fp !=3D NULL) { char line[8]; diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 2c2a5c449ffd..8f7cd32f524d 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -378,15 +378,9 @@ int perf_event_paranoid(void) =20 bool perf_event_paranoid_check(int 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() <=3D max_level; + return perf_cap__capable(CAP_SYS_ADMIN) || + perf_cap__capable(CAP_PERFMON) || + perf_event_paranoid() <=3D max_level; } =20 int perf_tip(char **strp, const char *dirpath) --=20 2.55.0.141.g00534a21ce-goog