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
|
/* $Id: getrawsock.c $ */
/** @file
* Obtain raw-sockets from a server when debugging unprivileged.
*/
/*
* Copyright (C) 2013-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/* XXX: this should be in a header, but isn't. naughty me. :( */
int getrawsock(int type);
int
getrawsock(int type)
{
struct sockaddr_un sux; /* because solaris */
struct passwd *pw;
size_t pathlen;
int rawsock, server;
struct msghdr mh;
struct iovec iov[1];
char buf[1];
struct cmsghdr *cmh;
char cmsg[CMSG_SPACE(sizeof(int))];
ssize_t nread, nsent;
int status;
server = -1;
rawsock = -1;
memset(&sux, 0, sizeof(sux));
sux.sun_family = AF_UNIX;
if (geteuid() == 0) {
return -1;
}
if (type == AF_INET) {
buf[0] = '4';
}
else if (type == AF_INET6) {
buf[0] = '6';
}
else {
return -1;
}
errno = 0;
pw = getpwuid(getuid());
if (pw == NULL) {
perror("getpwuid");
return -1;
}
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 -1;
}
server = socket(PF_UNIX, SOCK_STREAM, 0);
if (server < 0) {
perror("socket");
return -1;
}
status = connect(server, (struct sockaddr *)&sux,
(sizeof(sux) - sizeof(sux.sun_path)
+ strlen(sux.sun_path) + 1));
if (status < 0) {
perror(sux.sun_path);
goto out;
}
nsent = send(server, buf, 1, 0);
if (nsent != 1) {
if (nsent < 0) {
perror("send");
}
else {
fprintf(stderr, "failed to contact mkrawsock\n");
}
goto out;
}
buf[0] = '\0';
iov[0].iov_base = buf;
iov[0].iov_len = 1;
memset(&mh, 0, sizeof(mh));
mh.msg_iov = iov;
mh.msg_iovlen = 1;
mh.msg_control = cmsg;
mh.msg_controllen = sizeof(cmsg);
nread = recvmsg(server, &mh, 0);
if (nread != 1) {
if (nread < 0) {
perror("recvmsg");
}
else {
fprintf(stderr, "EOF from mkrawsock\n");
}
goto out;
}
if ((type == AF_INET && buf[0] != '4')
|| (type == AF_INET6 && buf[0] != '6')
|| mh.msg_controllen == 0)
{
goto out;
}
for (cmh = CMSG_FIRSTHDR(&mh); cmh != NULL; cmh = CMSG_NXTHDR(&mh, cmh)) {
if ((cmh->cmsg_level == SOL_SOCKET)
&& (cmh->cmsg_type == SCM_RIGHTS)
&& (cmh->cmsg_len == CMSG_LEN(sizeof(rawsock))))
{
rawsock = *((int *)CMSG_DATA(cmh));
break;
}
}
out:
if (server != -1) {
close(server);
}
if (rawsock != -1) {
printf("%s: got ICMPv%c socket %d\n",
__func__, type == AF_INET ? '4' : '6', rawsock);
}
return rawsock;
}
|