From: Zihan Xi <zihanx@nebusec.ai>
To: netdev@vger.kernel.org, linux-doc@vger.kernel.org
Cc: davem@davemloft.net, edumazet@kernel.org, pabeni@redhat.com,
willemb@google.com, horms@kernel.org, rjethwani@purestorage.com,
zirajs7@gmail.com, leitao@debian.org, brauner@kernel.org,
jiayuan.chen@shopee.com, runyu.xiao@seu.edu.cn,
tom@herbertland.com, corbet@lwn.net, skhan@linuxfoundation.org,
rdunlap@infradead.org, vega@nebusec.ai, zihanx@nebusec.ai
Subject: [PATCH net v2 0/2] kcm: bound message memory by send buffer
Date: Tue, 1 Dec 2026 06:37:59 +0000 [thread overview]
Message-ID: <cover.1796043121.git.zihanx@nebusec.ai> (raw)
Hi Linux kernel maintainers,
We found that AF_KCM message construction in net/kcm/kcmsock.c can
under-account memory retained by fragmented messages. A non-root process
can reach it without a TCP attachment or BPF program. The final v2 source
built successfully, and targeted tests completed under KVM; results follow.
An LLM was used to help create this patch.
This bug is tracked at: https://bugtracker.nebusec.ai/f/12034 (12034)
Bug details:
kcm_sendmsg() accounts message data, but fragmented messages also retain
page backing and skb metadata. Small fragments can therefore make a socket
retain more memory than the message-data charge accounts for.
The patch charges retained folios and skb metadata to the per-socket send
budget and releases the charge when messages are sent or discarded. It
also handles SO_SNDBUF changes while a message is open, rechecks space
after pushing queued messages, and lets splice coalesce at the skb fragment
limit before allocating another skb.
Page backing is charged conservatively by folio size, even when fragments
may share a folio. A message can consequently hit EMSGSIZE before its
payload alone reaches SO_SNDBUF. This is per-socket accounting; it does not
add KCM-wide aggregate or memcg accounting.
Reproducer:
Run the PoC as guest `test_user` (UID 1001) in the default user and network
namespaces, on an unpatched baseline and on the final v2 kernel. On the
unpatched baseline, with requested SO_SNDBUF=4096 (effective 8192), the
64 MiB DGRAM send succeeded; the following one-byte send returned EAGAIN.
On the final v2 kernel, both sends returned EMSGSIZE. No KASAN, BUG, Oops,
or kernel panic messages were found in the post-test dmesg.
In a 2-vCPU, 2-GB x86 QEMU guest with CONFIG_AF_KCM enabled, save the
inline PoC as `/tmp/kcm-mini-poc.c`. From a root shell, switch to
`test_user` (UID 1001):
```
su -s /bin/sh test_user
```
Then, in the `test_user` shell, compile and run:
```
cd /tmp
id -u
gcc -O2 -Wall -Wextra -o kcm-mini-poc kcm-mini-poc.c
./kcm-mini-poc
```
`id -u` should print `1001`.
Unpatched-baseline output:
```
requested_sndbuf=4096 actual_sndbuf=8192 payload=67108864 first_send=67108864
second_send=-1 (Resource temporarily unavailable)
```
Final v2 output:
```
requested_sndbuf=4096 actual_sndbuf=8192 payload=67108864 first_send=-1 (Message too long)
second_send=-1 (Message too long)
```
The same PoC was also run on the final source with larger requested
SO_SNDBUF values. At 8192 and 16384, both sends returned EMSGSIZE. At
32768 (effective 65536), the 64 MiB send returned EMSGSIZE and the one-byte
follow-up succeeded. Follow-up results varied across the tested buffer
values; these observations do not establish the underlying mechanism. The
4096-byte run did not have a successful follow-up. Results:
```
requested actual first send follow-up
4096 8192 EMSGSIZE EMSGSIZE
8192 16384 EMSGSIZE EMSGSIZE
16384 32768 EMSGSIZE EMSGSIZE
32768 65536 EMSGSIZE 1
```
The matrix ran as UID 1001 on the final source kernel under KVM. In a
`test_user` shell, compile the parameterized source and run these requested
buffer sizes:
```
gcc -O2 -Wall -Wextra -o kcm-mini-poc-sndbuf kcm-mini-poc-sndbuf.c
for sndbuf in 4096 8192 16384 32768; do
./kcm-mini-poc-sndbuf "$sndbuf"
done
```
The parameterized source is included below.
------BEGIN kcm-mini-poc-sndbuf.c------
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
#ifndef AF_KCM
#define AF_KCM 41
#endif
#ifndef KCMPROTO_CONNECTED
#define KCMPROTO_CONNECTED 0
#endif
int main(int argc, char **argv)
{
const size_t size = 64 * 1024 * 1024;
int sndbuf = argc > 1 ? atoi(argv[1]) : 4096;
int actual, fd;
char byte = 'x';
char *buf;
ssize_t sent;
fd = socket(AF_KCM, SOCK_DGRAM, KCMPROTO_CONNECTED);
if (fd < 0) {
perror("socket(AF_KCM)");
return 1;
}
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
perror("setsockopt(SO_SNDBUF)");
return 1;
}
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &actual,
&(socklen_t){ sizeof(actual) })) {
perror("getsockopt(SO_SNDBUF)");
return 1;
}
buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (buf == MAP_FAILED) {
perror("mmap");
return 1;
}
memset(buf, 'x', size);
sent = send(fd, buf, size, MSG_DONTWAIT);
printf("requested_sndbuf=%d actual_sndbuf=%d payload=%zu first_send=%zd",
sndbuf, actual, size, sent);
if (sent < 0)
printf(" (%s)", strerror(errno));
putchar('\n');
errno = 0;
sent = send(fd, &byte, sizeof(byte), MSG_DONTWAIT);
printf("second_send=%zd", sent);
if (sent < 0)
printf(" (%s)", strerror(errno));
putchar('\n');
close(fd);
munmap(buf, size);
return 0;
}
------END kcm-mini-poc-sndbuf.c------
------BEGIN PoC------
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
#ifndef AF_KCM
#define AF_KCM 41
#endif
#ifndef KCMPROTO_CONNECTED
#define KCMPROTO_CONNECTED 0
#endif
int main(void)
{
const size_t size = 64 * 1024 * 1024;
int sndbuf = 4096, actual, fd;
char byte = 'x';
char *buf;
ssize_t sent;
fd = socket(AF_KCM, SOCK_DGRAM, KCMPROTO_CONNECTED);
if (fd < 0) {
perror("socket(AF_KCM)");
return 1;
}
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
perror("setsockopt(SO_SNDBUF)");
return 1;
}
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &actual,
&(socklen_t){ sizeof(actual) })) {
perror("getsockopt(SO_SNDBUF)");
return 1;
}
buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (buf == MAP_FAILED) {
perror("mmap");
return 1;
}
memset(buf, 'x', size);
sent = send(fd, buf, size, MSG_DONTWAIT);
printf("requested_sndbuf=%d actual_sndbuf=%d payload=%zu first_send=%zd",
sndbuf, actual, size, sent);
if (sent < 0)
printf(" (%s)", strerror(errno));
putchar('\n');
errno = 0;
sent = send(fd, &byte, sizeof(byte), MSG_DONTWAIT);
printf("second_send=%zd", sent);
if (sent < 0)
printf(" (%s)", strerror(errno));
putchar('\n');
close(fd);
munmap(buf, size);
return 0;
}
------END PoC--------
Additional test results:
The final v2 source passed a full bzImage build and was tested under KVM in
a 2-vCPU, 2-GB guest. For the splice-boundary sweep, root set
`net.core.max_skb_frags=1`; the probe ran as UID 1001. It vmspliced two
adjacent bytes from one page into a pipe, then issued two one-byte KCM
DGRAM splice calls, with `SPLICE_F_MORE` on the first. Requested
SO_SNDBUF values from 2200 through 3200 were tested in steps of 8. At
2528, the first splice returned 1 and the second returned EMSGSIZE. At
2536 and above, both returned 1. Representative output:
```
sndbuf_request=2528 first=1 first_errno=0 second=-1 second_errno=90
sndbuf_request=2536 first=1 first_errno=0 second=1 second_errno=0
sndbuf_request=3200 first=1 first_errno=0 second=1 second_errno=0
```
A focused before/after comparison used the same single-shot binary and
configuration on pre-coalescing source commit `3e6450cdf004` and final KCM
code commit `c3949ece3d30`. Both builds used config SHA-256
`8c874562f1ce0eb8a79c2c8304c8cbec798f02d73e6ebe3da6d7d9bb35505011`;
root set `max_skb_frags=1`, and the probe ran as UID 1001. At requested
SO_SNDBUF 2672, the first splice returned 1 on both kernels. The second
returned EMSGSIZE before the fix and 1 on the final source:
```
pre_fix: first=1 errno=0 (ok); second=-1 errno=90 (Message too long)
final: first=1 errno=0 (ok); second=1 errno=0 (ok)
```
The probe source was compiled once and run on both kernels. The sweep and
focused comparison sources are included below. Neither post-test dmesg
contained KASAN, BUG, Oops, or kernel-panic messages. These return-value
tests did not generate a crash.
Sweep probe setup: set this guest-only sysctl as root:
```
echo 1 > /proc/sys/net/core/max_skb_frags
```
Then switch to `test_user` and run:
```
su -s /bin/sh test_user
```
In that shell:
```
gcc -static -O2 -Wall -Wextra -o coalesce-boundary-poc-unprivileged \
coalesce-boundary-poc-unprivileged.c
./coalesce-boundary-poc-unprivileged
```
Focused comparison probe: after setting the same sysctl as root, switch to
`test_user` and compile once in each guest:
```
su -s /bin/sh test_user
```
In that shell, compile and run on each guest:
```
gcc -static -O2 -Wall -Wextra -o coalesce-boundary-single \
coalesce-boundary-single.c
./coalesce-boundary-single
```
------BEGIN coalesce-boundary-poc-unprivileged.c------
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/kcm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#ifndef AF_KCM
#define AF_KCM 41
#endif
#ifndef KCMPROTO_CONNECTED
#define KCMPROTO_CONNECTED 0
#endif
static void try_budget(char *page, int sndbuf)
{
struct iovec iov = { .iov_base = page, .iov_len = 2 };
int p[2] = { -1, -1 }, fd = -1;
ssize_t vm, first = -1, second = -1;
int first_errno = 0, second_errno = 0;
if (pipe(p))
goto out;
vm = vmsplice(p[1], &iov, 1, 0);
if (vm != 2)
goto out;
fd = socket(AF_KCM, SOCK_DGRAM, KCMPROTO_CONNECTED);
if (fd < 0 || setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
&sndbuf, sizeof(sndbuf)))
goto out;
errno = 0;
first = splice(p[0], NULL, fd, NULL, 1, SPLICE_F_MORE);
first_errno = errno;
if (first == 1) {
errno = 0;
second = splice(p[0], NULL, fd, NULL, 1, 0);
second_errno = errno;
}
printf("sndbuf_request=%d first=%zd first_errno=%d second=%zd second_errno=%d\n",
sndbuf, first, first_errno, second, second_errno);
out:
if (fd >= 0)
close(fd);
if (p[0] >= 0)
close(p[0]);
if (p[1] >= 0)
close(p[1]);
}
int main(void)
{
char *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
int sndbuf;
if (page == MAP_FAILED) {
perror("mmap");
return 1;
}
memset(page, 0x5a, 4096);
for (sndbuf = 2200; sndbuf <= 3200; sndbuf += 8)
try_budget(page, sndbuf);
munmap(page, 4096);
return 0;
}
------END coalesce-boundary-poc-unprivileged.c------
------BEGIN coalesce-boundary-single.c------
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/kcm.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#ifndef AF_KCM
#define AF_KCM 41
#endif
#ifndef KCMPROTO_CONNECTED
#define KCMPROTO_CONNECTED 0
#endif
int main(void)
{
char *page = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
struct iovec iov;
int p[2], fd, sndbuf = 2672;
ssize_t n, first, second;
int first_errno, second_errno;
if (page == MAP_FAILED || pipe(p))
return 1;
memset(page, 0x5a, 4096);
iov.iov_base = page;
iov.iov_len = 2;
n = vmsplice(p[1], &iov, 1, 0);
if (n != 2)
return 2;
fd = socket(AF_KCM, SOCK_DGRAM, KCMPROTO_CONNECTED);
if (fd < 0 || setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
&sndbuf, sizeof(sndbuf)))
return 3;
errno = 0;
first = splice(p[0], NULL, fd, NULL, 1, SPLICE_F_MORE);
first_errno = errno;
printf("first=%zd errno=%d (%s)\n", first, first_errno,
first < 0 ? strerror(first_errno) : "ok");
fflush(stdout);
if (first == 1) {
errno = 0;
second = splice(p[0], NULL, fd, NULL, 1, 0);
second_errno = errno;
printf("second=%zd errno=%d (%s)\n", second, second_errno,
second < 0 ? strerror(second_errno) : "ok");
}
close(fd);
close(p[0]);
close(p[1]);
return 0;
}
------END coalesce-boundary-single.c------
Changes in v2:
Link: https://lore.kernel.org/all/cover.1795676623.git.zihanx@nebusec.ai/
- Account retained folio backing and skb metadata in addition to
message data, and pair the ledger across send, rollback, and close.
- Use head->len for payload length, handle SO_SNDBUF shrink and
recheck space after kcm_push().
- Let splice coalesce at the frag limit before allocating a child skb.
- Document the transmit-side SO_SNDBUF budget and EMSGSIZE behavior.
Best regards,
Zihan Xi
Zihan Xi (2):
net: kcm: account retained message memory
net: kcm: document transmit memory budget
Documentation/networking/kcm.rst | 8 +
include/net/kcm.h | 3 +
net/kcm/kcmsock.c | 494 +++++++++++++++++++++++++++----
3 files changed, 453 insertions(+), 52 deletions(-)
base-commit: e9e45fcb6d39bee9325e6a7dca72bd14e82760f3
--
2.43.0
next reply other threads:[~2026-12-01 6:38 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 6:37 Zihan Xi [this message]
2026-12-01 6:38 ` [PATCH net v2 1/2] net: kcm: account retained message memory Zihan Xi
2026-12-01 6:38 ` [PATCH net v2 2/2] net: kcm: document transmit memory budget Zihan Xi
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.1796043121.git.zihanx@nebusec.ai \
--to=zihanx@nebusec.ai \
--cc=brauner@kernel.org \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=jiayuan.chen@shopee.com \
--cc=leitao@debian.org \
--cc=linux-doc@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rdunlap@infradead.org \
--cc=rjethwani@purestorage.com \
--cc=runyu.xiao@seu.edu.cn \
--cc=skhan@linuxfoundation.org \
--cc=tom@herbertland.com \
--cc=vega@nebusec.ai \
--cc=willemb@google.com \
--cc=zirajs7@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox