Skip to contentSkip to conversation

xskmap: reject TX-only AF_XDP sockets

PATCH Applied Linpu Yu 4 participants 4 messages May 8 – May 9

Subsystems (4)
lore
Share & tools
lore

Linpu Yu

author
#0 lore raw

PATCH v4 1/1 xskmap: reject TX-only AF_XDP sockets

XSKMAP entries are used as redirect targets for incoming XDP frames. A TX-only AF_XDP socket lacks an Rx ring and cannot handle redirected traffic, but xsk_map_update_elem() currently allows such sockets to be inserted into the map.

Redirecting packets to such a socket on the veth generic-XDP path causes a kernel crash in xsk_generic_rcv().

This became possible after xsk_is_setup_for_bpf_map() was removed from the XSKMAP update path, which allowed bound TX-only sockets to be inserted into the map.

Reject TX-only sockets during XSKMAP updates to avoid the crash. They remain fully operational for pure Tx purposes outside XSKMAP.

Fixes 968be23ceaca ("xsk: Fix possible segfault at xskmap entry insertion")

---
Changes in v4:
- retarget the patch from bpf-next to bpf
- resend after no feedback

 net/xdp/xskmap.c | 4 ++++
 1 file changed, 4 insertions(+)
net/xdp/xskmap.c +4 -0
index afa457506274..3bff346308d0 100644
@@ -184,6 +184,10 @@ static long xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
 	}
 
 	xs = (struct xdp_sock *)sock->sk;
+	if (!READ_ONCE(xs->rx)) {
+		sockfd_put(sock);
+		return -ENOBUFS;
+	}
 
 	map_entry = &m->xsk_map[i];
 	node = xsk_map_node_alloc(m, map_entry);
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Returning -ENOBUFS when rejecting a TX-only AF_XDP socket during map insertion is semantically…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Returning -ENOBUFS when rejecting a TX-only AF_XDP socket during map insertion is semantically incorrect and deviates from previous behavior. --

commit cc939da4429db85f32555fd19f4546a51d44ca3d
Author: Linpu Yu <[email protected]>

xskmap: reject TX-only AF_XDP sockets

This commit rejects TX-only AF_XDP sockets during XSKMAP updates to prevent a kernel crash in xsk_generic_rcv(). It checks if the socket lacks an Rx ring and returns an error before inserting it into the map.

net/xdp/xskmap.c static long xsk_map_update_elem(struct bpf_map *map, void *key, void *value, · line 189

Quoted 11 lines
net/xdp/xskmap.c +3 -0
index afa457506274c..3bff346308d0f 100644
@@ -184,6 +184,10 @@ static long xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
 	}
 
 	xs = (struct xdp_sock *)sock->sk;
+	if (!READ_ONCE(xs->rx)) {
+		sockfd_put(sock);
+		return -ENOBUFS;

Should this return -EOPNOTSUPP or -EINVAL instead?

Earlier in this same function, -EOPNOTSUPP is returned when the socket family is not PF_XDP. Returning -ENOBUFS might be semantically misleading for userspace since the socket intentionally lacks an Rx ring by design, rather than experiencing a temporary buffer shortage.

Quoted 4 lines
net/xdp/xskmap.c +1 -0
+	}
 
 	map_entry = &m->xsk_map[i];
 	node = xsk_map_node_alloc(m, map_entry);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v4 1/1
#2 lore raw
In reply to Linpu Yu
Quoted 16 lines · Linpu Yu

XSKMAP entries are used as redirect targets for incoming XDP frames. A TX-only AF_XDP socket lacks an Rx ring and cannot handle redirected traffic, but xsk_map_update_elem() currently allows such sockets to be inserted into the map.

Redirecting packets to such a socket on the veth generic-XDP path causes a kernel crash in xsk_generic_rcv().

This became possible after xsk_is_setup_for_bpf_map() was removed from the XSKMAP update path, which allowed bound TX-only sockets to be inserted into the map.

Reject TX-only sockets during XSKMAP updates to avoid the crash. They remain fully operational for pure Tx purposes outside XSKMAP.

Quoted 6 lines
Fixes 968be23ceaca ("xsk: Fix possible segfault at xskmap entry insertion")

Even though AI suggested using EOPNOTSUPP instead, prior to this thread, I pointed out ENOBUFS makes more sense because it is identical to how xsk_recvmsg() detects the failure of the rx ring. If needed, we can cook a follow-up patch to replace that in __xsk_recvmsg().

It looks good to me:

Thanks,
Jason

Quoted 25 lines

---
Changes in v4:
- retarget the patch from bpf-next to bpf
- resend after no feedback

 net/xdp/xskmap.c | 4 ++++
 1 file changed, 4 insertions(+)
net/xdp/xskmap.c +4 -1
index afa457506274..3bff346308d0 100644
@@ -184,6 +184,10 @@ static long xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
        }
 
        xs = (struct xdp_sock *)sock->sk;
+       if (!READ_ONCE(xs->rx)) {
+               sockfd_put(sock);
+               return -ENOBUFS;
+       }
 
        map_entry = &m->xsk_map[i];
        node = xsk_map_node_alloc(m, map_entry);
--

2.54.0

#3 lore raw
In reply to Linpu Yu
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Fri, 8 May 2026 22:43:43 +0800 you wrote:

XSKMAP entries are used as redirect targets for incoming XDP frames. A TX-only AF_XDP socket lacks an Rx ring and cannot handle redirected traffic, but xsk_map_update_elem() currently allows such sockets to be inserted into the map.

Redirecting packets to such a socket on the veth generic-XDP path causes a kernel crash in xsk_generic_rcv().

[...]

Here is the summary with links:
- [bpf,v4] xskmap: reject TX-only AF_XDP sockets

    https://git.kernel.org/bpf/bpf/c/bf6d507f7e3c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html