| OLD | NEW |
| 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> |
| 11 #include <pthread.h> | 11 #include <pthread.h> |
| 12 #include <signal.h> | 12 #include <signal.h> |
| 13 #include <stdarg.h> | 13 #include <stdarg.h> |
| 14 #include <stdio.h> | 14 #include <stdio.h> |
| 15 #include <stdlib.h> | 15 #include <stdlib.h> |
| 16 #include <string.h> | 16 #include <string.h> |
| 17 #include <sys/ioctl.h> | 17 #include <sys/ioctl.h> |
| 18 #include <sys/ipc.h> | 18 #include <sys/ipc.h> |
| 19 #include <sys/mman.h> | 19 #include <sys/mman.h> |
| 20 #include <sys/prctl.h> | 20 #include <sys/prctl.h> |
| 21 #include <sys/resource.h> | 21 #include <sys/resource.h> |
| 22 #include <sys/shm.h> | 22 #include <sys/shm.h> |
| 23 #include <sys/socket.h> | 23 #include <sys/socket.h> |
| 24 #include <sys/time.h> | 24 #include <sys/time.h> |
| 25 #include <sys/types.h> | 25 #include <sys/types.h> |
| 26 #include <time.h> | 26 #include <time.h> |
| 27 #include <unistd.h> | 27 #include <unistd.h> |
| 28 | 28 |
| 29 #include "base/macros.h" |
| 29 #include "base/posix/eintr_wrapper.h" | 30 #include "base/posix/eintr_wrapper.h" |
| 30 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 31 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 32 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" |
| 31 #include "sandbox/linux/services/linux_syscalls.h" | 33 #include "sandbox/linux/services/linux_syscalls.h" |
| 32 | 34 |
| 33 using sandbox::ErrorCode; | 35 using sandbox::ErrorCode; |
| 34 using sandbox::SandboxBPF; | 36 using sandbox::SandboxBPF; |
| 37 using sandbox::SandboxBPFPolicy; |
| 35 using sandbox::arch_seccomp_data; | 38 using sandbox::arch_seccomp_data; |
| 36 | 39 |
| 37 #define ERR EPERM | 40 #define ERR EPERM |
| 38 | 41 |
| 39 // We don't expect our sandbox to do anything useful yet. So, we will fail | 42 // We don't expect our sandbox to do anything useful yet. So, we will fail |
| 40 // almost immediately. For now, force the code to continue running. The | 43 // almost immediately. For now, force the code to continue running. The |
| 41 // following line should be removed as soon as the sandbox is starting to | 44 // following line should be removed as soon as the sandbox is starting to |
| 42 // actually enforce restrictions in a meaningful way: | 45 // actually enforce restrictions in a meaningful way: |
| 43 #define _exit(x) do { } while (0) | 46 #define _exit(x) do { } while (0) |
| 44 | 47 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 233 |
| 231 ptr = strrchr(ptr, '\000'); | 234 ptr = strrchr(ptr, '\000'); |
| 232 strncat(ptr, msg1, sizeof(buf) - (ptr - buf)); | 235 strncat(ptr, msg1, sizeof(buf) - (ptr - buf)); |
| 233 | 236 |
| 234 ptr = strrchr(ptr, '\000'); | 237 ptr = strrchr(ptr, '\000'); |
| 235 if (HANDLE_EINTR(write(2, buf, ptr - buf))) { } | 238 if (HANDLE_EINTR(write(2, buf, ptr - buf))) { } |
| 236 | 239 |
| 237 return -ERR; | 240 return -ERR; |
| 238 } | 241 } |
| 239 | 242 |
| 240 ErrorCode Evaluator(SandboxBPF* sandbox, int sysno, void *) { | 243 class DemoPolicy : public SandboxBPFPolicy { |
| 244 public: |
| 245 DemoPolicy() {} |
| 246 virtual ErrorCode EvaluateSyscall(SandboxBPF* sandbox, |
| 247 int sysno) const OVERRIDE; |
| 248 |
| 249 private: |
| 250 DISALLOW_COPY_AND_ASSIGN(DemoPolicy); |
| 251 }; |
| 252 |
| 253 ErrorCode DemoPolicy::EvaluateSyscall(SandboxBPF* sandbox, int sysno) const { |
| 241 switch (sysno) { | 254 switch (sysno) { |
| 242 #if defined(__NR_accept) | 255 #if defined(__NR_accept) |
| 243 case __NR_accept: case __NR_accept4: | 256 case __NR_accept: case __NR_accept4: |
| 244 #endif | 257 #endif |
| 245 case __NR_alarm: | 258 case __NR_alarm: |
| 246 case __NR_brk: | 259 case __NR_brk: |
| 247 case __NR_clock_gettime: | 260 case __NR_clock_gettime: |
| 248 case __NR_close: | 261 case __NR_close: |
| 249 case __NR_dup: case __NR_dup2: | 262 case __NR_dup: case __NR_dup2: |
| 250 case __NR_epoll_create: case __NR_epoll_ctl: case __NR_epoll_wait: | 263 case __NR_epoll_create: case __NR_epoll_ctl: case __NR_epoll_wait: |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 if (argc) { } | 426 if (argc) { } |
| 414 if (argv) { } | 427 if (argv) { } |
| 415 int proc_fd = open("/proc", O_RDONLY|O_DIRECTORY); | 428 int proc_fd = open("/proc", O_RDONLY|O_DIRECTORY); |
| 416 if (SandboxBPF::SupportsSeccompSandbox(proc_fd) != | 429 if (SandboxBPF::SupportsSeccompSandbox(proc_fd) != |
| 417 SandboxBPF::STATUS_AVAILABLE) { | 430 SandboxBPF::STATUS_AVAILABLE) { |
| 418 perror("sandbox"); | 431 perror("sandbox"); |
| 419 _exit(1); | 432 _exit(1); |
| 420 } | 433 } |
| 421 SandboxBPF sandbox; | 434 SandboxBPF sandbox; |
| 422 sandbox.set_proc_fd(proc_fd); | 435 sandbox.set_proc_fd(proc_fd); |
| 423 sandbox.SetSandboxPolicyDeprecated(Evaluator, NULL); | 436 sandbox.SetSandboxPolicy(new DemoPolicy()); |
| 424 if (!sandbox.StartSandbox(SandboxBPF::PROCESS_SINGLE_THREADED)) { | 437 if (!sandbox.StartSandbox(SandboxBPF::PROCESS_SINGLE_THREADED)) { |
| 425 fprintf(stderr, "StartSandbox() failed"); | 438 fprintf(stderr, "StartSandbox() failed"); |
| 426 _exit(1); | 439 _exit(1); |
| 427 } | 440 } |
| 428 | 441 |
| 429 // Check that we can create threads | 442 // Check that we can create threads |
| 430 pthread_t thr; | 443 pthread_t thr; |
| 431 if (!pthread_create(&thr, NULL, ThreadFnc, | 444 if (!pthread_create(&thr, NULL, ThreadFnc, |
| 432 reinterpret_cast<void *>(0x1234))) { | 445 reinterpret_cast<void *>(0x1234))) { |
| 433 void *ret; | 446 void *ret; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 perror("pthread_create"); | 533 perror("pthread_create"); |
| 521 _exit(1); | 534 _exit(1); |
| 522 } | 535 } |
| 523 } | 536 } |
| 524 for (int i = 0; i < kSendmsgStressNumThreads; ++i) { | 537 for (int i = 0; i < kSendmsgStressNumThreads; ++i) { |
| 525 pthread_join(sendmsgStressThreads[i], NULL); | 538 pthread_join(sendmsgStressThreads[i], NULL); |
| 526 } | 539 } |
| 527 | 540 |
| 528 return 0; | 541 return 0; |
| 529 } | 542 } |
| OLD | NEW |