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

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

Issue 524603002: sandbox: Fix RedirectToUserSpacePolicyWrapper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: code review changes Created 6 years, 3 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
« no previous file with comments | « sandbox/linux/seccomp-bpf/sandbox_bpf.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <pthread.h> 6 #include <pthread.h>
7 #include <sched.h> 7 #include <sched.h>
8 #include <signal.h> 8 #include <signal.h>
9 #include <sys/prctl.h> 9 #include <sys/prctl.h>
10 #include <sys/ptrace.h> 10 #include <sys/ptrace.h>
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 } 497 }
498 498
499 ErrorCode GreyListedPolicy(SandboxBPF* sandbox, int sysno, int* aux) { 499 ErrorCode GreyListedPolicy(SandboxBPF* sandbox, int sysno, int* aux) {
500 // Set the global environment for unsafe traps once. 500 // Set the global environment for unsafe traps once.
501 if (sysno == MIN_SYSCALL) { 501 if (sysno == MIN_SYSCALL) {
502 EnableUnsafeTraps(); 502 EnableUnsafeTraps();
503 } 503 }
504 504
505 // Some system calls must always be allowed, if our policy wants to make 505 // Some system calls must always be allowed, if our policy wants to make
506 // use of UnsafeTrap() 506 // use of UnsafeTrap()
507 if (sysno == __NR_rt_sigprocmask || sysno == __NR_rt_sigreturn 507 if (SandboxBPF::IsRequiredForUnsafeTrap(sysno)) {
508 #if defined(__NR_sigprocmask)
509 ||
510 sysno == __NR_sigprocmask
511 #endif
512 #if defined(__NR_sigreturn)
513 ||
514 sysno == __NR_sigreturn
515 #endif
516 ) {
517 return ErrorCode(ErrorCode::ERR_ALLOWED); 508 return ErrorCode(ErrorCode::ERR_ALLOWED);
518 } else if (sysno == __NR_getpid) { 509 } else if (sysno == __NR_getpid) {
519 // Disallow getpid() 510 // Disallow getpid()
520 return ErrorCode(EPERM); 511 return ErrorCode(EPERM);
521 } else if (SandboxBPF::IsValidSyscallNumber(sysno)) { 512 } else if (SandboxBPF::IsValidSyscallNumber(sysno)) {
522 // Allow (and count) all other system calls. 513 // Allow (and count) all other system calls.
523 return sandbox->UnsafeTrap(CountSyscalls, aux); 514 return sandbox->UnsafeTrap(CountSyscalls, aux);
524 } else { 515 } else {
525 return ErrorCode(ENOSYS); 516 return ErrorCode(ENOSYS);
526 } 517 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 }; 621 };
631 622
632 ErrorCode RedirectAllSyscallsPolicy::EvaluateSyscall(SandboxBPF* sandbox, 623 ErrorCode RedirectAllSyscallsPolicy::EvaluateSyscall(SandboxBPF* sandbox,
633 int sysno) const { 624 int sysno) const {
634 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno)); 625 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno));
635 setenv(kSandboxDebuggingEnv, "t", 0); 626 setenv(kSandboxDebuggingEnv, "t", 0);
636 Die::SuppressInfoMessages(true); 627 Die::SuppressInfoMessages(true);
637 628
638 // Some system calls must always be allowed, if our policy wants to make 629 // Some system calls must always be allowed, if our policy wants to make
639 // use of UnsafeTrap() 630 // use of UnsafeTrap()
640 if (sysno == __NR_rt_sigprocmask || sysno == __NR_rt_sigreturn 631 if (SandboxBPF::IsRequiredForUnsafeTrap(sysno))
641 #if defined(__NR_sigprocmask)
642 ||
643 sysno == __NR_sigprocmask
644 #endif
645 #if defined(__NR_sigreturn)
646 ||
647 sysno == __NR_sigreturn
648 #endif
649 ) {
650 return ErrorCode(ErrorCode::ERR_ALLOWED); 632 return ErrorCode(ErrorCode::ERR_ALLOWED);
651 }
652 return sandbox->UnsafeTrap(AllowRedirectedSyscall, NULL); 633 return sandbox->UnsafeTrap(AllowRedirectedSyscall, NULL);
653 } 634 }
654 635
655 int bus_handler_fd_ = -1; 636 int bus_handler_fd_ = -1;
656 637
657 void SigBusHandler(int, siginfo_t* info, void* void_context) { 638 void SigBusHandler(int, siginfo_t* info, void* void_context) {
658 BPF_ASSERT(write(bus_handler_fd_, "\x55", 1) == 1); 639 BPF_ASSERT(write(bus_handler_fd_, "\x55", 1) == 1);
659 } 640 }
660 641
661 BPF_TEST_C(SandboxBPF, SigBus, RedirectAllSyscallsPolicy) { 642 BPF_TEST_C(SandboxBPF, SigBus, RedirectAllSyscallsPolicy) {
(...skipping 1592 matching lines...) Expand 10 before | Expand all | Expand 10 after
2254 #if !defined(THREAD_SANITIZER) 2235 #if !defined(THREAD_SANITIZER)
2255 SANDBOX_DEATH_TEST(SandboxBPF, StartSingleThreadedAsMultiThreaded, 2236 SANDBOX_DEATH_TEST(SandboxBPF, StartSingleThreadedAsMultiThreaded,
2256 DEATH_MESSAGE("Cannot start sandbox; process may be single-threaded when " 2237 DEATH_MESSAGE("Cannot start sandbox; process may be single-threaded when "
2257 "reported as not")) { 2238 "reported as not")) {
2258 SandboxBPF sandbox; 2239 SandboxBPF sandbox;
2259 sandbox.SetSandboxPolicy(new AllowAllPolicy()); 2240 sandbox.SetSandboxPolicy(new AllowAllPolicy());
2260 BPF_ASSERT(!sandbox.StartSandbox(SandboxBPF::PROCESS_MULTI_THREADED)); 2241 BPF_ASSERT(!sandbox.StartSandbox(SandboxBPF::PROCESS_MULTI_THREADED));
2261 } 2242 }
2262 #endif // !defined(THREAD_SANITIZER) 2243 #endif // !defined(THREAD_SANITIZER)
2263 2244
2245 // A stub handler for the UnsafeTrap. Never called.
2246 intptr_t NoOpHandler(const struct arch_seccomp_data& args, void*) {
2247 return -1;
2248 }
2249
2250 class UnsafeTrapWithCondPolicy : public SandboxBPFPolicy {
2251 public:
2252 UnsafeTrapWithCondPolicy() {}
2253 virtual ErrorCode EvaluateSyscall(SandboxBPF* sandbox,
2254 int sysno) const OVERRIDE {
2255 DCHECK(SandboxBPF::IsValidSyscallNumber(sysno));
2256 setenv(kSandboxDebuggingEnv, "t", 0);
2257 Die::SuppressInfoMessages(true);
2258
2259 if (SandboxBPF::IsRequiredForUnsafeTrap(sysno))
2260 return ErrorCode(ErrorCode::ERR_ALLOWED);
2261
2262 switch (sysno) {
2263 case __NR_close:
2264 return sandbox->Cond(0,
2265 ErrorCode::TP_32BIT,
2266 ErrorCode::OP_EQUAL,
2267 100,
2268 ErrorCode(ErrorCode::ERR_ALLOWED),
2269 ErrorCode(EPERM));
2270 case __NR_setgid:
2271 return sandbox->Cond(0,
2272 ErrorCode::TP_32BIT,
2273 ErrorCode::OP_EQUAL,
2274 100,
2275 ErrorCode(ErrorCode(ENOMEM)),
2276 sandbox->Cond(0,
2277 ErrorCode::TP_32BIT,
2278 ErrorCode::OP_EQUAL,
2279 200,
2280 ErrorCode(ENOSYS),
2281 ErrorCode(EPERM)));
2282 case __NR_write:
2283 case __NR_exit_group:
2284 return ErrorCode(ErrorCode::ERR_ALLOWED);
2285 case __NR_getppid:
2286 return sandbox->UnsafeTrap(NoOpHandler, NULL);
2287 default:
2288 return ErrorCode(EPERM);
2289 }
2290 }
2291
2292 private:
2293 DISALLOW_COPY_AND_ASSIGN(UnsafeTrapWithCondPolicy);
2294 };
2295
2296 BPF_TEST_C(SandboxBPF, UnsafeTrapWithCond, UnsafeTrapWithCondPolicy) {
2297 BPF_ASSERT_EQ(-1, syscall(__NR_close, 100));
jln (very slow on Chromium) 2014/09/02 18:41:54 In general it's not a good idea to add strong assu
leecam 2014/09/02 19:00:12 I agree, I was trying to think of a syscall that t
2298 BPF_ASSERT_EQ(EBADF, errno);
2299
2300 BPF_ASSERT_EQ(-1, syscall(__NR_close, 101));
2301 BPF_ASSERT_EQ(EPERM, errno);
2302
2303 BPF_ASSERT_EQ(-1, syscall(__NR_setgid, 100));
2304 BPF_ASSERT_EQ(ENOMEM, errno);
2305
2306 BPF_ASSERT_EQ(-1, syscall(__NR_setgid, 200));
2307 BPF_ASSERT_EQ(ENOSYS, errno);
2308
2309 BPF_ASSERT_EQ(-1, syscall(__NR_setgid, 300));
2310 BPF_ASSERT_EQ(EPERM, errno);
2311 }
2312
2264 } // namespace 2313 } // namespace
2265 2314
2266 } // namespace sandbox 2315 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/seccomp-bpf/sandbox_bpf.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698