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