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