| 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/net.h> | 9 #include <linux/net.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| 11 #include <sys/prctl.h> | 11 #include <sys/prctl.h> |
| 12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
| 13 #include <sys/syscall.h> | 13 #include <sys/syscall.h> |
| 14 #include <sys/time.h> | 14 #include <sys/time.h> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "content/public/common/sandbox_init.h" | 20 #include "content/public/common/sandbox_init.h" |
| 21 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" | 21 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" |
| 22 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" | 22 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" |
| 23 #include "sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.h" | 23 #include "sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.h" |
| 24 #include "sandbox/linux/system_headers/linux_futex.h" | 24 #include "sandbox/linux/system_headers/linux_futex.h" |
| 25 #include "sandbox/linux/system_headers/linux_signal.h" | 25 #include "sandbox/linux/system_headers/linux_signal.h" |
| 26 #include "sandbox/linux/system_headers/linux_syscalls.h" | 26 #include "sandbox/linux/system_headers/linux_syscalls.h" |
| 27 | 27 |
| 28 #if !defined(OS_NACL_NONSFI) |
| 29 #error "nonsfi_sandbox.cc must be built for nacl_helper_nonsfi." |
| 30 #endif |
| 31 |
| 28 // Chrome OS Daisy (ARM) build environment and PNaCl toolchain do not define | 32 // Chrome OS Daisy (ARM) build environment and PNaCl toolchain do not define |
| 29 // MAP_STACK. | 33 // MAP_STACK. |
| 30 #if !defined(MAP_STACK) | 34 #if !defined(MAP_STACK) |
| 31 # if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) | 35 # if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) |
| 32 # define MAP_STACK 0x20000 | 36 # define MAP_STACK 0x20000 |
| 33 # elif defined(ARCH_CPU_MIPS_FAMILY) | 37 # elif defined(ARCH_CPU_MIPS_FAMILY) |
| 34 # define MAP_STACK 0x40000 | 38 # define MAP_STACK 0x40000 |
| 35 # else | 39 # else |
| 36 // Note that, on other architectures, MAP_STACK has different value, | 40 // Note that, on other architectures, MAP_STACK has different value, |
| 37 // though Non-SFI is not supported on such architectures. | 41 // though Non-SFI is not supported on such architectures. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // the return value of F_GETFL, so we need to allow O_ACCMODE in | 75 // the return value of F_GETFL, so we need to allow O_ACCMODE in |
| 72 // addition to O_NONBLOCK. | 76 // addition to O_NONBLOCK. |
| 73 const uint64_t kAllowedMask = O_ACCMODE | O_NONBLOCK; | 77 const uint64_t kAllowedMask = O_ACCMODE | O_NONBLOCK; |
| 74 return If((cmd == F_SETFD && long_arg == FD_CLOEXEC) || cmd == F_GETFL || | 78 return If((cmd == F_SETFD && long_arg == FD_CLOEXEC) || cmd == F_GETFL || |
| 75 (cmd == F_SETFL && (long_arg & ~kAllowedMask) == 0), | 79 (cmd == F_SETFL && (long_arg & ~kAllowedMask) == 0), |
| 76 Allow()).Else(CrashSIGSYS()); | 80 Allow()).Else(CrashSIGSYS()); |
| 77 } | 81 } |
| 78 | 82 |
| 79 ResultExpr RestrictClone() { | 83 ResultExpr RestrictClone() { |
| 80 // We allow clone only for new thread creation. | 84 // We allow clone only for new thread creation. |
| 81 int clone_flags = | 85 const int kCloneFlags = |
| 82 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | | 86 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | |
| 83 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID; | 87 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID; |
| 84 #if !defined(OS_NACL_NONSFI) | |
| 85 clone_flags |= CLONE_CHILD_CLEARTID; | |
| 86 #endif | |
| 87 const Arg<int> flags(0); | 88 const Arg<int> flags(0); |
| 88 return If(flags == clone_flags, Allow()).Else(CrashSIGSYSClone()); | 89 return If(flags == kCloneFlags, Allow()).Else(CrashSIGSYSClone()); |
| 89 } | 90 } |
| 90 | 91 |
| 91 ResultExpr RestrictFutexOperation() { | 92 ResultExpr RestrictFutexOperation() { |
| 92 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG futexes. | 93 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG futexes. |
| 93 const uint64_t kAllowedFutexFlags = FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME; | 94 const uint64_t kAllowedFutexFlags = FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME; |
| 94 const Arg<int> op(1); | 95 const Arg<int> op(1); |
| 95 return Switch(op & ~kAllowedFutexFlags) | 96 return Switch(op & ~kAllowedFutexFlags) |
| 96 .CASES((FUTEX_WAIT, | 97 .CASES((FUTEX_WAIT, |
| 97 FUTEX_WAKE, | 98 FUTEX_WAKE, |
| 98 FUTEX_REQUEUE, | 99 FUTEX_REQUEUE, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 109 // EPERM for it. Otherwise, we will raise SIGSYS. | 110 // EPERM for it. Otherwise, we will raise SIGSYS. |
| 110 const Arg<int> option(0); | 111 const Arg<int> option(0); |
| 111 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); | 112 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); |
| 112 } | 113 } |
| 113 | 114 |
| 114 #if defined(__i386__) | 115 #if defined(__i386__) |
| 115 ResultExpr RestrictSocketcall() { | 116 ResultExpr RestrictSocketcall() { |
| 116 // We only allow socketpair, sendmsg, and recvmsg. | 117 // We only allow socketpair, sendmsg, and recvmsg. |
| 117 const Arg<int> call(0); | 118 const Arg<int> call(0); |
| 118 return If( | 119 return If( |
| 119 #if !defined(OS_NACL_NONSFI) | |
| 120 // nacl_helper in Non-SFI mode still uses socketpair() internally | |
| 121 // via libevent. | |
| 122 // TODO(hidehiko): Remove this when the switching to nacl_helper_nonsfi | |
| 123 // is completed. | |
| 124 call == SYS_SOCKETPAIR || | |
| 125 #endif | |
| 126 call == SYS_SHUTDOWN || call == SYS_SENDMSG || call == SYS_RECVMSG, | 120 call == SYS_SHUTDOWN || call == SYS_SENDMSG || call == SYS_RECVMSG, |
| 127 Allow()).Else(CrashSIGSYS()); | 121 Allow()).Else(CrashSIGSYS()); |
| 128 } | 122 } |
| 129 #endif | 123 #endif |
| 130 | 124 |
| 131 ResultExpr RestrictMprotect() { | 125 ResultExpr RestrictMprotect() { |
| 132 // TODO(jln, keescook, drewry): Limit the use of mprotect by adding | 126 // TODO(jln, keescook, drewry): Limit the use of mprotect by adding |
| 133 // some features to linux kernel. | 127 // some features to linux kernel. |
| 134 const uint64_t kAllowedMask = PROT_READ | PROT_WRITE | PROT_EXEC; | 128 const uint64_t kAllowedMask = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 135 const Arg<int> prot(2); | 129 const Arg<int> prot(2); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 153 // Only sending SIGUSR1 to a thread in the same process is allowed. | 147 // Only sending SIGUSR1 to a thread in the same process is allowed. |
| 154 return If(tgid == policy_pid && | 148 return If(tgid == policy_pid && |
| 155 // Arg does not support a greater-than operator, so two separate | 149 // Arg does not support a greater-than operator, so two separate |
| 156 // checks are needed to ensure tid is positive. | 150 // checks are needed to ensure tid is positive. |
| 157 tid != 0 && | 151 tid != 0 && |
| 158 (tid & (1u << 31)) == 0 && // tid is non-negative. | 152 (tid & (1u << 31)) == 0 && // tid is non-negative. |
| 159 signum == LINUX_SIGUSR1, | 153 signum == LINUX_SIGUSR1, |
| 160 Allow()).Else(CrashSIGSYS()); | 154 Allow()).Else(CrashSIGSYS()); |
| 161 } | 155 } |
| 162 | 156 |
| 163 #if !defined(OS_NACL_NONSFI) && (defined(__x86_64__) || defined(__arm__)) | |
| 164 ResultExpr RestrictSocketpair() { | |
| 165 // Only allow AF_UNIX, PF_UNIX. Crash if anything else is seen. | |
| 166 static_assert(AF_UNIX == PF_UNIX, "AF_UNIX must equal PF_UNIX."); | |
| 167 const Arg<int> domain(0); | |
| 168 return If(domain == AF_UNIX, Allow()).Else(CrashSIGSYS()); | |
| 169 } | |
| 170 #endif | |
| 171 | |
| 172 bool IsGracefullyDenied(int sysno) { | 157 bool IsGracefullyDenied(int sysno) { |
| 173 switch (sysno) { | 158 switch (sysno) { |
| 174 // libevent tries this first and then falls back to poll if | 159 // libevent tries this first and then falls back to poll if |
| 175 // epoll_create fails. | 160 // epoll_create fails. |
| 176 case __NR_epoll_create: | 161 case __NR_epoll_create: |
| 177 // third_party/libevent uses them, but we can just return -1 from | 162 // third_party/libevent uses them, but we can just return -1 from |
| 178 // them as it is just checking getuid() != geteuid() and | 163 // them as it is just checking getuid() != geteuid() and |
| 179 // getgid() != getegid() | 164 // getgid() != getegid() |
| 180 #if defined(__i386__) || defined(__arm__) | 165 #if defined(__i386__) || defined(__arm__) |
| 181 case __NR_getegid32: | 166 case __NR_getegid32: |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 296 |
| 312 #if defined(__i386__) | 297 #if defined(__i386__) |
| 313 case __NR_socketcall: | 298 case __NR_socketcall: |
| 314 return RestrictSocketcall(); | 299 return RestrictSocketcall(); |
| 315 #endif | 300 #endif |
| 316 #if defined(__x86_64__) || defined(__arm__) | 301 #if defined(__x86_64__) || defined(__arm__) |
| 317 case __NR_recvmsg: | 302 case __NR_recvmsg: |
| 318 case __NR_sendmsg: | 303 case __NR_sendmsg: |
| 319 case __NR_shutdown: | 304 case __NR_shutdown: |
| 320 return Allow(); | 305 return Allow(); |
| 321 #if !defined(OS_NACL_NONSFI) | |
| 322 // nacl_helper in Non-SFI mode still uses socketpair() internally | |
| 323 // via libevent. | |
| 324 // TODO(hidehiko): Remove this when the switching to nacl_helper_nonsfi | |
| 325 // is completed. | |
| 326 case __NR_socketpair: | |
| 327 return RestrictSocketpair(); | |
| 328 #endif | |
| 329 #endif | 306 #endif |
| 330 | 307 |
| 331 case __NR_tgkill: | 308 case __NR_tgkill: |
| 332 return RestrictTgkill(policy_pid_); | 309 return RestrictTgkill(policy_pid_); |
| 333 | 310 |
| 334 case __NR_brk: | 311 case __NR_brk: |
| 335 // The behavior of brk on Linux is different from other system | 312 // The behavior of brk on Linux is different from other system |
| 336 // calls. It does not return errno but the current break on | 313 // calls. It does not return errno but the current break on |
| 337 // failure. glibc thinks brk failed if the return value of brk | 314 // failure. glibc thinks brk failed if the return value of brk |
| 338 // is less than the requested address (i.e., brk(addr) < addr). | 315 // is less than the requested address (i.e., brk(addr) < addr). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 357 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy()), | 334 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy()), |
| 358 proc_fd.Pass()); | 335 proc_fd.Pass()); |
| 359 if (!sandbox_is_initialized) | 336 if (!sandbox_is_initialized) |
| 360 return false; | 337 return false; |
| 361 RunSandboxSanityChecks(); | 338 RunSandboxSanityChecks(); |
| 362 return true; | 339 return true; |
| 363 } | 340 } |
| 364 | 341 |
| 365 } // namespace nonsfi | 342 } // namespace nonsfi |
| 366 } // namespace nacl | 343 } // namespace nacl |
| OLD | NEW |