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