1 // Copyright (c) 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.
5 #include "content/common/sandbox_linux/bpf_cros_arm_gpu_policy_linux.h"
10 #include <sys/socket.h>
12 #include <sys/types.h>
18 #include "base/bind.h"
19 #include "base/compiler_specific.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "build/build_config.h"
23 #include "content/common/sandbox_linux/sandbox_bpf_base_policy_linux.h"
24 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h"
25 #include "sandbox/linux/seccomp-bpf-helpers/syscall_sets.h"
26 #include "sandbox/linux/services/linux_syscalls.h"
28 using sandbox::SyscallSets
;
29 using sandbox::bpf_dsl::Allow
;
30 using sandbox::bpf_dsl::Arg
;
31 using sandbox::bpf_dsl::Error
;
32 using sandbox::bpf_dsl::If
;
33 using sandbox::bpf_dsl::ResultExpr
;
39 inline bool IsChromeOS() {
40 #if defined(OS_CHROMEOS)
47 inline bool IsArchitectureArm() {
55 void AddArmMaliGpuWhitelist(std::vector
<std::string
>* read_whitelist
,
56 std::vector
<std::string
>* write_whitelist
) {
57 // Device file needed by the ARM GPU userspace.
58 static const char kMali0Path
[] = "/dev/mali0";
60 // Devices needed for video decode acceleration on ARM.
61 static const char kDevMfcDecPath
[] = "/dev/mfc-dec";
62 static const char kDevGsc1Path
[] = "/dev/gsc1";
64 // Devices needed for video encode acceleration on ARM.
65 static const char kDevMfcEncPath
[] = "/dev/mfc-enc";
67 read_whitelist
->push_back(kMali0Path
);
68 read_whitelist
->push_back(kDevMfcDecPath
);
69 read_whitelist
->push_back(kDevGsc1Path
);
70 read_whitelist
->push_back(kDevMfcEncPath
);
72 write_whitelist
->push_back(kMali0Path
);
73 write_whitelist
->push_back(kDevMfcDecPath
);
74 write_whitelist
->push_back(kDevGsc1Path
);
75 write_whitelist
->push_back(kDevMfcEncPath
);
78 void AddArmGpuWhitelist(std::vector
<std::string
>* read_whitelist
,
79 std::vector
<std::string
>* write_whitelist
) {
80 // On ARM we're enabling the sandbox before the X connection is made,
81 // so we need to allow access to |.Xauthority|.
82 static const char kXAuthorityPath
[] = "/home/chronos/.Xauthority";
83 static const char kLdSoCache
[] = "/etc/ld.so.cache";
85 // Files needed by the ARM GPU userspace.
86 static const char kLibGlesPath
[] = "/usr/lib/libGLESv2.so.2";
87 static const char kLibEglPath
[] = "/usr/lib/libEGL.so.1";
89 read_whitelist
->push_back(kXAuthorityPath
);
90 read_whitelist
->push_back(kLdSoCache
);
91 read_whitelist
->push_back(kLibGlesPath
);
92 read_whitelist
->push_back(kLibEglPath
);
94 AddArmMaliGpuWhitelist(read_whitelist
, write_whitelist
);
97 class CrosArmGpuBrokerProcessPolicy
: public CrosArmGpuProcessPolicy
{
99 static sandbox::bpf_dsl::SandboxBPFDSLPolicy
* Create() {
100 return new CrosArmGpuBrokerProcessPolicy();
102 virtual ~CrosArmGpuBrokerProcessPolicy() {}
104 virtual ResultExpr
EvaluateSyscall(int system_call_number
) const OVERRIDE
;
107 CrosArmGpuBrokerProcessPolicy() : CrosArmGpuProcessPolicy(false) {}
108 DISALLOW_COPY_AND_ASSIGN(CrosArmGpuBrokerProcessPolicy
);
111 // A GPU broker policy is the same as a GPU policy with open and
113 ResultExpr
CrosArmGpuBrokerProcessPolicy::EvaluateSyscall(int sysno
) const {
120 return CrosArmGpuProcessPolicy::EvaluateSyscall(sysno
);
126 CrosArmGpuProcessPolicy::CrosArmGpuProcessPolicy(bool allow_shmat
)
127 : allow_shmat_(allow_shmat
) {}
129 CrosArmGpuProcessPolicy::~CrosArmGpuProcessPolicy() {}
131 ResultExpr
CrosArmGpuProcessPolicy::EvaluateSyscall(int sysno
) const {
133 if (allow_shmat_
&& sysno
== __NR_shmat
)
135 #endif // defined(__arm__)
139 // ARM GPU sandbox is started earlier so we need to allow networking
142 case __NR_getpeername
:
143 case __NR_getsockname
:
147 // Allow only AF_UNIX for |domain|.
149 case __NR_socketpair
: {
150 const Arg
<int> domain(0);
151 return If(domain
== AF_UNIX
, Allow()).Else(Error(EPERM
));
153 #endif // defined(__arm__)
155 if (SyscallSets::IsAdvancedScheduler(sysno
))
158 // Default to the generic GPU policy.
159 return GpuProcessPolicy::EvaluateSyscall(sysno
);
163 bool CrosArmGpuProcessPolicy::PreSandboxHook() {
164 DCHECK(IsChromeOS() && IsArchitectureArm());
165 // Create a new broker process.
166 DCHECK(!broker_process());
168 std::vector
<std::string
> read_whitelist_extra
;
169 std::vector
<std::string
> write_whitelist_extra
;
170 // Add ARM-specific files to whitelist in the broker.
172 AddArmGpuWhitelist(&read_whitelist_extra
, &write_whitelist_extra
);
173 InitGpuBrokerProcess(CrosArmGpuBrokerProcessPolicy::Create
,
174 read_whitelist_extra
,
175 write_whitelist_extra
);
177 const int dlopen_flag
= RTLD_NOW
| RTLD_GLOBAL
| RTLD_NODELETE
;
179 // Preload the Mali library.
180 dlopen("/usr/lib/libmali.so", dlopen_flag
);
181 // Preload the Tegra V4L2 (video decode acceleration) library.
182 dlopen("/usr/lib/libtegrav4l2.so", dlopen_flag
);
183 // Resetting errno since platform-specific libraries will fail on other
190 } // namespace content