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/services/linux_syscalls.h"
32 #include "sandbox/linux/syscall_broker/broker_file_permission.h"
33 #include "sandbox/linux/syscall_broker/broker_process.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() {
79 bool IsAcceleratedVideoEnabled() {
80 const base::CommandLine
& command_line
=
81 *base::CommandLine::ForCurrentProcess();
82 bool accelerated_encode_enabled
= false;
83 #if defined(OS_CHROMEOS)
84 accelerated_encode_enabled
=
85 !command_line
.HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode
);
87 return !command_line
.HasSwitch(switches::kDisableAcceleratedVideoDecode
) ||
88 accelerated_encode_enabled
;
91 intptr_t GpuSIGSYS_Handler(const struct arch_seccomp_data
& args
,
92 void* aux_broker_process
) {
93 RAW_CHECK(aux_broker_process
);
94 BrokerProcess
* broker_process
=
95 static_cast<BrokerProcess
*>(aux_broker_process
);
98 return broker_process
->Access(reinterpret_cast<const char*>(args
.args
[0]),
99 static_cast<int>(args
.args
[1]));
101 #if defined(MEMORY_SANITIZER)
102 // http://crbug.com/372840
103 __msan_unpoison_string(reinterpret_cast<const char*>(args
.args
[0]));
105 return broker_process
->Open(reinterpret_cast<const char*>(args
.args
[0]),
106 static_cast<int>(args
.args
[1]));
108 // Allow using openat() as open().
109 if (static_cast<int>(args
.args
[0]) == AT_FDCWD
) {
111 broker_process
->Open(reinterpret_cast<const char*>(args
.args
[1]),
112 static_cast<int>(args
.args
[2]));
122 class GpuBrokerProcessPolicy
: public GpuProcessPolicy
{
124 static sandbox::bpf_dsl::Policy
* Create() {
125 return new GpuBrokerProcessPolicy();
127 ~GpuBrokerProcessPolicy() override
{}
129 ResultExpr
EvaluateSyscall(int system_call_number
) const override
;
132 GpuBrokerProcessPolicy() {}
133 DISALLOW_COPY_AND_ASSIGN(GpuBrokerProcessPolicy
);
136 // x86_64/i386 or desktop ARM.
137 // A GPU broker policy is the same as a GPU policy with access, open,
138 // openat and in the non-Chrome OS case unlink allowed.
139 ResultExpr
GpuBrokerProcessPolicy::EvaluateSyscall(int sysno
) const {
144 #if !defined(OS_CHROMEOS)
145 // The broker process needs to able to unlink the temporary
146 // files that it may create. This is used by DRI3.
151 return GpuProcessPolicy::EvaluateSyscall(sysno
);
155 void UpdateProcessTypeToGpuBroker() {
156 base::CommandLine::StringVector exec
=
157 base::CommandLine::ForCurrentProcess()->GetArgs();
158 base::CommandLine::Reset();
159 base::CommandLine::Init(0, NULL
);
160 base::CommandLine::ForCurrentProcess()->InitFromArgv(exec
);
161 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
162 switches::kProcessType
, "gpu-broker");
164 // Update the process title. The argv was already cached by the call to
165 // SetProcessTitleFromCommandLine in content_main_runner.cc, so we can pass
166 // NULL here (we don't have the original argv at this point).
167 SetProcessTitleFromCommandLine(NULL
);
170 bool UpdateProcessTypeAndEnableSandbox(
171 sandbox::bpf_dsl::Policy
* (*broker_sandboxer_allocator
)(void)) {
172 DCHECK(broker_sandboxer_allocator
);
173 UpdateProcessTypeToGpuBroker();
174 return SandboxSeccompBPF::StartSandboxWithExternalPolicy(
175 make_scoped_ptr(broker_sandboxer_allocator()), base::ScopedFD());
180 GpuProcessPolicy::GpuProcessPolicy() : GpuProcessPolicy(false) {
183 GpuProcessPolicy::GpuProcessPolicy(bool allow_mincore
)
184 : broker_process_(NULL
), allow_mincore_(allow_mincore
) {
187 GpuProcessPolicy::~GpuProcessPolicy() {}
189 // Main policy for x86_64/i386. Extended by CrosArmGpuProcessPolicy.
190 ResultExpr
GpuProcessPolicy::EvaluateSyscall(int sysno
) const {
192 #if !defined(OS_CHROMEOS)
198 if (allow_mincore_
) {
201 return SandboxBPFBasePolicy::EvaluateSyscall(sysno
);
203 #if defined(__i386__) || defined(__x86_64__) || defined(__mips__)
204 // The Nvidia driver uses flags not in the baseline policy
205 // (MAP_LOCKED | MAP_EXECUTABLE | MAP_32BIT)
208 // We also hit this on the linux_chromeos bot but don't yet know what
209 // weird flags were involved.
211 // TODO(jln): restrict prctl.
217 DCHECK(broker_process_
);
218 return Trap(GpuSIGSYS_Handler
, broker_process_
);
219 case __NR_setpriority
:
220 return sandbox::RestrictGetSetpriority(GetPolicyPid());
221 case __NR_sched_getaffinity
:
222 case __NR_sched_setaffinity
:
223 return sandbox::RestrictSchedTarget(GetPolicyPid(), sysno
);
225 if (SyscallSets::IsEventFd(sysno
))
228 // Default on the baseline policy.
229 return SandboxBPFBasePolicy::EvaluateSyscall(sysno
);
233 bool GpuProcessPolicy::PreSandboxHook() {
234 // Warm up resources needed by the policy we're about to enable and
235 // eventually start a broker process.
236 const bool chromeos_arm_gpu
= IsChromeOS() && IsArchitectureArm();
237 // This policy is for x86 or Desktop.
238 DCHECK(!chromeos_arm_gpu
);
240 DCHECK(!broker_process());
241 // Create a new broker process.
242 InitGpuBrokerProcess(
243 GpuBrokerProcessPolicy::Create
,
244 std::vector
<BrokerFilePermission
>()); // No extra files in whitelist.
246 if (IsArchitectureX86_64() || IsArchitectureI386()) {
247 // Accelerated video dlopen()'s some shared objects
248 // inside the sandbox, so preload them now.
249 if (IsAcceleratedVideoEnabled()) {
250 const char* I965DrvVideoPath
= NULL
;
252 if (IsArchitectureX86_64()) {
253 I965DrvVideoPath
= "/usr/lib64/va/drivers/i965_drv_video.so";
254 } else if (IsArchitectureI386()) {
255 I965DrvVideoPath
= "/usr/lib/va/drivers/i965_drv_video.so";
258 dlopen(I965DrvVideoPath
, RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
259 dlopen("libva.so.1", RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
260 dlopen("libva-x11.so.1", RTLD_NOW
|RTLD_GLOBAL
|RTLD_NODELETE
);
267 void GpuProcessPolicy::InitGpuBrokerProcess(
268 sandbox::bpf_dsl::Policy
* (*broker_sandboxer_allocator
)(void),
269 const std::vector
<BrokerFilePermission
>& permissions_extra
) {
270 static const char kDriRcPath
[] = "/etc/drirc";
271 static const char kDriCard0Path
[] = "/dev/dri/card0";
272 static const char kDevShm
[] = "/dev/shm/";
274 CHECK(broker_process_
== NULL
);
276 // All GPU process policies need these files brokered out.
277 std::vector
<BrokerFilePermission
> permissions
;
278 permissions
.push_back(BrokerFilePermission::ReadWrite(kDriCard0Path
));
279 permissions
.push_back(BrokerFilePermission::ReadOnly(kDriRcPath
));
281 permissions
.push_back(
282 BrokerFilePermission::ReadWriteCreateUnlinkRecursive(kDevShm
));
285 // Add eventual extra files from permissions_extra.
286 for (const auto& perm
: permissions_extra
) {
287 permissions
.push_back(perm
);
290 broker_process_
= new BrokerProcess(GetFSDeniedErrno(), permissions
);
291 // The initialization callback will perform generic initialization and then
292 // call broker_sandboxer_callback.
293 CHECK(broker_process_
->Init(base::Bind(&UpdateProcessTypeAndEnableSandbox
,
294 broker_sandboxer_allocator
)));
297 } // namespace content