From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383631; b=J9ycFJacafdHUeUB7MIdDsPtCPEkzi/ciHtfAh/RQTGHuSJo2eNbnA94/glUnYP5w3+Y fgVS54oh93svKoP0Eqi9Qk4sRGtfLyoKAZ4cS6iPGs4thXMGxJsI5sBmQ6MHJpYp16vb+ mWvW5xc0BHR0XfCb1L9fA0mjd9wSwoTBw5RL6zmsh5l66qL8xP3ppSaZomaZN6lzzAj2c O2M1xTFPbzEC43qf4fRAPyk7b0bM1iTI0wzZnfmmV17uAWtvUGFeAUBODCHgEFd6kA6bC eFOV1WeRt1aHZvS4XqUse4CBSVs1yhZOndIYHiSDXfuPAjVoq3rAIrf+m8SE0s4jeaA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383631; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=9rb5rrVzLmNWZPi2PcsHnzyoSIz2oKwCBSkr0lvguPU=; b=pW4pSltJuziuhO5STYc9ThwWUQl5BMIlB39eBHzt1ugWK/ypNmnqxTrEhpPXnQhjA70B 7KnrTUePAeXESRrb5MwteenIvZo1tbOq9d6SQiBiUC33DeyCoychv1R0GKGe7FIV1WF59 EwRuyepzIsU36CmgY1d2C/CcwoA8eqXIhQ5991rQWcdHJ0FrPzGqGcTah+5DBKtY5g51C J5u1lJoX9NtoHQiOMNEGdV0nPv+c4xRAVfvfWP+GVD9Ou6lAYDV+yT9wO4KrfmrOin5+C zPurlLK4aqd47Go1tOHEJt/uuV4Qll7XZ+U+79RXJ1RUeS/ENHM8Cb4XhPUav0kCSQA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=i2s+nfid; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A792606A322; Fri, 4 Dec 2026 11:27:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=i2s+nfid Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54D891F000C2; Fri, 4 Dec 2026 11:27:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383630; bh=9rb5rrVzLmNWZPi2PcsHnzyoSIz2oKwCBSkr0lvguPU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=i2s+nfidHDSJ9mWpdZH6qnOIZzVjxHWOg+6DZ5pCzoK74A3mS62C425ZpeZUETnBs Brv3VS6VWYjz7engRCiN/ExwsAw0s+E1ROHIV6jlBpiOPR0xA7aW+iD45f0cWxFVs9 k/pktaXUuQhIGo1XynBQDIKNF0pm9YRcl4aizXkc= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.12 304/555] bpf: Fail loads from a network header that was not set Date: Fri, 4 Dec 2026 19:05:47 +0900 Message-ID: <20261204100620.971570729@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexei Starovoitov [ Upstream commit 766907cdaeefb57faddc5d09a49bdd4ce20a6272 ] skb->network_header is 0 until something sets it. Loads at SKF_NET_OFF and bpf_skb_load_bytes_relative() start at skb->head then, and read headroom that nobody has written. qrtr and AF_XDP in copy mode make such skbs. Fail the load, like for the MAC header. A network header at skb->head counts as set when skb->data or the MAC header is there too. CAN and 6LoWPAN make such skbs. Fixes: 2e07fa9cd3ba ("[SK_BUFF]: Use offsets for skb->{mac,network,transport}_header on 64bit architectures") Fixes: 0f5d82f187e1 ("net/filter: Permit reading NET in load_bytes_relative when MAC not set") Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261125032938.670542-40-alexei.starovoitov@gmail.com Signed-off-by: Sasha Levin --- include/linux/filter.h | 9 +++++++++ kernel/bpf/core.c | 2 ++ net/core/filter.c | 5 ++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index 8aeb073792c39..87fcbb14c8fa0 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -925,6 +925,15 @@ static inline void bpf_restore_data_end( cb->data_end = saved_data_end; } +/* + * If nothing has set the header, it points to headroom that was not written. + * The packet starts at skb->head when skb->data or the MAC header is there. + */ +static inline bool bpf_skb_has_network_header(const struct sk_buff *skb) +{ + return skb->network_header || !skb->mac_header || !skb_headroom(skb); +} + static inline u8 *bpf_skb_cb(const struct sk_buff *skb) { /* eBPF programs may read/write skb->cb[] area to transfer meta diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index c6b2b43b843cb..d478e8e298a29 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -78,6 +78,8 @@ void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, uns u8 *ptr = NULL; if (k >= SKF_NET_OFF) { + if (unlikely(!bpf_skb_has_network_header(skb))) + return NULL; ptr = skb_network_header(skb) + k - SKF_NET_OFF; } else if (k >= SKF_LL_OFF) { if (unlikely(!skb_mac_header_was_set(skb))) diff --git a/net/core/filter.c b/net/core/filter.c index 659b8e64591c1..1575424d4ca65 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -224,7 +224,8 @@ static int bpf_skb_load_helper_convert_offset(const struct sk_buff *skb, int off return offset; if (offset >= SKF_NET_OFF) - return offset - SKF_NET_OFF + skb_network_offset(skb); + return bpf_skb_has_network_header(skb) ? + offset - SKF_NET_OFF + skb_network_offset(skb) : INT_MIN; if (offset >= SKF_LL_OFF && skb_mac_header_was_set(skb)) return offset - SKF_LL_OFF + skb_mac_offset(skb); @@ -1867,6 +1868,8 @@ BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb, start = skb_mac_header(skb); break; case BPF_HDR_START_NET: + if (unlikely(!bpf_skb_has_network_header(skb))) + goto err_clear; start = skb_network_header(skb); break; default: -- 2.53.0