1 #include <sys/socket.h>
2 #include <sys/un.h>
3
4 #include <string.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "unveil.h"
9
10 static int
test_bind_unix_socket(int do_uv)11 test_bind_unix_socket(int do_uv)
12 {
13 struct sockaddr_un sun1, sun2, sun3;
14 char *path1, *path2, *path3;
15 int c_fd1, c_fd2, fd1, fd2, fd3;
16
17 char uv_dir3[] = "/tmp/uvdir3.XXXXXX";
18
19 if (asprintf(&path1, "%s/1.sock", uv_dir1) == -1)
20 err(1, NULL);
21 if (asprintf(&path2, "%s/2.sock", uv_dir2) == -1)
22 err(1, NULL);
23 if (asprintf(&path3, "%s/3.sock", uv_dir3) == -1)
24 err(1, NULL);
25
26 memset(&sun1, 0, sizeof(sun1));
27 sun1.sun_family = AF_UNIX;
28 strlcpy(sun1.sun_path, path1, sizeof(sun1.sun_path));
29
30 memset(&sun2, 0, sizeof(sun2));
31 sun2.sun_family = AF_UNIX;
32 strlcpy(sun2.sun_path, path2, sizeof(sun2.sun_path));
33
34 memset(&sun3, 0, sizeof(sun3));
35 sun3.sun_family = AF_UNIX;
36 strlcpy(sun3.sun_path, path3, sizeof(sun3.sun_path));
37
38 if (unlink(path1) == -1)
39 if (errno != ENOENT) {
40 warn("%s: unlink %s", __func__, path1);
41 return -1;
42 }
43 if (unlink(path2) == -1)
44 if (errno != ENOENT) {
45 warn("%s: unlink %s", __func__, path2);
46 return -1;
47 }
48 if (unlink(path3) == -1)
49 if (errno != ENOENT) {
50 warn("%s: unlink %s", __func__, path3);
51 return -1;
52 }
53
54 if (do_uv) {
55 printf("testing bind and connect on unix socket\n");
56 /* printf("testing bind on unix socket %s and %s\n", path1, path2); */
57 if (unveil(uv_dir1, "wc") == -1) /* both bind and connect work */
58 err(1, "unveil");
59 if (unveil(uv_dir2, "c") == -1) /* bind works, connect fails */
60 err(1, "unveil");
61 if (unveil(uv_dir3, "") == -1) /* no bind, dont test anything else */
62 err(1, "unveil");
63 }
64
65 if ((fd1 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
66 err(1, "%s: socket", __func__);
67 UV_SHOULD_SUCCEED(
68 (bind(fd1, (struct sockaddr *)&sun1, sizeof(sun1)) == -1), "bind");
69 if (listen(fd1, 5) == -1)
70 err(1, "%s: listen", __func__);
71
72 if ((fd2 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
73 err(1, "%s: socket", __func__);
74 UV_SHOULD_SUCCEED(
75 (bind(fd2, (struct sockaddr *)&sun2, sizeof(sun2)) == -1), "bind");
76 if (listen(fd2, 5) == -1)
77 err(1, "%s: listen", __func__);
78
79 if ((fd3 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
80 err(1, "%s: socket", __func__);
81 UV_SHOULD_ENOENT(
82 (bind(fd3, (struct sockaddr *)&sun3, sizeof(sun3)) == -1), "bind");
83
84 /* Connect to control socket. */
85
86 if ((c_fd1 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
87 err(1, "socket");
88 UV_SHOULD_SUCCEED(
89 (connect(c_fd1, (struct sockaddr *)&sun1, sizeof(sun1)) == -1),
90 "connect");
91
92 if ((c_fd2 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
93 err(1, "socket");
94 UV_SHOULD_EACCES(
95 (connect(c_fd2, (struct sockaddr *)&sun2, sizeof(sun2)) == -1),
96 "connect");
97
98 close(fd1);
99 close(c_fd1);
100 close(fd2);
101 close(c_fd2);
102 return 0;
103 }
104
105 int
main(void)106 main(void)
107 {
108 int failures = 0;
109
110 test_setup();
111
112 failures += runcompare(test_bind_unix_socket);
113 exit(failures);
114 }
115