Skip to contentSkip to conversation

bpf: clear stale IPv4 options after LWT encapsulation

PATCH Superseded Weiming Shi 2 participants 2 messages Sep 15

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: clear stale IPv4 options after LWT encapsulation

From: Xiang Mei <[email protected]>

bpf_lwt_push_ip_encap() rebases the network header after prepending an IP header, but leaves IPCB(skb)->opt describing the inner IPv4 header. An ingress LWT route can consequently make an ICMP error interpret an inner-header byte as an option length and copy 255 bytes into 40 bytes of stack storage. The trace decoded with scripts/decode_stacktrace.sh is:

  BUG: KASAN: stack-out-of-bounds in __ip_options_echo
  Write of size 255
  Call Trace:
   <IRQ>
   __asan_memcpy (mm/kasan/shadow.c:106)
   __ip_options_echo (net/ipv4/ip_options.c:96)
   __icmp_send (net/ipv4/icmp.c:949)
   ip_forward (net/ipv4/ip_forward.c:176)
   lwtunnel_input (net/core/lwtunnel.c:465)
   ip_rcv (net/ipv4/ip_input.c:612)
   __netif_receive_skb_one_core (net/core/dev.c:6264)
   process_backlog (net/core/dev.c:6728)
   __napi_poll (net/core/dev.c:7787)
   net_rx_action (net/core/dev.c:8007)
   handle_softirqs (kernel/softirq.c:645)
   do_softirq.part.0 (kernel/softirq.c:546)
   </IRQ>
   <TASK>
   __local_bh_enable_ip (kernel/softirq.c:473)
   __dev_queue_xmit (net/core/dev.c:4961)
   packet_sendmsg (net/packet/af_packet.c:3143)
   __sys_sendto (net/socket.c:2281)
   __x64_sys_sendto (net/socket.c:2288)
   do_syscall_64 (arch/x86/entry/syscall_64.c:84)
   entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
   </TASK>

A helper-only reset can be restored by bpf_prog_run_save_cb(), while a program without ctx->cb[] access can clone-redirect the skb before a return-only reset. Track active LWT runs and their control-block family in the BPF network context. Reset it immediately when the control block is not BPF scratch space, and after bpf_prog_run_save_cb() restores it. Preserve the state across nested runs and retain the ingress interface and L3-slave state when the protocol family changes.

Fixes 52f278774e79 ("bpf: implement BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap")
Assisted-by LLM
---
 include/linux/filter.h | 10 ++++++
 net/core/lwt_bpf.c     | 81 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
include/linux/filter.h +10 -0
index 39decde7fc730..0edd3e6ce563f 100644
@@ -848,6 +848,15 @@ struct bpf_nh_params {
 #define BPF_RI_F_CPU_MAP_INIT	BIT(2)
 #define BPF_RI_F_DEV_MAP_INIT	BIT(3)
 #define BPF_RI_F_XSK_MAP_INIT	BIT(4)
+#define BPF_RI_F_LWT_IP_ENCAP	BIT(5)
+#define BPF_RI_F_LWT_RUN	BIT(6)
+
+struct bpf_lwt_ip_encap_state {
+	int iif;
+	__be16 cb_proto;
+	bool l3slave;
+	bool cb_access;
+};
 
 struct bpf_redirect_info {
 	u64 tgt_index;
@@ -858,6 +867,7 @@ struct bpf_redirect_info {
 	enum bpf_map_type map_type;
 	struct bpf_nh_params nh;
 	u32 kern_flags;
+	struct bpf_lwt_ip_encap_state lwt_ip_encap;
 };
 
 struct bpf_net_context {
net/core/lwt_bpf.c +81 -0

118 lines not on this page · raw message

#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: incorrect SKB CB state when cb_access is false --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: incorrect SKB CB state when cb_access is false
--

commit 81b40ecbc1d9468e9497a57756458b9bd7e91f03
Author: Xiang Mei <[email protected]>

bpf: clear stale IPv4 options after LWT encapsulation

This commit addresses an out-of-bounds stack write caused by a stale IPv4 header length being interpreted as an option length. It tracks active LWT runs and resets the control block state to preserve consistency across nested runs and protocol family changes.

Quoted 4 lines
net/core/lwt_bpf.c +0 -0
@@ -80,6 +80,31 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,

[ ... ]

net/core/lwt_bpf.c static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt) · line 114

Quoted 12 lines
net/core/lwt_bpf.c +10 -0
 	bpf_compute_data_pointers(skb);
 	ret = bpf_prog_run_save_cb(lwt->prog, skb);
+	lwt_ip_encap = ri->kern_flags & BPF_RI_F_LWT_IP_ENCAP;
+	ri->kern_flags &= ~BPF_RI_F_LWT_IP_ENCAP;
+
+	if (lwt_ip_encap && ri->lwt_ip_encap.cb_access)
+		bpf_lwt_reset_ip_cb(skb, ri->lwt_ip_encap.cb_proto,
+				    ri->lwt_ip_encap.iif,
+				    ri->lwt_ip_encap.l3slave,
+				    (ret == BPF_LWT_REROUTE &&
+				     lwt->prog->type != BPF_PROG_TYPE_LWT_OUT) ||
+				    (ret == BPF_REDIRECT && can_redirect));

[Severity: High]
Does skipping the control block reset when cb_access is false cause issues if the packet is returned to the old protocol handler?

If a BPF program with cb_access set to false changes the IP encapsulation family via bpf_lwt_push_ip_encap() and then returns BPF_OK, it seems the original protocol stack could end up reading a control block formatted for the wrong protocol family. Could this result in corrupted packet delivery state, such as lost routing flags like IPSKB_L3SLAVE or reading a zeroed nhoff in IPv6?

[ ... ]

Quoted 1 line
net/core/lwt_bpf.c +0 -0
@@ -669,6 +740,16 @@ int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)

[ ... ]

net/core/lwt_bpf.c int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress) · line 751

Quoted 12 lines
net/core/lwt_bpf.c +9 -0
 		skb->protocol = htons(ETH_P_IPV6);
 	}
 
+	if (ri->kern_flags & BPF_RI_F_LWT_RUN) {
+		ri->kern_flags |= BPF_RI_F_LWT_IP_ENCAP;
+		if (!ri->lwt_ip_encap.cb_access) {
+			bpf_lwt_reset_ip_cb(skb, ri->lwt_ip_encap.cb_proto,
+					    ri->lwt_ip_encap.iif,
+					    ri->lwt_ip_encap.l3slave, true);
+			ri->lwt_ip_encap.cb_proto = skb->protocol;
+		}
+	}

[Severity: High]
Is it safe to eagerly convert the control block to the new protocol here when cb_access is false?

This appears to assume the packet will always be subsequently processed as the new protocol. Since the restoration logic in run_lwt_bpf() is completely bypassed when cb_access is false, this might leave the control block in an inconsistent layout when the packet is handled by the original protocol handler.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1