| @@ -1,396 +1,424 @@ | | | @@ -1,396 +1,424 @@ |
1 | /* $NetBSD: t_unix.c,v 1.19 2020/07/06 16:24:06 christos Exp $ */ | | 1 | /* $NetBSD: t_unix.c,v 1.20 2020/08/26 22:52:58 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.19 2020/07/06 16:24:06 christos Exp $"); | | 41 | __RCSID("$Id: t_unix.c,v 1.20 2020/08/26 22:52:58 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 <sys/stat.h> |
55 | #include <stdio.h> | | 56 | #include <stdio.h> |
56 | #include <err.h> | | 57 | #include <err.h> |
57 | #include <errno.h> | | 58 | #include <errno.h> |
58 | #include <string.h> | | 59 | #include <string.h> |
59 | #include <stddef.h> | | 60 | #include <stddef.h> |
60 | #include <stdlib.h> | | 61 | #include <stdlib.h> |
61 | #include <unistd.h> | | 62 | #include <unistd.h> |
62 | #include <stdbool.h> | | 63 | #include <stdbool.h> |
63 | | | 64 | |
64 | #include "test.h" | | 65 | #include "test.h" |
65 | | | 66 | |
66 | #define OF offsetof(struct sockaddr_un, sun_path) | | 67 | #define OF offsetof(struct sockaddr_un, sun_path) |
67 | | | 68 | |
68 | static void | | 69 | static void |
69 | print(const char *msg, struct sockaddr_un *addr, socklen_t len) | | 70 | print(const char *msg, struct sockaddr_un *addr, socklen_t len) |
70 | { | | 71 | { |
71 | size_t i; | | 72 | size_t i; |
72 | | | 73 | |
73 | printf("%s: client socket length: %zu\n", msg, (size_t)len); | | 74 | printf("%s: client socket length: %zu\n", msg, (size_t)len); |
74 | printf("%s: client family %d\n", msg, addr->sun_family); | | 75 | printf("%s: client family %d\n", msg, addr->sun_family); |
75 | #ifdef BSD4_4 | | 76 | #ifdef BSD4_4 |
76 | printf("%s: client len %d\n", msg, addr->sun_len); | | 77 | printf("%s: client len %d\n", msg, addr->sun_len); |
77 | #endif | | 78 | #endif |
78 | printf("%s: socket name: ", msg); | | 79 | printf("%s: socket name: ", msg); |
79 | for (i = 0; i < len - OF; i++) { | | 80 | for (i = 0; i < len - OF; i++) { |
80 | int ch = addr->sun_path[i]; | | 81 | int ch = addr->sun_path[i]; |
81 | if (ch < ' ' || '~' < ch) | | 82 | if (ch < ' ' || '~' < ch) |
82 | printf("\\x%02x", ch); | | 83 | printf("\\x%02x", ch); |
83 | else | | 84 | else |
84 | printf("%c", ch); | | 85 | printf("%c", ch); |
85 | } | | 86 | } |
86 | printf("\n"); | | 87 | printf("\n"); |
87 | } | | 88 | } |
88 | | | 89 | |
89 | static int | | 90 | static int |
90 | acc(int s) | | 91 | acc(int s) |
91 | { | | 92 | { |
92 | char guard1; | | 93 | char guard1; |
93 | struct sockaddr_un sun; | | 94 | struct sockaddr_un sun; |
94 | char guard2; | | 95 | char guard2; |
95 | socklen_t len; | | 96 | socklen_t len; |
96 | | | 97 | |
97 | guard1 = guard2 = 's'; | | 98 | guard1 = guard2 = 's'; |
98 | | | 99 | |
99 | memset(&sun, 0, sizeof(sun)); | | 100 | memset(&sun, 0, sizeof(sun)); |
100 | len = sizeof(sun); | | 101 | len = sizeof(sun); |
101 | if ((s = accept(s, (struct sockaddr *)&sun, &len)) == -1) | | 102 | if ((s = accept(s, (struct sockaddr *)&sun, &len)) == -1) |
102 | FAIL("accept"); | | 103 | FAIL("accept"); |
103 | if (guard1 != 's') | | 104 | if (guard1 != 's') |
104 | FAIL("guard1 = '%c'", guard1); | | 105 | FAIL("guard1 = '%c'", guard1); |
105 | if (guard2 != 's') | | 106 | if (guard2 != 's') |
106 | FAIL("guard2 = '%c'", guard2); | | 107 | FAIL("guard2 = '%c'", guard2); |
107 | #ifdef DEBUG | | 108 | #ifdef DEBUG |
108 | print("accept", &sun, len); | | 109 | print("accept", &sun, len); |
109 | #endif | | 110 | #endif |
110 | if (len != 2) | | 111 | if (len != 2) |
111 | FAIL("len %d != 2", len); | | 112 | FAIL("len %d != 2", len); |
112 | if (sun.sun_family != AF_UNIX) | | 113 | if (sun.sun_family != AF_UNIX) |
113 | FAIL("sun->sun_family %d != AF_UNIX", sun.sun_family); | | 114 | FAIL("sun->sun_family %d != AF_UNIX", sun.sun_family); |
114 | #ifdef BSD4_4 | | 115 | #ifdef BSD4_4 |
115 | if (sun.sun_len != 2) | | 116 | if (sun.sun_len != 2) |
116 | FAIL("sun->sun_len %d != 2", sun.sun_len); | | 117 | FAIL("sun->sun_len %d != 2", sun.sun_len); |
117 | #endif | | 118 | #endif |
118 | for (size_t i = 0; i < sizeof(sun.sun_path); i++) | | 119 | for (size_t i = 0; i < sizeof(sun.sun_path); i++) |
119 | if (sun.sun_path[i]) | | 120 | if (sun.sun_path[i]) |
120 | FAIL("sun.sun_path[%zu] %d != NULL", i, | | 121 | FAIL("sun.sun_path[%zu] %d != NULL", i, |
121 | sun.sun_path[i]); | | 122 | sun.sun_path[i]); |
122 | return s; | | 123 | return s; |
123 | fail: | | 124 | fail: |
124 | if (s != -1) | | 125 | if (s != -1) |
125 | close(s); | | 126 | close(s); |
126 | return -1; | | 127 | return -1; |
127 | } | | 128 | } |
128 | | | 129 | |
129 | static int | | 130 | static int |
130 | peercred(int s, uid_t *euid, gid_t *egid, pid_t *pid) | | 131 | peercred(int s, uid_t *euid, gid_t *egid, pid_t *pid) |
131 | { | | 132 | { |
132 | #ifdef LOCAL_PEEREID | | 133 | #ifdef LOCAL_PEEREID |
133 | struct unpcbid cred; | | 134 | struct unpcbid cred; |
134 | socklen_t crl; | | 135 | socklen_t crl; |
135 | crl = sizeof(cred); | | 136 | crl = sizeof(cred); |
136 | if (getsockopt(s, 0, LOCAL_PEEREID, &cred, &crl) == -1) | | 137 | if (getsockopt(s, 0, LOCAL_PEEREID, &cred, &crl) == -1) |
137 | return -1; | | 138 | return -1; |
138 | *euid = cred.unp_euid; | | 139 | *euid = cred.unp_euid; |
139 | *egid = cred.unp_egid; | | 140 | *egid = cred.unp_egid; |
140 | *pid = cred.unp_pid; | | 141 | *pid = cred.unp_pid; |
141 | return 0; | | 142 | return 0; |
142 | #else | | 143 | #else |
143 | *pid = -1; | | 144 | *pid = -1; |
144 | return getpeereid(s, euid, egid); | | 145 | return getpeereid(s, euid, egid); |
145 | #endif | | 146 | #endif |
146 | } | | 147 | } |
147 | | | 148 | |
148 | static int | | 149 | static int |
149 | test(bool forkit, bool closeit, size_t len) | | 150 | check_cred(int fd, bool statit, pid_t checkpid, const char *s) |
| | | 151 | { |
| | | 152 | pid_t pid; |
| | | 153 | uid_t euid; |
| | | 154 | gid_t egid; |
| | | 155 | |
| | | 156 | if (statit) { |
| | | 157 | struct stat st; |
| | | 158 | if (fstat(fd, &st) == -1) |
| | | 159 | FAIL("fstat (%s)", s); |
| | | 160 | euid = st.st_uid; |
| | | 161 | egid = st.st_gid; |
| | | 162 | pid = checkpid; |
| | | 163 | } else { |
| | | 164 | if (peercred(fd, &euid, &egid, &pid) == -1) |
| | | 165 | FAIL("peercred (%s)", s); |
| | | 166 | } |
| | | 167 | printf("%s(%s) euid=%jd egid=%jd pid=%jd\n", |
| | | 168 | statit ? "fstat" : "peercred", s, |
| | | 169 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); |
| | | 170 | CHECK_EQUAL(euid, geteuid(), s); |
| | | 171 | CHECK_EQUAL(egid, getegid(), s); |
| | | 172 | CHECK_EQUAL(pid, checkpid, s); |
| | | 173 | return 0; |
| | | 174 | fail: |
| | | 175 | return -1; |
| | | 176 | } |
| | | 177 | |
| | | 178 | static int |
| | | 179 | test(bool forkit, bool closeit, bool statit, size_t len) |
150 | { | | 180 | { |
151 | size_t slen; | | 181 | size_t slen; |
152 | socklen_t sl; | | 182 | socklen_t sl; |
153 | int srvr = -1, clnt = -1, acpt = -1; | | 183 | int srvr = -1, clnt = -1, acpt = -1; |
154 | uid_t euid; | | | |
155 | gid_t egid; | | | |
156 | pid_t srvrpid, clntpid; | | 184 | pid_t srvrpid, clntpid; |
157 | struct sockaddr_un *sock_addr = NULL, *sun = NULL; | | 185 | struct sockaddr_un *sock_addr = NULL, *sun = NULL; |
158 | socklen_t sock_addrlen; | | 186 | socklen_t sock_addrlen; |
159 | socklen_t peer_addrlen; | | 187 | socklen_t peer_addrlen; |
160 | struct sockaddr_un peer_addr; | | 188 | struct sockaddr_un peer_addr; |
161 | | | 189 | |
162 | srvrpid = clntpid = getpid(); | | 190 | srvrpid = clntpid = getpid(); |
163 | srvr = socket(AF_UNIX, SOCK_STREAM, 0); | | 191 | srvr = socket(AF_UNIX, SOCK_STREAM, 0); |
164 | if (srvr == -1) | | 192 | if (srvr == -1) |
165 | FAIL("socket(server)"); | | 193 | FAIL("socket(server)"); |
166 | | | 194 | |
167 | slen = len + OF + 1; | | 195 | slen = len + OF + 1; |
168 | | | 196 | |
169 | if ((sun = calloc(1, slen)) == NULL) | | 197 | if ((sun = calloc(1, slen)) == NULL) |
170 | FAIL("calloc"); | | 198 | FAIL("calloc"); |
171 | | | 199 | |
172 | memset(sun->sun_path, 'a', len); | | 200 | memset(sun->sun_path, 'a', len); |
173 | sun->sun_path[len] = '\0'; | | 201 | sun->sun_path[len] = '\0'; |
174 | (void)unlink(sun->sun_path); | | 202 | (void)unlink(sun->sun_path); |
175 | | | 203 | |
176 | sl = SUN_LEN(sun); | | 204 | sl = SUN_LEN(sun); |
177 | #ifdef BSD4_4 | | 205 | #ifdef BSD4_4 |
178 | sun->sun_len = sl; | | 206 | sun->sun_len = sl; |
179 | #endif | | 207 | #endif |
180 | sun->sun_family = AF_UNIX; | | 208 | sun->sun_family = AF_UNIX; |
181 | | | 209 | |
182 | if (bind(srvr, (struct sockaddr *)sun, sl) == -1) { | | 210 | if (bind(srvr, (struct sockaddr *)sun, sl) == -1) { |
183 | if (errno == EINVAL && sl >= 256) { | | 211 | if (errno == EINVAL && sl >= 256) { |
184 | close(srvr); | | 212 | close(srvr); |
185 | return -1; | | 213 | return -1; |
186 | } | | 214 | } |
187 | FAIL("bind"); | | 215 | FAIL("bind"); |
188 | } | | 216 | } |
189 | | | 217 | |
190 | if (listen(srvr, SOMAXCONN) == -1) | | 218 | if (listen(srvr, SOMAXCONN) == -1) |
191 | FAIL("listen"); | | 219 | FAIL("listen"); |
192 | | | 220 | |
193 | if (forkit) { | | 221 | if (forkit) { |
194 | switch (clntpid = fork()) { | | 222 | switch (clntpid = fork()) { |
195 | case 0: /* child */ | | 223 | case 0: /* child */ |
196 | srvrpid = getppid(); | | 224 | srvrpid = getppid(); |
197 | clntpid = getpid(); | | 225 | clntpid = getpid(); |
198 | break; | | 226 | break; |
199 | case -1: | | 227 | case -1: |
200 | FAIL("fork"); | | 228 | FAIL("fork"); |
201 | default: | | 229 | default: |
202 | break; | | 230 | break; |
203 | } | | 231 | } |
204 | } | | 232 | } |
205 | | | 233 | |
206 | if (clntpid == getpid()) { | | 234 | if (clntpid == getpid()) { |
207 | pid_t pid = srvrpid; | | | |
208 | clnt = socket(AF_UNIX, SOCK_STREAM, 0); | | 235 | clnt = socket(AF_UNIX, SOCK_STREAM, 0); |
209 | if (clnt == -1) | | 236 | if (clnt == -1) |
210 | FAIL("socket(client)"); | | 237 | FAIL("socket(client)"); |
211 | | | 238 | |
212 | if (connect(clnt, (const struct sockaddr *)sun, sl) == -1) | | 239 | if (connect(clnt, (const struct sockaddr *)sun, sl) == -1) |
213 | FAIL("connect"); | | 240 | FAIL("connect"); |
| | | 241 | check_cred(clnt, statit, srvrpid, "client"); |
214 | | | 242 | |
215 | if (peercred(clnt, &euid, &egid, &pid) == -1) | | | |
216 | FAIL("peercred (client)"); | | | |
217 | printf("peercred(client) euid=%jd egid=%jd pid=%jd\n", | | | |
218 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); | | | |
219 | CHECK_EQUAL(euid, geteuid(), "client"); | | | |
220 | CHECK_EQUAL(egid, getegid(), "client"); | | | |
221 | CHECK_EQUAL(pid, srvrpid, "client"); | | | |
222 | } | | 243 | } |
223 | | | 244 | |
224 | if (srvrpid == getpid()) { | | 245 | if (srvrpid == getpid()) { |
225 | acpt = acc(srvr); | | 246 | acpt = acc(srvr); |
226 | | | | |
227 | peer_addrlen = sizeof(peer_addr); | | 247 | peer_addrlen = sizeof(peer_addr); |
228 | memset(&peer_addr, 0, sizeof(peer_addr)); | | 248 | memset(&peer_addr, 0, sizeof(peer_addr)); |
229 | if (getpeername(acpt, (struct sockaddr *)&peer_addr, | | 249 | if (getpeername(acpt, (struct sockaddr *)&peer_addr, |
230 | &peer_addrlen) == -1) | | 250 | &peer_addrlen) == -1) |
231 | FAIL("getpeername"); | | 251 | FAIL("getpeername"); |
232 | print("peer", &peer_addr, peer_addrlen); | | 252 | print("peer", &peer_addr, peer_addrlen); |
233 | } | | 253 | } |
234 | | | 254 | |
235 | if (clntpid == getpid()) { | | 255 | if (clntpid == getpid()) { |
236 | if (closeit) { | | 256 | if (closeit) { |
237 | if (close(clnt) == -1) | | 257 | if (close(clnt) == -1) |
238 | FAIL("close"); | | 258 | FAIL("close"); |
239 | clnt = -1; | | 259 | clnt = -1; |
240 | } | | 260 | } |
241 | } | | 261 | } |
242 | | | 262 | |
243 | if (srvrpid == getpid()) { | | 263 | if (srvrpid == getpid()) { |
244 | pid_t pid = clntpid; | | 264 | check_cred(acpt, statit, clntpid, "server"); |
245 | if (peercred(acpt, &euid, &egid, &pid) == -1) | | | |
246 | FAIL("peercred (server)"); | | | |
247 | printf("peercred(server) euid=%jd egid=%jd pid=%jd\n", | | | |
248 | (intmax_t)euid, (intmax_t)egid, (intmax_t)pid); | | | |
249 | CHECK_EQUAL(euid, geteuid(), "server"); | | | |
250 | CHECK_EQUAL(egid, getegid(), "server"); | | | |
251 | CHECK_EQUAL(pid, clntpid, "server"); | | | |
252 | | | | |
253 | if ((sock_addr = calloc(1, slen)) == NULL) | | 265 | if ((sock_addr = calloc(1, slen)) == NULL) |
254 | FAIL("calloc"); | | 266 | FAIL("calloc"); |
255 | sock_addrlen = slen; | | 267 | sock_addrlen = slen; |
256 | if (getsockname(srvr, (struct sockaddr *)sock_addr, | | 268 | if (getsockname(srvr, (struct sockaddr *)sock_addr, |
257 | &sock_addrlen) == -1) | | 269 | &sock_addrlen) == -1) |
258 | FAIL("getsockname"); | | 270 | FAIL("getsockname"); |
259 | print("sock", sock_addr, sock_addrlen); | | 271 | print("sock", sock_addr, sock_addrlen); |
260 | | | 272 | |
261 | if (sock_addr->sun_family != AF_UNIX) | | 273 | if (sock_addr->sun_family != AF_UNIX) |
262 | FAIL("sock_addr->sun_family %d != AF_UNIX", | | 274 | FAIL("sock_addr->sun_family %d != AF_UNIX", |
263 | sock_addr->sun_family); | | 275 | sock_addr->sun_family); |
264 | | | 276 | |
265 | len += OF; | | 277 | len += OF; |
266 | if (sock_addrlen LX != len) | | 278 | if (sock_addrlen LX != len) |
267 | FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen, | | 279 | FAIL("sock_addr_len %zu != %zu", (size_t)sock_addrlen, |
268 | len); | | 280 | len); |
269 | #ifdef BSD4_4 | | 281 | #ifdef BSD4_4 |
270 | if (sock_addr->sun_len != sl) | | 282 | if (sock_addr->sun_len != sl) |
271 | FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len, | | 283 | FAIL("sock_addr.sun_len %d != %zu", sock_addr->sun_len, |
272 | (size_t)sl); | | 284 | (size_t)sl); |
273 | #endif | | 285 | #endif |
274 | for (size_t i = 0; i < slen - OF; i++) | | 286 | for (size_t i = 0; i < slen - OF; i++) |
275 | if (sock_addr->sun_path[i] != sun->sun_path[i]) | | 287 | if (sock_addr->sun_path[i] != sun->sun_path[i]) |
276 | FAIL("sock_addr.sun_path[%zu] %d != " | | 288 | FAIL("sock_addr.sun_path[%zu] %d != " |
277 | "sun->sun_path[%zu] %d\n", i, | | 289 | "sun->sun_path[%zu] %d\n", i, |
278 | sock_addr->sun_path[i], i, | | 290 | sock_addr->sun_path[i], i, |
279 | sun->sun_path[i]); | | 291 | sun->sun_path[i]); |
280 | | | 292 | |
281 | if (acpt != -1) | | 293 | if (acpt != -1) |
282 | (void)close(acpt); | | 294 | (void)close(acpt); |
283 | if (srvr != -1) | | 295 | if (srvr != -1) |
284 | (void)close(srvr); | | 296 | (void)close(srvr); |
285 | free(sock_addr); | | 297 | free(sock_addr); |
286 | sock_addr = NULL; | | 298 | sock_addr = NULL; |
287 | if (forkit && waitpid(clntpid, NULL, 0) == -1) | | 299 | if (forkit && waitpid(clntpid, NULL, 0) == -1) |
288 | FAIL("waitpid"); | | 300 | FAIL("waitpid"); |
289 | } | | 301 | } |
290 | if (clnt != -1 && !closeit) | | 302 | if (clnt != -1 && !closeit) |
291 | (void)close(clnt); | | 303 | (void)close(clnt); |
292 | | | 304 | |
293 | free(sock_addr); | | 305 | free(sock_addr); |
294 | free(sun); | | 306 | free(sun); |
295 | return 0; | | 307 | return 0; |
296 | fail: | | 308 | fail: |
297 | if (srvrpid == getpid()) { | | 309 | if (srvrpid == getpid()) { |
298 | if (acpt != -1) | | 310 | if (acpt != -1) |
299 | (void)close(acpt); | | 311 | (void)close(acpt); |
300 | if (srvr != -1) | | 312 | if (srvr != -1) |
301 | (void)close(srvr); | | 313 | (void)close(srvr); |
302 | } | | 314 | } |
303 | if (clntpid == getpid()) { | | 315 | if (clntpid == getpid()) { |
304 | if (clnt != -1 && !closeit) | | 316 | if (clnt != -1 && !closeit) |
305 | (void)close(clnt); | | 317 | (void)close(clnt); |
306 | } | | 318 | } |
307 | free(sock_addr); | | 319 | free(sock_addr); |
308 | free(sun); | | 320 | free(sun); |
309 | return -1; | | 321 | return -1; |
310 | } | | 322 | } |
311 | | | 323 | |
312 | #ifndef TEST | | 324 | #ifndef TEST |
313 | | | 325 | |
314 | ATF_TC(sockaddr_un_len_exceed); | | 326 | ATF_TC(sockaddr_un_len_exceed); |
315 | ATF_TC_HEAD(sockaddr_un_len_exceed, tc) | | 327 | ATF_TC_HEAD(sockaddr_un_len_exceed, tc) |
316 | { | | 328 | { |
317 | | | 329 | |
318 | atf_tc_set_md_var(tc, "descr", "Check that exceeding the size of " | | 330 | atf_tc_set_md_var(tc, "descr", "Check that exceeding the size of " |
319 | "unix domain sockets does not trash memory or kernel when " | | 331 | "unix domain sockets does not trash memory or kernel when " |
320 | "exceeding the size of the fixed sun_path"); | | 332 | "exceeding the size of the fixed sun_path"); |
321 | } | | 333 | } |
322 | | | 334 | |
323 | ATF_TC_BODY(sockaddr_un_len_exceed, tc) | | 335 | ATF_TC_BODY(sockaddr_un_len_exceed, tc) |
324 | { | | 336 | { |
325 | ATF_REQUIRE_MSG(test(false, false, 254) == -1, | | 337 | ATF_REQUIRE_MSG(test(false, false, false, 254) == -1, |
326 | "test(false, false, 254): %s", strerror(errno)); | | 338 | "test(false, false, false, 254): %s", strerror(errno)); |
327 | } | | 339 | } |
328 | | | 340 | |
329 | ATF_TC(sockaddr_un_len_max); | | 341 | ATF_TC(sockaddr_un_len_max); |
330 | ATF_TC_HEAD(sockaddr_un_len_max, tc) | | 342 | ATF_TC_HEAD(sockaddr_un_len_max, tc) |
331 | { | | 343 | { |
332 | | | 344 | |
333 | atf_tc_set_md_var(tc, "descr", "Check that we can use the maximum " | | 345 | atf_tc_set_md_var(tc, "descr", "Check that we can use the maximum " |
334 | "unix domain socket pathlen (253): 255 - sizeof(sun_len) - " | | 346 | "unix domain socket pathlen (253): 255 - sizeof(sun_len) - " |
335 | "sizeof(sun_family)"); | | 347 | "sizeof(sun_family)"); |
336 | } | | 348 | } |
337 | | | 349 | |
338 | ATF_TC_BODY(sockaddr_un_len_max, tc) | | 350 | ATF_TC_BODY(sockaddr_un_len_max, tc) |
339 | { | | 351 | { |
340 | ATF_REQUIRE_MSG(test(false, false, 253) == 0, | | 352 | ATF_REQUIRE_MSG(test(false, false, false, 253) == 0, |
341 | "test(false, false, 253): %s", strerror(errno)); | | 353 | "test(false, false, false, 253): %s", strerror(errno)); |
342 | } | | 354 | } |
343 | | | 355 | |
344 | ATF_TC(sockaddr_un_closed); | | 356 | ATF_TC(sockaddr_un_closed); |
345 | ATF_TC_HEAD(sockaddr_un_closed, tc) | | 357 | ATF_TC_HEAD(sockaddr_un_closed, tc) |
346 | { | | 358 | { |
347 | | | 359 | |
348 | atf_tc_set_md_var(tc, "descr", "Check that we can use the accepted " | | 360 | atf_tc_set_md_var(tc, "descr", "Check that we can use the accepted " |
349 | "address of unix domain socket when closed"); | | 361 | "address of unix domain socket when closed"); |
350 | } | | 362 | } |
351 | | | 363 | |
352 | ATF_TC_BODY(sockaddr_un_closed, tc) | | 364 | ATF_TC_BODY(sockaddr_un_closed, tc) |
353 | { | | 365 | { |
354 | ATF_REQUIRE_MSG(test(false, true, 100) == 0, | | 366 | ATF_REQUIRE_MSG(test(false, true, false, 100) == 0, |
355 | "test(false, true, 100): %s", strerror(errno)); | | 367 | "test(false, true, false, 100): %s", strerror(errno)); |
356 | } | | 368 | } |
357 | | | 369 | |
358 | ATF_TC(sockaddr_un_local_peereid); | | 370 | ATF_TC(sockaddr_un_local_peereid); |
359 | ATF_TC_HEAD(sockaddr_un_local_peereid, tc) | | 371 | ATF_TC_HEAD(sockaddr_un_local_peereid, tc) |
360 | { | | 372 | { |
361 | | | 373 | |
362 | atf_tc_set_md_var(tc, "descr", "Check that we get the right information" | | 374 | atf_tc_set_md_var(tc, "descr", "Check that we get the right information" |
363 | " from LOCAL_PEEREID"); | | 375 | " from LOCAL_PEEREID"); |
364 | } | | 376 | } |
365 | | | 377 | |
366 | ATF_TC_BODY(sockaddr_un_local_peereid, tc) | | 378 | ATF_TC_BODY(sockaddr_un_local_peereid, tc) |
367 | { | | 379 | { |
368 | ATF_REQUIRE_MSG(test(true, true, 100) == 0, | | 380 | ATF_REQUIRE_MSG(test(true, true, false, 100) == 0, |
369 | "test(true, true, 100): %s", strerror(errno)); | | 381 | "test(true, true, false, 100): %s", strerror(errno)); |
| | | 382 | } |
| | | 383 | |
| | | 384 | ATF_TC(sockaddr_un_fstat); |
| | | 385 | ATF_TC_HEAD(sockaddr_un_fstat, tc) |
| | | 386 | { |
| | | 387 | |
| | | 388 | atf_tc_set_md_var(tc, "descr", "Check that we get the right information" |
| | | 389 | " from fstat"); |
| | | 390 | } |
| | | 391 | |
| | | 392 | ATF_TC_BODY(sockaddr_un_fstat, tc) |
| | | 393 | { |
| | | 394 | ATF_REQUIRE_MSG(test(true, true, true, 100) == 0, |
| | | 395 | "test(true, true, true, 100): %s", strerror(errno)); |
370 | } | | 396 | } |
371 | | | 397 | |
372 | ATF_TP_ADD_TCS(tp) | | 398 | ATF_TP_ADD_TCS(tp) |
373 | { | | 399 | { |
374 | | | 400 | |
375 | ATF_TP_ADD_TC(tp, sockaddr_un_len_exceed); | | 401 | ATF_TP_ADD_TC(tp, sockaddr_un_len_exceed); |
376 | ATF_TP_ADD_TC(tp, sockaddr_un_len_max); | | 402 | ATF_TP_ADD_TC(tp, sockaddr_un_len_max); |
377 | ATF_TP_ADD_TC(tp, sockaddr_un_closed); | | 403 | ATF_TP_ADD_TC(tp, sockaddr_un_closed); |
378 | ATF_TP_ADD_TC(tp, sockaddr_un_local_peereid); | | 404 | ATF_TP_ADD_TC(tp, sockaddr_un_local_peereid); |
| | | 405 | ATF_TP_ADD_TC(tp, sockaddr_un_fstat); |
379 | return atf_no_error(); | | 406 | return atf_no_error(); |
380 | } | | 407 | } |
381 | #else | | 408 | #else |
382 | int | | 409 | int |
383 | main(int argc, char *argv[]) | | 410 | main(int argc, char *argv[]) |
384 | { | | 411 | { |
385 | size_t len; | | 412 | size_t len; |
386 | | | 413 | |
387 | if (argc == 1) { | | 414 | if (argc == 1) { |
388 | fprintf(stderr, "Usage: %s <len>\n", getprogname()); | | 415 | fprintf(stderr, "Usage: %s <len>\n", getprogname()); |
389 | return EXIT_FAILURE; | | 416 | return EXIT_FAILURE; |
390 | } | | 417 | } |
391 | test(false, false, atoi(argv[1])); | | 418 | test(false, false, false, atoi(argv[1])); |
392 | test(false, true, atoi(argv[1])); | | 419 | test(false, true, false, atoi(argv[1])); |
393 | test(true, false, atoi(argv[1])); | | 420 | test(true, false, false, atoi(argv[1])); |
394 | test(true, true, atoi(argv[1])); | | 421 | test(true, true, false, atoi(argv[1])); |
| | | 422 | test(true, true, true, atoi(argv[1])); |
395 | } | | 423 | } |
396 | #endif | | 424 | #endif |