forked from google/nsjail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsjail.c
197 lines (174 loc) · 4.09 KB
/
nsjail.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
/*
nsjail
-----------------------------------------
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 "nsjail.h"
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "cmdline.h"
#include "log.h"
#include "net.h"
#include "subproc.h"
static __thread int nsjailSigFatal = 0;
static __thread bool nsjailShowProc = false;
static void nsjailSig(int sig)
{
if (sig == SIGALRM) {
return;
}
if (sig == SIGCHLD) {
return;
}
if (sig == SIGUSR1) {
nsjailShowProc = true;
return;
}
nsjailSigFatal = sig;
}
static bool nsjailSetSigHandler(int sig)
{
LOG_D("Setting sighandler for signal '%d' (%s)", sig, strsignal(sig));
sigset_t smask;
sigemptyset(&smask);
struct sigaction sa = {
.sa_handler = nsjailSig,
.sa_mask = smask,
.sa_flags = 0,
.sa_restorer = NULL,
};
if (sigaction(sig, &sa, NULL) == -1) {
PLOG_E("sigaction(%d)", sig);
return false;
}
return true;
}
static bool nsjailSetSigHandlers(void)
{
if (nsjailSetSigHandler(SIGINT) == false) {
return false;
}
if (nsjailSetSigHandler(SIGUSR1) == false) {
return false;
}
if (nsjailSetSigHandler(SIGALRM) == false) {
return false;
}
if (nsjailSetSigHandler(SIGCHLD) == false) {
return false;
}
if (nsjailSetSigHandler(SIGTERM) == false) {
return false;
}
return true;
}
static bool nsjailSetTimer(struct nsjconf_t *nsjconf)
{
if (nsjconf->mode == MODE_STANDALONE_EXECVE) {
return true;
}
struct itimerval it = {
.it_value = {.tv_sec = 1,.tv_usec = 0},
.it_interval = {.tv_sec = 1,.tv_usec = 0},
};
if (setitimer(ITIMER_REAL, &it, NULL) == -1) {
PLOG_E("setitimer(ITIMER_REAL)");
return false;
}
return true;
}
static void nsjailListenMode(struct nsjconf_t *nsjconf)
{
int listenfd = netGetRecvSocket(nsjconf->bindhost, nsjconf->port);
if (listenfd == -1) {
return;
}
for (;;) {
if (nsjailSigFatal > 0) {
subprocKillAll(nsjconf);
logStop(nsjailSigFatal);
close(listenfd);
return;
}
if (nsjailShowProc == true) {
nsjailShowProc = false;
subprocDisplay(nsjconf);
}
int connfd = netAcceptConn(listenfd);
if (connfd >= 0) {
subprocRunChild(nsjconf, connfd, connfd, connfd);
close(connfd);
}
subprocReap(nsjconf);
}
}
static int nsjailStandaloneMode(struct nsjconf_t *nsjconf)
{
subprocRunChild(nsjconf, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
for (;;) {
int child_status = subprocReap(nsjconf);
if (subprocCount(nsjconf) == 0) {
if (nsjconf->mode == MODE_STANDALONE_ONCE) {
return child_status;
}
subprocRunChild(nsjconf, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
continue;
}
if (nsjailShowProc == true) {
nsjailShowProc = false;
subprocDisplay(nsjconf);
}
if (nsjailSigFatal > 0) {
subprocKillAll(nsjconf);
logStop(nsjailSigFatal);
return -1;
}
pause();
}
// not reached
}
int main(int argc, char *argv[])
{
struct nsjconf_t nsjconf;
if (!cmdlineParse(argc, argv, &nsjconf)) {
exit(1);
}
if (nsjconf.clone_newuser == false && geteuid() != 0) {
LOG_W("--disable_clone_newuser requires root() privs");
}
if (nsjconf.daemonize && (daemon(0, 0) == -1)) {
PLOG_F("daemon");
}
cmdlineLogParams(&nsjconf);
if (nsjailSetSigHandlers() == false) {
exit(1);
}
if (nsjailSetTimer(&nsjconf) == false) {
exit(1);
}
if (nsjconf.mode == MODE_LISTEN_TCP) {
nsjailListenMode(&nsjconf);
} else {
return nsjailStandaloneMode(&nsjconf);
}
return 0;
}
/*
* To satisfy requirement for BlocksRuntime in clang -fblocks
*/
void *_NSConcreteStackBlock[32] = { 0 };