From nobody Tue Nov 17 17:05:28 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jTWMF1yCqz71Rc7 for ; Tue, 17 Nov 2026 17:05:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jTWMD6jTzz4HRQ for ; Tue, 17 Nov 2026 17:05:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794935129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/By/aAnPQXpYFIx6Y2ZsHIdSmwVRmTv+jCOB7OoRiJ8=; b=Xg5KFk07RneBovTh90zRQ75e/zfE6/rt+GZq0iH/YNCdofkkC1+Ff/0n3aOf8hofkiNs63 SgQHH1CJy/MCWgdwNtD9UKeQ0j+YuWBwamPPimTV0kSiMtCItbsE9zNdNd2sZilXODDLEE CntG+dicLCRTITv1RpyiGWSu5GB8CRutIL0btWC5nK1icJw4Oh7R7HAa+CerSHWzBnvzeW TBSFTgcaxIhFTSfdSl5TfaedY8yjOpQ61tRgC+DtCqs0fcz1VkG6ApAUxXII5uB5ls/szd qOd3f19ajWoV9qpsZykuBpnelfmB3UrKirJ8GvUpyVc4IyvzUPjtDSzXwUXyhg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794935129; b=dSintq/y3fblbry2MzXymFGAv5IcGwQsuiNjJ/4J4kTY2wgGswlyFbuherpBGHDWZZrOfG EwdQE25GiJlNODa1xxNfhzr0L9ZrCA5UU/eG+cHH2TXi+n9EVyoVy0/Cn0U1mlPKiZuLux EKk5m27R0bu7P3GqtgyrhFDTCXVNt5LpHcPw4gGcMCoEqpDtv4ORH1TwYBagn2f+40bklN oiNPZykW4HIlYDQ4fuvVIo1MouIuvYNR6zg37YOHlD1gPU/KvbKe3GGsckJqGsIzYJefnl QU9KsHrwSKCuBHIiIK9JF/+tfiEui+DU6Nync179oiD6+o3kefoJT856X3dqXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794935129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/By/aAnPQXpYFIx6Y2ZsHIdSmwVRmTv+jCOB7OoRiJ8=; b=MhLGN7oTQQUY9Hmsc6qMTfanowc5WVgaKOLklvOty1biV4B/bqhNDMtkZ6bETEw+yYzGSx goy6hbyZ1IkYML+O4gdLXZloupAa4YYnvxbXuZjB+jycGR50NetLPaRzPIKhOekzNYxElC gzuCFm71Ul+etJqPFh9ira6PWvV7/aabrO82IlFhiF4BHS/6e7Wlj9/3bo7+vTXkvKD1wm iGa0CxubbTPr8r172xpcNIuRPuLw/E4Z2LwxCxKnRwZt/bCmihWIxf4XQRW6gWVfpmpiD+ ufPprmKg38a3W7CNMRf+QtHYPAc42UM1TGQrX81J8pwEJhznwBQ942/iTa6Liw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jTWMD6MY2zrmK for ; Tue, 17 Nov 2026 17:05:28 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1f7c8 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 17 Nov 2026 17:05:28 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: c1eacc1471ad - releng/14.5 - tcp: Handle read-only mbufs in tcp_pulloutofband() List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.5 X-Git-Reftype: branch X-Git-Commit: c1eacc1471ad6db6f99016c225f093bd1c1f582a Auto-Submitted: auto-generated Date: Tue, 17 Nov 2026 17:05:28 +0000 Message-Id: <6afc8958.1f7c8.d30e52f@gitrepo.freebsd.org> The branch releng/14.5 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=c1eacc1471ad6db6f99016c225f093bd1c1f582a commit c1eacc1471ad6db6f99016c225f093bd1c1f582a Author: Mark Johnston AuthorDate: 2026-11-14 18:52:06 +0000 Commit: Mark Johnston CommitDate: 2026-11-16 21:17:13 +0000 tcp: Handle read-only mbufs in tcp_pulloutofband() tcp_pulloutofband() is used to implement the urgent flag: it pulls a byte out of the data stream, to be returned to the recevier when MSG_OOB is specified. That function extracts the byte assuming that the mbuf is safe to modify, which may not be the case, e.g., when transmitting file data with sendfile(). This flaw can be exploited by a local user to obtain root privileges. Generalize this subroutine so that it handles read-only mbufs as well. Add some regression tests. Approved by: so Security: FreeBSD-SA-26:93.tcp Security: CVE-2026-127748 Reported by: OpenAI Security Research Team Reviewed by: tuexen Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D61411 --- sys/netinet/tcp_input.c | 94 ++++++++------ sys/netinet/tcp_var.h | 2 - tests/sys/netinet/Makefile | 3 +- tests/sys/netinet/tcp_oob.c | 303 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 363 insertions(+), 39 deletions(-) diff --git a/sys/netinet/tcp_input.c b/sys/netinet/tcp_input.c index 5ff4597abe6c..924cad883b74 100644 --- a/sys/netinet/tcp_input.c +++ b/sys/netinet/tcp_input.c @@ -1512,6 +1512,62 @@ tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so, return (newsize); } +/* + * Pull out of band byte out of a segment so + * it doesn't appear in the user's data queue. + * It is still reflected in the segment length for + * sequencing purposes. + */ +static int +tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, + int off) +{ + int cnt = off + th->th_urp - 1; + + while (cnt >= 0) { + if (m->m_len > cnt) { + struct tcpcb *tp = sototcpcb(so); + char ub; + + INP_WLOCK_ASSERT(tptoinpcb(tp)); + + m_copydata(m, cnt, 1, &ub); + if (cnt == 0) { + m->m_data++; + m->m_len--; + } else if (m->m_len - 1 == cnt) { + m->m_len--; + } else if (M_WRITABLE(m)) { + char *cp; + + cp = mtodo(m, cnt); + memmove(cp, cp + 1, m->m_len - cnt - 1); + m->m_len--; + } else { + struct mbuf *n; + + n = m_copym(m, cnt + 1, m->m_len - cnt - 1, + M_NOWAIT); + if (n == NULL) + return (ENOBUFS); + n->m_next = m->m_next; + m->m_next = n; + m->m_len = cnt; + } + tp->t_iobc = ub; + tp->t_oobflags |= TCPOOB_HAVEDATA; + if ((m->m_flags & M_PKTHDR) != 0) + m->m_pkthdr.len--; + return (0); + } + cnt -= m->m_len; + m = m->m_next; + if (m == NULL) + break; + } + panic("tcp_pulloutofband"); +} + int tcp_input(struct mbuf **mp, int *offp, int proto) { @@ -3177,7 +3233,8 @@ step6: if (th->th_urp <= (uint32_t)tlen && !(so->so_options & SO_OOBINLINE)) { /* hdr drop is delayed */ - tcp_pulloutofband(so, th, m, drop_hdrlen); + if (tcp_pulloutofband(so, th, m, drop_hdrlen) != 0) + goto drop; } } else { /* @@ -3625,41 +3682,6 @@ tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) } } -/* - * Pull out of band byte out of a segment so - * it doesn't appear in the user's data queue. - * It is still reflected in the segment length for - * sequencing purposes. - */ -void -tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, - int off) -{ - int cnt = off + th->th_urp - 1; - - while (cnt >= 0) { - if (m->m_len > cnt) { - char *cp = mtod(m, caddr_t) + cnt; - struct tcpcb *tp = sototcpcb(so); - - INP_WLOCK_ASSERT(tptoinpcb(tp)); - - tp->t_iobc = *cp; - tp->t_oobflags |= TCPOOB_HAVEDATA; - bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); - m->m_len--; - if (m->m_flags & M_PKTHDR) - m->m_pkthdr.len--; - return; - } - cnt -= m->m_len; - m = m->m_next; - if (m == NULL) - break; - } - panic("tcp_pulloutofband"); -} - /* * Collect new round-trip time estimate * and update averages and current timeout. diff --git a/sys/netinet/tcp_var.h b/sys/netinet/tcp_var.h index 1196e8d4ef0e..8aefd58c1c2c 100644 --- a/sys/netinet/tcp_var.h +++ b/sys/netinet/tcp_var.h @@ -1425,8 +1425,6 @@ void tcp_reass_global_init(void); void tcp_reass_flush(struct tcpcb *); void tcp_dooptions(struct tcpopt *, u_char *, int, int); void tcp_dropwithreset(struct mbuf *, struct tcphdr *, struct tcpcb *, int); -void tcp_pulloutofband(struct socket *, - struct tcphdr *, struct mbuf *, int); void tcp_xmit_timer(struct tcpcb *, int); void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); void cc_ack_received(struct tcpcb *tp, struct tcphdr *th, diff --git a/tests/sys/netinet/Makefile b/tests/sys/netinet/Makefile index acb259701a3f..bc781cf241ac 100644 --- a/tests/sys/netinet/Makefile +++ b/tests/sys/netinet/Makefile @@ -12,7 +12,8 @@ ATF_TESTS_C= fibs_multibind_test \ so_reuseport_lb_test \ socket_afinet \ tcp_connect_port_test \ - tcp_md5_getsockopt + tcp_md5_getsockopt \ + tcp_oob ATF_TESTS_SH= arp \ carp \ diff --git a/tests/sys/netinet/tcp_oob.c b/tests/sys/netinet/tcp_oob.c new file mode 100644 index 000000000000..42d2a72fb038 --- /dev/null +++ b/tests/sys/netinet/tcp_oob.c @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2026 Mark Johnston + * + * This software was written by Mark Johnston under sponsorship from + * the FreeBSD Foundation. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* Never appears in in-band data generated by fill(). */ +static const char oob_byte = 'A'; + +static void +fill(char *buf, size_t len, size_t off) +{ + for (size_t i = 0; i < len; i++) + buf[i] = 'a' + (i + off) % 26; +} + +static void +tcp_socketpair(int sd[2], int domain) +{ + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + struct sockaddr *sinp; + int ls; + + if (domain == PF_INET) { + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_len = sizeof(sin); + sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + sin.sin_port = htons(0); + sinp = (struct sockaddr *)&sin; + } else { + ATF_REQUIRE(domain == PF_INET6); + memset(&sin6, 0, sizeof(sin6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_len = sizeof(sin6); + sin6.sin6_addr = in6addr_loopback; + sin6.sin6_port = htons(0); + sinp = (struct sockaddr *)&sin6; + } + + ls = socket(domain, SOCK_STREAM, 0); + ATF_REQUIRE_MSG(ls >= 0, "socket: %s", strerror(errno)); + ATF_REQUIRE_MSG(bind(ls, (struct sockaddr *)sinp, sinp->sa_len) == 0, + "bind: %s", strerror(errno)); + ATF_REQUIRE_MSG(getsockname(ls, (struct sockaddr *)sinp, + &(socklen_t){ sinp->sa_len }) == 0, + "getsockname: %s", strerror(errno)); + ATF_REQUIRE_MSG(listen(ls, 1) == 0, "listen: %s", strerror(errno)); + + sd[0] = socket(domain, SOCK_STREAM, 0); + ATF_REQUIRE_MSG(sd[0] >= 0, "socket: %s", strerror(errno)); + ATF_REQUIRE_MSG(connect(sd[0], (struct sockaddr *)sinp, sinp->sa_len) == + 0, "connect: %s", strerror(errno)); + sd[1] = accept(ls, NULL, NULL); + ATF_REQUIRE_MSG(sd[1] >= 0, "accept: %s", strerror(errno)); + ATF_REQUIRE(close(ls) == 0); + + ATF_REQUIRE_MSG(setsockopt(sd[0], IPPROTO_TCP, TCP_NODELAY, + &(int){ 1 }, sizeof(int)) == 0, "setsockopt: %s", strerror(errno)); + ATF_REQUIRE_MSG(setsockopt(sd[1], IPPROTO_TCP, TCP_NODELAY, + &(int){ 1 }, sizeof(int)) == 0, "setsockopt: %s", strerror(errno)); +} + +static void +sendit(int s, const void *buf, size_t len, int flags) +{ + ssize_t n; + + n = send(s, buf, len, flags); + ATF_REQUIRE_MSG(n >= 0, "send: %s", strerror(errno)); + ATF_REQUIRE((size_t)n == len); +} + +static void +recvit(int s, const char *expected, size_t len) +{ + char buf[16384]; + size_t off; + ssize_t n; + + for (off = 0; off < len; off += n) { + n = recv(s, buf, MIN(sizeof(buf), len - off), 0); + ATF_REQUIRE_MSG(n >= 0, "recv: %s", strerror(errno)); + ATF_REQUIRE_MSG(n != 0, "EOF after %zu of %zu bytes", off, len); + for (ssize_t i = 0; i < n; i++) + ATF_REQUIRE_MSG(buf[i] == expected[off + i], + "byte %zu is wrong (%x vs %x)", off + i, + buf[i], expected[off + i]); + } +} + +static void +recv_oob(int s, char *cp) +{ + struct pollfd pfd; + ssize_t n; + int atmark, error; + + pfd.fd = s; + pfd.events = POLLPRI; + error = poll(&pfd, 1, 10 * 1000); + ATF_REQUIRE_EQ(error, 1); + atmark = sockatmark(s); + ATF_REQUIRE_EQ(atmark, 1); + + for (int i = 0; i < 1000; i++) { + n = recv(s, cp, 1, MSG_OOB); + if (n < 0) { + ATF_REQUIRE_EQ(errno, EWOULDBLOCK); + } else { + ATF_REQUIRE_EQ(n, 1); + break; + } + usleep(10000); + } +} + +/* + * Try sending an OOB byte at different offsets in a stream, and with varying + * write sizes, so as to hopefully test handling of the urgent pointer with + * different mbuf storage types. + */ +static void +oob_offsets_test(int domain) +{ +#define OOB_MAXOFF (8192 + 512) +#define SUFFIXLEN 17 + char buf[OOB_MAXOFF + 1], data[OOB_MAXOFF + SUFFIXLEN]; + int sd[2]; + char c; + + fill(data, sizeof(data), 0); + tcp_socketpair(sd, domain); + + for (size_t off = 0; off <= OOB_MAXOFF; off++) { + for (int split = 0; split < 2; split++) { + + if (split) { + /* The urgent byte ends a larger write. */ + memcpy(buf, data, off); + buf[off] = oob_byte; + sendit(sd[0], buf, off + 1, MSG_OOB); + } else { + if (off > 0) + sendit(sd[0], data, off, 0); + sendit(sd[0], &oob_byte, 1, MSG_OOB); + } + sendit(sd[0], data, SUFFIXLEN, 0); + recvit(sd[1], data, off); + recv_oob(sd[1], &c); + ATF_REQUIRE_MSG(c == oob_byte, + "offset %zu, %d: urgent byte is %x, expected %x", + off, split, c, oob_byte); + recvit(sd[1], data, SUFFIXLEN); + } + } + + ATF_REQUIRE(close(sd[0]) == 0); + ATF_REQUIRE_MSG(recv(sd[1], &c, 1, 0) == 0, + "unexpected data in stream"); + ATF_REQUIRE(close(sd[1]) == 0); +#undef OOB_MAXOFF +#undef SUFFIXLEN +} + +ATF_TC_WITHOUT_HEAD(oob_offsets); +ATF_TC_BODY(oob_offsets, tc) +{ + oob_offsets_test(PF_INET); +} + +ATF_TC_WITHOUT_HEAD(oob_offsets6); +ATF_TC_BODY(oob_offsets6, tc) +{ + oob_offsets_test(PF_INET6); +} + +/* + * Send file data with sendfile(2) and then an urgent byte, with the whole file + * still in flight. Try different file sizes. Make sure the backing file isn't + * modified. + */ +static void +oob_sendfile_test(int domain) +{ + const size_t sendfile_sizes[] = { + 1, 100, 1000, 4096, 8192, 65535, 65536, 65537, 100000, 131072, + 131073, 200000, 262144, 524289, + }; + char *buf, *expected; + + expected = malloc(sendfile_sizes[nitems(sendfile_sizes) - 1]); + ATF_REQUIRE(expected != NULL); + buf = malloc(sendfile_sizes[nitems(sendfile_sizes) - 1]); + ATF_REQUIRE(buf != NULL); + + for (size_t i = 0; i < nitems(sendfile_sizes); i++) { + char path[NAME_MAX]; + off_t sbytes; + size_t size; + ssize_t n; + int fd, sd[2], sbsize, rbsize; + + size = sendfile_sizes[i]; + fill(expected, size, i); + + snprintf(path, sizeof(path), "file%zu", i); + fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); + ATF_REQUIRE_MSG(fd >= 0, "open: %s", strerror(errno)); + n = write(fd, expected, size); + ATF_REQUIRE_MSG(n >= 0, "write: %s", strerror(errno)); + ATF_REQUIRE_EQ(n, (ssize_t)size); + + tcp_socketpair(sd, domain); + + /* + * Let sendfile() queue the whole file without blocking, but + * keep the receive window small so that the file is still in + * flight when the urgent byte is sent, wrapping the urgent + * pointer for the larger files. + */ + sbsize = size + 65536; + ATF_REQUIRE_MSG(setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF, + &sbsize, sizeof(sbsize)) == 0, + "setsockopt: %s", strerror(errno)); + rbsize = 65536; + ATF_REQUIRE_MSG(setsockopt(sd[1], SOL_SOCKET, SO_RCVBUF, + &rbsize, sizeof(rbsize)) == 0, + "setsockopt: %s", strerror(errno)); + + ATF_REQUIRE_MSG(sendfile(fd, sd[0], 0, size, NULL, &sbytes, + 0) == 0, + "sendfile: %s", strerror(errno)); + ATF_REQUIRE_EQ(sbytes, (off_t)size); + sendit(sd[0], &oob_byte, 1, MSG_OOB); + ATF_REQUIRE(close(sd[0]) == 0); + + for (;;) { + char null[65536]; + + n = recv(sd[1], null, sizeof(null), 0); + ATF_REQUIRE_MSG(n >= 0, "recv: %s", strerror(errno)); + if (n == 0) + break; + } + ATF_REQUIRE(close(sd[1]) == 0); + + /* Did the file somehow get modified? */ + n = pread(fd, buf, size, 0); + ATF_REQUIRE_MSG(n >= 0, "pread: %s", strerror(errno)); + ATF_REQUIRE_EQ(n, (ssize_t)size); + for (size_t j = 0; j < size; j++) { + ATF_REQUIRE_MSG(buf[j] == expected[j], + "file size %zu was modified at offset %zu", + size, j); + } + ATF_REQUIRE(close(fd) == 0); + ATF_REQUIRE(unlink(path) == 0); + } +} + +ATF_TC_WITHOUT_HEAD(oob_sendfile); +ATF_TC_BODY(oob_sendfile, tc) +{ + oob_sendfile_test(PF_INET); +} + +ATF_TC_WITHOUT_HEAD(oob_sendfile6); +ATF_TC_BODY(oob_sendfile6, tc) +{ + oob_sendfile_test(PF_INET6); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, oob_offsets); + ATF_TP_ADD_TC(tp, oob_offsets6); + ATF_TP_ADD_TC(tp, oob_sendfile); + ATF_TP_ADD_TC(tp, oob_sendfile6); + return (atf_no_error()); +}