From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796109436; b=trqHoeOogD10dnCaW2l51xbf0i0ok0ZpPn7RdlRD+AOrPvN7OPxXlVI0LLSiDIrgEeAx fsR8ZmKZu4j1BuZ6N5Bojh474kK3suqid7EPTR7ZiETrTP+oIGA8H2FRZITOVIgeIfM5c 6vSj7xgQ00LmN37qFNA4qRduvsWD4idejbUeIlBcAkY14FSSUt/6LmVV8ddbVP+eKT0Mj cnbiNsdWAf/vriRDq3C3Yn4Wl25gCePTN5a5p7qjiOzchcGEFbQAlq872Pm0LwEnhjy3/ HhQIMCAbwvgWD5qDHTIdBLVZZs8RJvDB5GRtli/50N9tYtO0puluvo/yHvB09Ns0sIA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796109436; 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=C8+9WqPrLLXpgBOk7biKM7LuDMFjILO5nrrUoQY2AV4=; b=sAOYVU0EDtfOcmjsjPSAjUKqzEZNyc32K6+MjvEDWaDDyTtfwsHT4ZfNz1txDyi9/g+Z Deg4ryEf2SXJX0lNiABGmf1sMcfdyEmsow06Qoq2eXWcs8su0ROgaA2FP/s2ddQwAxfog UZUNkUtKX5DEj4jy/WVd9kD8xRWfWRpR0ARod62f9CAg61McUNRCjuQOVjE44PEdZnEj9 oSszcYKFQw2zPNpQFkiLhdDTHxlJj/aorm9mhTVd+GJ9jqLkkIDerSBHIeMx85YOeTDBK uPMW6curXISzwTRwrEq/LMo7hSLqsHt+gYfKbDzHnEBp6+1clZCPMVu3Y1xPt2+xX3g== 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=L7HAm4j6; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (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 29204606A6A1 for ; Tue, 1 Dec 2026 07:17:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 29204606A6A1 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=L7HAm4j6 Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-375bac6bd21so10887667eec.0 for ; Mon, 30 Nov 2026 23:17:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796109425; x=1796714225; 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=C8+9WqPrLLXpgBOk7biKM7LuDMFjILO5nrrUoQY2AV4=; b=L7HAm4j65URR5er0LdWVndiGYShnurnBOgY6AFwr2BeZo415gY7/TOVzUaEaqM5ZYr EvwyTpVvKYoP/pUAhq4HV8lCy9zehErav6y2n37wlJLLkm4LzGP/v8Idf7/RnL6I7f+f S2Ycs+4GsPSQy3X09KQkMWSu5H/VUtgUJ0jjQ3tJWfm5eFSzKnlzTn1u40+2E2gfUctB bst/sWTWXsTmxGrZdCXF5TD6HT3jhDYX/Fn3jn4Muxyc/tvyCO+mgGMLWTDAjPHvW9jL 9si0rpIMBW9222g493qcEBSRxUhZL0DDeEeJCzszrVHxUDXrrfRk/RFzHERvEDB/gCDQ fjnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796109425; x=1796714225; 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=C8+9WqPrLLXpgBOk7biKM7LuDMFjILO5nrrUoQY2AV4=; b=lt2edDpQxkqTBYVInFmC4n9JBS/GyuSWswkd7Cw2V/dY8m3NqxWR5yGgU26rVixjcu oWM85PmdBTNQRZRjv7MR4p2mLxxrrYYNKm/j6G2uVQnSjxDScQJLsouKLQmSifit/ZPh Y55QkHe7IlX9Tr0p/chZ0f7pkAik35iqxx9zmmaLOKt94mRygpwiqMsnvhwsrKO8yER7 33r0z2bqDLUXGIr0KtmewK/30N3qRyDPzERNqsLbUKOhzX0GKKZ7Q4olLrkKOH3EgDfs 0fi8gEjwoIw+zh4a1Hib0XtNatPCF+24E9T0FI7Um5bWrFJaQ/wfGSgR9T0KlUNDcG/m kMOQ== X-Forwarded-Encrypted: i=1; AL+hErHAkFGJBlvp4IgX6rOLEtjM3X0WUTe2txZXWNxoKl5NWFi2F8IDWX1CqmYkGDLr+a94b8aVVlrZtXcyvbA=@vger.kernel.org X-Gm-Message-State: APuU4Z/hVLbpZ8FTdoCBz3RZnwcbY7a9AlvVCWTjPqhLWoNWskePUDj0 VO3kvHwkzh2URBqPc/wu4DUV7hpWovq2FDtDj5qG0KZYqpmrPl4xC0CUFRFkGGsnnAu6nKjgJPu WMIsifJdNMg== X-Received: from dycph1.prod.google.com ([2002:a05:7300:dd41:b0:36a:ead:4dcc]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:a228:b0:386:6685:1492 with SMTP id 5a478bee46e88-386668523cfmr34438317eec.12.1796109424916; Mon, 30 Nov 2026 23:17:04 -0800 (PST) Date: Mon, 30 Nov 2026 23:16:53 -0800 In-Reply-To: <20261201071653.827828-1-irogers@google.com> X-Mailing-List: linux-kernel@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-3-irogers@google.com> Subject: [PATCH v1 2/2] perf/bpf: Fix racy event->prog dereference in __perf_event_overflow() 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" __perf_event_overflow() checks event->prog and dereferences event->prog->type using plain loads outside an RCU read-side critical section before calling bpf_overflow_handler(). When a BPF perf link attached to an active sampling event is released concurrently via bpf_perf_link_release() -> perf_event_free_bpf_handler(), event->prog can be cleared and freed on another CPU between the check and the dereference (causing a NULL dereference or use-after-free in IRQ/NMI context), or cleared just before bpf_overflow_handler() reads it (causing bpf_overflow_handler() to return 0 and silently drop the sample). Guard the fast path in __perf_event_overflow() with !perf_event_is_tracing(event) && READ_ONCE(event->prog) without dereferencing event->prog outside RCU, and move the prog->type == BPF_PROG_TYPE_PERF_EVENT check inside bpf_overflow_handler() under rcu_read_lock() using smp_load_acquire(). If event->prog was cleared before bpf_overflow_handler() reads it, return 1 so normal overflow sample delivery is not suppressed. Fixes: 100bff23818e ("perf/bpf: Don't call bpf_overflow_handler() for tracing events") Signed-off-by: Ian Rogers --- kernel/events/core.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 369595d95b97..526c486d1647 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10836,13 +10836,16 @@ static int bpf_overflow_handler(struct perf_event *event, if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) goto out; rcu_read_lock(); - prog = READ_ONCE(event->prog); - if (prog) { + /* Pairs with smp_store_release() in perf_event_set_bpf_handler() */ + prog = smp_load_acquire(&event->prog); + if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) { perf_prepare_sample(data, event, regs); /* bpf_perf_event_data::addr is readable without PERF_SAMPLE_ADDR */ if (!(data->sample_flags & PERF_SAMPLE_ADDR)) data->addr = 0; ret = bpf_prog_run(prog, &ctx); + } else { + ret = 1; } rcu_read_unlock(); out: @@ -10919,6 +10922,15 @@ static inline void perf_event_free_bpf_handler(struct perf_event *event) } #endif +#ifdef CONFIG_EVENT_TRACING +static bool perf_event_is_tracing(struct perf_event *event); +#else +static inline bool perf_event_is_tracing(struct perf_event *event) +{ + return false; +} +#endif + /* * Generic event overflow handling, sampling. */ @@ -10942,7 +10954,7 @@ static int __perf_event_overflow(struct perf_event *event, if (event->attr.aux_pause) perf_event_aux_pause(event->aux_event, true); - if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT && + if (!perf_event_is_tracing(event) && READ_ONCE(event->prog) && !bpf_overflow_handler(event, data, regs)) goto out; @@ -11854,7 +11866,7 @@ static void perf_event_free_filter(struct perf_event *event) * returns true if the event is a tracepoint, or a kprobe/upprobe created * with perf_event_open() */ -static inline bool perf_event_is_tracing(struct perf_event *event) +static bool perf_event_is_tracing(struct perf_event *event) { if (event->pmu == &perf_tracepoint) return true; -- 2.56.1.839.g22a8290db9-goog