-
Notifications
You must be signed in to change notification settings - Fork 4
/
rawsocket.c
178 lines (147 loc) · 3.19 KB
/
rawsocket.c
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
/*
* Python module to create raw sockets as unprivileged user.
*
* Copyright (c) 2015, Michael Walle <[email protected]>
* See LICENSE for licensing terms.
*/
#include <Python.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define RAW_SOCK_HELPER "rawsocket-helper"
static int spawn_helper(int fd)
{
int status;
pid_t pid;
pid = fork();
if (pid < 0) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
if (pid == 0) {
/* child */
char fd_str[8];
char *argv[] = { RAW_SOCK_HELPER, fd_str, NULL };
char *pathenv = NULL;
char *envp[] = { pathenv, NULL };
char *path = getenv("PATH");
if (path) {
pathenv = malloc(6+strlen(path));
if (!pathenv) {
exit(1);
}
sprintf(pathenv, "PATH=%s", path);
envp[0] = pathenv;
}
snprintf(fd_str, sizeof(fd_str), "%d", fd);
execvpe(argv[0], argv, envp);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
return 0;
PyErr_Format(PyExc_IOError, "Could not spawn helper '%s' (not SUID root?).",
RAW_SOCK_HELPER);
return -1;
}
static int raw_socket(void)
{
int sv[2];
int rc;
struct iovec iov;
struct msghdr msg = {0};
struct cmsghdr *cmsg;
char buf[CMSG_SPACE(sizeof(int))];
char dummy;
/* dummy read */
iov.iov_base = &dummy;
iov.iov_len = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = buf;
msg.msg_controllen = sizeof(buf);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* create communication socket */
rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, sv);
if (rc == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
/* fork our privileged helper program */
rc = spawn_helper(sv[1]);
if (rc == -1)
return -1;
/* receive the privileged socket */
rc = recvmsg(sv[0], &msg, 0);
if (rc == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
cmsg = CMSG_FIRSTHDR(&msg);
if (!cmsg
|| cmsg->cmsg_len != CMSG_LEN(sizeof(int))
|| cmsg->cmsg_level != SOL_SOCKET
|| cmsg->cmsg_type != SCM_RIGHTS) {
PyErr_SetFromErrno(PyExc_IOError);
return -1;
}
close(sv[0]);
close(sv[1]);
return *((int *)CMSG_DATA(cmsg));
}
static PyObject *
rawsocket_fd(PyObject *self, PyObject *args)
{
int fd;
if (!PyArg_ParseTuple(args, ""))
return NULL;
fd = raw_socket();
if (fd < 0)
return NULL;
return PyLong_FromLong(fd);
}
PyDoc_STRVAR(rawsocket_fd_doc,
"rawsocket_fd()\n\
\n\
Create a new raw socket using a privileged helper program.");
static PyMethodDef rawsocket_methods[] = {
{"rawsocket_fd", rawsocket_fd,
METH_VARARGS, rawsocket_fd_doc},
{NULL, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef rawsocket_def = {
PyModuleDef_HEAD_INIT,
"rawsocket",
"A Python module to create raw sockets as unprivileged user.",
-1,
rawsocket_methods
};
#endif
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_rawsocket(void)
#else
initrawsocket(void)
#endif
{
PyObject *m = NULL, *ver;
ver = PyUnicode_FromString(VERSION);
if (ver == NULL)
goto out;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&rawsocket_def);
#else
m = Py_InitModule("rawsocket", rawsocket_methods);
#endif
if (m == NULL)
goto out;
PyModule_AddObject(m, "__version__", ver);
out:
#if PY_MAJOR_VERSION >= 3
return m;
#else
return;
#endif
}