xref: /xnu-8796.121.2/tests/scm_rights_leak.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 
32 #include <stdlib.h>
33 #include <unistd.h>
34 
35 #include <darwintest.h>
36 
37 #define MAX_SOCK 10
38 
39 T_DECL(scm_rights_leak, "test leak of file pointers by peeking SCM_RIGHTS")
40 {
41 	int pair[2] = { -1, -1 };
42 
43 	T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_STREAM, 0, pair),
44 	    NULL);
45 
46 	struct cmsghdr *cmsg = NULL;
47 	T_ASSERT_NOTNULL(cmsg = calloc(1, CMSG_SPACE(MAX_SOCK * sizeof(int))), "calloc");
48 	cmsg->cmsg_len = CMSG_LEN(MAX_SOCK * sizeof(int));
49 	cmsg->cmsg_level = SOL_SOCKET;
50 	cmsg->cmsg_type = SCM_RIGHTS;
51 	T_LOG("send cmsg_len %u", cmsg->cmsg_len);
52 
53 	int *sock_fds = (int *)(void *)CMSG_DATA(cmsg);
54 	for (int i = 0; i < MAX_SOCK; i++) {
55 		T_ASSERT_POSIX_SUCCESS(sock_fds[i] = socket(AF_UNIX, SOCK_DGRAM, 0), NULL);
56 	}
57 	for (int i = 0; i < MAX_SOCK; i++) {
58 		fprintf(stderr, "sock_fds[%d] %i\n", i, sock_fds[i]);
59 	}
60 
61 	char data = 'x';
62 	struct iovec iovec = { .iov_base = &data, .iov_len = 1 };
63 
64 	struct msghdr mh;
65 	mh.msg_name = 0;
66 	mh.msg_namelen = 0;
67 	mh.msg_iov = &iovec;
68 	mh.msg_iovlen = 1;
69 	mh.msg_control = cmsg;
70 	mh.msg_controllen = cmsg->cmsg_len;
71 	mh.msg_flags = 0;
72 
73 	ssize_t ssize;
74 	ssize = sendmsg(pair[0], &mh, 0);
75 	T_ASSERT_EQ(ssize, (ssize_t)1, "sendmsg");
76 
77 	/* Allocate twice the size of the worst case */
78 	socklen_t rcmsg_size = CMSG_SPACE(MAX_SOCK * 2 * sizeof(uintptr_t));
79 	struct cmsghdr *rcmsg = NULL;
80 	T_EXPECT_POSIX_SUCCESS_(rcmsg = calloc(1, rcmsg_size), "calloc");
81 
82 	mh.msg_name = 0;
83 	mh.msg_namelen = 0;
84 	mh.msg_iov = &iovec;
85 	mh.msg_iovlen = 1;
86 	mh.msg_control = rcmsg;
87 	mh.msg_controllen = rcmsg_size;
88 	mh.msg_flags = 0;
89 
90 	ssize = recvmsg(pair[1], &mh, MSG_PEEK);
91 	T_ASSERT_POSIX_SUCCESS(ssize, "recvmsg");
92 	T_LOG("recvmsg MSG_PEEK cmsg_len %u", rcmsg->cmsg_len);
93 
94 	uintptr_t *r_ptrs = (uintptr_t *)(void *)CMSG_DATA(rcmsg);
95 	socklen_t nptrs = (rcmsg->cmsg_len - CMSG_LEN(0)) / sizeof(uintptr_t);
96 	for (socklen_t i = 0; i < nptrs; i++) {
97 		T_EXPECT_EQ(r_ptrs[i], (uintptr_t)0, "r_ptrs[%u] 0x%lx\n", i, r_ptrs[i]);
98 	}
99 
100 	mh.msg_name = 0;
101 	mh.msg_namelen = 0;
102 	mh.msg_iov = &iovec;
103 	mh.msg_iovlen = 1;
104 	mh.msg_control = rcmsg;
105 	mh.msg_controllen = rcmsg_size;
106 	mh.msg_flags = 0;
107 
108 	ssize = recvmsg(pair[1], &mh, 0);
109 	T_ASSERT_POSIX_SUCCESS(ssize, "recvmsg");
110 	T_LOG("recvmsg cmsg_len %u", rcmsg->cmsg_len);
111 
112 	int *r_fds = (int *)(void *)CMSG_DATA(rcmsg);
113 	socklen_t nfds = (rcmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
114 	T_ASSERT_EQ(nfds, MAX_SOCK, "number received fds %u == %u", nfds, MAX_SOCK);
115 	for (socklen_t i = 0; i < nfds; i++) {
116 		T_EXPECT_NE(r_fds[i], 0, "r_fds[%d] %i\n", i, r_fds[i]);
117 	}
118 
119 	free(cmsg);
120 	free(rcmsg);
121 	close(pair[0]);
122 	close(pair[1]);
123 }
124