Prevent oracle attacks in the legacy protocol (CVE-2018-16737, CVE-2018-16738)
[tinc] / test / splice.c
1 /*
2     splice.c -- Splice two outgoing tinc connections together
3     Copyright (C) 2018 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netdb.h>
27
28 #ifdef HAVE_MINGW
29 extern const char *winerror(int);
30 #define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
31 #define sockerrno WSAGetLastError()
32 #define sockstrerror(x) winerror(x)
33 #else
34 #define sockerrno errno
35 #define sockstrerror(x) strerror(x)
36 #endif
37
38 int main(int argc, char *argv[]) {
39         if(argc < 7) {
40                 fprintf(stderr, "Usage: %s name1 host1 port1 name2 host2 port2 [protocol]\n", argv[0]);
41                 return 1;
42         }
43
44         const char *protocol;
45
46         if(argc >= 8) {
47                 protocol = argv[7];
48         } else {
49                 protocol = "17.7";
50         }
51
52 #ifdef HAVE_MINGW
53         static struct WSAData wsa_state;
54
55         if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
56                 return 1;
57         }
58
59 #endif
60         int sock[2];
61         char buf[1024];
62
63         struct addrinfo *ai, hint;
64         memset(&hint, 0, sizeof(hint));
65
66         hint.ai_family = AF_UNSPEC;
67         hint.ai_socktype = SOCK_STREAM;
68         hint.ai_protocol = IPPROTO_TCP;
69         hint.ai_flags = 0;
70
71         for (int i = 0; i < 2; i++) {
72                 if(getaddrinfo(argv[2 + 3 * i], argv[3 + 3 * i], &hint, &ai) || !ai) {
73                         fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
74                         return 1;
75                 }
76
77                 sock[i] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
78
79                 if(sock[i] == -1) {
80                         fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
81                         return 1;
82                 }
83
84                 if(connect(sock[i], ai->ai_addr, ai->ai_addrlen)) {
85                         fprintf(stderr, "Could not connect to %s: %s\n", argv[i + 3 * i], sockstrerror(sockerrno));
86                         return 1;
87                 }
88
89                 fprintf(stderr, "Connected to %s\n", argv[1 + 3 * i]);
90
91                 /* Pretend to be the other one */
92                 int len = snprintf(buf, sizeof buf, "0 %s %s\n", argv[4 - 3 * i], protocol);
93                 if (send(sock[i], buf, len, 0) != len) {
94                         fprintf(stderr, "Error sending data to %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
95                         return 1;
96                 }
97
98                 /* Ignore the response */
99                 do {
100                         if (recv(sock[i], buf, 1, 0) != 1) {
101                                 fprintf(stderr, "Error reading data from %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
102                                 return 1;
103                         }
104                 } while(*buf != '\n');
105         }
106
107         fprintf(stderr, "Splicing...\n");
108
109         int nfds = (sock[0] > sock[1] ? sock[0] : sock[1]) + 1;
110
111         while(true) {
112                 fd_set fds;
113                 FD_ZERO(&fds);
114                 FD_SET(sock[0], &fds);
115                 FD_SET(sock[1], &fds);
116
117                 if(select(nfds, &fds, NULL, NULL, NULL) <= 0) {
118                         return 1;
119                 }
120
121                 for(int i = 0; i < 2; i++ ) {
122                         if(FD_ISSET(sock[i], &fds)) {
123                                 ssize_t len = recv(sock[i], buf, sizeof buf, 0);
124
125                                 if(len < 0) {
126                                         fprintf(stderr, "Error while reading from %s: %s\n", argv[1 + i * 3], sockstrerror(sockerrno));
127                                         return 1;
128                                 }
129
130                                 if(len == 0) {
131                                         fprintf(stderr, "Connection closed by %s\n", argv[1 + i * 3]);
132                                         return 0;
133                                 }
134
135                                 if(send(sock[i ^ 1], buf, len, 0) != len) {
136                                         fprintf(stderr, "Error while writing to %s: %s\n", argv[4 - i * 3], sockstrerror(sockerrno));
137                                         return 1;
138                                 }
139                         }
140                 }
141         }
142
143         return 0;
144 }