forked from google/nsjail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox.cc
More file actions
220 lines (186 loc) · 6.04 KB
/
sandbox.cc
File metadata and controls
220 lines (186 loc) · 6.04 KB
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
/*
nsjail - seccomp-bpf sandboxing
-----------------------------------------
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 "sandbox.h"
#include <fcntl.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "subproc.h"
extern "C" {
#include "kafel.h"
}
#include "logs.h"
#include "unotify/syscall_defs.h"
#include "util.h"
namespace sandbox {
#ifndef PR_SET_NO_NEW_PRIVS /* in prctl.h since Linux 3.5 */
#define PR_SET_NO_NEW_PRIVS 38
#endif /* PR_SET_NO_NEW_PRIVS */
#ifndef SECCOMP_FILTER_FLAG_TSYNC
#define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
#endif /* SECCOMP_FILTER_FLAG_TSYNC */
#ifndef SECCOMP_FILTER_FLAG_LOG
#define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
#endif /* SECCOMP_FILTER_FLAG_LOG */
#ifndef SECCOMP_FILTER_FLAG_NEW_LISTENER
#define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3)
#endif /* SECCOMP_FILTER_FLAG_NEW_LISTENER */
bool installUnotifyFilter(nsj_t* nsj, int pipefd) {
if (!nsj->njc.seccomp_unotify()) {
return true;
}
if (nsj->seccomp_unotify_fprog.len == 0) {
LOG_E("seccomp_unotify enabled but no BPF program compiled");
return false;
}
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
PLOG_W("prctl(PR_SET_NO_NEW_PRIVS, 1) failed");
return false;
}
int ret = util::syscall(__NR_seccomp, (uintptr_t)SECCOMP_SET_MODE_FILTER,
(uintptr_t)SECCOMP_FILTER_FLAG_NEW_LISTENER, (uintptr_t)&nsj->seccomp_unotify_fprog);
if (ret == -1) {
PLOG_E("seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_NEW_LISTENER) failed");
return false;
}
int unotif_fd = ret;
if (fcntl(unotif_fd, F_SETFD, FD_CLOEXEC) == -1) {
PLOG_E("fcntl(unotif_fd, F_SETFD, FD_CLOEXEC) failed");
close(unotif_fd);
return false;
}
if (!util::sendFd(pipefd, unotif_fd)) {
PLOG_E("sendFd(unotif_fd) to parent failed");
close(unotif_fd);
return false;
}
LOG_D("Child: sent unotif_fd=%d to parent", unotif_fd);
close(unotif_fd);
return true;
}
static bool prepareAndCommit(nsj_t* nsj) {
if (nsj->seccomp_fprog.len == 0) {
return true;
}
/* PR_SET_NO_NEW_PRIVS is idempotent; may already be set by installUnotifyFilter */
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
PLOG_W("prctl(PR_SET_NO_NEW_PRIVS, 1) failed");
return false;
}
unsigned int flags = 0;
if (nsj->njc.seccomp_log()) {
flags |= (SECCOMP_FILTER_FLAG_LOG | SECCOMP_FILTER_FLAG_TSYNC);
}
if (flags != 0) {
int ret = util::syscall(__NR_seccomp, (uintptr_t)SECCOMP_SET_MODE_FILTER,
(uintptr_t)flags, (uintptr_t)&nsj->seccomp_fprog);
if (ret == -1) {
PLOG_E("seccomp(SECCOMP_SET_MODE_FILTER) failed");
return false;
}
} else {
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &nsj->seccomp_fprog, 0, 0)) {
PLOG_W("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER) failed");
return false;
}
}
return true;
}
bool applyPolicy(nsj_t* nsj, int pipefd) {
if (pipefd != -1 && nsj->njc.seccomp_unotify()) {
if (!installUnotifyFilter(nsj, pipefd)) {
return false;
}
}
return prepareAndCommit(nsj);
}
bool preparePolicy(nsj_t* nsj) {
nsj->seccomp_fprog.len = 0;
nsj->seccomp_fprog.filter = NULL;
nsj->seccomp_unotify_fprog.len = 0;
nsj->seccomp_unotify_fprog.filter = NULL;
if (nsj->njc.seccomp_policy_file().empty() && nsj->njc.seccomp_string().empty() &&
!nsj->njc.seccomp_unotify()) {
return true;
}
if (!nsj->njc.seccomp_policy_file().empty() && !nsj->njc.seccomp_string().empty()) {
LOG_W("You specified both kafel seccomp policy, and kafel seccomp file. Specify "
"one only");
return false;
}
if (nsj->njc.seccomp_unotify()) {
kafel_ctxt_t unotify_ctxt = kafel_ctxt_create();
std::string unotify_policy = unotify::buildKafelPolicy();
kafel_set_input_string(unotify_ctxt, unotify_policy.c_str());
if (kafel_compile(unotify_ctxt, &nsj->seccomp_unotify_fprog) != 0) {
LOG_E("Could not compile the default unotify seccomp policy: %s",
kafel_error_msg(unotify_ctxt));
kafel_ctxt_destroy(&unotify_ctxt);
return false;
}
kafel_ctxt_destroy(&unotify_ctxt);
}
if (nsj->njc.seccomp_policy_file().empty() && nsj->njc.seccomp_string().empty()) {
return true;
}
kafel_ctxt_t ctxt = kafel_ctxt_create();
std::string combined_seccomp_policy;
if (!nsj->njc.seccomp_policy_file().empty()) {
FILE* f = fopen(nsj->njc.seccomp_policy_file().c_str(), "r");
if (!f) {
PLOG_W("Couldn't open the kafel seccomp policy file '%s'",
nsj->njc.seccomp_policy_file().c_str());
kafel_ctxt_destroy(&ctxt);
return false;
}
LOG_D("Compiling seccomp policy from file: '%s'",
nsj->njc.seccomp_policy_file().c_str());
kafel_set_input_file(ctxt, f);
} else {
for (const auto& s : nsj->njc.seccomp_string()) {
combined_seccomp_policy += s;
combined_seccomp_policy += '\n';
}
if (!combined_seccomp_policy.empty()) {
LOG_D("Compiling seccomp policy from string:\n%s",
combined_seccomp_policy.c_str());
kafel_set_input_string(ctxt, combined_seccomp_policy.c_str());
}
}
if (kafel_compile(ctxt, &nsj->seccomp_fprog) != 0) {
LOG_E("Could not compile policy: %s", kafel_error_msg(ctxt));
kafel_ctxt_destroy(&ctxt);
return false;
}
kafel_ctxt_destroy(&ctxt);
return true;
}
void closePolicy(nsj_t* nsj) {
if (nsj->seccomp_fprog.filter) {
free(nsj->seccomp_fprog.filter);
nsj->seccomp_fprog.filter = nullptr;
nsj->seccomp_fprog.len = 0;
}
if (nsj->seccomp_unotify_fprog.filter) {
free(nsj->seccomp_unotify_fprog.filter);
nsj->seccomp_unotify_fprog.filter = nullptr;
nsj->seccomp_unotify_fprog.len = 0;
}
}
} // namespace sandbox