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