Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(500)

Side by Side Diff: content/common/sandbox_linux/sandbox_bpf_gpu_policy_linux.cc

Issue 99133015: Linux Sandbox: split the GPU policies to their own file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove obsolete InitGpuBrokerProcess argument. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "content/common/sandbox_linux/sandbox_bpf_gpu_policy_linux.h"
6
7 #include <dlfcn.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/socket.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include <string>
16 #include <vector>
17
18 #include "base/command_line.h"
19 #include "base/compiler_specific.h"
20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "content/common/sandbox_linux/sandbox_bpf_base_policy_linux.h"
23 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h"
24 #include "content/public/common/content_switches.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/broker_process.h"
28 #include "sandbox/linux/services/linux_syscalls.h"
29
30 using sandbox::BrokerProcess;
31 using sandbox::ErrorCode;
32 using sandbox::SandboxBPF;
33 using sandbox::SyscallSets;
34 using sandbox::arch_seccomp_data;
35
36 namespace content {
37
38 namespace {
39
40 inline bool IsChromeOS() {
41 #if defined(OS_CHROMEOS)
42 return true;
43 #else
44 return false;
45 #endif
46 }
47
48 inline bool IsArchitectureX86_64() {
49 #if defined(__x86_64__)
50 return true;
51 #else
52 return false;
53 #endif
54 }
55
56 inline bool IsArchitectureI386() {
57 #if defined(__i386__)
58 return true;
59 #else
60 return false;
61 #endif
62 }
63
64 inline bool IsArchitectureArm() {
65 #if defined(__arm__)
66 return true;
67 #else
68 return false;
69 #endif
70 }
71
72 bool IsAcceleratedVideoDecodeEnabled() {
73 // Accelerated video decode is currently enabled on Chrome OS,
74 // but not on Linux: crbug.com/137247.
75 bool is_enabled = IsChromeOS();
76
77 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
78 is_enabled = is_enabled &&
Robert Sesek 2013/12/12 21:33:48 Could switch to &=
jln (very slow on Chromium) 2013/12/12 22:15:14 Done.
79 !command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode);
80
81 return is_enabled;
82 }
83
84 intptr_t GpuSIGSYS_Handler(const struct arch_seccomp_data& args,
85 void* aux_broker_process) {
86 RAW_CHECK(aux_broker_process);
87 BrokerProcess* broker_process =
88 static_cast<BrokerProcess*>(aux_broker_process);
89 switch (args.nr) {
90 case __NR_access:
91 return broker_process->Access(reinterpret_cast<const char*>(args.args[0]),
92 static_cast<int>(args.args[1]));
93 case __NR_open:
94 return broker_process->Open(reinterpret_cast<const char*>(args.args[0]),
95 static_cast<int>(args.args[1]));
96 case __NR_openat:
97 // Allow using openat() as open().
98 if (static_cast<int>(args.args[0]) == AT_FDCWD) {
99 return
100 broker_process->Open(reinterpret_cast<const char*>(args.args[1]),
101 static_cast<int>(args.args[2]));
102 } else {
103 return -EPERM;
104 }
105 default:
106 RAW_CHECK(false);
107 return -ENOSYS;
108 }
109 }
110
111 class GpuBrokerProcessPolicy : public GpuProcessPolicy {
112 public:
113 GpuBrokerProcessPolicy() {}
114 virtual ~GpuBrokerProcessPolicy() {}
115
116 virtual ErrorCode EvaluateSyscall(SandboxBPF* sandbox_compiler,
117 int system_call_number) const OVERRIDE;
118
119 private:
120 DISALLOW_COPY_AND_ASSIGN(GpuBrokerProcessPolicy);
121 };
122
123 // x86_64/i386 or desktop ARM.
124 // A GPU broker policy is the same as a GPU policy with open and
125 // openat allowed.
126 ErrorCode GpuBrokerProcessPolicy::EvaluateSyscall(SandboxBPF* sandbox,
127 int sysno) const {
128 switch (sysno) {
129 case __NR_access:
130 case __NR_open:
131 case __NR_openat:
132 return ErrorCode(ErrorCode::ERR_ALLOWED);
133 default:
134 return GpuProcessPolicy::EvaluateSyscall(sandbox, sysno);
135 }
136 }
137
138 bool EnableGpuBrokerPolicyCallback() {
139 return SandboxSeccompBPF::StartSandboxWithExternalPolicy(
140 scoped_ptr<sandbox::SandboxBPFPolicy>(new GpuBrokerProcessPolicy));
141 }
142
143 } // namespace
144
145 // Main policy for x86_64/i386. Extended by CrosArmGpuProcessPolicy.
146 ErrorCode GpuProcessPolicy::EvaluateSyscall(SandboxBPF* sandbox,
147 int sysno) const {
148 switch (sysno) {
149 case __NR_ioctl:
150 #if defined(__i386__) || defined(__x86_64__)
151 // The Nvidia driver uses flags not in the baseline policy
152 // (MAP_LOCKED | MAP_EXECUTABLE | MAP_32BIT)
153 case __NR_mmap:
154 #endif
155 // We also hit this on the linux_chromeos bot but don't yet know what
156 // weird flags were involved.
157 case __NR_mprotect:
158 case __NR_sched_getaffinity:
159 case __NR_sched_setaffinity:
160 case __NR_setpriority:
161 return ErrorCode(ErrorCode::ERR_ALLOWED);
162 case __NR_access:
163 case __NR_open:
164 case __NR_openat:
165 DCHECK(broker_process_);
166 return sandbox->Trap(GpuSIGSYS_Handler, broker_process_);
167 default:
168 if (SyscallSets::IsEventFd(sysno))
169 return ErrorCode(ErrorCode::ERR_ALLOWED);
170
171 // Default on the baseline policy.
172 return SandboxBPFBasePolicy::EvaluateSyscall(sandbox, sysno);
173 }
174 }
175
176 bool GpuProcessPolicy::PreSandboxHook() {
177 // Warm up resources needed by the policy we're about to enable and
178 // eventually start a broker process.
179 const bool chromeos_arm_gpu = IsChromeOS() && IsArchitectureArm();
180 DCHECK(!chromeos_arm_gpu);
181
182 DCHECK(!broker_process());
183 // Create a new broker process.
184 InitGpuBrokerProcess(
185 EnableGpuBrokerPolicyCallback,
186 std::vector<std::string>(), // No extra files in whitelist.
187 std::vector<std::string>());
188
189 if (IsArchitectureX86_64() || IsArchitectureI386()) {
190 // Accelerated video decode dlopen()'s some shared objects
191 // inside the sandbox, so preload them now.
192 if (IsAcceleratedVideoDecodeEnabled()) {
193 const char* I965DrvVideoPath = NULL;
194
195 if (IsArchitectureX86_64()) {
196 I965DrvVideoPath = "/usr/lib64/va/drivers/i965_drv_video.so";
197 } else if (IsArchitectureI386()) {
198 I965DrvVideoPath = "/usr/lib/va/drivers/i965_drv_video.so";
199 }
200
201 dlopen(I965DrvVideoPath, RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE);
202 dlopen("libva.so.1", RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE);
203 dlopen("libva-x11.so.1", RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE);
204 }
205 }
206
207 return true;
208 }
209
210 void GpuProcessPolicy::InitGpuBrokerProcess(
211 bool (*broker_sandboxer_callback)(void),
212 const std::vector<std::string>& read_whitelist_extra,
213 const std::vector<std::string>& write_whitelist_extra) {
214 static const char kDriRcPath[] = "/etc/drirc";
215 static const char kDriCard0Path[] = "/dev/dri/card0";
216
217 CHECK(broker_process_ == NULL);
218
219 // All GPU process policies need these files brokered out.
220 std::vector<std::string> read_whitelist;
221 read_whitelist.push_back(kDriCard0Path);
222 read_whitelist.push_back(kDriRcPath);
223 // Add eventual extra files from read_whitelist_extra.
224 read_whitelist.insert(read_whitelist.end(),
225 read_whitelist_extra.begin(),
226 read_whitelist_extra.end());
227
228 std::vector<std::string> write_whitelist;
229 write_whitelist.push_back(kDriCard0Path);
230 // Add eventual extra files from write_whitelist_extra.
231 write_whitelist.insert(write_whitelist.end(),
232 write_whitelist_extra.begin(),
233 write_whitelist_extra.end());
234
235 broker_process_ = new BrokerProcess(GetFSDeniedErrno(),
236 read_whitelist,
237 write_whitelist);
238 // Initialize the broker process and give it a sandbox callback.
239 CHECK(broker_process_->Init(broker_sandboxer_callback));
240 }
241
242 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698