forked from RishavT/nsjail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subproc.c
302 lines (266 loc) · 8.05 KB
/
subproc.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
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
/*
nsjail - subprocess management
-----------------------------------------
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "subproc.h"
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/queue.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "common.h"
#include "contain.h"
#include "log.h"
#include "net.h"
#include "sandbox.h"
#include "user.h"
#include "util.h"
const char subprocDoneChar = 'D';
static int subprocNewProc(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_err, int pipefd)
{
if (containSetupFD(nsjconf, fd_in, fd_out, fd_err) == false) {
exit(1);
}
if (pipefd == -1) {
if (userInitNsFromParent(nsjconf, syscall(__NR_getpid)) == false) {
LOG_E("Couldn't initialize user namespaces");
exit(1);
}
} else {
char doneChar;
if (utilReadFromFd(pipefd, &doneChar, sizeof(doneChar)) != sizeof(doneChar)) {
exit(1);
}
if (doneChar != subprocDoneChar) {
exit(1);
}
}
if (containContain(nsjconf) == false) {
exit(1);
}
if (nsjconf->keep_env == false) {
clearenv();
}
struct charptr_t *p;
TAILQ_FOREACH(p, &nsjconf->envs, pointers) {
putenv(p->val);
}
LOG_D("Trying to execve('%s')", nsjconf->argv[0]);
for (size_t i = 0; nsjconf->argv[i]; i++) {
LOG_D(" Arg[%zu]: '%s'", i, nsjconf->argv[i]);
}
/* Should be the last one in the sequence */
if (sandboxApply(nsjconf) == false) {
exit(1);
}
execv(nsjconf->argv[0], &nsjconf->argv[0]);
PLOG_E("execve('%s') failed", nsjconf->argv[0]);
_exit(1);
}
static void subprocAdd(struct nsjconf_t *nsjconf, pid_t pid, int sock)
{
struct pids_t *p = utilMalloc(sizeof(struct pids_t));
p->pid = pid;
p->start = time(NULL);
netConnToText(sock, true /* remote */ , p->remote_txt, sizeof(p->remote_txt),
&p->remote_addr);
TAILQ_INSERT_HEAD(&nsjconf->pids, p, pointers);
LOG_D("Added pid '%d' with start time '%u' to the queue for IP: '%s'", pid,
(unsigned int)p->start, p->remote_txt);
}
static void subprocRemove(struct nsjconf_t *nsjconf, pid_t pid)
{
struct pids_t *p;
TAILQ_FOREACH(p, &nsjconf->pids, pointers) {
if (p->pid == pid) {
LOG_D("Removing pid '%d' from the queue (IP:'%s', start time:'%u')", p->pid,
p->remote_txt, (unsigned int)p->start);
TAILQ_REMOVE(&nsjconf->pids, p, pointers);
free(p);
return;
}
}
LOG_W("PID: %d not found (?)", pid);
}
int subprocCount(struct nsjconf_t *nsjconf)
{
int cnt = 0;
struct pids_t *p;
TAILQ_FOREACH(p, &nsjconf->pids, pointers) {
cnt++;
}
return cnt;
}
void subprocDisplay(struct nsjconf_t *nsjconf)
{
LOG_I("Total number of spawned namespaces: %d", subprocCount(nsjconf));
time_t now = time(NULL);
struct pids_t *p;
TAILQ_FOREACH(p, &nsjconf->pids, pointers) {
time_t diff = now - p->start;
time_t left = nsjconf->tlimit ? nsjconf->tlimit - diff : 0;
LOG_I("PID: %d, Remote host: %s, Run time: %ld sec. (time left: %ld sec.)", p->pid,
p->remote_txt, (long)diff, (long)left);
}
}
static void subprocSeccompViolation(siginfo_t * si)
{
LOG_W("PID %d commited syscall/seccomp violation and exited with SIGSYS", si->si_pid);
}
int subprocReap(struct nsjconf_t *nsjconf)
{
int status;
int rv = 0;
siginfo_t si;
for (;;) {
si.si_pid = 0;
if (waitid(P_ALL, 0, &si, WNOHANG | WNOWAIT | WEXITED) == -1) {
break;
}
if (si.si_pid == 0) {
break;
}
if (si.si_code == CLD_KILLED && si.si_status == SIGSYS) {
subprocSeccompViolation(&si);
}
if (wait4(si.si_pid, &status, WNOHANG, NULL) == si.si_pid) {
if (WIFEXITED(status)) {
subprocRemove(nsjconf, si.si_pid);
LOG_I("PID: %d exited with status: %d, (PIDs left: %d)", si.si_pid,
WEXITSTATUS(status), subprocCount(nsjconf));
rv = WEXITSTATUS(status) % 100;
if (rv == 0 && WEXITSTATUS(status) != 0) {
rv = 1;
}
}
if (WIFSIGNALED(status)) {
subprocRemove(nsjconf, si.si_pid);
LOG_I("PID: %d terminated with signal: %d, (PIDs left: %d)",
si.si_pid, WTERMSIG(status), subprocCount(nsjconf));
rv = 100 + WTERMSIG(status);
}
}
}
time_t now = time(NULL);
struct pids_t *p;
TAILQ_FOREACH(p, &nsjconf->pids, pointers) {
if (nsjconf->tlimit == 0) {
continue;
}
pid_t pid = p->pid;
time_t diff = now - p->start;
if (diff >= nsjconf->tlimit) {
LOG_I("PID: %d run time >= time limit (%ld >= %ld) (%s). Killing it", pid,
(long)diff, (long)nsjconf->tlimit, p->remote_txt);
/* Probably a kernel bug - some processes cannot be killed with KILL if
* they're namespaced, and in a stopped state */
kill(pid, SIGCONT);
PLOG_D("Sent SIGCONT to PID: %d", pid);
kill(pid, SIGKILL);
PLOG_D("Sent SIGKILL to PID: %d", pid);
}
}
return rv;
}
void subprocKillAll(struct nsjconf_t *nsjconf)
{
struct pids_t *p;
TAILQ_FOREACH(p, &nsjconf->pids, pointers) {
kill(p->pid, SIGKILL);
}
}
static bool subprocInitParent(struct nsjconf_t *nsjconf, pid_t pid, int pipefd)
{
if (netInitNsFromParent(nsjconf, pid) == false) {
LOG_E("Couldn't create and put MACVTAP interface into NS of PID '%d'", pid);
return false;
}
if (userInitNsFromParent(nsjconf, pid) == false) {
LOG_E("Couldn't initialize user namespaces for pid %d", pid);
return false;
}
if (utilWriteToFd(pipefd, &subprocDoneChar, sizeof(subprocDoneChar)) !=
sizeof(subprocDoneChar)) {
LOG_E("Couldn't signal the new process via a socketpair");
return false;
}
return true;
}
void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_err)
{
if (netLimitConns(nsjconf, fd_in) == false) {
return;
}
unsigned long flags = 0UL;
flags |= (nsjconf->clone_newnet ? CLONE_NEWNET : 0);
flags |= (nsjconf->clone_newuser ? CLONE_NEWUSER : 0);
flags |= (nsjconf->clone_newns ? CLONE_NEWNS : 0);
flags |= (nsjconf->clone_newpid ? CLONE_NEWPID : 0);
flags |= (nsjconf->clone_newipc ? CLONE_NEWIPC : 0);
flags |= (nsjconf->clone_newuts ? CLONE_NEWUTS : 0);
if (nsjconf->mode == MODE_STANDALONE_EXECVE) {
if (nsjconf->clone_newpid) {
LOG_D("CLONE_NEWPID requested. It causes troubles with unshare() "
"[ENOMEM with clone/fork/vfork]. Disabling it");
flags &= ~(CLONE_NEWPID);
}
LOG_D("Entering namespace with flags: %#lx", flags);
if (unshare(flags) == -1) {
PLOG_E("unshare(%#lx)", flags);
_exit(EXIT_FAILURE);
}
subprocNewProc(nsjconf, fd_in, fd_out, fd_err, -1);
}
flags |= SIGCHLD;
LOG_D("Creating new process with clone flags: %#lx", flags);
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) {
PLOG_E("socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC) failed");
return;
}
pid_t pid = syscall(__NR_clone, (uintptr_t) flags, NULL, NULL, NULL, (uintptr_t) 0);
if (pid == 0) {
close(sv[1]);
subprocNewProc(nsjconf, fd_in, fd_out, fd_err, sv[0]);
}
defer {
close(sv[1]);
}
close(sv[0]);
if (pid == -1) {
PLOG_E("clone(flags=%#lx) failed. You probably need root privileges if your system "
"doesn't support CLONE_NEWUSER. Alternatively, you might want to recompile your "
"kernel with support for namespaces or check the setting of the "
"kernel.unprivileged_userns_clone sysctl", flags);
return;
}
subprocAdd(nsjconf, pid, fd_in);
if (subprocInitParent(nsjconf, pid, sv[1]) == false) {
return;
}
char cs_addr[64];
netConnToText(fd_in, true /* remote */ , cs_addr, sizeof(cs_addr), NULL);
LOG_I("PID: %d about to execute '%s' for %s", pid, nsjconf->argv[0], cs_addr);
}