Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/nacl/loader/nonsfi/nonsfi_sandbox.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 #include <fcntl.h> | |
| 9 #include <linux/net.h> | |
| 10 #include <sys/prctl.h> | |
| 11 #include <sys/ptrace.h> | |
| 12 #include <sys/mman.h> | |
| 13 #include <sys/socket.h> | |
| 14 #include <sys/syscall.h> | |
| 15 | |
| 16 #include "base/basictypes.h" | |
| 17 #include "base/logging.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "content/public/common/sandbox_init.h" | |
| 20 #include "sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.h" | |
| 21 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | |
| 22 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h" | |
| 23 #include "sandbox/linux/seccomp-bpf/trap.h" | |
| 24 #include "sandbox/linux/services/linux_syscalls.h" | |
| 25 | |
| 26 #if defined(__arm__) && !defined(MAP_STACK) | |
| 27 #define MAP_STACK 0x20000 // Daisy build environment has old headers. | |
| 28 #endif | |
| 29 | |
| 30 using sandbox::ErrorCode; | |
| 31 using sandbox::SandboxBPF; | |
| 32 | |
| 33 namespace nacl { | |
| 34 namespace nonsfi { | |
| 35 namespace { | |
| 36 | |
| 37 ErrorCode RestrictFcntlCommandsForNaClNonSfi(SandboxBPF* sb) { | |
| 38 ErrorCode::ArgType mask_long_type; | |
| 39 if (sizeof(long) == 8) | |
| 40 mask_long_type = ErrorCode::TP_64BIT; | |
| 41 else if (sizeof(long) == 4) | |
| 42 mask_long_type = ErrorCode::TP_32BIT; | |
| 43 else | |
| 44 NOTREACHED(); | |
| 45 // We allow following cases: | |
| 46 // 1. F_SETFD + FD_CLOEXEC: libevent's epoll_init uses this. | |
| 47 // 2. F_GETFL: Used by SetNonBlocking in | |
| 48 // message_pump_libevent.cc and Channel::ChannelImpl::CreatePipe | |
| 49 // in ipc_channel_posix.cc. Note that the latter does not work | |
| 50 // with EPERM. | |
| 51 // 3. F_SETFL: Used by evutil_make_socket_nonblocking in | |
| 52 // libevent and SetNonBlocking. As the latter mix O_NONBLOCK to | |
| 53 // the return value of F_GETFL, so we need to allow O_ACCMODE in | |
| 54 // addition to O_NONBLOCK. | |
| 55 unsigned long denied_mask = ~(O_ACCMODE | O_NONBLOCK); | |
|
jln (very slow on Chromium)
2014/04/10 21:00:03
Nit: const
hamaji
2014/04/11 01:25:58
Done.
| |
| 56 return sb->Cond(1, ErrorCode::TP_32BIT, | |
| 57 ErrorCode::OP_EQUAL, F_SETFD, | |
| 58 sb->Cond(2, mask_long_type, | |
| 59 ErrorCode::OP_EQUAL, FD_CLOEXEC, | |
| 60 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 61 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)), | |
| 62 sb->Cond(1, ErrorCode::TP_32BIT, | |
| 63 ErrorCode::OP_EQUAL, F_GETFL, | |
| 64 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 65 sb->Cond(1, ErrorCode::TP_32BIT, | |
| 66 ErrorCode::OP_EQUAL, F_SETFL, | |
| 67 sb->Cond(2, mask_long_type, | |
| 68 ErrorCode::OP_HAS_ANY_BITS, denied_mask, | |
| 69 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL), | |
| 70 ErrorCode(ErrorCode::ERR_ALLOWED)), | |
| 71 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)))); | |
|
jln (very slow on Chromium)
2014/04/10 21:00:03
Nit: shouldn't this be aligned with sb->Cond()?
hamaji
2014/04/11 01:25:58
Done. I'm not sure if I understand the preferred s
| |
| 72 } | |
| 73 | |
| 74 ErrorCode RestrictCloneForNaClNonSfi(SandboxBPF* sb) { | |
| 75 // We allow clone only for new thread creation. | |
| 76 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, | |
| 77 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | | |
| 78 CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS | | |
| 79 CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID, | |
| 80 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 81 sb->Trap(sandbox::SIGSYSCloneFailure, NULL)); | |
| 82 } | |
| 83 | |
| 84 ErrorCode RestrictPrctlForNaClNonSfi(SandboxBPF* sb) { | |
| 85 // base::PlatformThread::SetName() uses PR_SET_NAME so we return | |
| 86 // EPERM for it. Otherwise, we will raise SIGSYS. | |
| 87 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, | |
| 88 PR_SET_NAME, ErrorCode(EPERM), | |
| 89 sb->Trap(sandbox::SIGSYSPrctlFailure, NULL)); | |
| 90 } | |
| 91 | |
| 92 #if defined(__i386__) | |
| 93 ErrorCode RestrictSocketcallForNaClNonSfi(SandboxBPF* sb) { | |
| 94 // We only allow socketpair, sendmsg, and recvmsg. | |
| 95 return sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, | |
| 96 SYS_SOCKETPAIR, | |
| 97 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 98 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, | |
| 99 SYS_SENDMSG, | |
| 100 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 101 sb->Cond(0, ErrorCode::TP_32BIT, ErrorCode::OP_EQUAL, | |
| 102 SYS_RECVMSG, | |
| 103 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 104 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)))); | |
| 105 } | |
| 106 #endif | |
| 107 | |
| 108 ErrorCode RestrictMemoryProtectionForNaClNonSfi(SandboxBPF* sb, int argno) { | |
| 109 // TODO(jln, keescook, drewry): Limit the use of mmap/mprotect by | |
| 110 // adding some features to linux kernel. | |
| 111 uint32_t denied_mask = ~(PROT_READ | PROT_WRITE | PROT_EXEC); | |
|
jln (very slow on Chromium)
2014/04/10 21:00:03
nit const:
hamaji
2014/04/11 01:25:58
Done.
| |
| 112 return sb->Cond(argno, ErrorCode::TP_32BIT, | |
| 113 ErrorCode::OP_HAS_ANY_BITS, | |
| 114 denied_mask, | |
| 115 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL), | |
| 116 ErrorCode(ErrorCode::ERR_ALLOWED)); | |
| 117 } | |
| 118 | |
| 119 ErrorCode RestrictMmapForNaClNonSfi(SandboxBPF* sb) { | |
| 120 uint32_t denied_flag_mask = ~(MAP_SHARED | MAP_PRIVATE | MAP_ANONYMOUS | | |
|
hamaji
2014/04/11 01:25:58
I also added const here.
| |
| 121 MAP_STACK | MAP_FIXED); | |
| 122 // TODO(hamaji): Disallow RWX mmap. | |
| 123 return sb->Cond(3, ErrorCode::TP_32BIT, | |
| 124 ErrorCode::OP_HAS_ANY_BITS, | |
| 125 denied_flag_mask, | |
| 126 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL), | |
| 127 RestrictMemoryProtectionForNaClNonSfi(sb, 2)); | |
| 128 } | |
| 129 | |
| 130 ErrorCode RestrictSocketpairForNaClNonSfi(SandboxBPF* sb) { | |
| 131 // Only allow AF_UNIX, PF_UNIX. Crash if anything else is seen. | |
| 132 COMPILE_ASSERT(AF_UNIX == PF_UNIX, af_unix_pf_unix_different); | |
| 133 return sb->Cond(0, ErrorCode::TP_32BIT, | |
| 134 ErrorCode::OP_EQUAL, AF_UNIX, | |
| 135 ErrorCode(ErrorCode::ERR_ALLOWED), | |
| 136 sb->Trap(sandbox::CrashSIGSYS_Handler, NULL)); | |
| 137 } | |
| 138 | |
| 139 bool IsGracefullyDenied(int sysno) { | |
| 140 switch (sysno) { | |
| 141 // third_party/libevent uses them, but we can just return -1 from | |
| 142 // them as it is just checking getuid() != geteuid() and | |
| 143 // getgid() != getegid() | |
| 144 #if defined(__i386__) || defined(__arm__) | |
| 145 case __NR_getegid32: | |
| 146 case __NR_geteuid32: | |
| 147 case __NR_getgid32: | |
| 148 case __NR_getuid32: | |
| 149 #endif | |
| 150 #if defined(__x86_64__) | |
| 151 case __NR_getegid: | |
| 152 case __NR_geteuid: | |
| 153 case __NR_getgid: | |
| 154 case __NR_getuid: | |
| 155 #endif | |
| 156 // tcmalloc calls madvise in TCMalloc_SystemRelease. | |
| 157 case __NR_madvise: | |
| 158 // EPERM instead of SIGSYS as glibc tries to open files in /proc. | |
| 159 // TODO(hamaji): Remove this when we switch to newlib. | |
| 160 case __NR_open: | |
| 161 // For RunSandboxSanityChecks(). | |
| 162 case __NR_ptrace: | |
| 163 // glibc uses this for its pthread implementation. If we return | |
| 164 // EPERM for this, glibc will stop using this. | |
| 165 // TODO(hamaji): newlib does not use this. Make this SIGTRAP once | |
| 166 // we have switched to newlib. | |
| 167 case __NR_set_robust_list: | |
| 168 // This is obsolete in ARM EABI, but x86 glibc indirectly calls | |
| 169 // this in sysconf. | |
| 170 #if defined(__i386__) || defined(__x86_64__) | |
| 171 case __NR_time: | |
| 172 #endif | |
| 173 return true; | |
| 174 | |
| 175 default: | |
| 176 return false; | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 void RunSandboxSanityChecks() { | |
| 181 errno = 0; | |
| 182 // Make a ptrace request with an invalid PID. | |
| 183 long ptrace_ret = ptrace(PTRACE_PEEKUSER, -1 /* pid */, NULL, NULL); | |
| 184 CHECK_EQ(-1, ptrace_ret); | |
| 185 // Without the sandbox on, this ptrace call would ESRCH instead. | |
| 186 CHECK_EQ(EPERM, errno); | |
| 187 } | |
| 188 | |
| 189 } // namespace | |
| 190 | |
| 191 ErrorCode NaClNonSfiBPFSandboxPolicy::EvaluateSyscall( | |
| 192 SandboxBPF* sb, int sysno) const { | |
| 193 return EvaluateSyscallImpl(sb, sysno, NULL); | |
| 194 } | |
| 195 | |
| 196 ErrorCode NaClNonSfiBPFSandboxPolicy::EvaluateSyscallImpl( | |
| 197 SandboxBPF* sb, int sysno, void*) { | |
| 198 switch (sysno) { | |
| 199 // Allowed syscalls. | |
| 200 #if defined(__i386__) || defined(__arm__) | |
| 201 case __NR__llseek: | |
| 202 #endif | |
| 203 #if defined(__x86_64__) | |
| 204 case __NR_lseek: | |
| 205 #endif | |
| 206 case __NR_clock_gettime: | |
| 207 case __NR_close: | |
| 208 case __NR_dup: | |
| 209 case __NR_dup2: | |
| 210 case __NR_epoll_create: | |
| 211 case __NR_epoll_ctl: | |
| 212 case __NR_epoll_wait: | |
| 213 case __NR_exit: | |
| 214 case __NR_exit_group: | |
| 215 #if defined(__i386__) || defined(__arm__) | |
| 216 case __NR_fstat64: | |
| 217 #endif | |
| 218 #if defined(__x86_64__) | |
| 219 case __NR_fstat: | |
| 220 #endif | |
| 221 case __NR_futex: | |
| 222 case __NR_gettid: | |
| 223 case __NR_gettimeofday: | |
| 224 case __NR_munmap: | |
| 225 case __NR_nanosleep: | |
| 226 case __NR_pipe: | |
| 227 case __NR_pread64: | |
| 228 case __NR_read: | |
| 229 case __NR_restart_syscall: | |
| 230 case __NR_sched_yield: | |
| 231 case __NR_sigaltstack: | |
| 232 case __NR_write: | |
| 233 #if defined(__arm__) | |
| 234 case __ARM_NR_cacheflush: | |
| 235 #endif | |
| 236 // NaCl runtime exposes clock_getres to untrusted code. | |
| 237 case __NR_clock_getres: | |
| 238 // __NR_times needed as clock() is called by CommandBufferHelper, which is | |
| 239 // used by NaCl applications that use Pepper's 3D interfaces. | |
| 240 // See crbug.com/264856 for details. | |
| 241 case __NR_times: | |
| 242 return ErrorCode(ErrorCode::ERR_ALLOWED); | |
| 243 | |
| 244 case __NR_clone: | |
| 245 return RestrictCloneForNaClNonSfi(sb); | |
| 246 | |
| 247 #if defined(__x86_64__) | |
| 248 case __NR_fcntl: | |
| 249 #endif | |
| 250 #if defined(__i386__) || defined(__arm__) | |
| 251 case __NR_fcntl64: | |
| 252 #endif | |
| 253 return RestrictFcntlCommandsForNaClNonSfi(sb); | |
| 254 | |
| 255 #if defined(__x86_64__) | |
| 256 case __NR_mmap: | |
| 257 #endif | |
| 258 #if defined(__i386__) || defined(__arm__) | |
| 259 case __NR_mmap2: | |
| 260 #endif | |
| 261 return RestrictMmapForNaClNonSfi(sb); | |
| 262 case __NR_mprotect: | |
| 263 return RestrictMemoryProtectionForNaClNonSfi(sb, 2); | |
| 264 | |
| 265 case __NR_prctl: | |
| 266 return RestrictPrctlForNaClNonSfi(sb); | |
| 267 | |
| 268 #if defined(__i386__) | |
| 269 case __NR_socketcall: | |
| 270 return RestrictSocketcallForNaClNonSfi(sb); | |
| 271 #endif | |
| 272 #if defined(__x86_64__) || defined(__arm__) | |
| 273 case __NR_recvmsg: | |
| 274 case __NR_sendmsg: | |
| 275 return ErrorCode(ErrorCode::ERR_ALLOWED); | |
| 276 case __NR_socketpair: | |
| 277 return RestrictSocketpairForNaClNonSfi(sb); | |
| 278 #endif | |
| 279 | |
| 280 case __NR_brk: | |
| 281 // The behavior of brk on Linux is different from other system | |
| 282 // calls. It does not return errno but the current break on | |
| 283 // failure. glibc thinks brk failed the return value of brk | |
| 284 // is lesser than the requested address (i.e., brk(addr) < addr) | |
| 285 // So, glibc thinks brk succeeded if we return -EPERM and we | |
| 286 // need to return zero instead. | |
| 287 return ErrorCode(0); | |
| 288 | |
| 289 default: | |
| 290 if (IsGracefullyDenied(sysno)) | |
| 291 return ErrorCode(EPERM); | |
| 292 return sb->Trap(sandbox::CrashSIGSYS_Handler, NULL); | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 void InitializeBPFSandbox() { | |
| 297 bool sandbox_is_initialized = content::InitializeSandbox( | |
| 298 scoped_ptr<sandbox::SandboxBPFPolicy>( | |
| 299 new nacl::nonsfi::NaClNonSfiBPFSandboxPolicy())); | |
| 300 CHECK(sandbox_is_initialized) | |
| 301 << "InitializeBPFSandbox for non-SFI NaCl failed"; | |
| 302 RunSandboxSanityChecks(); | |
| 303 } | |
| 304 | |
| 305 } // namespace nonsfi | |
| 306 } // namespace nacl | |
| OLD | NEW |