| 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 |
| 33 #define CASES SANDBOX_BPF_DSL_CASES |
| 34 |
| 31 using sandbox::CrashSIGSYS; | 35 using sandbox::CrashSIGSYS; |
| 32 using sandbox::CrashSIGSYSClone; | 36 using sandbox::CrashSIGSYSClone; |
| 37 using sandbox::CrashSIGSYSFutex; |
| 33 using sandbox::CrashSIGSYSPrctl; | 38 using sandbox::CrashSIGSYSPrctl; |
| 34 using sandbox::bpf_dsl::Allow; | 39 using sandbox::bpf_dsl::Allow; |
| 35 using sandbox::bpf_dsl::Arg; | 40 using sandbox::bpf_dsl::Arg; |
| 41 using sandbox::bpf_dsl::BoolExpr; |
| 36 using sandbox::bpf_dsl::Error; | 42 using sandbox::bpf_dsl::Error; |
| 37 using sandbox::bpf_dsl::If; | 43 using sandbox::bpf_dsl::If; |
| 38 using sandbox::bpf_dsl::ResultExpr; | 44 using sandbox::bpf_dsl::ResultExpr; |
| 39 | 45 |
| 40 // TODO(mdempsky): Make BoolExpr a standalone class so these operators can | 46 // TODO(mdempsky): Make BoolExpr a standalone class so these operators can |
| 41 // be resolved via argument-dependent lookup. | 47 // be resolved via argument-dependent lookup. |
| 42 using sandbox::bpf_dsl::operator&&; | 48 using sandbox::bpf_dsl::operator&&; |
| 43 using sandbox::bpf_dsl::operator||; | 49 using sandbox::bpf_dsl::operator||; |
| 44 | 50 |
| 45 namespace nacl { | 51 namespace nacl { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 94 |
| 89 ResultExpr RestrictClone() { | 95 ResultExpr RestrictClone() { |
| 90 // We allow clone only for new thread creation. | 96 // We allow clone only for new thread creation. |
| 91 const Arg<int> flags(0); | 97 const Arg<int> flags(0); |
| 92 return If(flags == (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | | 98 return If(flags == (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | |
| 93 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | | 99 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | |
| 94 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID), | 100 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID), |
| 95 Allow()).Else(CrashSIGSYSClone()); | 101 Allow()).Else(CrashSIGSYSClone()); |
| 96 } | 102 } |
| 97 | 103 |
| 104 ResultExpr RestrictFutexOperation() { |
| 105 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG futexes. |
| 106 const int kAllowedFutexFlags = FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME; |
| 107 const int kOperationMask = ~kAllowedFutexFlags; |
| 108 const Arg<int> op(1); |
| 109 return Switch(op & kOperationMask) |
| 110 .CASES((FUTEX_WAIT, |
| 111 FUTEX_WAKE, |
| 112 FUTEX_REQUEUE, |
| 113 FUTEX_CMP_REQUEUE, |
| 114 FUTEX_WAKE_OP, |
| 115 FUTEX_WAIT_BITSET, |
| 116 FUTEX_WAKE_BITSET), |
| 117 Allow()) |
| 118 .Default(CrashSIGSYSFutex()); |
| 119 } |
| 120 |
| 98 ResultExpr RestrictPrctl() { | 121 ResultExpr RestrictPrctl() { |
| 99 // base::PlatformThread::SetName() uses PR_SET_NAME so we return | 122 // base::PlatformThread::SetName() uses PR_SET_NAME so we return |
| 100 // EPERM for it. Otherwise, we will raise SIGSYS. | 123 // EPERM for it. Otherwise, we will raise SIGSYS. |
| 101 const Arg<int> option(0); | 124 const Arg<int> option(0); |
| 102 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); | 125 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); |
| 103 } | 126 } |
| 104 | 127 |
| 105 #if defined(__i386__) | 128 #if defined(__i386__) |
| 106 ResultExpr RestrictSocketcall() { | 129 ResultExpr RestrictSocketcall() { |
| 107 // We only allow socketpair, sendmsg, and recvmsg. | 130 // We only allow socketpair, sendmsg, and recvmsg. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 case __NR_close: | 230 case __NR_close: |
| 208 case __NR_dup: | 231 case __NR_dup: |
| 209 case __NR_dup2: | 232 case __NR_dup2: |
| 210 case __NR_exit: | 233 case __NR_exit: |
| 211 case __NR_exit_group: | 234 case __NR_exit_group: |
| 212 #if defined(__i386__) || defined(__arm__) | 235 #if defined(__i386__) || defined(__arm__) |
| 213 case __NR_fstat64: | 236 case __NR_fstat64: |
| 214 #elif defined(__x86_64__) | 237 #elif defined(__x86_64__) |
| 215 case __NR_fstat: | 238 case __NR_fstat: |
| 216 #endif | 239 #endif |
| 217 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG. | |
| 218 case __NR_futex: | |
| 219 // TODO(hamaji): Remove the need of gettid. Currently, this is | 240 // TODO(hamaji): Remove the need of gettid. Currently, this is |
| 220 // called from PlatformThread::CurrentId(). | 241 // called from PlatformThread::CurrentId(). |
| 221 case __NR_gettid: | 242 case __NR_gettid: |
| 222 case __NR_gettimeofday: | 243 case __NR_gettimeofday: |
| 223 case __NR_munmap: | 244 case __NR_munmap: |
| 224 case __NR_nanosleep: | 245 case __NR_nanosleep: |
| 225 // TODO(hamaji): Remove the need of pipe. Currently, this is | 246 // TODO(hamaji): Remove the need of pipe. Currently, this is |
| 226 // called from base::MessagePumpLibevent::Init(). | 247 // called from base::MessagePumpLibevent::Init(). |
| 227 case __NR_pipe: | 248 case __NR_pipe: |
| 228 case __NR_poll: | 249 case __NR_poll: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 249 return RestrictClone(); | 270 return RestrictClone(); |
| 250 | 271 |
| 251 #if defined(__x86_64__) | 272 #if defined(__x86_64__) |
| 252 case __NR_fcntl: | 273 case __NR_fcntl: |
| 253 #endif | 274 #endif |
| 254 #if defined(__i386__) || defined(__arm__) | 275 #if defined(__i386__) || defined(__arm__) |
| 255 case __NR_fcntl64: | 276 case __NR_fcntl64: |
| 256 #endif | 277 #endif |
| 257 return RestrictFcntlCommands(); | 278 return RestrictFcntlCommands(); |
| 258 | 279 |
| 280 case __NR_futex: |
| 281 return RestrictFutexOperation(); |
| 282 |
| 259 #if defined(__x86_64__) | 283 #if defined(__x86_64__) |
| 260 case __NR_mmap: | 284 case __NR_mmap: |
| 261 #endif | 285 #endif |
| 262 #if defined(__i386__) || defined(__arm__) | 286 #if defined(__i386__) || defined(__arm__) |
| 263 case __NR_mmap2: | 287 case __NR_mmap2: |
| 264 #endif | 288 #endif |
| 265 return RestrictMmap(); | 289 return RestrictMmap(); |
| 266 case __NR_mprotect: | 290 case __NR_mprotect: |
| 267 return RestrictMprotect(); | 291 return RestrictMprotect(); |
| 268 | 292 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 scoped_ptr<sandbox::SandboxBPFPolicy>( | 331 scoped_ptr<sandbox::SandboxBPFPolicy>( |
| 308 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy())); | 332 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy())); |
| 309 if (!sandbox_is_initialized) | 333 if (!sandbox_is_initialized) |
| 310 return false; | 334 return false; |
| 311 RunSandboxSanityChecks(); | 335 RunSandboxSanityChecks(); |
| 312 return true; | 336 return true; |
| 313 } | 337 } |
| 314 | 338 |
| 315 } // namespace nonsfi | 339 } // namespace nonsfi |
| 316 } // namespace nacl | 340 } // namespace nacl |
| OLD | NEW |