From nobody Thu Feb 08 17:01:05 2024 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 4TW3Ck1Z8Qz5B2L7; Thu, 8 Feb 2024 17:01:06 +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 4TW3Cj6QZcz4V9s; Thu, 8 Feb 2024 17:01:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1707411665; 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=tieT7bRvJ847lO1RFzvuA46nYML//ydfbEhgtqDsFbE=; b=BNMD0F3FG78B/5D/daSFvaV4dFUyVy8BHam3jVzb1dt9mTDOl5nSSMjcBPiRs8J0mWmUil FV8tDe6ZqTmyhln9GhnnjpkoTo3CzntyVWX1lXOtYEPsmRI2kQRZzdo919zIk05zgG59HB d6GXSfZ0EOR2a+s+XofrTPbv0x4Y60JrJ1i0ePUILBhzKR8dGAUViM3MzPZ1WArq7FKmUy uYanW0xYXvUejE7PIxtHtaYHkiPqU+8Pq/JCuWxlarDyt1WSUjQulEYLfFgGsL7AC6wvgM iMUwbpYTsxkQqdHMjf5As5ejBBTiXzOBxyeZmsaLGtS4T5wO7DWpi8ntKpAsSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1707411665; 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=tieT7bRvJ847lO1RFzvuA46nYML//ydfbEhgtqDsFbE=; b=BIuXWCBB03lT9XWmQPn2ueLwE320zH/If3WkCz3N62HVYDwFzw+dEEHcdcwsB4WIv1JV+E vGwLWAyvX7a2uAPj389J1EF138MbW2trcTSDDT/KG0/utwbzRsIKPDoyoBsLjgIXml0n60 f2tALzhZOkj2IK5XuM5Wgq9TZRYZ+khkafO/kxLdHd+8J3Pu3V8aqBhuyHEz3moULgUQEK Ny+oVVXOxOfsWg5plGDCN5lxYLaUFwuP0kQ3Ve54JA0ORTv2z0GlZczoUkxOwsz90ngaEJ +RiM7Smjp0FSCPunTBGdHBudNkhebG7ofHpZUItlfGm7D4esSQOmq80ReiydHA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1707411665; a=rsa-sha256; cv=none; b=UGTkvuHmI9RMogqgEF4L4WmpzKPo5pnj8o1vESFhntEOILyPBud8FJ6iCXy6DDQlwoRl1N VsG9WOpKUoKXqfcEoOXEAo3bTf68dYi+lLVNFiXJak41/LXiIVJseGL4hq+18OD6KXM6E3 tSXhfKeVmckX/E3iplquzE4Z3KJVWd2c7PkIDLW30FrPLGHT3QmTm/j4WyCXAPBWTgaXnd DJdZivxq1+0mM0KDopayKIMtnWa/HrFw1qSUBH7u+V64w2lA8E8muzZkvhPYy39plPnVc6 TQa6R4D7yY1kzrJyn9T6R/7RBV97wBh6ffObLbvpCZrUNCpaVilG6Qh5afElWA== 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 4TW3Cj5BRRzyKh; Thu, 8 Feb 2024 17:01:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 418H15dd065744; Thu, 8 Feb 2024 17:01:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 418H15jo065741; Thu, 8 Feb 2024 17:01:05 GMT (envelope-from git) Date: Thu, 8 Feb 2024 17:01:05 GMT Message-Id: <202402081701.418H15jo065741@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 1e68b8d9a90f - main - tests/unix_passfd: test that control mixed with data creates records 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: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1e68b8d9a90f3ddf5d0766ea3b5a6c6ec9088b2f Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=1e68b8d9a90f3ddf5d0766ea3b5a6c6ec9088b2f commit 1e68b8d9a90f3ddf5d0766ea3b5a6c6ec9088b2f Author: Gleb Smirnoff AuthorDate: 2024-02-08 17:00:23 +0000 Commit: Gleb Smirnoff CommitDate: 2024-02-08 17:00:41 +0000 tests/unix_passfd: test that control mixed with data creates records If socket has data interleaved with control it would never allow to read two pieces of data, neither two pieces of control with one recvmsg(2). In other words, presence of control makes a SOCK_STREAM socket behave like SOCK_SEQPACKET, where control marks the records. This is not a documented or specified behavior, but this is how it worked always for BSD sockets. If you look closer at it, this actually makes a lot of sense, as if it were the opposite both the kernel code and an application code would become way more complex. The change made recvfd_payload() to return received length and requires caller to do ATF_REQUIRE() itself. This required a small change to existing test rights_creds_payload. It also refactors a bit f28532a0f363, pushing two identical calls out of TEST_PROTO ifdef. Reviwed by: markj Differential Revision: https://reviews.freebsd.org/D43724 --- tests/sys/kern/unix_passfd_test.c | 56 +++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/tests/sys/kern/unix_passfd_test.c b/tests/sys/kern/unix_passfd_test.c index 67171e62c963..143ccd098583 100644 --- a/tests/sys/kern/unix_passfd_test.c +++ b/tests/sys/kern/unix_passfd_test.c @@ -204,7 +204,7 @@ localcreds(int sockfd) return (val != 0); } -static void +static ssize_t recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen, size_t cmsgsz, int recvmsg_flags) { @@ -230,8 +230,6 @@ recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen, len = recvmsg(sockfd, &msghdr, recvmsg_flags); ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno)); - ATF_REQUIRE_MSG((size_t)len == buflen, - "recvmsg: %zd bytes received; expected %zd", len, buflen); cmsghdr = CMSG_FIRSTHDR(&msghdr); ATF_REQUIRE_MSG(cmsghdr != NULL, @@ -254,15 +252,20 @@ recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen, "recvmsg: expected credentials were not received"); ATF_REQUIRE_MSG((msghdr.msg_flags & MSG_TRUNC) == 0, "recvmsg: MSG_TRUNC is set while buffer is sufficient"); + + return (len); } static void recvfd(int sockfd, int *recv_fd, int flags) { + ssize_t len; char ch = 0; - recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch), + len = recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch), CMSG_SPACE(sizeof(int)), flags); + ATF_REQUIRE_MSG((size_t)len == sizeof(ch), + "recvmsg: %zd bytes received; expected %zd", len, sizeof(ch)); } #if TEST_PROTO == SOCK_STREAM @@ -632,7 +635,7 @@ ATF_TC_BODY(rights_creds_payload, tc) { const int on = 1; u_long sendspace; - ssize_t len; + ssize_t len, rlen; void *buf; int fd[2], getfd, putfd, rc; @@ -651,20 +654,19 @@ ATF_TC_BODY(rights_creds_payload, tc) strerror(errno)); len = sendfd_payload(fd[0], putfd, buf, sendspace); -#if TEST_PROTO == SOCK_STREAM ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno)); +#if TEST_PROTO == SOCK_STREAM ATF_REQUIRE_MSG((size_t)len < sendspace, "sendmsg: %zd bytes sent", len); - recvfd_payload(fd[1], &getfd, buf, len, - CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0); #endif #if TEST_PROTO == SOCK_DGRAM - ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno)); ATF_REQUIRE_MSG((size_t)len == sendspace, "sendmsg: %zd bytes sent", len); - recvfd_payload(fd[1], &getfd, buf, len, - CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0); #endif + rlen = recvfd_payload(fd[1], &getfd, buf, len, + CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0); + ATF_REQUIRE_MSG(rlen == len, + "recvmsg: %zd bytes received; expected %zd", rlen, len); close(putfd); close(getfd); @@ -945,6 +947,37 @@ ATF_TC_BODY(empty_rights_message, tc) (void)close(putfd); } +/* + * Check that sending control creates records in a stream socket, making it + * behave like a seqpacket socket. If we stack several control+data writes + * on a stream socket, we won't be able to read them all at once, even if we + * provide a buffer large enough to receive all at once. + * + * XXXGL: adding MSG_WAITALL to the recvmsg() flags will make this test stuck. + */ +ATF_TC_WITHOUT_HEAD(control_creates_records); +ATF_TC_BODY(control_creates_records, tc) +{ + int fd[2], putfd, getfd; + char buf[2]; + ssize_t rlen; + + domainsocketpair(fd); + tempfile(&putfd); + + for (int i = 1; i <= 2; i++) + ATF_REQUIRE(sendfd_payload(fd[0], putfd, buf, 1) == 1); + ATF_REQUIRE(close(putfd) == 0); + for (int i = 1; i <= 2; i++) { + rlen = recvfd_payload(fd[1], &getfd, buf, 2, + CMSG_SPACE(sizeof(int)) * 2, 0); + ATF_REQUIRE_MSG(rlen == 1, + "recvmsg: %zd bytes received; expected 1", rlen); + ATF_REQUIRE(close(getfd) == 0); + } + closesocketpair(fd); +} + ATF_TP_ADD_TCS(tp) { @@ -963,6 +996,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, truncated_rights); ATF_TP_ADD_TC(tp, copyout_rights_error); ATF_TP_ADD_TC(tp, empty_rights_message); + ATF_TP_ADD_TC(tp, control_creates_records); return (atf_no_error()); }