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