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