xref: /xnu-11215.61.5/tests/uds-to-self.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions  *  uds-to-self.c
3*4f1223e8SApple OSS Distributions  *
4*4f1223e8SApple OSS Distributions  *  Test for rdar://137498122 (panic when calling unp_get_locks_in_order without checking if the 2 sockets are equal).
5*4f1223e8SApple OSS Distributions  */
6*4f1223e8SApple OSS Distributions 
7*4f1223e8SApple OSS Distributions #include <sys/socket.h>
8*4f1223e8SApple OSS Distributions #include <sys/ucred.h>
9*4f1223e8SApple OSS Distributions #include <sys/un.h>
10*4f1223e8SApple OSS Distributions 
11*4f1223e8SApple OSS Distributions #include <limits.h>
12*4f1223e8SApple OSS Distributions #include <stdio.h>
13*4f1223e8SApple OSS Distributions #include <string.h>
14*4f1223e8SApple OSS Distributions #include <unistd.h>
15*4f1223e8SApple OSS Distributions 
16*4f1223e8SApple OSS Distributions #include <darwintest.h>
17*4f1223e8SApple OSS Distributions 
18*4f1223e8SApple OSS Distributions static char buffer[LINE_MAX];
19*4f1223e8SApple OSS Distributions 
20*4f1223e8SApple OSS Distributions #define FILE_PATH "/tmp/uds-to-self.sock"
21*4f1223e8SApple OSS Distributions 
22*4f1223e8SApple OSS Distributions T_GLOBAL_META(
23*4f1223e8SApple OSS Distributions 	T_META_NAMESPACE("xnu.net"),
24*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
25*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("networking"),
26*4f1223e8SApple OSS Distributions 	T_META_CHECK_LEAKS(false));
27*4f1223e8SApple OSS Distributions 
28*4f1223e8SApple OSS Distributions 
29*4f1223e8SApple OSS Distributions T_DECL(uds_self_connection, "self-connecting Unix domain sockets")
30*4f1223e8SApple OSS Distributions {
31*4f1223e8SApple OSS Distributions 	int fd;
32*4f1223e8SApple OSS Distributions 	struct sockaddr_un sun = { 0 };
33*4f1223e8SApple OSS Distributions 	socklen_t solen;
34*4f1223e8SApple OSS Distributions 	ssize_t nsent;
35*4f1223e8SApple OSS Distributions 	ssize_t nrcvd;
36*4f1223e8SApple OSS Distributions 	struct xucred xucred;
37*4f1223e8SApple OSS Distributions 	pid_t pid;
38*4f1223e8SApple OSS Distributions 	uuid_t uuid;
39*4f1223e8SApple OSS Distributions 	audit_token_t token;
40*4f1223e8SApple OSS Distributions 
41*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd = socket(AF_UNIX, SOCK_DGRAM, 0), NULL);
42*4f1223e8SApple OSS Distributions 
43*4f1223e8SApple OSS Distributions 	sun.sun_family = AF_UNIX;
44*4f1223e8SApple OSS Distributions 	snprintf(sun.sun_path, sizeof(sun.sun_path), FILE_PATH);
45*4f1223e8SApple OSS Distributions 	sun.sun_len = (unsigned char) SUN_LEN(&sun);
46*4f1223e8SApple OSS Distributions 
47*4f1223e8SApple OSS Distributions 	unlink(FILE_PATH);
48*4f1223e8SApple OSS Distributions 
49*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(bind(fd, (struct sockaddr *)&sun, sun.sun_len), NULL);
50*4f1223e8SApple OSS Distributions 
51*4f1223e8SApple OSS Distributions 	solen = sizeof(struct sockaddr_un);
52*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getsockname(fd, (struct sockaddr *)&sun, &solen), NULL);
53*4f1223e8SApple OSS Distributions 	T_LOG("socket bound to %s", sun.sun_path);
54*4f1223e8SApple OSS Distributions 
55*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(connect(fd, (struct sockaddr *)&sun, sun.sun_len), NULL);
56*4f1223e8SApple OSS Distributions 
57*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(getpeername(fd, (struct sockaddr *)&sun, &solen), NULL);
58*4f1223e8SApple OSS Distributions 	T_LOG("socket connected to %s", sun.sun_path);
59*4f1223e8SApple OSS Distributions 
60*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(nsent = send(fd, buffer, strlen(buffer) + 1, 0), NULL);
61*4f1223e8SApple OSS Distributions 	T_LOG("send %ld bytes\n", nsent);
62*4f1223e8SApple OSS Distributions 
63*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(nrcvd = recv(fd, buffer, sizeof(buffer), 0), NULL);
64*4f1223e8SApple OSS Distributions 
65*4f1223e8SApple OSS Distributions 	/*
66*4f1223e8SApple OSS Distributions 	 * The return value of getsockopt() is not important, what matters is to not panic
67*4f1223e8SApple OSS Distributions 	 */
68*4f1223e8SApple OSS Distributions 	solen = sizeof(xucred);
69*4f1223e8SApple OSS Distributions 	(void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERCRED, &xucred, &solen);
70*4f1223e8SApple OSS Distributions 
71*4f1223e8SApple OSS Distributions 	solen = sizeof(pid);
72*4f1223e8SApple OSS Distributions 	(void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &pid, &solen);
73*4f1223e8SApple OSS Distributions 
74*4f1223e8SApple OSS Distributions 	solen = sizeof(pid);
75*4f1223e8SApple OSS Distributions 	(void)getsockopt(fd, SOL_LOCAL, LOCAL_PEEREPID, &pid, &solen);
76*4f1223e8SApple OSS Distributions 
77*4f1223e8SApple OSS Distributions 	solen = sizeof(uuid);
78*4f1223e8SApple OSS Distributions 	(void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERUUID, &uuid, &solen);
79*4f1223e8SApple OSS Distributions 
80*4f1223e8SApple OSS Distributions 	solen = sizeof(uuid);
81*4f1223e8SApple OSS Distributions 	(void)getsockopt(fd, SOL_LOCAL, LOCAL_PEEREUUID, &uuid, &solen);
82*4f1223e8SApple OSS Distributions 
83*4f1223e8SApple OSS Distributions 	solen = sizeof(token);
84*4f1223e8SApple OSS Distributions 	(void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERTOKEN, &token, &solen);
85*4f1223e8SApple OSS Distributions 
86*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(shutdown(fd, SHUT_RDWR), NULL);
87*4f1223e8SApple OSS Distributions 
88*4f1223e8SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(fd), NULL);
89*4f1223e8SApple OSS Distributions 
90*4f1223e8SApple OSS Distributions 	unlink(FILE_PATH);
91*4f1223e8SApple OSS Distributions }
92