1 /* $OpenBSD: undgram_selfconn.c,v 1.1 2021/12/09 17:25:54 mvs Exp $ */
2
3 /*
4 * Copyright (c) 2021 Vitaliy Makkoveev <mvs@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /*
20 * unix(4) datagram socket could be connected to itself. There are two
21 * cases: temporary connection for sendto(2) syscall and normal connection
22 * provided by connect(2) syscall.
23 * Be sure socket doesn't deadlock itself and doesn't crash kernel while
24 * disconnecting and connecting again.
25 */
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/un.h>
31 #include <stdio.h>
32 #include <err.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 int
main(void)37 main(void)
38 {
39 static struct sockaddr_un sun;
40 int s, buf;
41 ssize_t ret;
42
43 umask(0077);
44
45 memset(&sun, 0, sizeof(sun));
46 sun.sun_len = sizeof(sun);
47 sun.sun_family = AF_UNIX;
48 snprintf(sun.sun_path, sizeof(sun.sun_path) - 1,
49 "undgram_selfconn%d.socket", getpid());
50
51 unlink(sun.sun_path);
52
53 if ((s = socket(AF_UNIX, SOCK_DGRAM|SOCK_NONBLOCK, 0)) < 0)
54 err(1, "socket");
55 if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
56 err(1, "bind");
57
58 if (sendto(s, &s, sizeof(s), 0,
59 (struct sockaddr *)&sun, sizeof(sun)) < 0)
60 err(1, "sendto");
61
62 /*
63 * Check received data to be sure the temporary connection
64 * was successful.
65 */
66 if ((ret = recvfrom(s, &buf, sizeof(buf), 0, NULL, NULL)) < 0)
67 err(1, "recvfrom");
68 if (ret != sizeof(s))
69 errx(1, "recvfrom: wrong size");
70 if (buf != s)
71 errx(1, "recvfrom: wrong data");
72
73 if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
74 err(1, "connect");
75 /* Disconnect and connect it again */
76 if (connect(s, (struct sockaddr *)&sun, sizeof(sun)) < 0)
77 err(1, "connect");
78 close(s);
79
80 return 0;
81 }
82