summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/NAT/mkrawsock.c
blob: 50e9831fc95b3f48aed18b2a09019828212ed553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* $Id: mkrawsock.c $ */
/** @file
 * Auxiliary server to create raw-sockets when debugging unprivileged.
 */

/*
 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#ifdef __linux__
#define _GNU_SOURCE
#endif

#ifdef __sun__
#if __STDC_VERSION__ - 0 >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif
#define __EXTENSIONS__ 1
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#ifdef __linux__
#include <linux/icmp.h>         /* for ICMP_FILTER */
#endif
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


static void handler(int sig);
static void serve(int s);
static int mkrawsock(int family);

volatile sig_atomic_t signaled = 0;

int
main(int argc, char **argv)
{
    struct sigaction sa;
    struct sockaddr_un sux; /* because solaris */
    struct passwd *pw;
    size_t pathlen;
    char *slash;
    int s, client;
    int status;

    memset(&sux, 0, sizeof(sux));
    sux.sun_family = AF_UNIX;

    if (getuid() == 0) {
        if (argc != 2) {
            fprintf(stderr, "username required when run as root\n");
            return EXIT_FAILURE;
        }

        errno = 0;
        pw = getpwnam(argv[1]);
        if (pw == NULL) {
            perror("getpwnam");
            return EXIT_FAILURE;
        }
        if (pw->pw_uid == 0) {
            fprintf(stderr, "%s is superuser\n", pw->pw_name);
            return EXIT_FAILURE;
        }
    }
    else {
        errno = 0;
        pw = getpwuid(getuid());
        if (pw == NULL) {
            perror("getpwuid");
            return EXIT_FAILURE;
        }
    }

    pathlen = snprintf(sux.sun_path, sizeof(sux.sun_path),
                       "/tmp/.vbox-%s-aux/mkrawsock", pw->pw_name);
    if (pathlen > sizeof(sux.sun_path)) {
        fprintf(stderr, "socket pathname truncated\n");
        return EXIT_FAILURE;
    }

    slash = strrchr(sux.sun_path, '/');
    if (slash == NULL) {
        fprintf(stderr, "%s: no directory separator\n", sux.sun_path);
        return EXIT_FAILURE;
    }

    *slash = '\0';

    status = mkdir(sux.sun_path, 0700);
    if (status == 0) {
        status = chown(sux.sun_path, pw->pw_uid, pw->pw_gid);
        if (status < 0) {
            perror("chown");
            return EXIT_FAILURE;
        }
    }
    else if (errno != EEXIST) {
        perror("mkdir");
        return EXIT_FAILURE;
    }
    else {
        int dirfd;
        struct stat st;

        dirfd = open(sux.sun_path, O_RDONLY, O_DIRECTORY);
        if (dirfd < 0) {
            perror(sux.sun_path);
            return EXIT_FAILURE;
        }

        status = fstat(dirfd, &st);
        close(dirfd);

        if (status < 0) {
            perror(sux.sun_path);
            return EXIT_FAILURE;
        }

        if (st.st_uid != pw->pw_uid) {
            fprintf(stderr, "%s: exists but not owned by %s\n",
                    sux.sun_path, pw->pw_name);
            return EXIT_FAILURE;
        }

        if ((st.st_mode & 0777) != 0700) {
            fprintf(stderr, "%s: bad mode %04o\n",
                    sux.sun_path, (unsigned int)(st.st_mode & 0777));
            return EXIT_FAILURE;
        }
    }

    *slash = '/';

#if 0
    status = unlink(sux.sun_path);
    if (status < 0 && errno != ENOENT) {
        perror("unlink");
    }
#endif

    s = socket(PF_UNIX, SOCK_STREAM, 0);
    if (s < 0) {
        perror("socket");
        return EXIT_FAILURE;
    }

    status = bind(s, (struct sockaddr *)&sux,
                  (sizeof(sux) - sizeof(sux.sun_path)
                   + strlen(sux.sun_path) + 1));
    if (status < 0) {
        perror(sux.sun_path);
        close(s);
        return EXIT_FAILURE;
    }

    status = chown(sux.sun_path, pw->pw_uid, pw->pw_gid);
    if (status < 0) {
        perror("chown");
        close(s);
        return EXIT_FAILURE;
    }

    status = chmod(sux.sun_path, 0600);
    if (status < 0) {
        perror("chmod");
        close(s);
        return EXIT_FAILURE;
    }

    status = listen(s, 1);
    if (status < 0) {
        perror("listen");
        close(s);
        return EXIT_FAILURE;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = handler;
    sigemptyset(&sa.sa_mask);

    sigaction(SIGINT,  &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);

    while (!signaled) {
        client = accept(s, NULL, 0);
        if (client < 0) {
            perror("accept");
            continue;
        }

        serve(client);
        close(client);
    }

    close(s);
    status = unlink(sux.sun_path);
    if (status < 0) {
        perror("unlink");
    }

    return EXIT_SUCCESS;
}


static void
handler(int sig)
{
    signaled = 1;
}


static void
serve(int client)
{
#ifdef SO_PEERCRED
    struct ucred cr;
    socklen_t crlen;
#endif
    ssize_t nread, nsent;
    struct msghdr mh;
    struct iovec iov[1];
    char buf[1];
    struct cmsghdr *cmh;
    char cmsg[CMSG_SPACE(sizeof(int))];
    int fd;
    int status;

#ifdef SO_PEERCRED
    crlen = sizeof(cr);
    status = getsockopt(client, SOL_SOCKET, SO_PEERCRED, &cr, &crlen);
    if (status < 0) {
        perror("SO_PEERCRED");
        return;
    }

    fprintf(stderr, "request from pid %lu uid %lu ",
            (unsigned long)cr.pid, (unsigned long)cr.uid);
#endif

    nread = read(client, buf, 1);
    if (nread < 0) {
        perror("recv");
        return;
    }

    fd = -1;
    switch (buf[0]) {

    case '4':
        fprintf(stderr, "for ICMPv4 socket\n");
        fd = mkrawsock(PF_INET);
        break;

    case '6':
        fprintf(stderr, "for ICMPv6 socket\n");
        fd = mkrawsock(PF_INET6);
        break;

    default:
        fprintf(stderr, "bad request 0x%02x\n", (unsigned int)buf[0]);
        return;
    }

    if (fd < 0) {
        buf[0] = '\0';  /* NAK */
        nsent = write(client, buf, 1);
        (void)nsent;
        return;
    }

    memset(&mh, 0, sizeof(mh));
    memset(cmsg, 0, sizeof(cmsg));

    iov[0].iov_base = buf;
    iov[0].iov_len = 1;

    mh.msg_iov = iov;
    mh.msg_iovlen = 1;
    mh.msg_control = cmsg;
    mh.msg_controllen = sizeof(cmsg);

    cmh = CMSG_FIRSTHDR(&mh);
    cmh->cmsg_level = SOL_SOCKET;
    cmh->cmsg_type = SCM_RIGHTS;
    cmh->cmsg_len = CMSG_LEN(sizeof(fd));
    *((int *) CMSG_DATA(cmh)) = fd;

    nsent = sendmsg(client, &mh, 0);
    if (nsent < 0) {
        perror("sendmsg");
    }

    close(fd);
}


static int
mkrawsock(int family)
{
    int fd;

    if (family == PF_INET) {
        fd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
        if (fd < 0) {
            perror("IPPROTO_ICMP");
            return -1;
        }
    }
    else {
        fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
        if (fd < 0) {
            perror("IPPROTO_ICMPV6");
            return -1;
        }
    }

    return fd;
}