git: d8735eb7acc0 - main - socket tests: Add a regression test for ktrace+recv(MSG_TRUNC)

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Oct 2023 13:37:10 UTC
The branch main has been updated by markj:

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

commit d8735eb7acc0613fd19f74a49d3bdcb7ed0e9b0e
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-10-16 22:23:36 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-10-17 13:12:57 +0000

    socket tests: Add a regression test for ktrace+recv(MSG_TRUNC)
    
    MFC after:      1 week
---
 tests/sys/kern/socket_msg_trunc.c | 57 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/tests/sys/kern/socket_msg_trunc.c b/tests/sys/kern/socket_msg_trunc.c
index 469b9e1503cd..279170158643 100644
--- a/tests/sys/kern/socket_msg_trunc.c
+++ b/tests/sys/kern/socket_msg_trunc.c
@@ -27,11 +27,13 @@
 
 #include <sys/param.h>
 #include <sys/errno.h>
+#include <sys/ktrace.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 
 #include <netinet/in.h>
 
+#include <fcntl.h>
 #include <poll.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -182,12 +184,67 @@ ATF_TC_BODY(recv_trunc_afunix_seqpacket, tc)
 	ATF_REQUIRE(close(ss) == 0);
 }
 
+/*
+ * Exercise the case where ktrace was used to dump a truncated buffer.
+ */
+ATF_TC_WITHOUT_HEAD(recvmsg_trunc_ktrace_uio);
+ATF_TC_BODY(recvmsg_trunc_ktrace_uio, tc)
+{
+	struct ktr_header ktr;
+	struct msghdr msg;
+	struct iovec iov;
+	const char *tracepath;
+	char buf[128];
+	ssize_t nbytes;
+	int error, fd, sd[2];
+
+	tracepath = "ktrace";
+
+	error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd);
+	ATF_REQUIRE(error == 0);
+
+	memset(buf, 0, sizeof(buf));
+	nbytes = send(sd[0], buf, sizeof(buf), 0);
+	ATF_REQUIRE_MSG(nbytes >= 0, "send failed: %s", strerror(errno));
+	ATF_REQUIRE((size_t)nbytes == sizeof(buf));
+
+	fd = open(tracepath, O_RDWR | O_CREAT | O_TRUNC, 0644);
+	ATF_REQUIRE_MSG(fd >= 0, "open failed: %s", strerror(errno));
+	error = ktrace(tracepath, KTROP_SET, KTRFAC_GENIO, getpid());
+	ATF_REQUIRE_MSG(error == 0,
+	    "ktrace(SET) failed: %s", strerror(errno));
+
+	iov.iov_base = buf;
+	iov.iov_len = sizeof(buf) - 1; /* truncate */
+	memset(&msg, 0, sizeof(msg));
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	nbytes = recvmsg(sd[1], &msg, MSG_TRUNC);
+	ATF_REQUIRE_MSG(nbytes >= 0, "recvmsg failed: %s", strerror(errno));
+	ATF_REQUIRE((size_t)nbytes == sizeof(buf));
+	ATF_REQUIRE((msg.msg_flags & MSG_TRUNC) != 0);
+
+	error = ktrace(tracepath, KTROP_CLEARFILE, 0, getpid());
+	ATF_REQUIRE_MSG(error == 0,
+	    "ktrace(CLEARFILE) failed: %s", strerror(errno));
+
+	nbytes = read(fd, &ktr, sizeof(ktr));
+	ATF_REQUIRE_MSG(nbytes >= 0, "read failed: %s", strerror(errno));
+	ATF_REQUIRE((size_t)nbytes == sizeof(ktr));
+	ATF_REQUIRE_MSG((ktr.ktr_type & ~KTR_TYPE) == KTR_GENIO);
+
+	ATF_REQUIRE(close(fd) == 0);
+	ATF_REQUIRE(close(sd[0]) == 0);
+	ATF_REQUIRE(close(sd[1]) == 0);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, recv_trunc_afinet_udp);
 	ATF_TP_ADD_TC(tp, recv_trunc_afinet6_udp);
 	ATF_TP_ADD_TC(tp, recv_trunc_afunix_dgram);
 	ATF_TP_ADD_TC(tp, recv_trunc_afunix_seqpacket);
+	ATF_TP_ADD_TC(tp, recvmsg_trunc_ktrace_uio);
 
 	return (atf_no_error());
 }