From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796107091; b=jC1cwDCkOWIrBkqmM1bXr0WYLyruI0gWoergedWxldyr3wbwy3zqrdJRG3bt80BbDcfs c/9yUJtK4/zdNzMleYZCeshfeDZELvJ9hCriEk7WMYSJsQuwwwrEfGVGYOE0OAFn/ipOd 1UaMCEmISFZumu7CnNs/G780BFPCe8Jj+LrCN4duCFcv342tkdU/PUKZOpl5F47cV3BSw g+eYnsGST8gDJ98B+QsZQgSo5tVfpq+ljJ+OG9m9ccIHEW8mC0t6pSKYJKPFH2/HuPRNL nb7SB36tUUBQ5GwQ/dt3i+1loZm02gbbI9EGPHjgbnDkbV9Pkxxs/bhXLUZCcpMYEjQ== 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-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=rvDkVGGkAZ4pCkrCjTQsaQ2A9GSVwM7BOZ+VR6isLgjLczI7/7QnZdh5dVrteOEEIPMv lyb8+e4w0jAiGIq8kdsxgOVp4Dg4M30dc3+t0ErwQ8cGqi1ySRGtBspuzXuHqRzhwhpZl dgv9CuIEkIWPv9POH/RQN/n/43JcztBFsmTC0MfQEsAKvHj4SAukita5YbANc2mW6mAU4 s6bAZVlx59ZRE+Ck5LbJYvLQ9dONFum4Nq0/eU8JQ2IStPGOgMqiuJfH0G6wHqimYcP+l ye50WK6GHfsEUSkRmz2OCmIh1vpPBPE56IdDdoVbWqwneTo1fpTsBJOYfohWEljK9vg== 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.179 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 362C9606A31A for ; Tue, 1 Dec 2026 06:38:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 362C9606A31A 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, unprotected) header.d=nebusec.ai header.i=@nebusec.ai header.a=rsa-sha256 header.s=google header.b=QX73O4mD Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2efddf749e5so45184035ad.1 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=l7RzuWdq682AmZePyj2vSOIuRjtfncet2uUfN2t//LJGMjN7vIG73CPwckQPIIT+WG GsZRAbNjQbrxsmv5AGs1ats8HINiOnu87X9GgLYYAertDnFSG6ZEjte/rvP13e5VBHoh hJMyn/yvs8mdrjA+MlWDARev3XsoAItbslEtFFi61iNPP1e0lhytcFiAww2tyzTlu6/a 2JHUOtF598MV1RW9C+O0mtrag5vpV2bBtIl1roMLTJ80lmUc+CshbE2qWee2tXuBlFQU vYDewf1J/sy8v6WUUo+NWqYeCLvZTJb4X3eIx9CCzSfj08/PgX/AzeqnXx9TeLhUuVl0 0Q6A== X-Gm-Message-State: APuU4Z8kdWiCTtTF49IYpYw4UvWKSYfJSMvaepkpuWim9X1xFDw3LsO7 Oo9iYCT15wGIObYqbmjntrZNoDFY1e1aqx2hVUJ5+SHZNjpYLbNtcyfCQOpEW2rjK0EZV3hdG8N YpBiY0mRb X-Gm-Gg: AZFdTZebegr0dLDInM4tryVPJ52s7Z9Hw/kVuj9j4gDROblDSc69f71sRPfY/da4zVa inrXzmn/CFEn79DcAkqdxHJrqV4QD7n3Z3Wi+vIZF9oRMbQqMl+wGsG1cMr3VBbMEN3tgl1X7Zk IQ7IhHufc2qmcekhKcl+Qc1BoWTTvS4XU+qiNy9v2TclxhtSi0bqGQeOtGBJfzQ5FB3bhzebMKz nn7Q3Io2+a6JI/2SHVLNxeKkzqRZxetFpGzxo2BiDWu58yhfIBr51TYP/sI5Q30jKsk4zI/1y9V hM61e5FwmyNKo+8J7oRzRK3Uw6E+6NMNMAmpBbnFmTtWZ5cd0Y05y1RnwUfbJ/CtpIY8Tv/0kow mHdr56QzdEA8cmL08LbSDco3zv+k7bz4QXNxYlJouilm+t7d6nAGNkOSX4iRmtWfFIwHtSfo3VR nlS1bBPXKRZ1EPt7BqY5l+/QxVDZ7e0hdAPZQKZpaOQCrWvqoAl4jMTNTl8XjbtJiUEqSJKjiLG U41ESubcmam4/K6JLvWt+O4rG9MzPPMGCyVrvlG82JaNnY3psU9bMpb0HN6I+QCleve9frXWxEQ ow/aisVpZc3+FM+ykfviNs/4oopu5ZDjjMtol+zck+pqGscPckvoNiFpdt+mIpx2ZciqxxY4Pg5 KGvN8BnyLDSs/Ud5LHzMA 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: netdev@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