git: f5c21693e43e - main - tests: unix: Check that MSG_PEEK returns -1 for SCM_RIGHTS

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:33:24 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=f5c21693e43eae2827a8d13984221009a4134393

commit f5c21693e43eae2827a8d13984221009a4134393
Author:     Patrick Crandol <patrick@crandol.com>
AuthorDate: 2026-11-17 16:57:16 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2026-11-17 17:32:07 +0000

    tests: unix: Check that MSG_PEEK returns -1 for SCM_RIGHTS
    
    Add a regression test that a MSG_PEEK receive of a queued SCM_RIGHTS
    message returns -1 in place of the descriptor, while other control
    messages, here SCM_CREDS, are still returned, and that the following
    receive still delivers the descriptor.
    
    Signed-off-by: Patrick Crandol <patrick@crandol.com>
---
 tests/sys/kern/unix_passfd_test.c | 59 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/tests/sys/kern/unix_passfd_test.c b/tests/sys/kern/unix_passfd_test.c
index 330d045885cd..215cf6cad485 100644
--- a/tests/sys/kern/unix_passfd_test.c
+++ b/tests/sys/kern/unix_passfd_test.c
@@ -773,6 +773,64 @@ ATF_TC_BODY(peek, tc)
 	closesocketpair(fd);
 }
 
+/*
+ * MSG_PEEK must not copy out the internalized SCM_RIGHTS (kernel pointers):
+ * each descriptor is returned as -1.  Other control messages, here
+ * SCM_CREDS, are still returned.
+ */
+ATF_TC_WITHOUT_HEAD(peek_rights);
+ATF_TC_BODY(peek_rights, tc)
+{
+	char buf[1], cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) +
+	    CMSG_SPACE(sizeof(void *) * 2)];
+	struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
+	struct msghdr msghdr;
+	struct cmsghdr *cmsghdr;
+	int fd[2], getfd, on, peekfd, putfd;
+	bool foundcreds, foundrights;
+
+	domainsocketpair(fd);
+	on = 1;
+	ATF_REQUIRE_MSG(setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on)) == 0,
+	    "setsockopt(LOCAL_CREDS) failed: %s", strerror(errno));
+	tempfile(&putfd);
+	sendfd(fd[0], putfd);
+
+	memset(&msghdr, 0, sizeof(msghdr));
+	msghdr.msg_iov = &iov;
+	msghdr.msg_iovlen = 1;
+	msghdr.msg_control = cbuf;
+	msghdr.msg_controllen = sizeof(cbuf);
+	ATF_REQUIRE(recvmsg(fd[1], &msghdr, MSG_PEEK) == 1);
+	ATF_REQUIRE((msghdr.msg_flags & MSG_CTRUNC) == 0);
+	foundcreds = foundrights = false;
+	for (cmsghdr = CMSG_FIRSTHDR(&msghdr); cmsghdr != NULL;
+	    cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
+		if (cmsghdr->cmsg_level != SOL_SOCKET)
+			continue;
+		if (cmsghdr->cmsg_type == SCM_CREDS)
+			foundcreds = true;
+		if (cmsghdr->cmsg_type == SCM_RIGHTS) {
+			ATF_REQUIRE_MSG(cmsghdr->cmsg_len ==
+			    CMSG_LEN(sizeof(int)),
+			    "MSG_PEEK returned SCM_RIGHTS with len %u",
+			    cmsghdr->cmsg_len);
+			memcpy(&peekfd, CMSG_DATA(cmsghdr), sizeof(peekfd));
+			ATF_REQUIRE_MSG(peekfd == -1,
+			    "MSG_PEEK returned descriptor %d", peekfd);
+			foundrights = true;
+		}
+	}
+	ATF_REQUIRE_MSG(foundcreds, "MSG_PEEK did not return SCM_CREDS");
+	ATF_REQUIRE_MSG(foundrights, "MSG_PEEK did not return SCM_RIGHTS");
+
+	ATF_REQUIRE(recvfd_payload(fd[1], &getfd, buf, sizeof(buf),
+	    sizeof(cbuf), 0) == 1);
+	close(putfd);
+	close(getfd);
+	closesocketpair(fd);
+}
+
 /*
  * Send two files.  Then receive them.  Make sure they are returned in the
  * right order, and both get there.
@@ -1622,6 +1680,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, send_rejected_late);
 	ATF_TP_ADD_TC(tp, send_rejected_noinherit);
 	ATF_TP_ADD_TC(tp, peek);
+	ATF_TP_ADD_TC(tp, peek_rights);
 	ATF_TP_ADD_TC(tp, two_files);
 	ATF_TP_ADD_TC(tp, bundle);
 	ATF_TP_ADD_TC(tp, bundle_cancel);