From nobody Thu Feb 09 18:01:56 2023 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 4PCPnx1JMxz3n1jC; Thu, 9 Feb 2023 18:01:57 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PCPnx0l6Lz3DRT; Thu, 9 Feb 2023 18:01:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675965717; 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=KQXu6/V5l3NpThHV7Q/hQzmcRV5NmeOV158rlELg2dw=; b=C4LNy0ZwJP/M56Gz3g9z+icbNTBhU95t+9cdnohQzo8suP/fvtazgFPfYJAAttGSSeX4UR eIc+GqVvy/ng4hkULQtRPN1w61odF1BE1xqhAZzLPYAbNLfW7wKcl6n/6dDhRqKuhyhaxE q3yn5xULbTlcXUp5VXvhFWDURqA22TeRGf+V/px2lQY4UGCeyIj4tzaEJSyq1SB3vHJFAl /WLglZ1GrbrPHCkGV0eDatWUoAexIObkKWH/V2KxjZVYoTqROjq7wvkBsdMe5NBjvEMyt8 FoSy1Gln7oNn1JjxYR9IO8RBO1DWpmjEZ5gbLArlVT0GWy+kTX1PjRRL2YhMbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675965717; 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=KQXu6/V5l3NpThHV7Q/hQzmcRV5NmeOV158rlELg2dw=; b=IZ+Zk+pjtMdGtOopngQoqeelNtWt6AZrXXPGXSOQ0W2W2tDN3QWlHtLV9IilOWnzjlpNzT PpjWqCO64NdJG7ijsMw9NUV8qVAbA2fNRvmHbyVA4yXgvVXIrD5GOoPocMNb0AcI5WNJr1 YFlor7rliykyH13t3RFS0l+35z1nwnst7KKGEEdDw4PGSXSrDAMsSh7gS3AND08HiQ3Xgu pIYkMIJv2qoQ2BfnydY7+1/wNIncK1UwodCu2mnSBx8NguDLNJ6I1S8fGN8a7iS6AyOZSI 2ZTUgShyHS7hLzrCeVMXzi7RV6q3MdamuVXESGYqXE1L/4Q/wKpFvjZVe/ZCJA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675965717; a=rsa-sha256; cv=none; b=DJxYAzcRn1ULiSPaQIoB08dciIrcj+8H+yLRIz//OdXKDz4WJh3lAcvxsvRWbhOjoBH3Xt t4mLkuJpUJjk0EE0o7bpjldhRgizJu13mnNp5E1wFcX0jkPxFhAO8mxMEjkymN5hUbHlnE ViKopwfr3IGx3+dTpc2NeQnwA62HKJnURexVGLwPgDGfDg4NRQM4etDqLL1cE91IWdOmFp UL8eXtokaxu7rEuTqfAVtN72Z1x/ZcWFFnqQNjL5grAtLF+vUgGTILOpK1zQK58y2+rUyg 7v038A9bXlj4BdQ1D+bKN9ciNF9Aofv1zYb7o0768jp7O8wn1GGKEgDpQH6nJw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PCPnw6vYzzmfv; Thu, 9 Feb 2023 18:01:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 319I1uTp067684; Thu, 9 Feb 2023 18:01:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 319I1uCi067683; Thu, 9 Feb 2023 18:01:56 GMT (envelope-from git) Date: Thu, 9 Feb 2023 18:01:56 GMT Message-Id: <202302091801.319I1uCi067683@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: adaf752192c1 - stable/13 - sockets: add MSG_TRUNC flag handling for recvfrom()/recvmsg(). List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: adaf752192c165b66875e81db62d274a09591d0c Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=adaf752192c165b66875e81db62d274a09591d0c commit adaf752192c165b66875e81db62d274a09591d0c Author: Alexander V. Chernikov AuthorDate: 2022-07-25 19:46:40 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-02-09 18:01:49 +0000 sockets: add MSG_TRUNC flag handling for recvfrom()/recvmsg(). Implement Linux-variant of MSG_TRUNC input flag used in recv(), recvfrom() and recvmsg(). Posix defines MSG_TRUNC as an output flag, indicating packet/datagram truncation. Linux extended it a while (~15+ years) ago to act as input flag, resulting in returning the full packet size regarless of the input buffer size. It's a (relatively) popular pattern to do recvmsg( MSG_PEEK | MSG_TRUNC) to get the packet size, allocate the buffer and issue another call to fetch the packet. In particular, it's popular in userland netlink code, which is the primary driving factor of this change. This commit implements the MSG_TRUNC support for SOCK_DGRAM sockets (udp, unix and all soreceive_generic() users). PR: kern/176322 Reviewed by: pauamma(doc) Differential Revision: https://reviews.freebsd.org/D35909 MFC after: 1 month (cherry picked from commit be1f485d7d6bebc53b055cc165a11ada0ab5fb17) --- lib/libc/sys/recv.2 | 12 +++ sys/kern/uipc_socket.c | 13 ++- tests/sys/kern/Makefile | 1 + tests/sys/kern/socket_msg_trunc.c | 169 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+), 4 deletions(-) diff --git a/lib/libc/sys/recv.2 b/lib/libc/sys/recv.2 index 46e0cf8163dd..95a9e83446a0 100644 --- a/lib/libc/sys/recv.2 +++ b/lib/libc/sys/recv.2 @@ -163,6 +163,7 @@ one or more of the values: .Bl -column ".Dv MSG_CMSG_CLOEXEC" -offset indent .It Dv MSG_OOB Ta process out-of-band data .It Dv MSG_PEEK Ta peek at incoming message +.It Dv MSG_TRUNC Ta return real packet or datagram length .It Dv MSG_WAITALL Ta wait for full request or error .It Dv MSG_DONTWAIT Ta do not block .It Dv MSG_CMSG_CLOEXEC Ta set received fds close-on-exec @@ -185,6 +186,17 @@ from the beginning of the receive queue without removing that data from the queue. Thus, a subsequent receive call will return the same data. The +.Dv MSG_TRUNC +flag causes the receive operation to return the full length of the packet +or datagram even if larger than provided buffer. The flag is supported +on SOCK_DGRAM sockets for +.Dv AF_INET +, +.Dv AF_INET6 +and +.Dv AF_UNIX +families. +The .Dv MSG_WAITALL flag requests that the operation block until the full request is satisfied. diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c index ce7279357fab..f8931b653a4d 100644 --- a/sys/kern/uipc_socket.c +++ b/sys/kern/uipc_socket.c @@ -1911,15 +1911,18 @@ soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio, struct mbuf *nextrecord; int moff, type = 0; ssize_t orig_resid = uio->uio_resid; + bool report_real_len = false; mp = mp0; if (psa != NULL) *psa = NULL; if (controlp != NULL) *controlp = NULL; - if (flagsp != NULL) + if (flagsp != NULL) { + report_real_len = *flagsp & MSG_TRUNC; + *flagsp &= ~MSG_TRUNC; flags = *flagsp &~ MSG_EOR; - else + } else flags = 0; if (flags & MSG_OOB) return (soreceive_rcvoob(so, uio, flags)); @@ -1993,7 +1996,7 @@ restart: error = ENOTCONN; goto release; } - if (uio->uio_resid == 0) { + if (uio->uio_resid == 0 && !report_real_len) { SOCKBUF_UNLOCK(&so->so_rcv); goto release; } @@ -2341,6 +2344,8 @@ dontblock: SOCKBUF_LOCK_ASSERT(&so->so_rcv); if (m != NULL && pr->pr_flags & PR_ATOMIC) { + if (report_real_len) + uio->uio_resid -= m_length(m, NULL) - moff; flags |= MSG_TRUNC; if ((flags & MSG_PEEK) == 0) (void) sbdroprecord_locked(&so->so_rcv); @@ -2639,7 +2644,7 @@ soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio, * For any complicated cases, fall back to the full * soreceive_generic(). */ - if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB)) + if (mp0 != NULL || (flags & (MSG_PEEK | MSG_OOB | MSG_TRUNC))) return (soreceive_generic(so, psa, uio, mp0, controlp, flagsp)); diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index 0fda50f592a0..3f6043f19f88 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -25,6 +25,7 @@ ATF_TESTS_C+= reaper ATF_TESTS_C+= sched_affinity ATF_TESTS_C+= sigaltstack ATF_TESTS_C+= sigwait +ATF_TESTS_C+= socket_msg_trunc TEST_METADATA.sigwait+= is_exclusive="true" .if ${MACHINE_ARCH} != "i386" && ${MACHINE_ARCH:Mpowerpc*} == "" && \ ${MACHINE_CPUARCH} != "mips" diff --git a/tests/sys/kern/socket_msg_trunc.c b/tests/sys/kern/socket_msg_trunc.c new file mode 100644 index 000000000000..5041842f32b7 --- /dev/null +++ b/tests/sys/kern/socket_msg_trunc.c @@ -0,0 +1,169 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2022 Alexander V. Chernikov + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include + +static void +check_recvmsg(const char *test_name) +{ + int ss, cs, rc; + struct sockaddr *sa; + struct sockaddr_in sin; + struct sockaddr_in6 sin6; + struct sockaddr_un saun; + int *sizes, sizes_count; + int one = 1; + + + if (!strcmp(test_name, "udp")) { + ss = socket(PF_INET, SOCK_DGRAM, 0); + ATF_CHECK(ss >= 0); + rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); + ATF_CHECK_EQ(0, rc); + bzero(&sin, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_len = sizeof(sin); + sin.sin_port = htons(6666); + sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + sa = (struct sockaddr *)&sin; + rc = bind(ss, sa, sa->sa_len); + ATF_CHECK_EQ(0, rc); + + cs = socket(PF_INET, SOCK_DGRAM, 0); + ATF_CHECK(cs >= 0); + int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000}; + sizes_count = sizeof(inet_sizes) / sizeof(int); + sizes = malloc(sizeof(inet_sizes)); + memcpy(sizes, inet_sizes, sizeof(inet_sizes)); + + } else if (!strcmp(test_name, "udp6")) { + ss = socket(PF_INET6, SOCK_DGRAM, 0); + ATF_CHECK(ss >= 0); + rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); + ATF_CHECK_EQ(0, rc); + bzero(&sin6, sizeof(sin6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_len = sizeof(sin6); + sin6.sin6_port = htons(6666); + const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT; + sin6.sin6_addr = in6loopback; + sa = (struct sockaddr *)&sin6; + rc = bind(ss, sa, sa->sa_len); + ATF_CHECK_EQ(0, rc); + + cs = socket(PF_INET6, SOCK_DGRAM, 0); + ATF_CHECK(cs >= 0); + int inet_sizes[] = {80, 255, 256, 1024, 4096, 9000}; + sizes_count = sizeof(inet_sizes) / sizeof(int); + sizes = malloc(sizeof(inet_sizes)); + memcpy(sizes, inet_sizes, sizeof(inet_sizes)); + + } else if (!strcmp(test_name, "unix")) { + const char *PATH = "/tmp/test_check_recvmsg_socket"; + ss = socket(PF_UNIX, SOCK_DGRAM, 0); + ATF_CHECK(ss >= 0); + rc = setsockopt(ss, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); + ATF_CHECK_EQ(0, rc); + bzero(&saun, sizeof(saun)); + saun.sun_family = AF_UNIX; + strcpy(saun.sun_path, PATH); + saun.sun_len = sizeof(saun); + sa = (struct sockaddr *)&saun; + unlink(PATH); + rc = bind(ss, sa, sa->sa_len); + ATF_CHECK_EQ(0, rc); + + cs = socket(PF_UNIX, SOCK_DGRAM, 0); + ATF_CHECK(cs >= 0); + int unix_sizes[] = {80, 255, 256, 1024, 2000}; + sizes_count = sizeof(unix_sizes) / sizeof(int); + sizes = malloc(sizeof(unix_sizes)); + memcpy(sizes, unix_sizes, sizeof(unix_sizes)); + } else + return; + + char buf[4096]; + memset(buf, 0xFF, sizeof(buf)); + for (int i = 0; i < sizes_count; i++) { + int sz = sizes[i]; + char tbuf[1]; + rc = sendto(cs, buf, sz, 0, sa, sa->sa_len); + ATF_REQUIRE_EQ(rc, sz); + + rc = recv(ss, NULL, 0, MSG_PEEK | MSG_TRUNC); + ATF_CHECK_EQ(rc, sz); + + rc = recv(ss, tbuf, sizeof(tbuf), MSG_PEEK | MSG_TRUNC); + ATF_CHECK_EQ(rc, sz); + + rc = recv(ss, tbuf, sizeof(tbuf), MSG_TRUNC); + ATF_CHECK_EQ(rc, sz); + } + + close(ss); + close(cs); +} + +ATF_TC_WITHOUT_HEAD(socket_afinet_udp_recv_trunc); +ATF_TC_BODY(socket_afinet_udp_recv_trunc, tc) +{ + check_recvmsg("udp"); +} + +ATF_TC_WITHOUT_HEAD(socket_afinet6_udp_recv_trunc); +ATF_TC_BODY(socket_afinet6_udp_recv_trunc, tc) +{ + check_recvmsg("udp6"); +} + +ATF_TC_WITHOUT_HEAD(socket_afunix_recv_trunc); +ATF_TC_BODY(socket_afunix_recv_trunc, tc) +{ + check_recvmsg("unix"); +} + + +ATF_TP_ADD_TCS(tp) +{ + + ATF_TP_ADD_TC(tp, socket_afinet_udp_recv_trunc); + ATF_TP_ADD_TC(tp, socket_afinet6_udp_recv_trunc); + ATF_TP_ADD_TC(tp, socket_afunix_recv_trunc); + + return atf_no_error(); +}