Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Side by Side Diff: sandbox/linux/seccomp-bpf/demo.cc

Issue 12223109: SECCOMP-BPF: Refactor the BPF sandbox API to use fewer "static" fields and methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make sure unnamed namespaces are always top-level Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <errno.h> 5 #include <errno.h>
6 #include <fcntl.h> 6 #include <fcntl.h>
7 #include <linux/unistd.h> 7 #include <linux/unistd.h>
8 #include <netinet/in.h> 8 #include <netinet/in.h>
9 #include <netinet/tcp.h> 9 #include <netinet/tcp.h>
10 #include <netinet/udp.h> 10 #include <netinet/udp.h>
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 ptr = strrchr(ptr, '\000'); 131 ptr = strrchr(ptr, '\000');
132 strncat(ptr, msg1, sizeof(buf) - (ptr - buf)); 132 strncat(ptr, msg1, sizeof(buf) - (ptr - buf));
133 133
134 ptr = strrchr(ptr, '\000'); 134 ptr = strrchr(ptr, '\000');
135 if (HANDLE_EINTR(write(2, buf, ptr - buf))) { } 135 if (HANDLE_EINTR(write(2, buf, ptr - buf))) { }
136 136
137 return -ERR; 137 return -ERR;
138 } 138 }
139 139
140 static ErrorCode evaluator(int sysno, void *) { 140 static ErrorCode evaluator(Sandbox *sandbox, int sysno, void *) {
141 switch (sysno) { 141 switch (sysno) {
142 #if defined(__NR_accept) 142 #if defined(__NR_accept)
143 case __NR_accept: case __NR_accept4: 143 case __NR_accept: case __NR_accept4:
144 #endif 144 #endif
145 case __NR_alarm: 145 case __NR_alarm:
146 case __NR_brk: 146 case __NR_brk:
147 case __NR_clock_gettime: 147 case __NR_clock_gettime:
148 case __NR_close: 148 case __NR_close:
149 case __NR_dup: case __NR_dup2: 149 case __NR_dup: case __NR_dup2:
150 case __NR_epoll_create: case __NR_epoll_ctl: case __NR_epoll_wait: 150 case __NR_epoll_create: case __NR_epoll_ctl: case __NR_epoll_wait:
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 #if defined(__NR_socketpair) 219 #if defined(__NR_socketpair)
220 case __NR_socketpair: 220 case __NR_socketpair:
221 #endif 221 #endif
222 case __NR_time: 222 case __NR_time:
223 case __NR_uname: 223 case __NR_uname:
224 case __NR_write: case __NR_writev: 224 case __NR_write: case __NR_writev:
225 return ErrorCode(ErrorCode::ERR_ALLOWED); 225 return ErrorCode(ErrorCode::ERR_ALLOWED);
226 226
227 case __NR_prctl: 227 case __NR_prctl:
228 // Allow PR_SET_DUMPABLE and PR_GET_DUMPABLE. Do not allow anything else. 228 // Allow PR_SET_DUMPABLE and PR_GET_DUMPABLE. Do not allow anything else.
229 return Sandbox::Cond(1, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, 229 return sandbox->Cond(1, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
230 PR_SET_DUMPABLE, 230 PR_SET_DUMPABLE,
231 ErrorCode(ErrorCode::ERR_ALLOWED), 231 ErrorCode(ErrorCode::ERR_ALLOWED),
232 Sandbox::Cond(1, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, 232 sandbox->Cond(1, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL,
233 PR_GET_DUMPABLE, 233 PR_GET_DUMPABLE,
234 ErrorCode(ErrorCode::ERR_ALLOWED), 234 ErrorCode(ErrorCode::ERR_ALLOWED),
235 Sandbox::Trap(defaultHandler, NULL))); 235 sandbox->Trap(defaultHandler, NULL)));
236 236
237 // The following system calls are temporarily permitted. This must be 237 // The following system calls are temporarily permitted. This must be
238 // tightened later. But we currently don't implement enough of the sandboxing 238 // tightened later. But we currently don't implement enough of the sandboxing
239 // API to do so. 239 // API to do so.
240 // As is, this sandbox isn't exactly safe :-/ 240 // As is, this sandbox isn't exactly safe :-/
241 #if defined(__NR_sendmsg) 241 #if defined(__NR_sendmsg)
242 case __NR_sendmsg: case __NR_sendto: 242 case __NR_sendmsg: case __NR_sendto:
243 case __NR_recvmsg: case __NR_recvfrom: 243 case __NR_recvmsg: case __NR_recvfrom:
244 case __NR_getsockopt: case __NR_setsockopt: 244 case __NR_getsockopt: case __NR_setsockopt:
245 #elif defined(__NR_socketcall) 245 #elif defined(__NR_socketcall)
(...skipping 14 matching lines...) Expand all
260 #endif 260 #endif
261 case __NR_getrlimit: 261 case __NR_getrlimit:
262 case __NR_ioctl: 262 case __NR_ioctl:
263 case __NR_clone: 263 case __NR_clone:
264 case __NR_munmap: case __NR_mprotect: case __NR_madvise: 264 case __NR_munmap: case __NR_mprotect: case __NR_madvise:
265 case __NR_remap_file_pages: 265 case __NR_remap_file_pages:
266 return ErrorCode(ErrorCode::ERR_ALLOWED); 266 return ErrorCode(ErrorCode::ERR_ALLOWED);
267 267
268 // Everything that isn't explicitly allowed is denied. 268 // Everything that isn't explicitly allowed is denied.
269 default: 269 default:
270 return Sandbox::Trap(defaultHandler, NULL); 270 return sandbox->Trap(defaultHandler, NULL);
271 } 271 }
272 } 272 }
273 273
274 static void *threadFnc(void *arg) { 274 static void *threadFnc(void *arg) {
275 return arg; 275 return arg;
276 } 276 }
277 277
278 static void *sendmsgStressThreadFnc(void *arg) { 278 static void *sendmsgStressThreadFnc(void *arg) {
279 if (arg) { } 279 if (arg) { }
280 static const int repetitions = 100; 280 static const int repetitions = 100;
(...skipping 28 matching lines...) Expand all
309 309
310 int main(int argc, char *argv[]) { 310 int main(int argc, char *argv[]) {
311 if (argc) { } 311 if (argc) { }
312 if (argv) { } 312 if (argv) { }
313 int proc_fd = open("/proc", O_RDONLY|O_DIRECTORY); 313 int proc_fd = open("/proc", O_RDONLY|O_DIRECTORY);
314 if (Sandbox::SupportsSeccompSandbox(proc_fd) != 314 if (Sandbox::SupportsSeccompSandbox(proc_fd) !=
315 Sandbox::STATUS_AVAILABLE) { 315 Sandbox::STATUS_AVAILABLE) {
316 perror("sandbox"); 316 perror("sandbox");
317 _exit(1); 317 _exit(1);
318 } 318 }
319 Sandbox::set_proc_fd(proc_fd); 319 Sandbox sandbox;
320 Sandbox::SetSandboxPolicy(evaluator, NULL); 320 sandbox.set_proc_fd(proc_fd);
321 Sandbox::StartSandbox(); 321 sandbox.SetSandboxPolicy(evaluator, NULL);
322 sandbox.StartSandbox();
322 323
323 // Check that we can create threads 324 // Check that we can create threads
324 pthread_t thr; 325 pthread_t thr;
325 if (!pthread_create(&thr, NULL, threadFnc, 326 if (!pthread_create(&thr, NULL, threadFnc,
326 reinterpret_cast<void *>(0x1234))) { 327 reinterpret_cast<void *>(0x1234))) {
327 void *ret; 328 void *ret;
328 pthread_join(thr, &ret); 329 pthread_join(thr, &ret);
329 if (ret != reinterpret_cast<void *>(0x1234)) { 330 if (ret != reinterpret_cast<void *>(0x1234)) {
330 perror("clone() failed"); 331 perror("clone() failed");
331 _exit(1); 332 _exit(1);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 perror("pthread_create"); 415 perror("pthread_create");
415 _exit(1); 416 _exit(1);
416 } 417 }
417 } 418 }
418 for (int i = 0; i < kSendmsgStressNumThreads; ++i) { 419 for (int i = 0; i < kSendmsgStressNumThreads; ++i) {
419 pthread_join(sendmsgStressThreads[i], NULL); 420 pthread_join(sendmsgStressThreads[i], NULL);
420 } 421 }
421 422
422 return 0; 423 return 0;
423 } 424 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698