Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/nacl/loader/nonsfi/nonsfi_sandbox.h" | 5 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <linux/futex.h> | |
| 9 #include <linux/net.h> | 10 #include <linux/net.h> |
| 11 #include <sys/mman.h> | |
| 10 #include <sys/prctl.h> | 12 #include <sys/prctl.h> |
| 11 #include <sys/ptrace.h> | 13 #include <sys/ptrace.h> |
| 12 #include <sys/mman.h> | |
| 13 #include <sys/socket.h> | 14 #include <sys/socket.h> |
| 14 #include <sys/syscall.h> | 15 #include <sys/syscall.h> |
| 16 #include <sys/time.h> | |
| 15 | 17 |
| 16 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 17 #include "base/logging.h" | 19 #include "base/logging.h" |
| 18 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 19 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 20 #include "content/public/common/sandbox_init.h" | 22 #include "content/public/common/sandbox_init.h" |
| 21 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" | 23 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" |
| 22 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" | 24 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" |
| 23 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" | 25 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" |
| 24 #include "sandbox/linux/services/linux_syscalls.h" | 26 #include "sandbox/linux/services/linux_syscalls.h" |
| 25 | 27 |
| 26 #if defined(__arm__) && !defined(MAP_STACK) | 28 #if defined(__arm__) && !defined(MAP_STACK) |
| 27 // Chrome OS Daisy (ARM) build environment has old headers. | 29 // Chrome OS Daisy (ARM) build environment has old headers. |
| 28 #define MAP_STACK 0x20000 | 30 #define MAP_STACK 0x20000 |
| 29 #endif | 31 #endif |
| 30 | 32 |
| 31 using sandbox::CrashSIGSYS; | 33 using sandbox::CrashSIGSYS; |
| 32 using sandbox::CrashSIGSYSClone; | 34 using sandbox::CrashSIGSYSClone; |
| 35 using sandbox::CrashSIGSYSFutex; | |
| 33 using sandbox::CrashSIGSYSPrctl; | 36 using sandbox::CrashSIGSYSPrctl; |
| 34 using sandbox::bpf_dsl::Allow; | 37 using sandbox::bpf_dsl::Allow; |
| 35 using sandbox::bpf_dsl::Arg; | 38 using sandbox::bpf_dsl::Arg; |
| 39 using sandbox::bpf_dsl::BoolExpr; | |
| 36 using sandbox::bpf_dsl::Error; | 40 using sandbox::bpf_dsl::Error; |
| 37 using sandbox::bpf_dsl::If; | 41 using sandbox::bpf_dsl::If; |
| 38 using sandbox::bpf_dsl::ResultExpr; | 42 using sandbox::bpf_dsl::ResultExpr; |
| 39 | 43 |
| 40 // TODO(mdempsky): Make BoolExpr a standalone class so these operators can | 44 // TODO(mdempsky): Make BoolExpr a standalone class so these operators can |
| 41 // be resolved via argument-dependent lookup. | 45 // be resolved via argument-dependent lookup. |
| 42 using sandbox::bpf_dsl::operator&&; | 46 using sandbox::bpf_dsl::operator&&; |
| 43 using sandbox::bpf_dsl::operator||; | 47 using sandbox::bpf_dsl::operator||; |
| 44 | 48 |
| 45 namespace nacl { | 49 namespace nacl { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 | 92 |
| 89 ResultExpr RestrictClone() { | 93 ResultExpr RestrictClone() { |
| 90 // We allow clone only for new thread creation. | 94 // We allow clone only for new thread creation. |
| 91 const Arg<int> flags(0); | 95 const Arg<int> flags(0); |
| 92 return If(flags == (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | | 96 return If(flags == (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | |
| 93 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | | 97 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | |
| 94 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID), | 98 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID), |
| 95 Allow()).Else(CrashSIGSYSClone()); | 99 Allow()).Else(CrashSIGSYSClone()); |
| 96 } | 100 } |
| 97 | 101 |
| 102 ResultExpr RestrictFutexOperation() { | |
| 103 const int kAllowedFutexFlags = FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME; | |
|
jln (very slow on Chromium)
2014/09/05 23:40:52
Should I remove FUTEX_CLOCK_REALTIME?
Mark Seaborn
2014/09/06 00:36:47
glibc might be using it. I'm not sure.
It would
| |
| 104 const int kOperationMask = ~kAllowedFutexFlags; | |
| 105 const int kAllowedFutexOperations[] = { | |
| 106 FUTEX_WAIT, FUTEX_WAKE, FUTEX_FD, FUTEX_REQUEUE, | |
|
Mark Seaborn
2014/09/06 00:36:47
Don't need FUTEX_FD. The man page says "Because i
jln (very slow on Chromium)
2014/09/06 00:45:37
Done.
| |
| 107 FUTEX_CMP_REQUEUE, FUTEX_WAKE_OP, FUTEX_WAIT_BITSET, FUTEX_WAKE_BITSET}; | |
| 108 | |
| 109 const Arg<int> op(1); | |
| 110 | |
| 111 BoolExpr IsAllowedOp = (op & kOperationMask) == kAllowedFutexOperations[0]; | |
|
Mark Seaborn
2014/09/06 00:36:47
Style nit: shouldn't this be "is_allowed_op"?
jln (very slow on Chromium)
2014/09/06 00:45:37
Done.
| |
| 112 for (size_t i = 1; i < arraysize(kAllowedFutexOperations); ++i) { | |
| 113 IsAllowedOp = | |
| 114 IsAllowedOp || ((op & kOperationMask) == kAllowedFutexOperations[i]); | |
| 115 } | |
| 116 return If(IsAllowedOp, Allow()).Else(CrashSIGSYSFutex()); | |
| 117 } | |
| 118 | |
| 98 ResultExpr RestrictPrctl() { | 119 ResultExpr RestrictPrctl() { |
| 99 // base::PlatformThread::SetName() uses PR_SET_NAME so we return | 120 // base::PlatformThread::SetName() uses PR_SET_NAME so we return |
| 100 // EPERM for it. Otherwise, we will raise SIGSYS. | 121 // EPERM for it. Otherwise, we will raise SIGSYS. |
| 101 const Arg<int> option(0); | 122 const Arg<int> option(0); |
| 102 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); | 123 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); |
| 103 } | 124 } |
| 104 | 125 |
| 105 #if defined(__i386__) | 126 #if defined(__i386__) |
| 106 ResultExpr RestrictSocketcall() { | 127 ResultExpr RestrictSocketcall() { |
| 107 // We only allow socketpair, sendmsg, and recvmsg. | 128 // We only allow socketpair, sendmsg, and recvmsg. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 case __NR_close: | 228 case __NR_close: |
| 208 case __NR_dup: | 229 case __NR_dup: |
| 209 case __NR_dup2: | 230 case __NR_dup2: |
| 210 case __NR_exit: | 231 case __NR_exit: |
| 211 case __NR_exit_group: | 232 case __NR_exit_group: |
| 212 #if defined(__i386__) || defined(__arm__) | 233 #if defined(__i386__) || defined(__arm__) |
| 213 case __NR_fstat64: | 234 case __NR_fstat64: |
| 214 #elif defined(__x86_64__) | 235 #elif defined(__x86_64__) |
| 215 case __NR_fstat: | 236 case __NR_fstat: |
| 216 #endif | 237 #endif |
| 217 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG. | |
| 218 case __NR_futex: | |
| 219 // TODO(hamaji): Remove the need of gettid. Currently, this is | 238 // TODO(hamaji): Remove the need of gettid. Currently, this is |
| 220 // called from PlatformThread::CurrentId(). | 239 // called from PlatformThread::CurrentId(). |
| 221 case __NR_gettid: | 240 case __NR_gettid: |
| 222 case __NR_gettimeofday: | 241 case __NR_gettimeofday: |
| 223 case __NR_munmap: | 242 case __NR_munmap: |
| 224 case __NR_nanosleep: | 243 case __NR_nanosleep: |
| 225 // TODO(hamaji): Remove the need of pipe. Currently, this is | 244 // TODO(hamaji): Remove the need of pipe. Currently, this is |
| 226 // called from base::MessagePumpLibevent::Init(). | 245 // called from base::MessagePumpLibevent::Init(). |
| 227 case __NR_pipe: | 246 case __NR_pipe: |
| 228 case __NR_poll: | 247 case __NR_poll: |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 249 return RestrictClone(); | 268 return RestrictClone(); |
| 250 | 269 |
| 251 #if defined(__x86_64__) | 270 #if defined(__x86_64__) |
| 252 case __NR_fcntl: | 271 case __NR_fcntl: |
| 253 #endif | 272 #endif |
| 254 #if defined(__i386__) || defined(__arm__) | 273 #if defined(__i386__) || defined(__arm__) |
| 255 case __NR_fcntl64: | 274 case __NR_fcntl64: |
| 256 #endif | 275 #endif |
| 257 return RestrictFcntlCommands(); | 276 return RestrictFcntlCommands(); |
| 258 | 277 |
| 278 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG. | |
|
Mark Seaborn
2014/09/06 00:36:47
Move this comment into RestrictFutexOperation()?
jln (very slow on Chromium)
2014/09/06 00:45:37
Done.
| |
| 279 case __NR_futex: | |
| 280 return RestrictFutexOperation(); | |
| 281 | |
| 259 #if defined(__x86_64__) | 282 #if defined(__x86_64__) |
| 260 case __NR_mmap: | 283 case __NR_mmap: |
| 261 #endif | 284 #endif |
| 262 #if defined(__i386__) || defined(__arm__) | 285 #if defined(__i386__) || defined(__arm__) |
| 263 case __NR_mmap2: | 286 case __NR_mmap2: |
| 264 #endif | 287 #endif |
| 265 return RestrictMmap(); | 288 return RestrictMmap(); |
| 266 case __NR_mprotect: | 289 case __NR_mprotect: |
| 267 return RestrictMprotect(); | 290 return RestrictMprotect(); |
| 268 | 291 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 scoped_ptr<sandbox::SandboxBPFPolicy>( | 330 scoped_ptr<sandbox::SandboxBPFPolicy>( |
| 308 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy())); | 331 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy())); |
| 309 if (!sandbox_is_initialized) | 332 if (!sandbox_is_initialized) |
| 310 return false; | 333 return false; |
| 311 RunSandboxSanityChecks(); | 334 RunSandboxSanityChecks(); |
| 312 return true; | 335 return true; |
| 313 } | 336 } |
| 314 | 337 |
| 315 } // namespace nonsfi | 338 } // namespace nonsfi |
| 316 } // namespace nacl | 339 } // namespace nacl |
| OLD | NEW |