| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sandbox/linux/seccomp-bpf/sandbox_bpf.h" | 5 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| 6 | 6 |
| 7 // Some headers on Android are missing cdefs: crbug.com/172337. | 7 // Some headers on Android are missing cdefs: crbug.com/172337. |
| 8 // (We can't use OS_ANDROID here since build_config.h is not included). | 8 // (We can't use OS_ANDROID here since build_config.h is not included). |
| 9 #if defined(ANDROID) | 9 #if defined(ANDROID) |
| 10 #include <sys/cdefs.h> | 10 #include <sys/cdefs.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <errno.h> | 13 #include <errno.h> |
| 14 #include <linux/filter.h> | 14 #include <linux/filter.h> |
| 15 #include <sys/prctl.h> | 15 #include <sys/prctl.h> |
| 16 #include <sys/types.h> | 16 #include <sys/types.h> |
| 17 #include <unistd.h> | 17 #include <unistd.h> |
| 18 | 18 |
| 19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
| 20 #include "base/files/scoped_file.h" | 20 #include "base/files/scoped_file.h" |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/macros.h" | 22 #include "base/macros.h" |
| 23 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
| 24 #include "base/posix/eintr_wrapper.h" | 24 #include "base/posix/eintr_wrapper.h" |
| 25 #include "base/third_party/valgrind/valgrind.h" | 25 #include "base/third_party/valgrind/valgrind.h" |
| 26 #include "sandbox/linux/bpf_dsl/codegen.h" | 26 #include "sandbox/linux/bpf_dsl/codegen.h" |
| 27 #include "sandbox/linux/bpf_dsl/dump_bpf.h" | |
| 28 #include "sandbox/linux/bpf_dsl/policy.h" | 27 #include "sandbox/linux/bpf_dsl/policy.h" |
| 29 #include "sandbox/linux/bpf_dsl/policy_compiler.h" | 28 #include "sandbox/linux/bpf_dsl/policy_compiler.h" |
| 30 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" | 29 #include "sandbox/linux/bpf_dsl/seccomp_macros.h" |
| 31 #include "sandbox/linux/bpf_dsl/syscall_set.h" | 30 #include "sandbox/linux/bpf_dsl/syscall_set.h" |
| 32 #include "sandbox/linux/seccomp-bpf/die.h" | 31 #include "sandbox/linux/seccomp-bpf/die.h" |
| 33 #include "sandbox/linux/seccomp-bpf/errorcode.h" | |
| 34 #include "sandbox/linux/seccomp-bpf/syscall.h" | 32 #include "sandbox/linux/seccomp-bpf/syscall.h" |
| 35 #include "sandbox/linux/seccomp-bpf/trap.h" | 33 #include "sandbox/linux/seccomp-bpf/trap.h" |
| 36 #include "sandbox/linux/seccomp-bpf/verifier.h" | |
| 37 #include "sandbox/linux/services/proc_util.h" | 34 #include "sandbox/linux/services/proc_util.h" |
| 38 #include "sandbox/linux/services/syscall_wrappers.h" | 35 #include "sandbox/linux/services/syscall_wrappers.h" |
| 39 #include "sandbox/linux/services/thread_helpers.h" | 36 #include "sandbox/linux/services/thread_helpers.h" |
| 40 #include "sandbox/linux/system_headers/linux_seccomp.h" | 37 #include "sandbox/linux/system_headers/linux_seccomp.h" |
| 41 #include "sandbox/linux/system_headers/linux_syscalls.h" | 38 #include "sandbox/linux/system_headers/linux_syscalls.h" |
| 42 | 39 |
| 43 namespace sandbox { | 40 namespace sandbox { |
| 44 | 41 |
| 45 namespace { | 42 namespace { |
| 46 | 43 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 static_cast<intptr_t>(args.args[3]), static_cast<intptr_t>(args.args[4]), | 182 static_cast<intptr_t>(args.args[3]), static_cast<intptr_t>(args.args[4]), |
| 186 static_cast<intptr_t>(args.args[5])); | 183 static_cast<intptr_t>(args.args[5])); |
| 187 } | 184 } |
| 188 | 185 |
| 189 scoped_ptr<CodeGen::Program> SandboxBPF::AssembleFilter( | 186 scoped_ptr<CodeGen::Program> SandboxBPF::AssembleFilter( |
| 190 bool force_verification) { | 187 bool force_verification) { |
| 191 #if !defined(NDEBUG) | 188 #if !defined(NDEBUG) |
| 192 force_verification = true; | 189 force_verification = true; |
| 193 #endif | 190 #endif |
| 194 DCHECK(policy_); | 191 DCHECK(policy_); |
| 192 |
| 195 bpf_dsl::PolicyCompiler compiler(policy_.get(), Trap::Registry()); | 193 bpf_dsl::PolicyCompiler compiler(policy_.get(), Trap::Registry()); |
| 196 if (Trap::SandboxDebuggingAllowedByUser()) { | 194 if (Trap::SandboxDebuggingAllowedByUser()) { |
| 197 compiler.DangerousSetEscapePC(EscapePC()); | 195 compiler.DangerousSetEscapePC(EscapePC()); |
| 198 } | 196 } |
| 199 scoped_ptr<CodeGen::Program> program = compiler.Compile(); | 197 return compiler.Compile(force_verification); |
| 200 | |
| 201 // Make sure compilation resulted in a BPF program that executes | |
| 202 // correctly. Otherwise, there is an internal error in our BPF compiler. | |
| 203 // There is really nothing the caller can do until the bug is fixed. | |
| 204 if (force_verification) { | |
| 205 // Verification is expensive. We only perform this step, if we are | |
| 206 // compiled in debug mode, or if the caller explicitly requested | |
| 207 // verification. | |
| 208 | |
| 209 const char* err = NULL; | |
| 210 if (!Verifier::VerifyBPF(&compiler, *program, *policy_, &err)) { | |
| 211 bpf_dsl::DumpBPF::PrintProgram(*program); | |
| 212 SANDBOX_DIE(err); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 return program.Pass(); | |
| 217 } | 198 } |
| 218 | 199 |
| 219 void SandboxBPF::InstallFilter(bool must_sync_threads) { | 200 void SandboxBPF::InstallFilter(bool must_sync_threads) { |
| 220 // We want to be very careful in not imposing any requirements on the | 201 // We want to be very careful in not imposing any requirements on the |
| 221 // policies that are set with SetSandboxPolicy(). This means, as soon as | 202 // policies that are set with SetSandboxPolicy(). This means, as soon as |
| 222 // the sandbox is active, we shouldn't be relying on libraries that could | 203 // the sandbox is active, we shouldn't be relying on libraries that could |
| 223 // be making system calls. This, for example, means we should avoid | 204 // be making system calls. This, for example, means we should avoid |
| 224 // using the heap and we should avoid using STL functions. | 205 // using the heap and we should avoid using STL functions. |
| 225 // Temporarily copy the contents of the "program" vector into a | 206 // Temporarily copy the contents of the "program" vector into a |
| 226 // stack-allocated array; and then explicitly destroy that object. | 207 // stack-allocated array; and then explicitly destroy that object. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 } else { | 239 } else { |
| 259 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { | 240 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) { |
| 260 SANDBOX_DIE("Kernel refuses to turn on BPF filters"); | 241 SANDBOX_DIE("Kernel refuses to turn on BPF filters"); |
| 261 } | 242 } |
| 262 } | 243 } |
| 263 | 244 |
| 264 sandbox_has_started_ = true; | 245 sandbox_has_started_ = true; |
| 265 } | 246 } |
| 266 | 247 |
| 267 } // namespace sandbox | 248 } // namespace sandbox |
| OLD | NEW |