Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / nacl / loader / sandbox_linux / nacl_bpf_sandbox_linux.cc
1 // Copyright 2013 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/sandbox_linux/nacl_bpf_sandbox_linux.h"
6
7 #include "build/build_config.h"
8
9 #if defined(USE_SECCOMP_BPF)
10
11 #include <errno.h>
12 #include <signal.h>
13 #include <sys/ptrace.h>
14
15 #include "base/basictypes.h"
16 #include "base/callback.h"
17 #include "base/compiler_specific.h"
18 #include "base/logging.h"
19
20 #include "content/public/common/sandbox_init.h"
21 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
22 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_policy.h"
23 #include "sandbox/linux/services/linux_syscalls.h"
24
25 #endif  // defined(USE_SECCOMP_BPF)
26
27 namespace nacl {
28
29 #if defined(USE_SECCOMP_BPF)
30
31 namespace {
32
33 class NaClBPFSandboxPolicy : public sandbox::SandboxBPFPolicy {
34  public:
35   NaClBPFSandboxPolicy()
36       : baseline_policy_(content::GetBPFSandboxBaselinePolicy()) {}
37   virtual ~NaClBPFSandboxPolicy() {}
38
39   virtual sandbox::ErrorCode EvaluateSyscall(
40       sandbox::SandboxBPF* sandbox_compiler,
41       int system_call_number) const OVERRIDE;
42   virtual sandbox::ErrorCode InvalidSyscall(
43       sandbox::SandboxBPF* sandbox_compiler) const OVERRIDE {
44     return baseline_policy_->InvalidSyscall(sandbox_compiler);
45   }
46
47  private:
48   scoped_ptr<sandbox::SandboxBPFPolicy> baseline_policy_;
49   DISALLOW_COPY_AND_ASSIGN(NaClBPFSandboxPolicy);
50 };
51
52 sandbox::ErrorCode NaClBPFSandboxPolicy::EvaluateSyscall(
53     sandbox::SandboxBPF* sb, int sysno) const {
54   DCHECK(baseline_policy_);
55   switch (sysno) {
56     // TODO(jln): NaCl's GDB debug stub uses the following socket system calls,
57     // see if it can be restricted a bit.
58 #if defined(__x86_64__) || defined(__arm__) || defined(__mips__)
59     // transport_common.cc needs this.
60     case __NR_accept:
61     case __NR_setsockopt:
62 #elif defined(__i386__)
63     case __NR_socketcall:
64 #endif
65     // trusted/service_runtime/linux/thread_suspension.c needs sigwait() and is
66     // used by NaCl's GDB debug stub.
67     case __NR_rt_sigtimedwait:
68 #if defined(__i386__) || defined(__mips__)
69     // Needed on i386 to set-up the custom segments.
70     case __NR_modify_ldt:
71 #endif
72     // NaClAddrSpaceBeforeAlloc needs prlimit64.
73     case __NR_prlimit64:
74     // NaCl uses custom signal stacks.
75     case __NR_sigaltstack:
76     // Below is fairly similar to the policy for a Chromium renderer.
77 #if defined(__i386__) || defined(__x86_64__) || defined(__mips__)
78     case __NR_getrlimit:
79 #endif
80 #if defined(__i386__) || defined(__arm__)
81     case __NR_ugetrlimit:
82 #endif
83     // NaCl runtime exposes clock_getres to untrusted code.
84     case __NR_clock_getres:
85     // NaCl runtime uses flock to simulate POSIX behavior for pwrite.
86     case __NR_flock:
87     case __NR_pread64:
88     case __NR_pwrite64:
89     case __NR_sched_get_priority_max:
90     case __NR_sched_get_priority_min:
91     case __NR_sched_getaffinity:
92     case __NR_sched_getparam:
93     case __NR_sched_getscheduler:
94     case __NR_sched_setscheduler:
95     case __NR_setpriority:
96     case __NR_sysinfo:
97     // __NR_times needed as clock() is called by CommandBufferHelper, which is
98     // used by NaCl applications that use Pepper's 3D interfaces.
99     // See crbug.com/264856 for details.
100     case __NR_times:
101     case __NR_uname:
102       return sandbox::ErrorCode(sandbox::ErrorCode::ERR_ALLOWED);
103     case __NR_ioctl:
104     case __NR_ptrace:
105       return sandbox::ErrorCode(EPERM);
106     default:
107       return baseline_policy_->EvaluateSyscall(sb, sysno);
108   }
109   NOTREACHED();
110   // GCC wants this.
111   return sandbox::ErrorCode(EPERM);
112 }
113
114 void RunSandboxSanityChecks() {
115   errno = 0;
116   // Make a ptrace request with an invalid PID.
117   long ptrace_ret = ptrace(PTRACE_PEEKUSER, -1 /* pid */, NULL, NULL);
118   CHECK_EQ(-1, ptrace_ret);
119   // Without the sandbox on, this ptrace call would ESRCH instead.
120   CHECK_EQ(EPERM, errno);
121 }
122
123 }  // namespace
124
125 #else
126
127 #error "Seccomp-bpf disabled on supported architecture!"
128
129 #endif  // defined(USE_SECCOMP_BPF)
130
131 bool InitializeBPFSandbox() {
132 #if defined(USE_SECCOMP_BPF)
133   bool sandbox_is_initialized = content::InitializeSandbox(
134       scoped_ptr<sandbox::SandboxBPFPolicy>(new NaClBPFSandboxPolicy));
135   if (sandbox_is_initialized) {
136     RunSandboxSanityChecks();
137     return true;
138   }
139 #endif  // defined(USE_SECCOMP_BPF)
140   return false;
141 }
142
143 }  // namespace nacl