From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84A153ACA43; Fri, 4 Sep 2026 05:48:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788500933; cv=none; b=DbDlOydMNiSQPZjDGTsCR5hdkmOTu3zdx/uDACrEQ/kRHkdVSTczc7gjCbnVK7SmrFJjMXNJr8EC/Tb6BhQgynQASAwutBirOiJ9b4Lyr2zvw5FBRoyEtS1G7Ykha1AIkBBs5b7eAh053CbKI3lHKyOf872jFQ7rLc1TDROybqc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788500933; c=relaxed/simple; bh=x32vHAFtjrw9uB1yhj+Q6hR3q/tH6yci8pIimFGogo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LGTF8vO2un0dE/EIbd8pI61JNuR4cni4DxmW6y+xowtl/WZS2BTS6KsjpCOcVkKl6bxfr2ThFDtpglbaZ9zYeVSoO6ioT0rgodKnR+gIYW0YYtSzUHsLquAdWzNeFr6aIIADfaNb8uBgg6YEnHGHZe2Ftw2xGkA0yU7/ZLLTx9k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=0ZqQSO8x; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="0ZqQSO8x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0D451F00A3D; Fri, 4 Sep 2026 05:48:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1788500932; bh=Du2dla0sCeVXxqKfGgwETiIJXrLxgjb75AP0VcRoOsc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=0ZqQSO8xhFPJuNfIHI+Rb+5QSZhuQzZZLRkaB578J6vhfl21fuOj6411Wvyf5s+8b Aeuax84iXPPvsKmW+ZbFqoBH3q4ewg6o2m8xPkJyZHLPy58k+cOpbuIH8T34nNBDLA uEGXkpUsKWhyiXVkEeEDYEg2PPCEOazevYvPbxFA= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tao Chen , STAR Labs SG , Daniel Borkmann , Jiri Olsa , Andrii Nakryiko Subject: [PATCH 6.18 231/552] bpf: Disable preemption in __bpf_get_stack Date: Fri, 4 Sep 2026 06:56:28 +0200 Message-ID: <20260904045754.725427048@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260904045747.813364717@linuxfoundation.org> References: <20260904045747.813364717@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Daniel Borkmann commit b1a47b2708d4e95dbd23aee2ec83752190897b3f upstream. get_perf_callchain() returns a per-CPU perf_callchain_entry buffer and releases its recursion slot via put_callchain_entry() before returning, so nothing keeps the entry reserved while __bpf_get_stack() consumes it below. A preemptible BPF program (e.g. a non-sleepable raw tracepoint program on a PREEMPT kernel, which runs under migrate_disable() but not preempt_disable()) can be scheduled out between obtaining the entry and the copy. Another task scheduled on the same CPU then reuses the same per-CPU buffer and overwrites trace->nr with a larger value. copy_len is then computed from the inflated trace->nr and can exceed the caller's buffer, causing an out-of-bounds write in the memcpy() and in the build_id path. The rcu_read_lock() taken here alone does not prevent this. It is only taken on the may_fault path, and under CONFIG_PREEMPT_RCU it does not disable preemption; it merely keeps perf's callchain buffer array alive (freed via call_rcu()) and does nothing to stop another task from reusing the entry. Disable preemption around obtaining the callchain entry and copying it into the caller's buffer, so the entry cannot be reused underneath us and trace->nr stays bounded by max_depth. Build ID resolution may fault and is therefore deferred until after preemption is re-enabled; by then the instruction pointers have already been copied into buf, so it operates only on that private copy. Note, preempt_disable() also subsumes the buffer-lifetime guarantee the rcu_read_lock() provided, since a preempt-disabled section is an RCU read-side critical section for the callchain buffers' call_rcu() reclaim. Fixes: c195651e565a ("bpf: add bpf_get_stack helper") Reported-by: Tao Chen Reported-by: STAR Labs SG Signed-off-by: Daniel Borkmann Signed-off-by: Jiri Olsa Signed-off-by: Andrii Nakryiko Cc: stable@vger.kernel.org Link: https://lore.kernel.org/bpf/20260803210149.296496-11-jolsa@kernel.org Signed-off-by: Greg Kroah-Hartman Closes: https://lore.kernel.org/bpf/20260206090653.1336687-1-chen.dylane@linux.dev/ [ changed Fixes: commit ] --- kernel/bpf/stackmap.c | 3 +++ 1 file changed, 3 insertions(+) --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -460,6 +460,7 @@ static long __bpf_get_stack(struct pt_re max_depth = stack_map_calculate_max_depth(size, elem_size, flags); + preempt_disable(); if (may_fault) rcu_read_lock(); /* need RCU for perf's callchain below */ @@ -476,6 +477,7 @@ static long __bpf_get_stack(struct pt_re if (unlikely(!trace) || trace->nr < skip) { if (may_fault) rcu_read_unlock(); + preempt_enable(); goto err_fault; } @@ -496,6 +498,7 @@ static long __bpf_get_stack(struct pt_re /* trace/ips should not be dereferenced after this point */ if (may_fault) rcu_read_unlock(); + preempt_enable(); if (user_build_id) stack_map_get_build_id_offset(buf, trace_nr, user, may_fault);