From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796107091; b=w5sJ3Q/FaZ2WUfLmxu4/+pGyCF4m9KyYiO/FJpYhOocPLex+6tJW9g/iGWMlDZqP0HS4 L8zGd8lW1zwkCQZZ6/MZygb72dWy4behBssz9bhltFVjrKL064kL0Gz9X5HHM3vwEXnFC kMm9WD1WlgsMq/r6TK5Bj8Fth/I8iruv4eDtYNZGXLMNOARuqMO8DiP5KwYyJIZxUrzkA 5JslooFVSpoYX/ElJ+ZRiwHDOR7uVhoH2GQ3NGvrVh4IH9r/pa7xoQ93My9HqVvQs/VS7 7KiIRuKyVnLaSFAFQZ0Bws58XR403RVdS6gnll79+k+93Rq1l04Yow7lvk8rBUiMS4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796107091; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=qqnn03PRj9jhzKmrA18gEPA5Zlhw5CQUwLdFS3D/tY8=; b=lQ36Qlu7fgrJOp82KKx5V/gQwFX0iJyTPQvA6ZFDkrkyaXIY9XZlx+HFiK1MdNAPT3yN d0oysY5kbx5WXh1kGP2GGQP75py4tmACyJbdpWUJkCgByd98KQ3P6nvgRTL+EJjzd4hFj t/rjCFEtonACWQpx29po0bn0txxudORILfcDweFRBf/uutOothX4l1l53DTuX0Lcd6MkC PfAye1WkNjY1yFCZKnytKj6F5DcyxfpLEnO5LOJBpvFLuFEbPemLppganhnq9g8Csj5cE V50Pc7EpLGpDr/YxwXuhImzOUEEjUVImS+nY3cQw5mVzh+eJ3cd16erF/ZoGDmRUDYg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=nebusec.ai; spf=pass smtp.mailfrom=nebusec.ai; dkim=pass header.d=nebusec.ai header.i=@nebusec.ai header.a=rsa-sha256 header.s=google header.b=QX73O4mD; arc=none smtp.remote-ip=209.85.214.172 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46075606A320 for ; Tue, 1 Dec 2026 06:38:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 46075606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=nebusec.ai Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nebusec.ai Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nebusec.ai header.i=@nebusec.ai header.a=rsa-sha256 header.s=google header.b=QX73O4mD Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2f0634667aaso44232375ad.0 for ; Mon, 30 Nov 2026 22:38:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nebusec.ai; s=google; t=1796107090; x=1796711890; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=qqnn03PRj9jhzKmrA18gEPA5Zlhw5CQUwLdFS3D/tY8=; b=QX73O4mDzz8q0DE7aW0gqMYn6uldi0lJ2c/fephiTetM/BnpiUleGWJZMaimqCgVzM 4yX5S0NoCou+bJuvC+SdfZexFlWXeJ91T7rxGm9LSugG6dRWCl4uJDzElvJtDdDgk/cz hokN6nrDIRif0tAMyo9XMcwYCoEDgZYh0lcZYLTx8aZYF/6L5yJJOgOpxguO9uukEsuT ncvktLEpK5xXf+T8NbIwS9mge9ReJQJwoaD5UxGPxCMym6HOeNfGxSqW7HQ3mpINJcyR 1TIKOweX3IdXRvkxWmgzXpKzt/PXLzHg4L+6su3bseEg0SgSTysysjh/KVmAftkf/RKH usZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796107090; x=1796711890; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qqnn03PRj9jhzKmrA18gEPA5Zlhw5CQUwLdFS3D/tY8=; b=qq2P/gqF5Jb58VPR5Vejrj+syXguCdfwZYzJv/GwyCq4myQ9on0xvMei0oL18zEcWk joGSl1KyfjJJAK3cSbjoOpVFjxYxHcuq7KdBRJrtcKTyaFWFmqjBZBb0uk2WiUcvskbn HtEMzDxBGtzlkwixyq93uHKrIYgez5E3iOmMcebq1n5EB7hPclNqaNvZKT8tjCsDYRhe soVorDF5EsRNfMIq1GM5CTaGqMANK0so0vm/kif1JxZDlBAmEY7L0ujx/E6/pEgwcpHr FyzjkPmIX8M4NGCX/eKjc+6qpZKcaQL02B9yIuVMZhEJysbmYkF2w9wM1Y0ub2s0trKa nvmQ== X-Forwarded-Encrypted: i=1; AL+hErF9DqLJUW9kdZUEWW7CaSRK0I8hHpy+HHDW9j+fvNOfTqnCuoSobZoKUrJ1YanvkUG2nuVBt7FQS9Q=@vger.kernel.org X-Gm-Message-State: APuU4Z/IZ4RD/1gMwviYlfrzQNcO1h06H5TSFxv1Bu34Bw5Jl+9zAcqp wTSJ1rLJ6PSe0nQgGXOEbMvPSNftPrQj61bCe5iFUCILHIhwu7bUgyksyAdNDd1iTXE7 X-Gm-Gg: AZFdTZe20C1y8P8K0usG7LvZ4Sn3tqm8Gonfaz+8gjZ9x7yLClPF5yx7YQrC0hb/hMC +18HKrmzPQqLy3GUCzrUADn3CfjtGh6KGOD6taQoA2xO3tj07LCdFAu8P8NiKUrQsLoGO/yQoP3 TZrw8zR5/lspu409++VYuw7BCOpDnK7m7b/w2RkfqbydCXHdjBcnIyRlPV2D1+y8Yzd1H59UAmN M28u4vCTmbReHdjUfvNdVlg+wcG99FMOoGBsTV9lw6OMmVHdvKT4MLurXNtaFc8LenEobc3iJ9p xnoemSDOLMS6iE6xHK3GibHNcV3hdaVfa2mWEd/U8FUjIUqPjn02JiDqcFsYstX2OsAPioBBh5u rxkNjSuD+XwnnkKm4OUR92vWyouNI/VBqZYNpdjydPEJ37N/WM5ysuUzKNUC46Krov+Q+6tN4zQ 3HO9ie7OoCd0pscHFqZabBufJTszaVcV0QdlBessP9Rcl6jwSQNHAPyp1niP6urE+s/dht7ksfW p+aXc6EGlzcrkyoEQBHhluQpSRGN29OQL76iQc7P7cBVC42E7BAQlXoH9O4hQzHJwQ2+mvh4JWM 2ARuwp51xx1MkFQsUatNx2vQJa25wvYb/USl0DRXlPVwTcKJNLtBY0G9VJqXqfHy+qwLt1awioG 7UA/DnnH4h4GatLej/BCI X-Received: by 2002:a17:903:4b48:b0:2f1:2a68:11bd with SMTP id d9443c01a7336-2f12a681c6cmr83974045ad.6.1796107090418; Mon, 30 Nov 2026 22:38:10 -0800 (PST) Received: from 24c857d0c3be.. ([122.51.212.64]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f08164dacbsm146250455ad.68.2026.11.30.22.38.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 22:38:09 -0800 (PST) From: Zihan Xi 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 Message-ID: X-Mailer: git-send-email 2.47.3 X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 #include #include #include #include #include #include #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 #include #include #include #include #include #include #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 #include #include #include #include #include #include #include #include #include #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 #include #include #include #include #include #include #include #include #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