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=1796109426; b=m8nqOJYkvjCMPx8X5t4Q7701wiQ315Vt+Hhy3fb+2/84VkXf2cWqAPJatgXueHcWQICg zvx3YCRFphp0B8FRPjlyZJGYIW2aoMhfjj88HgMudqGOqq2/uc/VF/j9qhOKLn73EhwRX FMxHPUEj3qaKAjTd6inQScWtjWdtbLWF3maaOyQcw5IbQBAfBQvTUPbJLLPDVPv3ori2j ZAWRiOukefKyg+uu2SwTTTN4SaU1jyDROCEv2a8Nh6bCC21O1cvW8QM9Y2z5hFv3d0AB9 vGSPhoN+jsUzlXWmG42g7fJKETdOLPVu2kMBTPeXR3yjDOsInKRLAxz40Oqo1bszkbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796109426; 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=CiwhEU2TTmpnPDPzWvxqtpcO+70SDJA+Am+xUbjbrym5K0JG53RnDmeTR1jIsOaCwxE0 ak0kXXeEAvFLqrqSntIcyETn06Ml0LBE12Vogqu2nXYI/buV09Paoc1ari+DGfWcFPWTj +d9ln+WldQF/9lrfU+4Evbvg0GJGYiubLRV2bc5J1ma7Z8TFzdz+UoLa947dBCjeeAZSz VH2rbsngN2lDb4z84XHxVpua1M9naRMptGOOF09Fxa0D48Xc52nomRepw9b0Xy0USL11x RbIEtCKU7Gh+mulFC++ynM8J6zwOxHeIi0dow/pL7W6gh2bJCiCftXvsyQ2yF4mZ4SQ== 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.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 1C79E606A699 for ; Tue, 1 Dec 2026 07:17:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1C79E606A699 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-f197.google.com with SMTP id 5a478bee46e88-375bac6bd21so10887665eec.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=PFDv/M+MdBZzlnkVJhQzM8Zr9GtD5/gyERb5sLgGlhS8RljwPPUXKKqMfSJ6L3DODX Jh4fmX1IuZJDu5W8CbVOaIHu4GLhFGXEoj5GiHWNYpO/8zVWzsabGpTk3MdBOytKkB/W UExS6VO1ydN3VMdqszgcU0P3dEanFcqfSJALkjSdUu0gjprT4dVtM5Rdu2/1F52cSE9Y eFtF+Drnw6WluEoXxqRrhOxwKHz75ouMlG1yaFMZKa88KOX0fSIjdGukELy1cekJiBtl CmakPR+Tm4/9oRJJHLwNpZ8IMN3WuHy0lL1km+3+wbk+xoxU516veEpPrlhxQogvQrv1 jxkw== X-Forwarded-Encrypted: i=1; AL+hErGe0xhvh3EsZ4vl7yUiROGUxekeTDkmwOWOqkqCQbxPJQ2o9KAEOgwcsp+/pWJm/A8eLIE=@vger.kernel.org X-Gm-Message-State: APuU4Z84alDzhPr87++A86/M45QtJ56/WM5OR+zxdbw7CinXKscpuNj5 Ai9QePLMYLMDmOrD9VfbN1NYsqz60UwW4O2lJs1DbLTP3Mfqkue3YoLVwXWrT+WXyuAYhB46+Cm qF6H/Bvw3/g== 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: bpf@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