| @@ -1,400 +1,396 @@ | | | @@ -1,400 +1,396 @@ |
1 | /* $NetBSD: t_unix.c,v 1.18 2019/04/14 01:45:30 christos Exp $ */ | | 1 | /* $NetBSD: t_unix.c,v 1.19 2020/07/06 16:24:06 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Christos Zoulas. | | 8 | * by Christos Zoulas. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. All advertising materials mentioning features or use of this software | | 18 | * 3. All advertising materials mentioning features or use of this software |
19 | * must display the following acknowledgement: | | 19 | * must display the following acknowledgement: |
20 | * This product includes software developed by the NetBSD | | 20 | * This product includes software developed by the NetBSD |
21 | * Foundation, Inc. and its contributors. | | 21 | * Foundation, Inc. and its contributors. |
22 | * 4. Neither the name of The NetBSD Foundation nor the names of its | | 22 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
23 | * contributors may be used to endorse or promote products derived | | 23 | * contributors may be used to endorse or promote products derived |
24 | * from this software without specific prior written permission. | | 24 | * from this software without specific prior written permission. |
25 | * | | 25 | * |
26 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 26 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
27 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 27 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
28 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 28 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
30 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 30 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
36 | * POSSIBILITY OF SUCH DAMAGE. | | 36 | * POSSIBILITY OF SUCH DAMAGE. |
37 | */ | | 37 | */ |
38 | | | 38 | |
39 | #include <sys/cdefs.h> | | 39 | #include <sys/cdefs.h> |
40 | #ifdef __RCSID | | 40 | #ifdef __RCSID |
41 | __RCSID("$Id: t_unix.c,v 1.18 2019/04/14 01:45:30 christos Exp $"); | | 41 | __RCSID("$Id: t_unix.c,v 1.19 2020/07/06 16:24:06 christos Exp $"); |
42 | #else | | 42 | #else |
43 | #define getprogname() argv[0] | | 43 | #define getprogname() argv[0] |
44 | #endif | | 44 | #endif |
45 | | | 45 | |
46 | #ifdef __linux__ | | 46 | #ifdef __linux__ |
47 | #define LX -1 | | 47 | #define LX -1 |
48 | #else | | 48 | #else |
49 | #define LX | | 49 | #define LX |
50 | #endif | | 50 | #endif |
51 | #include <sys/param.h> | | 51 | #include <sys/param.h> |
52 | #include <sys/socket.h> | | 52 | #include <sys/socket.h> |
53 | #include <sys/un.h> | | 53 | #include <sys/un.h> |
54 | #include <sys/wait.h> | | 54 | #include <sys/wait.h> |
55 | #include <stdio.h> | | 55 | #include <stdio.h> |
56 | #include <err.h> | | 56 | #include <err.h> |
57 | #include <errno.h> | | 57 | #include <errno.h> |
58 | #include <string.h> | | 58 | #include <string.h> |
59 | #include <stddef.h> | | 59 | #include <stddef.h> |
60 | #include <stdlib.h> | | 60 | #include <stdlib.h> |
61 | #include <unistd.h> | | 61 | #include <unistd.h> |
62 | #include <stdbool.h> | | 62 | #include <stdbool.h> |
63 | | | 63 | |
64 | #include "test.h" | | 64 | #include "test.h" |
65 | | | 65 | |
66 | #define OF offsetof(struct sockaddr_un, sun_path) | | 66 | #define OF offsetof(struct sockaddr_un, sun_path) |
67 | | | 67 | |
68 | static void | | 68 | static void |
69 | print(const char *msg, struct sockaddr_un *addr, socklen_t len) | | 69 | print(const char *msg, struct sockaddr_un *addr, socklen_t len) |
70 | { | | 70 | { |
71 | size_t i; | | 71 | size_t i; |
72 | | | 72 | |
73 | printf("%s: client socket length: %zu\n", msg, (size_t)len); | | 73 | printf("%s: client socket length: %zu\n", msg, (size_t)len); |
74 | printf("%s: client family %d\n", msg, addr->sun_family); | | 74 | printf("%s: client family %d\n", msg, addr->sun_family); |
75 | #ifdef BSD4_4 | | 75 | #ifdef BSD4_4 |
76 | printf("%s: client len %d\n", msg, addr->sun_len); | | 76 | printf("%s: client len %d\n", msg, addr->sun_len); |
77 | #endif | | 77 | #endif |
78 | printf("%s: socket name: ", msg); | | 78 | printf("%s: socket name: ", msg); |
79 | for (i = 0; i < len - OF; i++) { | | 79 | for (i = 0; i < len - OF; i++) { |
80 | int ch = addr->sun_path[i]; | | 80 | int ch = addr->sun_path[i]; |
81 | if (ch < ' ' || '~' < ch) | | 81 | if (ch < ' ' || '~' < ch) |
82 | printf("\\x%02x", ch); | | 82 | printf("\\x%02x", ch); |
83 | else | | 83 | else |
84 | printf("%c", ch); | | 84 | printf("%c", ch); |
85 | } | | 85 | } |
86 | printf("\n"); | | 86 | printf("\n"); |
87 | } | | 87 | } |
88 | | | 88 | |
89 | static int | | 89 | static int |
90 | acc(int s) | | 90 | acc(int s) |
91 | { | | 91 | { |
92 | char guard1; | | 92 | char guard1; |
93 | struct sockaddr_un sun; | | 93 | struct sockaddr_un sun; |
94 | char guard2; | | 94 | char guard2; |
95 | socklen_t len; | | 95 | socklen_t len; |
96 | | | 96 | |
97 | guard1 = guard2 = 's'; | | 97 | guard1 = guard2 = 's'; |
98 | | | 98 | |
99 | memset(&sun, 0, sizeof(sun)); | | 99 | memset(&sun, 0, sizeof(sun)); |
100 | len = sizeof(sun); | | 100 | len = sizeof(sun); |
101 | if ((s = accept(s, (struct sockaddr *)&sun, &len)) == -1) | | 101 | if ((s = accept(s, (struct sockaddr *)&sun, &len)) == -1) |
102 | FAIL("accept"); | | 102 | FAIL("accept"); |
103 | if (guard1 != 's') | | 103 | if (guard1 != 's') |
104 | FAIL("guard1 = '%c'", guard1); | | 104 | FAIL("guard1 = '%c'", guard1); |
105 | if (guard2 != 's') | | 105 | if (guard2 != 's') |
106 | FAIL("guard2 = '%c'", guard2); | | 106 | FAIL("guard2 = '%c'", guard2); |
107 | #ifdef DEBUG | | 107 | #ifdef DEBUG |
108 | print("accept", &sun, len); | | 108 | print("accept", &sun, len); |
109 | #endif | | 109 | #endif |
110 | if (len != 2) | | 110 | if (len != 2) |
111 | FAIL("len %d != 2", len); | | 111 | FAIL("len %d != 2", len); |
112 | if (sun.sun_family != AF_UNIX) | | 112 | if (sun.sun_family != AF_UNIX) |
113 | FAIL("sun->sun_family %d != AF_UNIX", sun.sun_family); | | 113 | FAIL("sun->sun_family %d != AF_UNIX", sun.sun_family); |
114 | #ifdef BSD4_4 | | 114 | #ifdef BSD4_4 |
115 | if (sun.sun_len != 2) | | 115 | if (sun.sun_len != 2) |
116 | FAIL("sun->sun_len %d != 2", sun.sun_len); | | 116 | FAIL("sun->sun_len %d != 2", sun.sun_len); |
117 | #endif | | 117 | #endif |
118 | for (size_t i = 0; i < sizeof(sun.sun_path); i++) | | 118 | for (size_t i = 0; i < sizeof(sun.sun_path); i++) |
119 | if (sun.sun_path[i]) | | 119 | if (sun.sun_path[i]) |
120 | FAIL("sun.sun_path[%zu] %d != NULL", i, | | 120 | FAIL("sun.sun_path[%zu] %d != NULL", i, |
121 | sun.sun_path[i]); | | 121 | sun.sun_path[i]); |
122 | return s; | | 122 | return s; |
123 | fail: | | 123 | fail: |
124 | if (s != -1) | | 124 | if (s != -1) |
125 | close(s); | | 125 | close(s); |
126 | return -1; | | 126 | return -1; |
127 | } | | 127 | } |
128 | | | 128 | |
129 | static int | | 129 | static int |
130 | peercred(int s, uid_t *euid, gid_t *egid, pid_t *pid) | | 130 | peercred(int s, uid_t *euid, gid_t *egid, pid_t *pid) |
131 | { | | 131 | { |
132 | #ifdef LOCAL_PEEREID | | 132 | #ifdef LOCAL_PEEREID |
133 | struct unpcbid cred; | | 133 | struct unpcbid cred; |
134 | socklen_t crl; | | 134 | socklen_t crl; |
135 | crl = sizeof(cred); | | 135 | crl = sizeof(cred); |
136 | if (getsockopt(s, 0, LOCAL_PEEREID, &cred, &crl) == -1) | | 136 | if (getsockopt(s, 0, LOCAL_PEEREID, &cred, &crl) == -1) |
137 | return -1; | | 137 | return -1; |
138 | *euid = cred.unp_euid; | | 138 | *euid = cred.unp_euid; |
139 | *egid = cred.unp_egid; | | 139 | *egid = cred.unp_egid; |
140 | *pid = cred.unp_pid; | | 140 | *pid = cred.unp_pid; |
141 | return 0; | | 141 | return 0; |
142 | #else | | 142 | #else |
143 | *pid = -1; | | 143 | *pid = -1; |
144 | return getpeereid(s, euid, egid); | | 144 | return getpeereid(s, euid, egid); |
145 | #endif | | 145 | #endif |
146 | } | | 146 | } |
147 | | | 147 | |
148 | static int | | 148 | static int |
149 | test(bool forkit, bool closeit, size_t len) | | 149 | test(bool forkit, bool closeit, size_t len) |
150 | { | | 150 | { |
151 | size_t slen; | | 151 | size_t slen; |
152 | socklen_t sl; | | 152 | socklen_t sl; |
153 | int srvr = -1, clnt = -1, acpt = -1; | | 153 | int srvr = -1, clnt = -1, acpt = -1; |
154 | uid_t euid; | | 154 | uid_t euid; |
155 | gid_t egid; | | 155 | gid_t egid; |
156 | pid_t srvrpid, clntpid; | | 156 | pid_t srvrpid, clntpid; |
157 | struct sockaddr_un *sock_addr = NULL, *sun = NULL; | | 157 | struct sockaddr_un *sock_addr = NULL, *sun = NULL; |
158 | socklen_t sock_addrlen; | | 158 | socklen_t sock_addrlen; |
159 | socklen_t peer_addrlen; | | 159 | socklen_t peer_addrlen; |
160 | struct sockaddr_un peer_addr; | | 160 | struct sockaddr_un peer_addr; |
161 | | | 161 | |
162 | srvrpid = clntpid = getpid(); | | 162 | srvrpid = clntpid = getpid(); |
163 | srvr = socket(AF_UNIX, SOCK_STREAM, 0); | | 163 | srvr = socket(AF_UNIX, SOCK_STREAM, 0); |
164 | if (srvr == -1) | | 164 | if (srvr == -1) |
165 | FAIL("socket(srvrer)"); | | 165 | FAIL("socket(server)"); |
166 | | | 166 | |
167 | slen = len + OF + 1; | | 167 | slen = len + OF + 1; |
168 | | | 168 | |
169 | if ((sun = calloc(1, slen)) == NULL) | | 169 | if ((sun = calloc(1, slen)) == NULL) |
170 | FAIL("calloc"); | | 170 | FAIL("calloc"); |
171 | | | 171 | |
172 | srvr = socket(AF_UNIX, SOCK_STREAM, 0); | | | |
173 | if (srvr == -1) | | | |
174 | FAIL("socket"); | | | |
175 | | | | |
176 | memset(sun->sun_path, 'a', len); | | 172 | memset(sun->sun_path, 'a', len); |
177 | sun->sun_path[len] = '\0'; | | 173 | sun->sun_path[len] = '\0'; |
178 | (void)unlink(sun->sun_path); | | 174 | (void)unlink(sun->sun_path); |
179 | | | 175 | |
180 | sl = SUN_LEN(sun); | | 176 | sl = SUN_LEN(sun); |
181 | #ifdef BSD4_4 | | 177 | #ifdef BSD4_4 |
182 | sun->sun_len = sl; | | 178 | sun->sun_len = sl; |
183 | #endif | | 179 | #endif |
184 | sun->sun_family = AF_UNIX; | | 180 | sun->sun_family = AF_UNIX; |
185 | | | 181 | |
186 | if (bind(srvr, (struct sockaddr *)sun, sl) == -1) { | | 182 | if (bind(srvr, (struct sockaddr *)sun, sl) == -1) { |
187 | if (errno == EINVAL && sl >= 256) { | | 183 | if (errno == EINVAL && sl >= 256) { |
188 | close(srvr); | | 184 | close(srvr); |
189 | return -1; | | 185 | return -1; |
190 | } | | 186 | } |
191 | FAIL("bind"); | | 187 | FAIL("bind"); |
192 | } | | 188 | } |
193 | | | 189 | |
194 | if (listen(srvr, SOMAXCONN) == -1) | | 190 | if (listen(srvr, SOMAXCONN) == -1) |
195 | FAIL("listen"); | | 191 | FAIL("listen"); |
196 | | | 192 | |
197 | if (forkit) { | | 193 | if (forkit) { |
198 | switch (clntpid = fork()) { | | 194 | switch (clntpid = fork()) { |
199 | case 0: /* child */ | | 195 | case 0: /* child */ |
200 | srvrpid = getppid(); | | 196 | srvrpid = getppid(); |
201 | clntpid = getpid(); | | 197 | clntpid = getpid(); |
202 | break; | | 198 | break; |
203 | case -1: | | 199 | case -1: |
204 | FAIL("fork"); | | 200 | FAIL("fork"); |
205 | default: | | 201 | default: |
206 | break; | | 202 | break; |
207 | } | | 203 | } |
208 | } | | 204 | } |
209 | | | 205 | |
210 | if (clntpid == getpid()) { | | 206 | if (clntpid == getpid()) { |
211 | pid_t pid = srvrpid; | | 207 | pid_t pid = srvrpid; |
212 | clnt = socket(AF_UNIX, SOCK_STREAM, 0); | | 208 | clnt = socket(AF_UNIX, SOCK_STREAM, 0); |
213 | if (clnt == -1) | | 209 | if (clnt == -1) |
214 | FAIL("socket(client)"); | | 210 | FAIL("socket(client)"); |
215 | | | 211 | |
216 | if (connect(clnt, (const struct sockaddr *)sun, sl) == -1) | | 212 | if (connect(clnt, (const struct sockaddr *)sun, sl) == -1) |
217 | FAIL("connect"); | | 213 | FAIL("connect"); |
218 | | | 214 | |
219 | if (peercred(clnt, &euid, &egid, &pid) == -1) | | 215 | if (peercred(clnt, &euid, &egid, &pid) == -1) |
220 | FAIL("peercred (client)"); | | 216 | FAIL("peercred (client)"); |
221 | printf("peercred(client) euid=%jd egid=%jd pid=%jd\n", | | 217 | printf("peercred(client) euid=%jd egid=%jd pid=%jd\n", |
222 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); | | 218 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); |
223 | CHECK_EQUAL(euid, geteuid(), "client"); | | 219 | CHECK_EQUAL(euid, geteuid(), "client"); |
224 | CHECK_EQUAL(egid, getegid(), "client"); | | 220 | CHECK_EQUAL(egid, getegid(), "client"); |
225 | CHECK_EQUAL(pid, srvrpid, "client"); | | 221 | CHECK_EQUAL(pid, srvrpid, "client"); |
226 | } | | 222 | } |
227 | | | 223 | |
228 | if (srvrpid == getpid()) { | | 224 | if (srvrpid == getpid()) { |
229 | acpt = acc(srvr); | | 225 | acpt = acc(srvr); |
230 | | | 226 | |
231 | peer_addrlen = sizeof(peer_addr); | | 227 | peer_addrlen = sizeof(peer_addr); |
232 | memset(&peer_addr, 0, sizeof(peer_addr)); | | 228 | memset(&peer_addr, 0, sizeof(peer_addr)); |
233 | if (getpeername(acpt, (struct sockaddr *)&peer_addr, | | 229 | if (getpeername(acpt, (struct sockaddr *)&peer_addr, |
234 | &peer_addrlen) == -1) | | 230 | &peer_addrlen) == -1) |
235 | FAIL("getpeername"); | | 231 | FAIL("getpeername"); |
236 | print("peer", &peer_addr, peer_addrlen); | | 232 | print("peer", &peer_addr, peer_addrlen); |
237 | } | | 233 | } |
238 | | | 234 | |
239 | if (clntpid == getpid()) { | | 235 | if (clntpid == getpid()) { |
240 | if (closeit) { | | 236 | if (closeit) { |
241 | if (close(clnt) == -1) | | 237 | if (close(clnt) == -1) |
242 | FAIL("close"); | | 238 | FAIL("close"); |
243 | clnt = -1; | | 239 | clnt = -1; |
244 | } | | 240 | } |
245 | } | | 241 | } |
246 | | | 242 | |
247 | if (srvrpid == getpid()) { | | 243 | if (srvrpid == getpid()) { |
248 | pid_t pid = clntpid; | | 244 | pid_t pid = clntpid; |
249 | if (peercred(acpt, &euid, &egid, &pid) == -1) | | 245 | if (peercred(acpt, &euid, &egid, &pid) == -1) |
250 | FAIL("peercred (server)"); | | 246 | FAIL("peercred (server)"); |
251 | printf("peercred(server) euid=%jd egid=%jd pid=%jd\n", | | 247 | printf("peercred(server) euid=%jd egid=%jd pid=%jd\n", |
252 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); | | 248 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); |
253 | CHECK_EQUAL(euid, geteuid(), "server"); | | 249 | CHECK_EQUAL(euid, geteuid(), "server"); |
254 | CHECK_EQUAL(egid, getegid(), "server"); | | 250 | CHECK_EQUAL(egid, getegid(), "server"); |
255 | CHECK_EQUAL(pid, clntpid, "server"); | | 251 | CHECK_EQUAL(pid, clntpid, "server"); |
256 | | | 252 | |
257 | if ((sock_addr = calloc(1, slen)) == NULL) | | 253 | if ((sock_addr = calloc(1, slen)) == NULL) |
258 | FAIL("calloc"); | | 254 | FAIL("calloc"); |
259 | sock_addrlen = slen; | | 255 | sock_addrlen = slen; |
260 | if (getsockname(srvr, (struct sockaddr *)sock_addr, | | 256 | if (getsockname(srvr, (struct sockaddr *)sock_addr, |
261 | &sock_addrlen) == -1) | | 257 | &sock_addrlen) == -1) |
262 | FAIL("getsockname"); | | 258 | FAIL("getsockname"); |
263 | print("sock", sock_addr, sock_addrlen); | | 259 | print("sock", sock_addr, sock_addrlen); |
264 | | | 260 | |
265 | if (sock_addr->sun_family != AF_UNIX) | | 261 | if (sock_addr->sun_family != AF_UNIX) |
266 | FAIL("sock_addr->sun_family %d != AF_UNIX", | | 262 | FAIL("sock_addr->sun_family %d != AF_UNIX", |
267 | sock_addr->sun_family); | | 263 | sock_addr->sun_family); |
268 | | | 264 | |
269 | len += OF; | | 265 | len += OF; |
270 | if (sock_addrlen LX != len) | | 266 | if (sock_addrlen LX != len) |
271 | FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen, | | 267 | FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen, |
272 | len); | | 268 | len); |
273 | #ifdef BSD4_4 | | 269 | #ifdef BSD4_4 |
274 | if (sock_addr->sun_len != sl) | | 270 | if (sock_addr->sun_len != sl) |
275 | FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len, | | 271 | FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len, |
276 | (size_t)sl); | | 272 | (size_t)sl); |
277 | #endif | | 273 | #endif |
278 | for (size_t i = 0; i < slen - OF; i++) | | 274 | for (size_t i = 0; i < slen - OF; i++) |
279 | if (sock_addr->sun_path[i] != sun->sun_path[i]) | | 275 | if (sock_addr->sun_path[i] != sun->sun_path[i]) |
280 | FAIL("sock_addr.sun_path[%zu] %d != " | | 276 | FAIL("sock_addr.sun_path[%zu] %d != " |
281 | "sun->sun_path[%zu] %d\n", i, | | 277 | "sun->sun_path[%zu] %d\n", i, |
282 | sock_addr->sun_path[i], i, | | 278 | sock_addr->sun_path[i], i, |
283 | sun->sun_path[i]); | | 279 | sun->sun_path[i]); |
284 | | | 280 | |
285 | if (acpt != -1) | | 281 | if (acpt != -1) |
286 | (void)close(acpt); | | 282 | (void)close(acpt); |
287 | if (srvr != -1) | | 283 | if (srvr != -1) |
288 | (void)close(srvr); | | 284 | (void)close(srvr); |
289 | free(sock_addr); | | 285 | free(sock_addr); |
290 | sock_addr = NULL; | | 286 | sock_addr = NULL; |
291 | if (forkit && waitpid(clntpid, NULL, 0) == -1) | | 287 | if (forkit && waitpid(clntpid, NULL, 0) == -1) |
292 | FAIL("waitpid"); | | 288 | FAIL("waitpid"); |
293 | } | | 289 | } |
294 | if (clnt != -1 && !closeit) | | 290 | if (clnt != -1 && !closeit) |
295 | (void)close(clnt); | | 291 | (void)close(clnt); |
296 | | | 292 | |
297 | free(sock_addr); | | 293 | free(sock_addr); |
298 | free(sun); | | 294 | free(sun); |
299 | return 0; | | 295 | return 0; |
300 | fail: | | 296 | fail: |
301 | if (srvrpid == getpid()) { | | 297 | if (srvrpid == getpid()) { |
302 | if (acpt != -1) | | 298 | if (acpt != -1) |
303 | (void)close(acpt); | | 299 | (void)close(acpt); |
304 | if (srvr != -1) | | 300 | if (srvr != -1) |
305 | (void)close(srvr); | | 301 | (void)close(srvr); |
306 | } | | 302 | } |
307 | if (clntpid == getpid()) { | | 303 | if (clntpid == getpid()) { |
308 | if (clnt != -1 && !closeit) | | 304 | if (clnt != -1 && !closeit) |
309 | (void)close(clnt); | | 305 | (void)close(clnt); |
310 | } | | 306 | } |
311 | free(sock_addr); | | 307 | free(sock_addr); |
312 | free(sun); | | 308 | free(sun); |
313 | return -1; | | 309 | return -1; |
314 | } | | 310 | } |
315 | | | 311 | |
316 | #ifndef TEST | | 312 | #ifndef TEST |
317 | | | 313 | |
318 | ATF_TC(sockaddr_un_len_exceed); | | 314 | ATF_TC(sockaddr_un_len_exceed); |
319 | ATF_TC_HEAD(sockaddr_un_len_exceed, tc) | | 315 | ATF_TC_HEAD(sockaddr_un_len_exceed, tc) |
320 | { | | 316 | { |
321 | | | 317 | |
322 | atf_tc_set_md_var(tc, "descr", "Check that exceeding the size of " | | 318 | atf_tc_set_md_var(tc, "descr", "Check that exceeding the size of " |
323 | "unix domain sockets does not trash memory or kernel when " | | 319 | "unix domain sockets does not trash memory or kernel when " |
324 | "exceeding the size of the fixed sun_path"); | | 320 | "exceeding the size of the fixed sun_path"); |
325 | } | | 321 | } |
326 | | | 322 | |
327 | ATF_TC_BODY(sockaddr_un_len_exceed, tc) | | 323 | ATF_TC_BODY(sockaddr_un_len_exceed, tc) |
328 | { | | 324 | { |
329 | ATF_REQUIRE_MSG(test(false, false, 254) == -1, | | 325 | ATF_REQUIRE_MSG(test(false, false, 254) == -1, |
330 | "test(false, false, 254): %s", strerror(errno)); | | 326 | "test(false, false, 254): %s", strerror(errno)); |
331 | } | | 327 | } |
332 | | | 328 | |
333 | ATF_TC(sockaddr_un_len_max); | | 329 | ATF_TC(sockaddr_un_len_max); |
334 | ATF_TC_HEAD(sockaddr_un_len_max, tc) | | 330 | ATF_TC_HEAD(sockaddr_un_len_max, tc) |
335 | { | | 331 | { |
336 | | | 332 | |
337 | atf_tc_set_md_var(tc, "descr", "Check that we can use the maximum " | | 333 | atf_tc_set_md_var(tc, "descr", "Check that we can use the maximum " |
338 | "unix domain socket pathlen (253): 255 - sizeof(sun_len) - " | | 334 | "unix domain socket pathlen (253): 255 - sizeof(sun_len) - " |
339 | "sizeof(sun_family)"); | | 335 | "sizeof(sun_family)"); |
340 | } | | 336 | } |
341 | | | 337 | |
342 | ATF_TC_BODY(sockaddr_un_len_max, tc) | | 338 | ATF_TC_BODY(sockaddr_un_len_max, tc) |
343 | { | | 339 | { |
344 | ATF_REQUIRE_MSG(test(false, false, 253) == 0, | | 340 | ATF_REQUIRE_MSG(test(false, false, 253) == 0, |
345 | "test(false, false, 253): %s", strerror(errno)); | | 341 | "test(false, false, 253): %s", strerror(errno)); |
346 | } | | 342 | } |
347 | | | 343 | |
348 | ATF_TC(sockaddr_un_closed); | | 344 | ATF_TC(sockaddr_un_closed); |
349 | ATF_TC_HEAD(sockaddr_un_closed, tc) | | 345 | ATF_TC_HEAD(sockaddr_un_closed, tc) |
350 | { | | 346 | { |
351 | | | 347 | |
352 | atf_tc_set_md_var(tc, "descr", "Check that we can use the accepted " | | 348 | atf_tc_set_md_var(tc, "descr", "Check that we can use the accepted " |
353 | "address of unix domain socket when closed"); | | 349 | "address of unix domain socket when closed"); |
354 | } | | 350 | } |
355 | | | 351 | |
356 | ATF_TC_BODY(sockaddr_un_closed, tc) | | 352 | ATF_TC_BODY(sockaddr_un_closed, tc) |
357 | { | | 353 | { |
358 | ATF_REQUIRE_MSG(test(false, true, 100) == 0, | | 354 | ATF_REQUIRE_MSG(test(false, true, 100) == 0, |
359 | "test(false, true, 100): %s", strerror(errno)); | | 355 | "test(false, true, 100): %s", strerror(errno)); |
360 | } | | 356 | } |
361 | | | 357 | |
362 | ATF_TC(sockaddr_un_local_peereid); | | 358 | ATF_TC(sockaddr_un_local_peereid); |
363 | ATF_TC_HEAD(sockaddr_un_local_peereid, tc) | | 359 | ATF_TC_HEAD(sockaddr_un_local_peereid, tc) |
364 | { | | 360 | { |
365 | | | 361 | |
366 | atf_tc_set_md_var(tc, "descr", "Check that we get the right information" | | 362 | atf_tc_set_md_var(tc, "descr", "Check that we get the right information" |
367 | " from LOCAL_PEEREID"); | | 363 | " from LOCAL_PEEREID"); |
368 | } | | 364 | } |
369 | | | 365 | |
370 | ATF_TC_BODY(sockaddr_un_local_peereid, tc) | | 366 | ATF_TC_BODY(sockaddr_un_local_peereid, tc) |
371 | { | | 367 | { |
372 | ATF_REQUIRE_MSG(test(true, true, 100) == 0, | | 368 | ATF_REQUIRE_MSG(test(true, true, 100) == 0, |
373 | "test(true, true, 100): %s", strerror(errno)); | | 369 | "test(true, true, 100): %s", strerror(errno)); |
374 | } | | 370 | } |
375 | | | 371 | |
376 | ATF_TP_ADD_TCS(tp) | | 372 | ATF_TP_ADD_TCS(tp) |
377 | { | | 373 | { |
378 | | | 374 | |
379 | ATF_TP_ADD_TC(tp, sockaddr_un_len_exceed); | | 375 | ATF_TP_ADD_TC(tp, sockaddr_un_len_exceed); |
380 | ATF_TP_ADD_TC(tp, sockaddr_un_len_max); | | 376 | ATF_TP_ADD_TC(tp, sockaddr_un_len_max); |
381 | ATF_TP_ADD_TC(tp, sockaddr_un_closed); | | 377 | ATF_TP_ADD_TC(tp, sockaddr_un_closed); |
382 | ATF_TP_ADD_TC(tp, sockaddr_un_local_peereid); | | 378 | ATF_TP_ADD_TC(tp, sockaddr_un_local_peereid); |
383 | return atf_no_error(); | | 379 | return atf_no_error(); |
384 | } | | 380 | } |
385 | #else | | 381 | #else |
386 | int | | 382 | int |
387 | main(int argc, char *argv[]) | | 383 | main(int argc, char *argv[]) |
388 | { | | 384 | { |
389 | size_t len; | | 385 | size_t len; |
390 | | | 386 | |
391 | if (argc == 1) { | | 387 | if (argc == 1) { |
392 | fprintf(stderr, "Usage: %s <len>\n", getprogname()); | | 388 | fprintf(stderr, "Usage: %s <len>\n", getprogname()); |
393 | return EXIT_FAILURE; | | 389 | return EXIT_FAILURE; |
394 | } | | 390 | } |
395 | test(false, false, atoi(argv[1])); | | 391 | test(false, false, atoi(argv[1])); |
396 | test(false, true, atoi(argv[1])); | | 392 | test(false, true, atoi(argv[1])); |
397 | test(true, false, atoi(argv[1])); | | 393 | test(true, false, atoi(argv[1])); |
398 | test(true, true, atoi(argv[1])); | | 394 | test(true, true, atoi(argv[1])); |
399 | } | | 395 | } |
400 | #endif | | 396 | #endif |