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