2 * Check decoding of sockaddr structures
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 #include <linux/if_arp.h>
40 #include <linux/if_ether.h>
41 #include <linux/if_packet.h>
42 #include <linux/ipx.h>
43 #include <linux/netlink.h>
44 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
45 # include <bluetooth/bluetooth.h>
46 # include <bluetooth/hci.h>
47 # include <bluetooth/l2cap.h>
48 # include <bluetooth/rfcomm.h>
49 # include <bluetooth/sco.h>
52 #ifdef HAVE_IF_INDEXTONAME
53 /* <linux/if.h> used to conflict with <net/if.h> */
54 extern unsigned int if_nametoindex(const char *);
60 TAIL_ALLOC_OBJECT_VAR_PTR(struct sockaddr_un, un);
61 un->sun_family = AF_UNIX;
62 memset(un->sun_path, '0', sizeof(un->sun_path));
63 unsigned int len = sizeof(*un);
64 int ret = connect(-1, (void *) un, len);
65 printf("connect(-1, {sa_family=AF_UNIX, sun_path=\"%.*u\"}"
66 ", %u) = %d EBADF (%m)\n",
67 (int) sizeof(un->sun_path), 0, len, ret);
70 ret = connect(-1, (void *) un, len);
71 printf("connect(-1, {sa_family=AF_UNIX, sun_path=\"%u\"}, %u)"
72 " = %d EBADF (%m)\n", 0, len, ret);
76 ret = connect(-1, (void *) un, len);
77 printf("connect(-1, {sa_family=AF_UNIX, sun_path=@\"\\0\\001%.*u\"}"
78 ", %u) = %d EBADF (%m)\n",
79 (int) sizeof(un->sun_path) - 3, 0, len, ret);
81 un = ((void *) un) - 2;
82 un->sun_family = AF_UNIX;
83 memset(un->sun_path, '0', sizeof(un->sun_path));
84 len = sizeof(*un) + 2;
85 ret = connect(-1, (void *) un, len);
86 printf("connect(-1, {sa_family=AF_UNIX, sun_path=\"%.*u\"}"
87 ", %u) = %d EBADF (%m)\n",
88 (int) sizeof(un->sun_path), 0, len, ret);
91 ret = connect(-1, (void *) un, len);
92 printf("connect(-1, {sa_family=AF_UNIX, sun_path=@\"%.*u\"}"
93 ", %u) = %d EBADF (%m)\n",
94 (int) sizeof(un->sun_path) - 1, 0, len, ret);
96 un = ((void *) un) + 4;
97 un->sun_family = AF_UNIX;
98 len = sizeof(*un) - 2;
99 ret = connect(-1, (void *) un, len);
100 printf("connect(-1, {sa_family=AF_UNIX, sun_path=\"%.*u\"}"
101 ", %u) = %d EBADF (%m)\n",
102 (int) sizeof(un->sun_path) - 2, 0, len, ret);
105 ret = connect(-1, (void *) un, len);
106 printf("connect(-1, {sa_family=AF_UNIX, sun_path=@\"%.*u\"}"
107 ", %u) = %d EBADF (%m)\n",
108 (int) sizeof(un->sun_path) - 3, 0, len, ret);
111 ret = connect(-1, (void *) un, len);
112 printf("connect(-1, %p, %u) = %d EBADF (%m)\n", un, len, ret);
114 un = tail_alloc(sizeof(struct sockaddr_storage));
115 un->sun_family = AF_UNIX;
116 memset(un->sun_path, '0', sizeof(un->sun_path));
117 len = sizeof(struct sockaddr_storage) + 1;
118 ret = connect(-1, (void *) un, len);
119 printf("connect(-1, {sa_family=AF_UNIX, sun_path=\"%.*u\"}"
120 ", %u) = %d EBADF (%m)\n",
121 (int) sizeof(un->sun_path), 0, len, ret);
124 ret = connect(-1, (void *) un, len);
125 printf("connect(-1, {sa_family=AF_UNIX, sun_path=@\"%.*u\"}"
126 ", %u) = %d EBADF (%m)\n",
127 (int) sizeof(un->sun_path) - 1, 0, len, ret);
133 const unsigned short h_port = 12345;
134 static const char h_addr[] = "12.34.56.78";
136 TAIL_ALLOC_OBJECT_VAR_PTR(struct sockaddr_in, in);
137 in->sin_family = AF_INET;
138 in->sin_port = htons(h_port);
139 in->sin_addr.s_addr = inet_addr(h_addr);
140 unsigned int len = sizeof(*in);
141 int ret = connect(-1, (void *) in, len);
142 printf("connect(-1, {sa_family=AF_INET, sin_port=htons(%hu)"
143 ", sin_addr=inet_addr(\"%s\")}, %u) = %d EBADF (%m)\n",
144 h_port, h_addr, len, ret);
146 in = ((void *) in) - 4;
147 in->sin_family = AF_INET;
148 in->sin_port = htons(h_port);
149 in->sin_addr.s_addr = inet_addr(h_addr);
150 len = sizeof(*in) + 4;
151 ret = connect(-1, (void *) in, len);
152 printf("connect(-1, {sa_family=AF_INET, sin_port=htons(%hu)"
153 ", sin_addr=inet_addr(\"%s\")}, %u) = %d EBADF (%m)\n",
154 h_port, h_addr, len, ret);
156 in = ((void *) in) + 8;
157 in->sin_family = AF_INET;
159 in->sin_addr.s_addr = 0;
160 len = sizeof(*in) - 4;
161 ret = connect(-1, (void *) in, len);
162 printf("connect(-1, {sa_family=AF_INET, sa_data=\"%s\"}, %u)"
163 " = %d EBADF (%m)\n",
164 "\\0\\0\\0\\0\\0\\0\\377\\377\\377\\377",
168 ret = connect(-1, (void *) in, len);
169 printf("connect(-1, %p, %u) = %d EBADF (%m)\n", in, len, ret);
173 check_in6_linklocal(struct sockaddr_in6 *const in6, const char *const h_addr)
175 inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
177 in6->sin6_scope_id = 0xfacefeed;
178 unsigned int len = sizeof(*in6);
179 int ret = connect(-1, (void *) in6, len);
180 printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
181 ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
182 ", sin6_flowinfo=htonl(%u)"
183 ", sin6_scope_id=%u}, %u)"
184 " = %d EBADF (%m)\n",
185 ntohs(in6->sin6_port), h_addr,
186 ntohl(in6->sin6_flowinfo), in6->sin6_scope_id, len, ret);
188 #ifdef HAVE_IF_INDEXTONAME
189 in6->sin6_scope_id = if_nametoindex("lo");
190 if (in6->sin6_scope_id) {
191 ret = connect(-1, (void *) in6, len);
192 printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
193 ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
194 ", sin6_flowinfo=htonl(%u)"
195 ", sin6_scope_id=if_nametoindex(\"lo\")}, %u)"
196 " = %d EBADF (%m)\n",
197 ntohs(in6->sin6_port), h_addr,
198 ntohl(in6->sin6_flowinfo), len, ret);
206 const unsigned short h_port = 12345;
207 const unsigned int h_flowinfo = 1234567890;
208 static const char h_addr[] = "12:34:56:78:90:ab:cd:ef";
210 TAIL_ALLOC_OBJECT_VAR_PTR(struct sockaddr_in6, in6);
211 in6->sin6_family = AF_INET6;
212 in6->sin6_port = htons(h_port);
213 in6->sin6_flowinfo = htonl(h_flowinfo);
214 inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
215 in6->sin6_scope_id = 0xfacefeed;
216 unsigned int len = sizeof(*in6);
217 int ret = connect(-1, (void *) in6, len);
218 printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
219 ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
220 ", sin6_flowinfo=htonl(%u), sin6_scope_id=%u}, %u)"
221 " = %d EBADF (%m)\n",
222 h_port, h_addr, h_flowinfo, in6->sin6_scope_id, len, ret);
224 check_in6_linklocal(in6, "fe80::");
225 check_in6_linklocal(in6, "ff42::");
227 in6 = ((void *) in6) - 4;
228 in6->sin6_family = AF_INET6;
229 in6->sin6_port = htons(h_port);
230 in6->sin6_flowinfo = htonl(h_flowinfo);
231 inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
232 in6->sin6_scope_id = 0xfacefeed;
233 len = sizeof(*in6) + 4;
234 ret = connect(-1, (void *) in6, len);
235 printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
236 ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
237 ", sin6_flowinfo=htonl(%u), sin6_scope_id=%u}, %u)"
238 " = %d EBADF (%m)\n",
239 h_port, h_addr, h_flowinfo, in6->sin6_scope_id, len, ret);
241 in6 = ((void *) in6) + 4 + sizeof(in6->sin6_scope_id);
242 in6->sin6_family = AF_INET6;
243 in6->sin6_port = htons(h_port);
244 in6->sin6_flowinfo = htonl(h_flowinfo);
245 inet_pton(AF_INET6, h_addr, &in6->sin6_addr);
246 len = sizeof(*in6) - sizeof(in6->sin6_scope_id);
247 ret = connect(-1, (void *) in6, len);
248 printf("connect(-1, {sa_family=AF_INET6, sin6_port=htons(%hu)"
249 ", inet_pton(AF_INET6, \"%s\", &sin6_addr)"
250 ", sin6_flowinfo=htonl(%u)}, %u)"
251 " = %d EBADF (%m)\n",
252 h_port, h_addr, h_flowinfo, len, ret);
254 in6 = ((void *) in6) + 4;
255 in6->sin6_family = AF_INET6;
257 in6->sin6_flowinfo = 0;
258 memset(&in6->sin6_addr, '0', sizeof(in6->sin6_addr) - 4);
259 len = sizeof(*in6) - sizeof(in6->sin6_scope_id) - 4;
260 ret = connect(-1, (void *) in6, len);
261 printf("connect(-1, {sa_family=AF_INET6"
262 ", sa_data=\"\\0\\0\\0\\0\\0\\000%.*u\"}, %u)"
263 " = %d EBADF (%m)\n",
264 (int) (len - offsetof(struct sockaddr_in6, sin6_addr)), 0,
267 len = sizeof(*in6) - sizeof(in6->sin6_scope_id);
268 ret = connect(-1, (void *) in6, len);
269 printf("connect(-1, %p, %u) = %d EBADF (%m)\n", in6, len, ret);
275 const unsigned short h_port = 12345;
276 const unsigned int h_network = 0xfacefeed;
277 struct sockaddr_ipx c_ipx = {
278 .sipx_family = AF_IPX,
279 .sipx_port = htons(h_port),
280 .sipx_network = htonl(h_network),
281 .sipx_node = "ABCDEF",
284 void *ipx = tail_memdup(&c_ipx, sizeof(c_ipx));
285 unsigned int len = sizeof(c_ipx);
286 int ret = connect(-1, ipx, len);
287 printf("connect(-1, {sa_family=AF_IPX, sipx_port=htons(%u)"
288 ", sipx_network=htonl(%#x)"
289 ", sipx_node=[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
290 ", sipx_type=%#02x}, %u) = %d EBADF (%m)\n",
292 c_ipx.sipx_node[0], c_ipx.sipx_node[1],
293 c_ipx.sipx_node[2], c_ipx.sipx_node[3],
294 c_ipx.sipx_node[4], c_ipx.sipx_node[5],
295 c_ipx.sipx_type, len, ret);
301 TAIL_ALLOC_OBJECT_VAR_PTR(struct sockaddr_nl, nl);
302 nl->nl_family = AF_NETLINK;
303 nl->nl_pid = 1234567890;
304 nl->nl_groups = 0xfacefeed;
305 unsigned int len = sizeof(*nl);
306 int ret = connect(-1, (void *) nl, len);
307 printf("connect(-1, {sa_family=AF_NETLINK, nl_pid=%d"
308 ", nl_groups=%#08x}, %u) = %d EBADF (%m)\n",
309 nl->nl_pid, nl->nl_groups, len, ret);
311 nl = ((void *) nl) - 4;
312 nl->nl_family = AF_NETLINK;
313 nl->nl_pid = 1234567890;
314 nl->nl_groups = 0xfacefeed;
315 len = sizeof(*nl) + 4;
316 ret = connect(-1, (void *) nl, len);
317 printf("connect(-1, {sa_family=AF_NETLINK, nl_pid=%d"
318 ", nl_groups=%#08x}, %u) = %d EBADF (%m)\n",
319 nl->nl_pid, nl->nl_groups, len, ret);
325 struct sockaddr_ll c_ll = {
326 .sll_family = AF_PACKET,
327 .sll_protocol = htons(ETH_P_ALL),
328 .sll_ifindex = 0xfacefeed,
329 .sll_hatype = ARPHRD_ETHER,
330 .sll_pkttype = PACKET_HOST,
331 .sll_halen = sizeof(c_ll.sll_addr),
332 .sll_addr = "abcdefgh"
334 void *ll = tail_memdup(&c_ll, sizeof(c_ll));
335 unsigned int len = sizeof(c_ll);
336 int ret = connect(-1, ll, len);
337 printf("connect(-1, {sa_family=AF_PACKET"
338 ", sll_protocol=htons(ETH_P_ALL)"
339 ", sll_ifindex=%u, sll_hatype=ARPHRD_ETHER"
340 ", sll_pkttype=PACKET_HOST, sll_halen=%u, sll_addr="
341 "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x]"
342 "}, %u) = %d EBADF (%m)\n",
343 c_ll.sll_ifindex, c_ll.sll_halen,
344 c_ll.sll_addr[0], c_ll.sll_addr[1],
345 c_ll.sll_addr[2], c_ll.sll_addr[3],
346 c_ll.sll_addr[4], c_ll.sll_addr[5],
347 c_ll.sll_addr[6], c_ll.sll_addr[7],
350 ((struct sockaddr_ll *) ll)->sll_halen++;
351 ret = connect(-1, ll, len);
352 printf("connect(-1, {sa_family=AF_PACKET"
353 ", sll_protocol=htons(ETH_P_ALL)"
354 ", sll_ifindex=%u, sll_hatype=ARPHRD_ETHER"
355 ", sll_pkttype=PACKET_HOST, sll_halen=%u, sll_addr="
356 "[%#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, %#02x, ...]"
357 "}, %u) = %d EBADF (%m)\n",
358 c_ll.sll_ifindex, c_ll.sll_halen + 1,
359 c_ll.sll_addr[0], c_ll.sll_addr[1],
360 c_ll.sll_addr[2], c_ll.sll_addr[3],
361 c_ll.sll_addr[4], c_ll.sll_addr[5],
362 c_ll.sll_addr[6], c_ll.sll_addr[7],
365 ((struct sockaddr_ll *) ll)->sll_halen = 0;
366 ret = connect(-1, ll, len);
367 printf("connect(-1, {sa_family=AF_PACKET"
368 ", sll_protocol=htons(ETH_P_ALL)"
369 ", sll_ifindex=%u, sll_hatype=ARPHRD_ETHER"
370 ", sll_pkttype=PACKET_HOST, sll_halen=0}, %u)"
371 " = %d EBADF (%m)\n", c_ll.sll_ifindex, len, ret);
373 #ifdef HAVE_IF_INDEXTONAME
374 const int id = if_nametoindex("lo");
376 ((struct sockaddr_ll *) ll)->sll_ifindex = id;
377 ret = connect(-1, ll, len);
378 printf("connect(-1, {sa_family=AF_PACKET"
379 ", sll_protocol=htons(ETH_P_ALL)"
380 ", sll_ifindex=if_nametoindex(\"lo\")"
381 ", sll_hatype=ARPHRD_ETHER"
382 ", sll_pkttype=PACKET_HOST, sll_halen=0}, %u)"
383 " = %d EBADF (%m)\n", len, ret);
388 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
392 const unsigned short h_port = 12345;
393 TAIL_ALLOC_OBJECT_VAR_PTR(struct sockaddr_hci, hci);
394 hci->hci_family = AF_BLUETOOTH;
395 hci->hci_dev = htobs(h_port);
396 hci->hci_channel = HCI_CHANNEL_RAW;
397 unsigned int len = sizeof(*hci);
398 int ret = connect(-1, (void *) hci, len);
399 printf("connect(-1, {sa_family=AF_BLUETOOTH, hci_dev=htobs(%hu)"
400 ", hci_channel=HCI_CHANNEL_RAW}, %u) = %d EBADF (%m)\n",
407 const struct sockaddr_sco c_sco = {
408 .sco_family = AF_BLUETOOTH,
409 .sco_bdaddr.b = "abcdef"
411 void *sco = tail_memdup(&c_sco, sizeof(c_sco));
412 unsigned int len = sizeof(c_sco);
413 int ret = connect(-1, sco, len);
414 printf("connect(-1, {sa_family=AF_BLUETOOTH"
415 ", sco_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
416 "}, %u) = %d EBADF (%m)\n",
417 c_sco.sco_bdaddr.b[0], c_sco.sco_bdaddr.b[1],
418 c_sco.sco_bdaddr.b[2], c_sco.sco_bdaddr.b[3],
419 c_sco.sco_bdaddr.b[4], c_sco.sco_bdaddr.b[5],
426 const struct sockaddr_rc c_rc = {
427 .rc_family = AF_BLUETOOTH,
428 .rc_bdaddr.b = "abcdef",
431 void *rc = tail_memdup(&c_rc, sizeof(c_rc));
432 unsigned int len = sizeof(c_rc);
433 int ret = connect(-1, rc, len);
434 printf("connect(-1, {sa_family=AF_BLUETOOTH"
435 ", rc_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
436 ", rc_channel=%u}, %u) = %d EBADF (%m)\n",
437 c_rc.rc_bdaddr.b[0], c_rc.rc_bdaddr.b[1],
438 c_rc.rc_bdaddr.b[2], c_rc.rc_bdaddr.b[3],
439 c_rc.rc_bdaddr.b[4], c_rc.rc_bdaddr.b[5],
440 c_rc.rc_channel, len, ret);
446 const unsigned short h_psm = 12345;
447 const unsigned short h_cid = 13579;
448 const struct sockaddr_l2 c_l2 = {
449 .l2_family = AF_BLUETOOTH,
450 .l2_psm = htobs(h_psm),
451 .l2_bdaddr.b = "abcdef",
452 .l2_cid = htobs(h_cid),
455 void *l2 = tail_memdup(&c_l2, sizeof(c_l2));
456 unsigned int len = sizeof(c_l2);
457 int ret = connect(-1, l2, len);
458 printf("connect(-1, {sa_family=AF_BLUETOOTH"
459 ", l2_psm=htobs(%hu)"
460 ", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
461 ", l2_cid=htobs(%hu), l2_bdaddr_type=%u}"
462 ", %u) = %d EBADF (%m)\n", h_psm,
463 c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
464 c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
465 c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
466 h_cid, c_l2.l2_bdaddr_type, len, ret);
475 struct sockaddr_storage *st;
476 } u = { .st = tail_alloc(sizeof(*u.st)) };
477 memset(u.st, '0', sizeof(*u.st));
478 u.sa->sa_family = 0xff;
479 unsigned int len = sizeof(*u.st) + 8;
480 int ret = connect(-1, (void *) u.st, len);
481 printf("connect(-1, {sa_family=%#x /* AF_??? */, sa_data=\"%.*u\"}"
482 ", %u) = %d EBADF (%m)\n", u.sa->sa_family,
483 (int) (sizeof(*u.st) - sizeof(u.sa->sa_family)), 0, len, ret);
486 len = sizeof(u.sa->sa_family) + 1;
487 ret = connect(-1, (void *) u.st, len);
488 printf("connect(-1, {sa_family=AF_UNSPEC, sa_data=\"0\"}, %u)"
489 " = %d EBADF (%m)\n", len, ret);
491 u.sa->sa_family = AF_BLUETOOTH;
493 ret = connect(-1, (void *) u.st, len);
494 printf("connect(-1, {sa_family=AF_BLUETOOTH, sa_data=\"00\"}, %u)"
495 " = %d EBADF (%m)\n", len, ret);
507 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
515 puts("+++ exited with 0 +++");