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/seccomp-bpf/sandbox_bpf.h"
27 #include "sandbox/linux/services/linux_syscalls.h"
29 using sandbox::ErrorCode
;
30 using sandbox::SandboxBPF
;
31 using sandbox::SyscallSets
;
37 inline bool IsChromeOS() {
38 #if defined(OS_CHROMEOS)
45 inline bool IsArchitectureArm() {
53 void AddArmMaliGpuWhitelist(std::vector
<std::string
>* read_whitelist
,
54 std::vector
<std::string
>* write_whitelist
) {
55 // Device file needed by the ARM GPU userspace.
56 static const char kMali0Path
[] = "/dev/mali0";
58 // Devices needed for video decode acceleration on ARM.
59 static const char kDevMfcDecPath
[] = "/dev/mfc-dec";
60 static const char kDevGsc1Path
[] = "/dev/gsc1";
62 // Devices needed for video encode acceleration on ARM.
63 static const char kDevMfcEncPath
[] = "/dev/mfc-enc";
65 read_whitelist
->push_back(kMali0Path
);
66 read_whitelist
->push_back(kDevMfcDecPath
);
67 read_whitelist
->push_back(kDevGsc1Path
);
68 read_whitelist
->push_back(kDevMfcEncPath
);
70 write_whitelist
->push_back(kMali0Path
);
71 write_whitelist
->push_back(kDevMfcDecPath
);
72 write_whitelist
->push_back(kDevGsc1Path
);
73 write_whitelist
->push_back(kDevMfcEncPath
);
76 void AddArmGpuWhitelist(std::vector
<std::string
>* read_whitelist
,
77 std::vector
<std::string
>* write_whitelist
) {
78 // On ARM we're enabling the sandbox before the X connection is made,
79 // so we need to allow access to |.Xauthority|.
80 static const char kXAuthorityPath
[] = "/home/chronos/.Xauthority";
81 static const char kLdSoCache
[] = "/etc/ld.so.cache";
83 // Files needed by the ARM GPU userspace.
84 static const char kLibGlesPath
[] = "/usr/lib/libGLESv2.so.2";
85 static const char kLibEglPath
[] = "/usr/lib/libEGL.so.1";
87 read_whitelist
->push_back(kXAuthorityPath
);
88 read_whitelist
->push_back(kLdSoCache
);
89 read_whitelist
->push_back(kLibGlesPath
);
90 read_whitelist
->push_back(kLibEglPath
);
92 AddArmMaliGpuWhitelist(read_whitelist
, write_whitelist
);
95 class CrosArmGpuBrokerProcessPolicy
: public CrosArmGpuProcessPolicy
{
97 static sandbox::SandboxBPFPolicy
* Create() {
98 return new CrosArmGpuBrokerProcessPolicy();
100 virtual ~CrosArmGpuBrokerProcessPolicy() {}
102 virtual ErrorCode
EvaluateSyscall(SandboxBPF
* sandbox_compiler
,
103 int system_call_number
) const OVERRIDE
;
106 CrosArmGpuBrokerProcessPolicy() : CrosArmGpuProcessPolicy(false) {}
107 DISALLOW_COPY_AND_ASSIGN(CrosArmGpuBrokerProcessPolicy
);
110 // A GPU broker policy is the same as a GPU policy with open and
112 ErrorCode
CrosArmGpuBrokerProcessPolicy::EvaluateSyscall(SandboxBPF
* sandbox
,
118 return ErrorCode(ErrorCode::ERR_ALLOWED
);
120 return CrosArmGpuProcessPolicy::EvaluateSyscall(sandbox
, sysno
);
126 CrosArmGpuProcessPolicy::CrosArmGpuProcessPolicy(bool allow_shmat
)
127 : allow_shmat_(allow_shmat
) {}
129 CrosArmGpuProcessPolicy::~CrosArmGpuProcessPolicy() {}
131 ErrorCode
CrosArmGpuProcessPolicy::EvaluateSyscall(SandboxBPF
* sandbox
,
134 if (allow_shmat_
&& sysno
== __NR_shmat
)
135 return ErrorCode(ErrorCode::ERR_ALLOWED
);
136 #endif // defined(__arm__)
140 // ARM GPU sandbox is started earlier so we need to allow networking
143 case __NR_getpeername
:
144 case __NR_getsockname
:
147 return ErrorCode(ErrorCode::ERR_ALLOWED
);
148 // Allow only AF_UNIX for |domain|.
150 case __NR_socketpair
:
151 return sandbox
->Cond(0, ErrorCode::TP_32BIT
,
152 ErrorCode::OP_EQUAL
, AF_UNIX
,
153 ErrorCode(ErrorCode::ERR_ALLOWED
),
155 #endif // defined(__arm__)
157 if (SyscallSets::IsAdvancedScheduler(sysno
))
158 return ErrorCode(ErrorCode::ERR_ALLOWED
);
160 // Default to the generic GPU policy.
161 return GpuProcessPolicy::EvaluateSyscall(sandbox
, sysno
);
165 bool CrosArmGpuProcessPolicy::PreSandboxHook() {
166 DCHECK(IsChromeOS() && IsArchitectureArm());
167 // Create a new broker process.
168 DCHECK(!broker_process());
170 std::vector
<std::string
> read_whitelist_extra
;
171 std::vector
<std::string
> write_whitelist_extra
;
172 // Add ARM-specific files to whitelist in the broker.
174 AddArmGpuWhitelist(&read_whitelist_extra
, &write_whitelist_extra
);
175 InitGpuBrokerProcess(CrosArmGpuBrokerProcessPolicy::Create
,
176 read_whitelist_extra
,
177 write_whitelist_extra
);
179 const int dlopen_flag
= RTLD_NOW
| RTLD_GLOBAL
| RTLD_NODELETE
;
181 // Preload the Mali library.
182 dlopen("/usr/lib/libmali.so", dlopen_flag
);
183 // Preload the Tegra V4L2 (video decode acceleration) library.
184 dlopen("/usr/lib/libtegrav4l2.so", dlopen_flag
);
185 // Resetting errno since platform-specific libraries will fail on other
192 } // namespace content