From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796109432; b=RghIlQs2IoVhTB/Cqtr9EGxIdsdosg2ehydsrfbTR5TuUDSXUfpS2JhrmNRfBsWoWnuq jHUS1FXDItppuWUQKd+PFZpoMklk/w8UIl/WZjQT2LTP9TfVtKoJZRwU5YEQKSRQo/CE1 Mhxs7JfOc6pZwPQ31Prf+ZOh03PMbTnABI2i/raqiMcz78LbAeLp3XGy/cSi/g/pBY1FA EsdDFD8+JpqFqHJq3BiVbm7Vo15S8QglTP/JcZyflvO0hsDNcEuSjkuRppWpXccnM3x+U pi6X6fyPUEbHmUGil003HgaQ1cn3IyUNpJkP/xxpYBnprxfnW6MSWA4LGnK18Uoz3ww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796109432; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=Z/NI8A/rwON/0UGvNQNHyxQrtaaMYqsr3vfEx6HqZOs=; b=yQu4oM2F+nKs+NzdfrsLbbiaNFd4Oj5bVUL4ZY+4K+XIa2yoaV3yNfzIhrN4fVo0fx85 04aFW09GYz/gN9PZoptEh/gUUO+4obnIoFc7kj3Gev9qpujLzQjvlk9LZc7F8zWHwuGUr awdfqyNSbcz0z+9JzCK3TXnzHab7vWifZYXQyISNySXofO+sDDuYTAGHRhmEvDLJ/AbbE jsuee2CyBpBTM/8dlkbAagZrSjCjyuEvFOYUfjKxjzs5rIaiNRW7kcavnpn9wHoafLXMb eClashbFWpsMNO8XgkQrmW1Us1kGN44gWqSIiuTZz9AWhmJF33376QzbsqORo6D6kpw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=X04sPA6v; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13FCC606A6B4 for ; Tue, 1 Dec 2026 07:17:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 13FCC606A6B4 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.a=rsa-sha256 header.s=20260707 header.b=X04sPA6v Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-38531c1a566so11263197eec.1 for ; Mon, 30 Nov 2026 23:17:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796109423; x=1796714223; 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=Z/NI8A/rwON/0UGvNQNHyxQrtaaMYqsr3vfEx6HqZOs=; b=X04sPA6v3A01M8TGUdR2CvcuhIlrdp9Hc9TjVAwdXmILvg9bMNf21gvPyA65Xm5ZIf KhI1JdxsgrrLmdONJya8kmqj5DDZe4dAoEIHkxm148E0P5R1v4fzitsdyTJQeSIpP1OU wxa88upyOZQpYFPPeTO1t5OAB+VzGsmz4eDf0nNatCA9np+RzSIBlxz9OjSrSQA+MrW5 pmyDC+69xYRflL/2qbT6FJfvHdTZ4wIChZ1PM9HESsJzLFL635ed7AFyAxDhyhfbFEPG p3M0TAavvSmodCraGj3VE65pM9BIjc+ClN5KnrsYVg5PaYfkoafLdJGH7x0O8SkszAoC g7pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796109423; x=1796714223; 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=Z/NI8A/rwON/0UGvNQNHyxQrtaaMYqsr3vfEx6HqZOs=; b=EA+JNAilNcJfd+zLdba6UxknWUjjpkEwx+g4HDFYDDFRWceNdAAQSSnrIdHk88RkOJ XelidjoHfFcvyaWOFkN8xh+Fw1iGuqjo9jj+SFkHSaXSUYUEcCFvYFKf+sVEqSRp48Qr pxDv269mXqmHcsWgpAqACS/3j5Y4/BjgX6vXyxCXEmj292CRVvAE3oX1Lj+SSqlnhTwL Ob67WHEivPsyAUkdZVGC+R78kBUp7l41xkiBsTez+df7g3We/RsPXogc00IsSxklLn2+ pOev8KFFEySQrBySkgFv2eKm+Sk64e5Vf56EhSd8mRgVmU1qWo7brCUti7YsbLOCHP4Z dn9A== X-Forwarded-Encrypted: i=1; AL+hErH2wVKNlV8zWUNd14FAC0fU+EQ2ICoWCuObFOmvWbxrhf3iXXmZ7XAaao3KvZ/fP21KDq27t9fYDi5t1BxZu1PL@vger.kernel.org X-Gm-Message-State: APuU4Z/3/LDu4HohRsLa80s17zARxvHpw1p1U/J2/h/gFG2XqGr5lXJa 3LHG9SgNQ1sjrcu9Qyly9sUNV9wqRZkdtaIfpXpMUSFyCN26hcMhJnqdJ/vmbYkuie+OvDR4E9Y 7dVHO7tnKzg== X-Received: from dycnb22.prod.google.com ([2002:a05:7301:b16:b0:386:af9c:96b6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:f68e:b0:369:f291:49da with SMTP id 5a478bee46e88-38571814b1dmr35332612eec.40.1796109422718; Mon, 30 Nov 2026 23:17:02 -0800 (PST) Date: Mon, 30 Nov 2026 23:16:52 -0800 In-Reply-To: <20261201071653.827828-1-irogers@google.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201071653.827828-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201071653.827828-2-irogers@google.com> Subject: [PATCH v1 1/2] perf/bpf: Fix BPF program inheritance races, cookie, and leak in perf_event_alloc() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Eduard Zingerman , Kumar Kartikeya Dwivedi , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , James Clark , Song Liu , Yonghong Song , linux-perf-users@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" Fix four issues in how perf_event_alloc() inherits parent_event->prog and how non-tracing BPF handlers are installed and released: 1. Tracing events (tracepoints, kprobes, uprobes) attach BPF programs to the shared tp_event->prog_array rather than invoking event->prog via bpf_overflow_handler(). Commit f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not through overflow machinery") replaced the overflow_handler == bpf_overflow_handler check in perf_event_alloc() with an unconditional parent_event->prog check, causing inherited tracing children to copy parent_event->prog without attaching to tp_event->prog_array. When such a child exits, _free_event() calls perf_event_free_bpf_prog() -> perf_event_detach_bpf_prog(), removing the parent's BPF program from the shared tp_event->prog_array and disabling BPF execution for the still-live parent event. Skip inheritance when perf_event_is_tracing(parent_event) is true. 2. For non-tracing events, perf_event_alloc() reads parent_event->prog and calls bpf_prog_inc() locklessly, which races with concurrent bpf_perf_link_release() -> perf_event_free_bpf_handler() clearing event->prog and dropping its last reference, leading to a potential NULL dereference or use-after-free. Protect the lockless read and bpf_prog_inc_not_zero() with both rcu_tasks_trace and rcu read-side critical sections, paired with smp_store_release() in perf_event_set_bpf_handler() and xchg() (plus synchronize_rcu() when bpf_prog_runs_sleepable() is true) in perf_event_free_bpf_handler(). 3. Copy parent_event->bpf_cookie alongside parent_event->prog using smp_load_acquire() / READ_ONCE() paired with WRITE_ONCE() / smp_store_release() in perf_event_set_bpf_handler(), so inherited child events preserve the configured BPF cookie for bpf_get_attach_cookie_pe(). 4. If any fallible initialization step in perf_event_alloc() fails after acquiring the inherited BPF program reference, __free(__free_event) does not call perf_event_free_bpf_prog(), leaking the bpf_prog reference. Move perf_event_free_bpf_prog() from _free_event() into __free_event() (before event->destroy()), matching security_perf_event_free(). Fixes: aa6a5f3cb2b2 ("perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs") Fixes: 82e6b1eee6a8 ("bpf: Allow to specify user-provided bpf_cookie for BPF perf links") Fixes: f11f10bfa1ca ("perf/bpf: Call BPF handler directly, not through overflow machinery") Signed-off-by: Ian Rogers --- kernel/events/core.c | 64 +++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4ab3919ac78e..369595d95b97 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -5790,6 +5791,7 @@ static void __free_event(struct perf_event *event) { struct pmu *pmu = event->pmu; + perf_event_free_bpf_prog(event); security_perf_event_free(event); if (event->attach_state & PERF_ATTACH_CALLCHAIN) @@ -5868,7 +5870,6 @@ static void _free_event(struct perf_event *event) mutex_unlock(&event->mmap_mutex); } - perf_event_free_bpf_prog(event); perf_free_addr_filters(event); __free_event(event); @@ -10858,7 +10859,7 @@ static inline int perf_event_set_bpf_handler(struct perf_event *event, /* hw breakpoint or kernel counter */ return -EINVAL; - if (event->prog) + if (READ_ONCE(event->prog)) return -EEXIST; if (prog->type != BPF_PROG_TYPE_PERF_EVENT) @@ -10881,19 +10882,21 @@ static inline int perf_event_set_bpf_handler(struct perf_event *event, return -EPROTO; } - event->prog = prog; - event->bpf_cookie = bpf_cookie; + WRITE_ONCE(event->bpf_cookie, bpf_cookie); + /* Pairs with smp_load_acquire() in perf_event_inherit_bpf_prog() */ + smp_store_release(&event->prog, prog); return 0; } static inline void perf_event_free_bpf_handler(struct perf_event *event) { - struct bpf_prog *prog = event->prog; + struct bpf_prog *prog = xchg(&event->prog, NULL); if (!prog) return; - event->prog = NULL; + if (bpf_prog_runs_sleepable(prog)) + synchronize_rcu(); bpf_prog_put(prog); } #else @@ -11938,7 +11941,7 @@ int perf_event_set_bpf_prog(struct perf_event *event, void perf_event_free_bpf_prog(struct perf_event *event) { - if (!event->prog) + if (!READ_ONCE(event->prog)) return; if (!perf_event_is_tracing(event)) { @@ -13517,6 +13520,44 @@ static void account_event(struct perf_event *event) account_pmu_sb_event(event); } +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) +static void perf_event_inherit_bpf_prog(struct perf_event *event, + struct perf_event *parent_event) +{ + struct bpf_prog *prog; + u64 bpf_cookie; + + /* + * Only non-tracing events invoke event->prog directly. Tracing events + * attach to the shared tp_event->prog_array, so copying + * parent_event->prog without attaching would cause child teardown to + * detach the parent's program. + */ + if (perf_event_is_tracing(parent_event)) + return; + + guard(rcu_tasks_trace)(); + guard(rcu)(); + /* Pairs with smp_store_release() in perf_event_set_bpf_handler() */ + prog = smp_load_acquire(&parent_event->prog); + if (!prog) + return; + + bpf_cookie = READ_ONCE(parent_event->bpf_cookie); + prog = bpf_prog_inc_not_zero(prog); + if (IS_ERR(prog)) + return; + + event->bpf_cookie = bpf_cookie; + event->prog = prog; +} +#else +static inline void perf_event_inherit_bpf_prog(struct perf_event *event, + struct perf_event *parent_event) +{ +} +#endif + /* * Allocate and initialize an event structure */ @@ -13611,14 +13652,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, if (!overflow_handler && parent_event) { overflow_handler = parent_event->overflow_handler; context = parent_event->overflow_handler_context; -#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) - if (parent_event->prog) { - struct bpf_prog *prog = parent_event->prog; - - bpf_prog_inc(prog); - event->prog = prog; - } -#endif + perf_event_inherit_bpf_prog(event, parent_event); } if (overflow_handler) { -- 2.56.1.839.g22a8290db9-goog