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
3 // found in the LICENSE file.
5 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/scoped_file.h"
14 #include "base/logging.h"
15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/posix/eintr_wrapper.h"
18 #include "base/third_party/valgrind/valgrind.h"
19 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
20 #include "sandbox/linux/bpf_dsl/codegen.h"
21 #include "sandbox/linux/bpf_dsl/policy.h"
22 #include "sandbox/linux/bpf_dsl/policy_compiler.h"
23 #include "sandbox/linux/bpf_dsl/seccomp_macros.h"
24 #include "sandbox/linux/bpf_dsl/syscall_set.h"
25 #include "sandbox/linux/seccomp-bpf/die.h"
26 #include "sandbox/linux/seccomp-bpf/syscall.h"
27 #include "sandbox/linux/seccomp-bpf/trap.h"
28 #include "sandbox/linux/services/proc_util.h"
29 #include "sandbox/linux/services/syscall_wrappers.h"
30 #include "sandbox/linux/services/thread_helpers.h"
31 #include "sandbox/linux/system_headers/linux_filter.h"
32 #include "sandbox/linux/system_headers/linux_seccomp.h"
33 #include "sandbox/linux/system_headers/linux_syscalls.h"
39 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND
; }
41 bool IsSingleThreaded(int proc_fd
) {
42 return ThreadHelpers::IsSingleThreaded(proc_fd
);
45 // Check if the kernel supports seccomp-filter (a.k.a. seccomp mode 2) via
47 bool KernelSupportsSeccompBPF() {
49 const int rv
= prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, nullptr);
51 if (rv
== -1 && EFAULT
== errno
) {
57 // LG introduced a buggy syscall, sys_set_media_ext, with the same number as
58 // seccomp. Return true if the current kernel has this buggy syscall.
60 // We want this to work with upcoming versions of seccomp, so we pass bogus
61 // flags that are unlikely to ever be used by the kernel. A normal kernel would
62 // return -EINVAL, but a buggy LG kernel would return 1.
63 bool KernelHasLGBug() {
64 #if defined(OS_ANDROID)
65 // sys_set_media will see this as NULL, which should be a safe (non-crashing)
66 // way to invoke it. A genuine seccomp syscall will see it as
67 // SECCOMP_SET_MODE_STRICT.
68 const unsigned int operation
= 0;
69 // Chosen by fair dice roll. Guaranteed to be random.
70 const unsigned int flags
= 0xf7a46a5c;
71 const int rv
= sys_seccomp(operation
, flags
, nullptr);
72 // A genuine kernel would return -EINVAL (which would set rv to -1 and errno
73 // to EINVAL), or at the very least return some kind of error (which would
74 // set rv to -1). Any other behavior indicates that whatever code received
75 // our syscall was not the real seccomp.
79 #endif // defined(OS_ANDROID)
84 // Check if the kernel supports seccomp-filter via the seccomp system call
85 // and the TSYNC feature to enable seccomp on all threads.
86 bool KernelSupportsSeccompTsync() {
87 if (KernelHasLGBug()) {
93 sys_seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
, nullptr);
95 if (rv
== -1 && errno
== EFAULT
) {
98 // TODO(jln): turn these into DCHECK after 417888 is considered fixed.
100 CHECK(ENOSYS
== errno
|| EINVAL
== errno
);
105 uint64_t EscapePC() {
106 intptr_t rv
= Syscall::Call(-1);
107 if (rv
== -1 && errno
== ENOSYS
) {
110 return static_cast<uint64_t>(static_cast<uintptr_t>(rv
));
113 intptr_t SandboxPanicTrap(const struct arch_seccomp_data
&, void* aux
) {
114 SANDBOX_DIE(static_cast<const char*>(aux
));
117 bpf_dsl::ResultExpr
SandboxPanic(const char* error
) {
118 return bpf_dsl::Trap(SandboxPanicTrap
, error
);
123 SandboxBPF::SandboxBPF(bpf_dsl::Policy
* policy
)
124 : proc_fd_(), sandbox_has_started_(false), policy_(policy
) {
127 SandboxBPF::~SandboxBPF() {
131 bool SandboxBPF::SupportsSeccompSandbox(SeccompLevel level
) {
132 // Never pretend to support seccomp with Valgrind, as it
133 // throws the tool off.
134 if (IsRunningOnValgrind()) {
139 case SeccompLevel::SINGLE_THREADED
:
140 return KernelSupportsSeccompBPF();
141 case SeccompLevel::MULTI_THREADED
:
142 return KernelSupportsSeccompTsync();
148 bool SandboxBPF::StartSandbox(SeccompLevel seccomp_level
) {
150 CHECK(seccomp_level
== SeccompLevel::SINGLE_THREADED
||
151 seccomp_level
== SeccompLevel::MULTI_THREADED
);
153 if (sandbox_has_started_
) {
155 "Cannot repeatedly start sandbox. Create a separate Sandbox "
160 if (!proc_fd_
.is_valid()) {
161 SetProcFd(ProcUtil::OpenProc());
164 const bool supports_tsync
= KernelSupportsSeccompTsync();
166 if (seccomp_level
== SeccompLevel::SINGLE_THREADED
) {
167 // Wait for /proc/self/task/ to update if needed and assert the
168 // process is single threaded.
169 ThreadHelpers::AssertSingleThreaded(proc_fd_
.get());
170 } else if (seccomp_level
== SeccompLevel::MULTI_THREADED
) {
171 if (IsSingleThreaded(proc_fd_
.get())) {
172 SANDBOX_DIE("Cannot start sandbox; "
173 "process may be single-threaded when reported as not");
176 if (!supports_tsync
) {
177 SANDBOX_DIE("Cannot start sandbox; kernel does not support synchronizing "
178 "filters for a threadgroup");
183 // We no longer need access to any files in /proc. We want to do this
184 // before installing the filters, just in case that our policy denies
186 if (proc_fd_
.is_valid()) {
190 // Install the filters.
191 InstallFilter(supports_tsync
||
192 seccomp_level
== SeccompLevel::MULTI_THREADED
);
197 void SandboxBPF::SetProcFd(base::ScopedFD proc_fd
) {
198 proc_fd_
.swap(proc_fd
);
202 bool SandboxBPF::IsValidSyscallNumber(int sysnum
) {
203 return SyscallSet::IsValid(sysnum
);
207 bool SandboxBPF::IsRequiredForUnsafeTrap(int sysno
) {
208 return bpf_dsl::PolicyCompiler::IsRequiredForUnsafeTrap(sysno
);
212 intptr_t SandboxBPF::ForwardSyscall(const struct arch_seccomp_data
& args
) {
213 return Syscall::Call(
214 args
.nr
, static_cast<intptr_t>(args
.args
[0]),
215 static_cast<intptr_t>(args
.args
[1]), static_cast<intptr_t>(args
.args
[2]),
216 static_cast<intptr_t>(args
.args
[3]), static_cast<intptr_t>(args
.args
[4]),
217 static_cast<intptr_t>(args
.args
[5]));
220 CodeGen::Program
SandboxBPF::AssembleFilter() {
223 bpf_dsl::PolicyCompiler
compiler(policy_
.get(), Trap::Registry());
224 if (Trap::SandboxDebuggingAllowedByUser()) {
225 compiler
.DangerousSetEscapePC(EscapePC());
227 compiler
.SetPanicFunc(SandboxPanic
);
228 return compiler
.Compile();
231 void SandboxBPF::InstallFilter(bool must_sync_threads
) {
232 // We want to be very careful in not imposing any requirements on the
233 // policies that are set with SetSandboxPolicy(). This means, as soon as
234 // the sandbox is active, we shouldn't be relying on libraries that could
235 // be making system calls. This, for example, means we should avoid
236 // using the heap and we should avoid using STL functions.
237 // Temporarily copy the contents of the "program" vector into a
238 // stack-allocated array; and then explicitly destroy that object.
239 // This makes sure we don't ex- or implicitly call new/delete after we
240 // installed the BPF filter program in the kernel. Depending on the
241 // system memory allocator that is in effect, these operators can result
242 // in system calls to things like munmap() or brk().
243 CodeGen::Program program
= AssembleFilter();
245 struct sock_filter bpf
[program
.size()];
246 const struct sock_fprog prog
= {static_cast<unsigned short>(program
.size()),
248 memcpy(bpf
, &program
[0], sizeof(bpf
));
249 CodeGen::Program().swap(program
); // vector swap trick
251 // Make an attempt to release memory that is no longer needed here, rather
252 // than in the destructor. Try to avoid as much as possible to presume of
253 // what will be possible to do in the new (sandboxed) execution environment.
256 if (prctl(PR_SET_NO_NEW_PRIVS
, 1, 0, 0, 0)) {
257 SANDBOX_DIE("Kernel refuses to enable no-new-privs");
260 // Install BPF filter program. If the thread state indicates multi-threading
261 // support, then the kernel hass the seccomp system call. Otherwise, fall
262 // back on prctl, which requires the process to be single-threaded.
263 if (must_sync_threads
) {
265 sys_seccomp(SECCOMP_SET_MODE_FILTER
, SECCOMP_FILTER_FLAG_TSYNC
, &prog
);
268 "Kernel refuses to turn on and synchronize threads for BPF filters");
271 if (prctl(PR_SET_SECCOMP
, SECCOMP_MODE_FILTER
, &prog
)) {
272 SANDBOX_DIE("Kernel refuses to turn on BPF filters");
276 sandbox_has_started_
= true;
279 } // namespace sandbox