OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <dirent.h> | 5 #include <dirent.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <sys/resource.h> | 7 #include <sys/resource.h> |
8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 #include <sys/time.h> | 9 #include <sys/time.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <unistd.h> |
11 | 12 |
12 #include <limits> | 13 #include <limits> |
13 | 14 |
14 #include "base/bind.h" | 15 #include "base/bind.h" |
15 #include "base/callback_helpers.h" | 16 #include "base/callback_helpers.h" |
16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
17 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/scoped_ptr.h" |
18 #include "base/memory/singleton.h" | 20 #include "base/memory/singleton.h" |
19 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
20 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
21 #include "base/time/time.h" | 23 #include "base/time/time.h" |
22 #include "build/build_config.h" | 24 #include "build/build_config.h" |
23 #include "content/common/sandbox_linux/sandbox_linux.h" | 25 #include "content/common/sandbox_linux/sandbox_linux.h" |
24 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" | 26 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" |
25 #include "content/public/common/content_switches.h" | 27 #include "content/public/common/content_switches.h" |
26 #include "content/public/common/sandbox_linux.h" | 28 #include "content/public/common/sandbox_linux.h" |
27 #include "sandbox/linux/services/credentials.h" | 29 #include "sandbox/linux/services/credentials.h" |
| 30 #include "sandbox/linux/services/thread_helpers.h" |
28 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 31 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
29 | 32 |
30 namespace { | 33 namespace { |
31 | 34 |
| 35 struct FDCloser { |
| 36 inline void operator()(int* fd) const { |
| 37 DCHECK(fd); |
| 38 PCHECK(0 == IGNORE_EINTR(close(*fd))); |
| 39 *fd = -1; |
| 40 } |
| 41 }; |
| 42 |
| 43 // Don't use base::ScopedFD since it doesn't CHECK that the file descriptor was |
| 44 // closed. |
| 45 typedef scoped_ptr<int, FDCloser> SafeScopedFD; |
| 46 |
32 void LogSandboxStarted(const std::string& sandbox_name) { | 47 void LogSandboxStarted(const std::string& sandbox_name) { |
33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 48 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
34 const std::string process_type = | 49 const std::string process_type = |
35 command_line.GetSwitchValueASCII(switches::kProcessType); | 50 command_line.GetSwitchValueASCII(switches::kProcessType); |
36 const std::string activated_sandbox = | 51 const std::string activated_sandbox = |
37 "Activated " + sandbox_name + " sandbox for process type: " + | 52 "Activated " + sandbox_name + " sandbox for process type: " + |
38 process_type + "."; | 53 process_type + "."; |
39 #if defined(OS_CHROMEOS) | 54 #if defined(OS_CHROMEOS) |
40 LOG(WARNING) << activated_sandbox; | 55 LOG(WARNING) << activated_sandbox; |
41 #else | 56 #else |
(...skipping 15 matching lines...) Expand all Loading... |
57 } | 72 } |
58 | 73 |
59 bool IsRunningTSAN() { | 74 bool IsRunningTSAN() { |
60 #if defined(THREAD_SANITIZER) | 75 #if defined(THREAD_SANITIZER) |
61 return true; | 76 return true; |
62 #else | 77 #else |
63 return false; | 78 return false; |
64 #endif | 79 #endif |
65 } | 80 } |
66 | 81 |
| 82 // Try to open /proc/self/task/ with the help of |proc_fd|. |proc_fd| can be |
| 83 // -1. Will return -1 on error and set errno like open(2). |
| 84 int OpenProcTaskFd(int proc_fd) { |
| 85 int proc_self_task = -1; |
| 86 if (proc_fd >= 0) { |
| 87 // If a handle to /proc is available, use it. This allows to bypass file |
| 88 // system restrictions. |
| 89 proc_self_task = openat(proc_fd, "self/task/", O_RDONLY | O_DIRECTORY); |
| 90 } else { |
| 91 // Otherwise, make an attempt to access the file system directly. |
| 92 proc_self_task = open("/proc/self/task/", O_RDONLY | O_DIRECTORY); |
| 93 } |
| 94 return proc_self_task; |
| 95 } |
| 96 |
67 } // namespace | 97 } // namespace |
68 | 98 |
69 namespace content { | 99 namespace content { |
70 | 100 |
71 LinuxSandbox::LinuxSandbox() | 101 LinuxSandbox::LinuxSandbox() |
72 : proc_fd_(-1), | 102 : proc_fd_(-1), |
73 seccomp_bpf_started_(false), | 103 seccomp_bpf_started_(false), |
74 sandbox_status_flags_(kSandboxLinuxInvalid), | 104 sandbox_status_flags_(kSandboxLinuxInvalid), |
75 pre_initialized_(false), | 105 pre_initialized_(false), |
76 seccomp_bpf_supported_(false), | 106 seccomp_bpf_supported_(false), |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 148 } |
119 } | 149 } |
120 pre_initialized_ = true; | 150 pre_initialized_ = true; |
121 } | 151 } |
122 | 152 |
123 bool LinuxSandbox::InitializeSandbox() { | 153 bool LinuxSandbox::InitializeSandbox() { |
124 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); | 154 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
125 return linux_sandbox->InitializeSandboxImpl(); | 155 return linux_sandbox->InitializeSandboxImpl(); |
126 } | 156 } |
127 | 157 |
| 158 void LinuxSandbox::StopThread(base::Thread* thread) { |
| 159 LinuxSandbox* linux_sandbox = LinuxSandbox::GetInstance(); |
| 160 linux_sandbox->StopThreadImpl(thread); |
| 161 } |
| 162 |
128 int LinuxSandbox::GetStatus() { | 163 int LinuxSandbox::GetStatus() { |
129 CHECK(pre_initialized_); | 164 CHECK(pre_initialized_); |
130 if (kSandboxLinuxInvalid == sandbox_status_flags_) { | 165 if (kSandboxLinuxInvalid == sandbox_status_flags_) { |
131 // Initialize sandbox_status_flags_. | 166 // Initialize sandbox_status_flags_. |
132 sandbox_status_flags_ = 0; | 167 sandbox_status_flags_ = 0; |
133 if (setuid_sandbox_client_->IsSandboxed()) { | 168 if (setuid_sandbox_client_->IsSandboxed()) { |
134 sandbox_status_flags_ |= kSandboxLinuxSUID; | 169 sandbox_status_flags_ |= kSandboxLinuxSUID; |
135 if (setuid_sandbox_client_->IsInNewPIDNamespace()) | 170 if (setuid_sandbox_client_->IsInNewPIDNamespace()) |
136 sandbox_status_flags_ |= kSandboxLinuxPIDNS; | 171 sandbox_status_flags_ |= kSandboxLinuxPIDNS; |
137 if (setuid_sandbox_client_->IsInNewNETNamespace()) | 172 if (setuid_sandbox_client_->IsInNewNETNamespace()) |
138 sandbox_status_flags_ |= kSandboxLinuxNetNS; | 173 sandbox_status_flags_ |= kSandboxLinuxNetNS; |
139 } | 174 } |
140 | 175 |
141 // We report whether the sandbox will be activated when renderers, workers | 176 // We report whether the sandbox will be activated when renderers, workers |
142 // and PPAPI plugins go through sandbox initialization. | 177 // and PPAPI plugins go through sandbox initialization. |
143 if (seccomp_bpf_supported() && | 178 if (seccomp_bpf_supported() && |
144 SandboxSeccompBPF::ShouldEnableSeccompBPF(switches::kRendererProcess)) { | 179 SandboxSeccompBPF::ShouldEnableSeccompBPF(switches::kRendererProcess)) { |
145 sandbox_status_flags_ |= kSandboxLinuxSeccompBPF; | 180 sandbox_status_flags_ |= kSandboxLinuxSeccompBPF; |
146 } | 181 } |
147 } | 182 } |
148 | 183 |
149 return sandbox_status_flags_; | 184 return sandbox_status_flags_; |
150 } | 185 } |
151 | 186 |
152 // Threads are counted via /proc/self/task. This is a little hairy because of | 187 // Threads are counted via /proc/self/task. This is a little hairy because of |
153 // PID namespaces and existing sandboxes, so "self" must really be used instead | 188 // PID namespaces and existing sandboxes, so "self" must really be used instead |
154 // of using the pid. | 189 // of using the pid. |
155 bool LinuxSandbox::IsSingleThreaded() const { | 190 bool LinuxSandbox::IsSingleThreaded() const { |
156 struct stat task_stat; | 191 bool is_single_threaded = false; |
157 int fstat_ret; | 192 int proc_self_task = OpenProcTaskFd(proc_fd_); |
158 if (proc_fd_ >= 0) { | 193 |
159 // If a handle to /proc is available, use it. This allows to bypass file | 194 // In Debug mode, it's mandatory to be able to count threads to catch bugs. |
160 // system restrictions. | |
161 fstat_ret = fstatat(proc_fd_, "self/task/", &task_stat, 0); | |
162 } else { | |
163 // Otherwise, make an attempt to access the file system directly. | |
164 fstat_ret = fstatat(AT_FDCWD, "/proc/self/task/", &task_stat, 0); | |
165 } | |
166 // In Debug mode, it's mandatory to be able to count threads to catch bugs. | |
167 #if !defined(NDEBUG) | 195 #if !defined(NDEBUG) |
168 // Using DCHECK here would be incorrect. DCHECK can be enabled in non | 196 // Using DCHECK here would be incorrect. DCHECK can be enabled in |
169 // official release mode. | 197 // non-official release mode. |
170 CHECK_EQ(0, fstat_ret) << "Could not count threads, the sandbox was not " | 198 CHECK_LE(0, proc_self_task) << "Could not count threads, the sandbox was not " |
171 << "pre-initialized properly."; | 199 << "pre-initialized properly."; |
172 #endif // !defined(NDEBUG) | 200 #endif // !defined(NDEBUG) |
173 if (fstat_ret) { | 201 |
| 202 if (proc_self_task < 0) { |
174 // Pretend to be monothreaded if it can't be determined (for instance the | 203 // Pretend to be monothreaded if it can't be determined (for instance the |
175 // setuid sandbox is already engaged but no proc_fd_ is available). | 204 // setuid sandbox is already engaged but no proc_fd_ is available). |
176 return true; | 205 is_single_threaded = true; |
| 206 } else { |
| 207 SafeScopedFD task_closer(&proc_self_task); |
| 208 is_single_threaded = |
| 209 sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task); |
177 } | 210 } |
178 | 211 |
179 // At least "..", "." and the current thread should be present. | 212 return is_single_threaded; |
180 CHECK_LE(3UL, task_stat.st_nlink); | |
181 // Counting threads via /proc/self/task could be racy. For the purpose of | |
182 // determining if the current proces is monothreaded it works: if at any | |
183 // time it becomes monothreaded, it'll stay so. | |
184 return task_stat.st_nlink == 3; | |
185 } | 213 } |
186 | 214 |
187 bool LinuxSandbox::seccomp_bpf_started() const { | 215 bool LinuxSandbox::seccomp_bpf_started() const { |
188 return seccomp_bpf_started_; | 216 return seccomp_bpf_started_; |
189 } | 217 } |
190 | 218 |
191 sandbox::SetuidSandboxClient* | 219 sandbox::SetuidSandboxClient* |
192 LinuxSandbox::setuid_sandbox_client() const { | 220 LinuxSandbox::setuid_sandbox_client() const { |
193 return setuid_sandbox_client_.get(); | 221 return setuid_sandbox_client_.get(); |
194 } | 222 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 278 |
251 // Attempt to limit the future size of the address space of the process. | 279 // Attempt to limit the future size of the address space of the process. |
252 LimitAddressSpace(process_type); | 280 LimitAddressSpace(process_type); |
253 | 281 |
254 // Try to enable seccomp-bpf. | 282 // Try to enable seccomp-bpf. |
255 bool seccomp_bpf_started = StartSeccompBPF(process_type); | 283 bool seccomp_bpf_started = StartSeccompBPF(process_type); |
256 | 284 |
257 return seccomp_bpf_started; | 285 return seccomp_bpf_started; |
258 } | 286 } |
259 | 287 |
| 288 void LinuxSandbox::StopThreadImpl(base::Thread* thread) { |
| 289 DCHECK(thread); |
| 290 StopThreadAndEnsureNotCounted(thread); |
| 291 } |
260 | 292 |
261 bool LinuxSandbox::seccomp_bpf_supported() const { | 293 bool LinuxSandbox::seccomp_bpf_supported() const { |
262 CHECK(pre_initialized_); | 294 CHECK(pre_initialized_); |
263 return seccomp_bpf_supported_; | 295 return seccomp_bpf_supported_; |
264 } | 296 } |
265 | 297 |
266 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) { | 298 bool LinuxSandbox::LimitAddressSpace(const std::string& process_type) { |
267 (void) process_type; | 299 (void) process_type; |
268 #if !defined(ADDRESS_SANITIZER) | 300 #if !defined(ADDRESS_SANITIZER) |
269 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 301 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
(...skipping 26 matching lines...) Expand all Loading... |
296 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); | 328 const rlim_t kNewDataSegmentMaxSize = std::numeric_limits<int>::max(); |
297 | 329 |
298 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit); | 330 bool limited_as = AddResourceLimit(RLIMIT_AS, address_space_limit); |
299 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize); | 331 bool limited_data = AddResourceLimit(RLIMIT_DATA, kNewDataSegmentMaxSize); |
300 return limited_as && limited_data; | 332 return limited_as && limited_data; |
301 #else | 333 #else |
302 return false; | 334 return false; |
303 #endif // !defined(ADDRESS_SANITIZER) | 335 #endif // !defined(ADDRESS_SANITIZER) |
304 } | 336 } |
305 | 337 |
306 bool LinuxSandbox::HasOpenDirectories() { | 338 bool LinuxSandbox::HasOpenDirectories() const { |
307 return sandbox::Credentials().HasOpenDirectory(proc_fd_); | 339 return sandbox::Credentials().HasOpenDirectory(proc_fd_); |
308 } | 340 } |
309 | 341 |
310 void LinuxSandbox::SealSandbox() { | 342 void LinuxSandbox::SealSandbox() { |
311 if (proc_fd_ >= 0) { | 343 if (proc_fd_ >= 0) { |
312 int ret = IGNORE_EINTR(close(proc_fd_)); | 344 int ret = IGNORE_EINTR(close(proc_fd_)); |
313 CHECK_EQ(0, ret); | 345 CHECK_EQ(0, ret); |
314 proc_fd_ = -1; | 346 proc_fd_ = -1; |
315 } | 347 } |
316 } | 348 } |
317 | 349 |
318 void LinuxSandbox::CheckForBrokenPromises(const std::string& process_type) { | 350 void LinuxSandbox::CheckForBrokenPromises(const std::string& process_type) { |
319 // Make sure that any promise made with GetStatus() wasn't broken. | 351 // Make sure that any promise made with GetStatus() wasn't broken. |
320 bool promised_seccomp_bpf_would_start = false; | 352 bool promised_seccomp_bpf_would_start = false; |
321 if (process_type == switches::kRendererProcess || | 353 if (process_type == switches::kRendererProcess || |
322 process_type == switches::kWorkerProcess || | 354 process_type == switches::kWorkerProcess || |
323 process_type == switches::kPpapiPluginProcess) { | 355 process_type == switches::kPpapiPluginProcess) { |
324 promised_seccomp_bpf_would_start = | 356 promised_seccomp_bpf_would_start = |
325 (sandbox_status_flags_ != kSandboxLinuxInvalid) && | 357 (sandbox_status_flags_ != kSandboxLinuxInvalid) && |
326 (GetStatus() & kSandboxLinuxSeccompBPF); | 358 (GetStatus() & kSandboxLinuxSeccompBPF); |
327 } | 359 } |
328 if (promised_seccomp_bpf_would_start) { | 360 if (promised_seccomp_bpf_would_start) { |
329 CHECK(seccomp_bpf_started_); | 361 CHECK(seccomp_bpf_started_); |
330 } | 362 } |
331 } | 363 } |
332 | 364 |
| 365 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const { |
| 366 DCHECK(thread); |
| 367 int proc_self_task = OpenProcTaskFd(proc_fd_); |
| 368 PCHECK(proc_self_task >= 0); |
| 369 SafeScopedFD task_closer(&proc_self_task); |
| 370 CHECK( |
| 371 sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task, thread)); |
| 372 } |
| 373 |
333 } // namespace content | 374 } // namespace content |
OLD | NEW |