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_gpu_policy_linux.h"
10 #include <sys/socket.h>
12 #include <sys/types.h>
18 #include "base/bind.h"
19 #include "base/command_line.h"
20 #include "base/compiler_specific.h"
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "build/build_config.h"
24 #include "content/common/sandbox_linux/sandbox_bpf_base_policy_linux.h"
25 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h"
26 #include "content/common/set_process_title.h"
27 #include "content/public/common/content_switches.h"
28 #include "sandbox/linux/bpf_dsl/bpf_dsl.h"
29 #include "sandbox/linux/seccomp-bpf-helpers/syscall_parameters_restrictions.h"
30 #include "sandbox/linux/seccomp-bpf-helpers/syscall_sets.h"
31 #include "sandbox/linux/syscall_broker/broker_file_permission.h"
32 #include "sandbox/linux/syscall_broker/broker_process.h"
33 #include "sandbox/linux/system_headers/linux_syscalls.h"
35 using sandbox::arch_seccomp_data
;
36 using sandbox::bpf_dsl::Allow
;
37 using sandbox::bpf_dsl::ResultExpr
;
38 using sandbox::bpf_dsl::Trap
;
39 using sandbox::syscall_broker::BrokerFilePermission
;
40 using sandbox::syscall_broker::BrokerProcess
;
41 using sandbox::SyscallSets
;
47 inline bool IsChromeOS() {
48 #if defined(OS_CHROMEOS)
55 inline bool IsArchitectureX86_64() {
56 #if defined(__x86_64__)
63 inline bool IsArchitectureI386() {
71 inline bool IsArchitectureArm() {
72 #if defined(__arm__) || defined(__aarch64__)
79 inline bool IsOzone() {
80 #if defined(USE_OZONE)
87 inline bool UseLibV4L2() {
88 #if defined(USE_LIBV4L2)
95 bool IsAcceleratedVaapiVideoEncodeEnabled() {
96 bool accelerated_encode_enabled
= false;
97 #if defined(OS_CHROMEOS)
98 const base::CommandLine
& command_line
=
99 *base::CommandLine::ForCurrentProcess();
100 accelerated_encode_enabled
=
101 !command_line
.HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode
);
103 return accelerated_encode_enabled
;
106 bool IsAcceleratedVideoDecodeEnabled() {
107 const base::CommandLine
& command_line
=
108 *base::CommandLine::ForCurrentProcess();
109 return !command_line
.HasSwitch(switches::kDisableAcceleratedVideoDecode
);
112 intptr_t GpuSIGSYS_Handler(const struct arch_seccomp_data
& args
,
113 void* aux_broker_process
) {
114 RAW_CHECK(aux_broker_process
);
115 BrokerProcess
* broker_process
=
116 static_cast<BrokerProcess
*>(aux_broker_process
);
118 #if !defined(__aarch64__)
120 return broker_process
->Access(reinterpret_cast<const char*>(args
.args
[0]),
121 static_cast<int>(args
.args
[1]));
123 #if defined(MEMORY_SANITIZER)
124 // http://crbug.com/372840
125 __msan_unpoison_string(reinterpret_cast<const char*>(args
.args
[0]));
127 return broker_process
->Open(reinterpret_cast<const char*>(args
.args
[0]),
128 static_cast<int>(args
.args
[1]));
129 #endif // !defined(__aarch64__)
131 if (static_cast<int>(args
.args
[0]) == AT_FDCWD
) {
133 broker_process
->Access(reinterpret_cast<const char*>(args
.args
[1]),
134 static_cast<int>(args
.args
[2]));
139 // Allow using openat() as open().
140 if (static_cast<int>(args
.args
[0]) == AT_FDCWD
) {
142 broker_process
->Open(reinterpret_cast<const char*>(args
.args
[1]),
143 static_cast<int>(args
.args
[2]));
153 void AddV4L2GpuWhitelist(std::vector
<BrokerFilePermission
>* permissions
) {
154 if (IsAcceleratedVideoDecodeEnabled()) {
155 // Device node for V4L2 video decode accelerator drivers.
156 static const char kDevVideoDecPath
[] = "/dev/video-dec";
157 permissions
->push_back(BrokerFilePermission::ReadWrite(kDevVideoDecPath
));
160 // Device node for V4L2 video encode accelerator drivers.
161 static const char kDevVideoEncPath
[] = "/dev/video-enc";
162 permissions
->push_back(BrokerFilePermission::ReadWrite(kDevVideoEncPath
));
165 class GpuBrokerProcessPolicy
: public GpuProcessPolicy
{
167 static sandbox::bpf_dsl::Policy
* Create() {
168 return new GpuBrokerProcessPolicy();
170 ~GpuBrokerProcessPolicy() override
{}
172 ResultExpr
EvaluateSyscall(int system_call_number
) const override
;
175 GpuBrokerProcessPolicy() {}
176 DISALLOW_COPY_AND_ASSIGN(GpuBrokerProcessPolicy
);
179 // x86_64/i386 or desktop ARM.
180 // A GPU broker policy is the same as a GPU policy with access, open,
181 // openat and in the non-Chrome OS case unlink allowed.
182 ResultExpr
GpuBrokerProcessPolicy::EvaluateSyscall(int sysno
) const {
184 #if !defined(__aarch64__)
187 #endif // !defined(__aarch64__)
190 #if !defined(OS_CHROMEOS)
191 // The broker process needs to able to unlink the temporary
192 // files that it may create. This is used by DRI3.
197 return GpuProcessPolicy::EvaluateSyscall(sysno
);
201 void UpdateProcessTypeToGpuBroker() {
202 base::CommandLine::StringVector exec
=
203 base::CommandLine::ForCurrentProcess()->GetArgs();
204 base::CommandLine::Reset();
205 base::CommandLine::Init(0, NULL
);
206 base::CommandLine::ForCurrentProcess()->InitFromArgv(exec
);
207 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
208 switches::kProcessType
, "gpu-broker");
210 // Update the process title. The argv was already cached by the call to
211 // SetProcessTitleFromCommandLine in content_main_runner.cc, so we can pass
212 // NULL here (we don't have the original argv at this point).
213 SetProcessTitleFromCommandLine(NULL
);
216 bool UpdateProcessTypeAndEnableSandbox(
217 sandbox::bpf_dsl::Policy
* (*broker_sandboxer_allocator
)(void)) {
218 DCHECK(broker_sandboxer_allocator
);
219 UpdateProcessTypeToGpuBroker();
220 return SandboxSeccompBPF::StartSandboxWithExternalPolicy(
221 make_scoped_ptr(broker_sandboxer_allocator()), base::ScopedFD());
226 GpuProcessPolicy::GpuProcessPolicy() : GpuProcessPolicy(false) {
229 GpuProcessPolicy::GpuProcessPolicy(bool allow_mincore
)
230 : broker_process_(NULL
), allow_mincore_(allow_mincore
) {
233 GpuProcessPolicy::~GpuProcessPolicy() {}
235 // Main policy for x86_64/i386. Extended by CrosArmGpuProcessPolicy.
236 ResultExpr
GpuProcessPolicy::EvaluateSyscall(int sysno
) const {
238 #if !defined(OS_CHROMEOS)
244 if (allow_mincore_
) {
247 return SandboxBPFBasePolicy::EvaluateSyscall(sysno
);
249 #if defined(__i386__) || defined(__x86_64__) || defined(__mips__)
250 // The Nvidia driver uses flags not in the baseline policy
251 // (MAP_LOCKED | MAP_EXECUTABLE | MAP_32BIT)
254 // We also hit this on the linux_chromeos bot but don't yet know what
255 // weird flags were involved.
257 // TODO(jln): restrict prctl.
260 #if !defined(__aarch64__)
263 #endif // !defined(__aarch64__)
266 DCHECK(broker_process_
);
267 return Trap(GpuSIGSYS_Handler
, broker_process_
);
268 case __NR_sched_getaffinity
:
269 case __NR_sched_setaffinity
:
270 return sandbox::RestrictSchedTarget(GetPolicyPid(), sysno
);
272 if (SyscallSets::IsEventFd(sysno
))
275 // Default on the baseline policy.
276 return SandboxBPFBasePolicy::EvaluateSyscall(sysno
);
280 bool GpuProcessPolicy::PreSandboxHook() {
281 // Warm up resources needed by the policy we're about to enable and
282 // eventually start a broker process.
283 const bool chromeos_arm_gpu
= IsChromeOS() && IsArchitectureArm();
284 // This policy is for x86 or Desktop.
285 DCHECK(!chromeos_arm_gpu
);
287 DCHECK(!broker_process());
288 // Create a new broker process.
289 InitGpuBrokerProcess(
290 GpuBrokerProcessPolicy::Create
,
291 std::vector
<BrokerFilePermission
>()); // No extra files in whitelist.
293 if (IsArchitectureX86_64() || IsArchitectureI386()) {
294 // Accelerated video dlopen()'s some shared objects
295 // inside the sandbox, so preload them now.
296 if (IsAcceleratedVaapiVideoEncodeEnabled() ||
297 IsAcceleratedVideoDecodeEnabled()) {
298 const char* I965DrvVideoPath
= NULL
;
300 if (IsArchitectureX86_64()) {
301 I965DrvVideoPath
= "/usr/lib64/va/drivers/i965_drv_video.so";
302 } else if (IsArchitectureI386()) {
303 I965DrvVideoPath
= "/usr/lib/va/drivers/i965_drv_video.so";
306 dlopen(I965DrvVideoPath
, RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
307 dlopen("libva.so.1", RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
308 #if defined(USE_OZONE)
309 dlopen("libva-drm.so.1", RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
310 #elif defined(USE_X11)
311 dlopen("libva-x11.so.1", RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
319 void GpuProcessPolicy::InitGpuBrokerProcess(
320 sandbox::bpf_dsl::Policy
* (*broker_sandboxer_allocator
)(void),
321 const std::vector
<BrokerFilePermission
>& permissions_extra
) {
322 static const char kDriRcPath
[] = "/etc/drirc";
323 static const char kDriCard0Path
[] = "/dev/dri/card0";
324 static const char kDevShm
[] = "/dev/shm/";
326 CHECK(broker_process_
== NULL
);
328 // All GPU process policies need these files brokered out.
329 std::vector
<BrokerFilePermission
> permissions
;
330 permissions
.push_back(BrokerFilePermission::ReadWrite(kDriCard0Path
));
331 permissions
.push_back(BrokerFilePermission::ReadOnly(kDriRcPath
));
333 permissions
.push_back(
334 BrokerFilePermission::ReadWriteCreateUnlinkRecursive(kDevShm
));
335 } else if (IsArchitectureArm() || IsOzone()){
336 AddV4L2GpuWhitelist(&permissions
);
338 dlopen("/usr/lib/libv4l2.so", RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
339 // This is a device-specific encoder plugin.
340 dlopen("/usr/lib/libv4l/plugins/libv4l-encplugin.so",
341 RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
345 // Add eventual extra files from permissions_extra.
346 for (const auto& perm
: permissions_extra
) {
347 permissions
.push_back(perm
);
350 broker_process_
= new BrokerProcess(GetFSDeniedErrno(), permissions
);
351 // The initialization callback will perform generic initialization and then
352 // call broker_sandboxer_callback.
353 CHECK(broker_process_
->Init(base::Bind(&UpdateProcessTypeAndEnableSandbox
,
354 broker_sandboxer_allocator
)));
357 } // namespace content