OLD | NEW |
| (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/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 &= | |
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 GpuProcessPolicy::GpuProcessPolicy() : broker_process_(NULL) {} | |
146 | |
147 GpuProcessPolicy::~GpuProcessPolicy() {} | |
148 | |
149 // Main policy for x86_64/i386. Extended by CrosArmGpuProcessPolicy. | |
150 ErrorCode GpuProcessPolicy::EvaluateSyscall(SandboxBPF* sandbox, | |
151 int sysno) const { | |
152 switch (sysno) { | |
153 case __NR_ioctl: | |
154 #if defined(__i386__) || defined(__x86_64__) | |
155 // The Nvidia driver uses flags not in the baseline policy | |
156 // (MAP_LOCKED | MAP_EXECUTABLE | MAP_32BIT) | |
157 case __NR_mmap: | |
158 #endif | |
159 // We also hit this on the linux_chromeos bot but don't yet know what | |
160 // weird flags were involved. | |
161 case __NR_mprotect: | |
162 case __NR_sched_getaffinity: | |
163 case __NR_sched_setaffinity: | |
164 case __NR_setpriority: | |
165 return ErrorCode(ErrorCode::ERR_ALLOWED); | |
166 case __NR_access: | |
167 case __NR_open: | |
168 case __NR_openat: | |
169 DCHECK(broker_process_); | |
170 return sandbox->Trap(GpuSIGSYS_Handler, broker_process_); | |
171 default: | |
172 if (SyscallSets::IsEventFd(sysno)) | |
173 return ErrorCode(ErrorCode::ERR_ALLOWED); | |
174 | |
175 // Default on the baseline policy. | |
176 return SandboxBPFBasePolicy::EvaluateSyscall(sandbox, sysno); | |
177 } | |
178 } | |
179 | |
180 bool GpuProcessPolicy::PreSandboxHook() { | |
181 // Warm up resources needed by the policy we're about to enable and | |
182 // eventually start a broker process. | |
183 const bool chromeos_arm_gpu = IsChromeOS() && IsArchitectureArm(); | |
184 // This policy is for x86 or Desktop. | |
185 DCHECK(!chromeos_arm_gpu); | |
186 | |
187 DCHECK(!broker_process()); | |
188 // Create a new broker process. | |
189 InitGpuBrokerProcess( | |
190 EnableGpuBrokerPolicyCallback, | |
191 std::vector<std::string>(), // No extra files in whitelist. | |
192 std::vector<std::string>()); | |
193 | |
194 if (IsArchitectureX86_64() || IsArchitectureI386()) { | |
195 // Accelerated video decode dlopen()'s some shared objects | |
196 // inside the sandbox, so preload them now. | |
197 if (IsAcceleratedVideoDecodeEnabled()) { | |
198 const char* I965DrvVideoPath = NULL; | |
199 | |
200 if (IsArchitectureX86_64()) { | |
201 I965DrvVideoPath = "/usr/lib64/va/drivers/i965_drv_video.so"; | |
202 } else if (IsArchitectureI386()) { | |
203 I965DrvVideoPath = "/usr/lib/va/drivers/i965_drv_video.so"; | |
204 } | |
205 | |
206 dlopen(I965DrvVideoPath, RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE); | |
207 dlopen("libva.so.1", RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE); | |
208 dlopen("libva-x11.so.1", RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE); | |
209 } | |
210 } | |
211 | |
212 return true; | |
213 } | |
214 | |
215 void GpuProcessPolicy::InitGpuBrokerProcess( | |
216 bool (*broker_sandboxer_callback)(void), | |
217 const std::vector<std::string>& read_whitelist_extra, | |
218 const std::vector<std::string>& write_whitelist_extra) { | |
219 static const char kDriRcPath[] = "/etc/drirc"; | |
220 static const char kDriCard0Path[] = "/dev/dri/card0"; | |
221 | |
222 CHECK(broker_process_ == NULL); | |
223 | |
224 // All GPU process policies need these files brokered out. | |
225 std::vector<std::string> read_whitelist; | |
226 read_whitelist.push_back(kDriCard0Path); | |
227 read_whitelist.push_back(kDriRcPath); | |
228 // Add eventual extra files from read_whitelist_extra. | |
229 read_whitelist.insert(read_whitelist.end(), | |
230 read_whitelist_extra.begin(), | |
231 read_whitelist_extra.end()); | |
232 | |
233 std::vector<std::string> write_whitelist; | |
234 write_whitelist.push_back(kDriCard0Path); | |
235 // Add eventual extra files from write_whitelist_extra. | |
236 write_whitelist.insert(write_whitelist.end(), | |
237 write_whitelist_extra.begin(), | |
238 write_whitelist_extra.end()); | |
239 | |
240 broker_process_ = new BrokerProcess(GetFSDeniedErrno(), | |
241 read_whitelist, | |
242 write_whitelist); | |
243 // Initialize the broker process and give it a sandbox callback. | |
244 CHECK(broker_process_->Init(broker_sandboxer_callback)); | |
245 } | |
246 | |
247 } // namespace content | |
OLD | NEW |