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 | |
|
Mark Seaborn
2015/10/15 18:10:11
Nit: don't add empty line here
hidehiko
2015/10/19 04:39:17
Done.
| |
| 5 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" | 6 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" |
| 6 | 7 |
| 7 #include <errno.h> | 8 #include <errno.h> |
| 8 #include <fcntl.h> | 9 #include <fcntl.h> |
| 9 #include <linux/net.h> | 10 #include <linux/net.h> |
| 10 #include <sys/mman.h> | 11 #include <sys/mman.h> |
| 11 #include <sys/prctl.h> | 12 #include <sys/prctl.h> |
| 12 #include <sys/socket.h> | 13 #include <sys/socket.h> |
| 13 #include <sys/syscall.h> | 14 #include <sys/syscall.h> |
| 14 #include <sys/time.h> | 15 #include <sys/time.h> |
| 15 | 16 |
| 16 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
| 17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 20 #include "content/public/common/sandbox_init.h" | 21 #include "content/public/common/sandbox_init.h" |
| 21 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" | 22 #include "sandbox/linux/bpf_dsl/bpf_dsl.h" |
| 22 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" | 23 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" |
| 23 #include "sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.h" | 24 #include "sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.h" |
| 24 #include "sandbox/linux/system_headers/linux_futex.h" | 25 #include "sandbox/linux/system_headers/linux_futex.h" |
| 25 #include "sandbox/linux/system_headers/linux_signal.h" | 26 #include "sandbox/linux/system_headers/linux_signal.h" |
| 26 #include "sandbox/linux/system_headers/linux_syscalls.h" | 27 #include "sandbox/linux/system_headers/linux_syscalls.h" |
| 27 | 28 |
| 29 #if !defined(OS_NACL_NONSFI) | |
| 30 #error "nonsfi_sandbox.cc must be built for nacl_helper_nonsfi." | |
| 31 #endif | |
| 32 | |
| 28 // Chrome OS Daisy (ARM) build environment and PNaCl toolchain do not define | 33 // Chrome OS Daisy (ARM) build environment and PNaCl toolchain do not define |
| 29 // MAP_STACK. | 34 // MAP_STACK. |
| 30 #if !defined(MAP_STACK) | 35 #if !defined(MAP_STACK) |
| 31 # if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) | 36 # if defined(ARCH_CPU_X86_FAMILY) || defined(ARCH_CPU_ARM_FAMILY) |
| 32 # define MAP_STACK 0x20000 | 37 # define MAP_STACK 0x20000 |
| 33 # elif defined(ARCH_CPU_MIPS_FAMILY) | 38 # elif defined(ARCH_CPU_MIPS_FAMILY) |
| 34 # define MAP_STACK 0x40000 | 39 # define MAP_STACK 0x40000 |
| 35 # else | 40 # else |
| 36 // Note that, on other architectures, MAP_STACK has different value, | 41 // Note that, on other architectures, MAP_STACK has different value, |
| 37 // though Non-SFI is not supported on such architectures. | 42 // 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 | 76 // the return value of F_GETFL, so we need to allow O_ACCMODE in |
| 72 // addition to O_NONBLOCK. | 77 // addition to O_NONBLOCK. |
| 73 const uint64_t kAllowedMask = O_ACCMODE | O_NONBLOCK; | 78 const uint64_t kAllowedMask = O_ACCMODE | O_NONBLOCK; |
| 74 return If((cmd == F_SETFD && long_arg == FD_CLOEXEC) || cmd == F_GETFL || | 79 return If((cmd == F_SETFD && long_arg == FD_CLOEXEC) || cmd == F_GETFL || |
| 75 (cmd == F_SETFL && (long_arg & ~kAllowedMask) == 0), | 80 (cmd == F_SETFL && (long_arg & ~kAllowedMask) == 0), |
| 76 Allow()).Else(CrashSIGSYS()); | 81 Allow()).Else(CrashSIGSYS()); |
| 77 } | 82 } |
| 78 | 83 |
| 79 ResultExpr RestrictClone() { | 84 ResultExpr RestrictClone() { |
| 80 // We allow clone only for new thread creation. | 85 // We allow clone only for new thread creation. |
| 81 int clone_flags = | 86 const int kCloneFlags = |
| 82 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | | 87 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | |
| 83 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | CLONE_PARENT_SETTID; | 88 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); | 89 const Arg<int> flags(0); |
| 88 return If(flags == clone_flags, Allow()).Else(CrashSIGSYSClone()); | 90 return If(flags == kCloneFlags, Allow()).Else(CrashSIGSYSClone()); |
| 89 } | 91 } |
| 90 | 92 |
| 91 ResultExpr RestrictFutexOperation() { | 93 ResultExpr RestrictFutexOperation() { |
| 92 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG futexes. | 94 // TODO(hamaji): Allow only FUTEX_PRIVATE_FLAG futexes. |
| 93 const uint64_t kAllowedFutexFlags = FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME; | 95 const uint64_t kAllowedFutexFlags = FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME; |
| 94 const Arg<int> op(1); | 96 const Arg<int> op(1); |
| 95 return Switch(op & ~kAllowedFutexFlags) | 97 return Switch(op & ~kAllowedFutexFlags) |
| 96 .CASES((FUTEX_WAIT, | 98 .CASES((FUTEX_WAIT, |
| 97 FUTEX_WAKE, | 99 FUTEX_WAKE, |
| 98 FUTEX_REQUEUE, | 100 FUTEX_REQUEUE, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 109 // EPERM for it. Otherwise, we will raise SIGSYS. | 111 // EPERM for it. Otherwise, we will raise SIGSYS. |
| 110 const Arg<int> option(0); | 112 const Arg<int> option(0); |
| 111 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); | 113 return If(option == PR_SET_NAME, Error(EPERM)).Else(CrashSIGSYSPrctl()); |
| 112 } | 114 } |
| 113 | 115 |
| 114 #if defined(__i386__) | 116 #if defined(__i386__) |
| 115 ResultExpr RestrictSocketcall() { | 117 ResultExpr RestrictSocketcall() { |
| 116 // We only allow socketpair, sendmsg, and recvmsg. | 118 // We only allow socketpair, sendmsg, and recvmsg. |
| 117 const Arg<int> call(0); | 119 const Arg<int> call(0); |
| 118 return If( | 120 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, | 121 call == SYS_SHUTDOWN || call == SYS_SENDMSG || call == SYS_RECVMSG, |
| 127 Allow()).Else(CrashSIGSYS()); | 122 Allow()).Else(CrashSIGSYS()); |
| 128 } | 123 } |
| 129 #endif | 124 #endif |
| 130 | 125 |
| 131 ResultExpr RestrictMprotect() { | 126 ResultExpr RestrictMprotect() { |
| 132 // TODO(jln, keescook, drewry): Limit the use of mprotect by adding | 127 // TODO(jln, keescook, drewry): Limit the use of mprotect by adding |
| 133 // some features to linux kernel. | 128 // some features to linux kernel. |
| 134 const uint64_t kAllowedMask = PROT_READ | PROT_WRITE | PROT_EXEC; | 129 const uint64_t kAllowedMask = PROT_READ | PROT_WRITE | PROT_EXEC; |
| 135 const Arg<int> prot(2); | 130 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. | 148 // Only sending SIGUSR1 to a thread in the same process is allowed. |
| 154 return If(tgid == policy_pid && | 149 return If(tgid == policy_pid && |
| 155 // Arg does not support a greater-than operator, so two separate | 150 // Arg does not support a greater-than operator, so two separate |
| 156 // checks are needed to ensure tid is positive. | 151 // checks are needed to ensure tid is positive. |
| 157 tid != 0 && | 152 tid != 0 && |
| 158 (tid & (1u << 31)) == 0 && // tid is non-negative. | 153 (tid & (1u << 31)) == 0 && // tid is non-negative. |
| 159 signum == LINUX_SIGUSR1, | 154 signum == LINUX_SIGUSR1, |
| 160 Allow()).Else(CrashSIGSYS()); | 155 Allow()).Else(CrashSIGSYS()); |
| 161 } | 156 } |
| 162 | 157 |
| 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) { | 158 bool IsGracefullyDenied(int sysno) { |
| 173 switch (sysno) { | 159 switch (sysno) { |
| 174 // libevent tries this first and then falls back to poll if | 160 // libevent tries this first and then falls back to poll if |
| 175 // epoll_create fails. | 161 // epoll_create fails. |
| 176 case __NR_epoll_create: | 162 case __NR_epoll_create: |
| 177 // third_party/libevent uses them, but we can just return -1 from | 163 // third_party/libevent uses them, but we can just return -1 from |
| 178 // them as it is just checking getuid() != geteuid() and | 164 // them as it is just checking getuid() != geteuid() and |
| 179 // getgid() != getegid() | 165 // getgid() != getegid() |
| 180 #if defined(__i386__) || defined(__arm__) | 166 #if defined(__i386__) || defined(__arm__) |
| 181 case __NR_getegid32: | 167 case __NR_getegid32: |
| 182 case __NR_geteuid32: | 168 case __NR_geteuid32: |
| 183 case __NR_getgid32: | 169 case __NR_getgid32: |
| 184 case __NR_getuid32: | 170 case __NR_getuid32: |
| 185 #endif | 171 #endif |
| 186 case __NR_getegid: | 172 case __NR_getegid: |
| 187 case __NR_geteuid: | 173 case __NR_geteuid: |
| 188 case __NR_getgid: | 174 case __NR_getgid: |
| 189 case __NR_getuid: | 175 case __NR_getuid: |
| 190 // tcmalloc calls madvise in TCMalloc_SystemRelease. | 176 // tcmalloc calls madvise in TCMalloc_SystemRelease. |
| 191 case __NR_madvise: | 177 case __NR_madvise: |
| 192 // EPERM instead of SIGSYS as glibc tries to open files in /proc. | 178 // EPERM instead of SIGSYS as glibc tries to open files in /proc. |
| 193 // openat via opendir via get_nprocs_conf and open via get_nprocs. | 179 // openat via opendir via get_nprocs_conf and open via get_nprocs. |
| 194 // TODO(hamaji): Remove this when we switch to newlib. | 180 // TODO(hamaji): Remove this when we switch to newlib. |
| 195 case __NR_open: | 181 case __NR_open: |
| 196 case __NR_openat: | 182 case __NR_openat: |
| 197 // For RunSandboxSanityChecks(). | 183 // For RunSandboxSanityChecks(). |
| 198 case __NR_ptrace: | 184 case __NR_ptrace: |
| 199 // glibc uses this for its pthread implementation. If we return | 185 // glibc uses this for its pthread implementation. If we return |
| 200 // EPERM for this, glibc will stop using this. | 186 // EPERM for this, glibc will stop using this. |
| 201 // TODO(hamaji): newlib does not use this. Make this SIGTRAP once | 187 // TODO(hamaji): newlib does not use this. Make this SIGTRAP once |
|
Mark Seaborn
2015/10/15 18:10:11
There are a few references to glibc and TODOs in t
hidehiko
2015/10/19 04:39:18
I see. Let me give it a try on another CL.
| |
| 202 // we have switched to newlib. | 188 // we have switched to newlib. |
| 203 case __NR_set_robust_list: | 189 case __NR_set_robust_list: |
| 204 // This is obsolete in ARM EABI, but x86 glibc indirectly calls | 190 // This is obsolete in ARM EABI, but x86 glibc indirectly calls |
| 205 // this in sysconf. | 191 // this in sysconf. |
| 206 #if defined(__i386__) || defined(__x86_64__) | 192 #if defined(__i386__) || defined(__x86_64__) |
| 207 case __NR_time: | 193 case __NR_time: |
| 208 #endif | 194 #endif |
| 209 return true; | 195 return true; |
| 210 | 196 |
| 211 default: | 197 default: |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 | 297 |
| 312 #if defined(__i386__) | 298 #if defined(__i386__) |
| 313 case __NR_socketcall: | 299 case __NR_socketcall: |
| 314 return RestrictSocketcall(); | 300 return RestrictSocketcall(); |
| 315 #endif | 301 #endif |
| 316 #if defined(__x86_64__) || defined(__arm__) | 302 #if defined(__x86_64__) || defined(__arm__) |
| 317 case __NR_recvmsg: | 303 case __NR_recvmsg: |
| 318 case __NR_sendmsg: | 304 case __NR_sendmsg: |
| 319 case __NR_shutdown: | 305 case __NR_shutdown: |
| 320 return Allow(); | 306 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 | 307 #endif |
| 330 | 308 |
| 331 case __NR_tgkill: | 309 case __NR_tgkill: |
| 332 return RestrictTgkill(policy_pid_); | 310 return RestrictTgkill(policy_pid_); |
| 333 | 311 |
| 334 case __NR_brk: | 312 case __NR_brk: |
|
Mark Seaborn
2015/10/15 18:10:11
We should be able to remove brk.
hidehiko
2015/10/19 04:39:18
Ditto.
| |
| 335 // The behavior of brk on Linux is different from other system | 313 // The behavior of brk on Linux is different from other system |
| 336 // calls. It does not return errno but the current break on | 314 // calls. It does not return errno but the current break on |
| 337 // failure. glibc thinks brk failed if the return value of brk | 315 // failure. glibc thinks brk failed if the return value of brk |
| 338 // is less than the requested address (i.e., brk(addr) < addr). | 316 // is less than the requested address (i.e., brk(addr) < addr). |
| 339 // So, glibc thinks brk succeeded if we return -EPERM and we | 317 // So, glibc thinks brk succeeded if we return -EPERM and we |
| 340 // need to return zero instead. | 318 // need to return zero instead. |
| 341 return Error(0); | 319 return Error(0); |
| 342 | 320 |
| 343 default: | 321 default: |
| 344 if (IsGracefullyDenied(sysno)) | 322 if (IsGracefullyDenied(sysno)) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 357 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy()), | 335 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy()), |
| 358 proc_fd.Pass()); | 336 proc_fd.Pass()); |
| 359 if (!sandbox_is_initialized) | 337 if (!sandbox_is_initialized) |
| 360 return false; | 338 return false; |
| 361 RunSandboxSanityChecks(); | 339 RunSandboxSanityChecks(); |
| 362 return true; | 340 return true; |
| 363 } | 341 } |
| 364 | 342 |
| 365 } // namespace nonsfi | 343 } // namespace nonsfi |
| 366 } // namespace nacl | 344 } // namespace nacl |
| OLD | NEW |