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 #include <unistd.h> |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/callback_helpers.h" | 16 #include "base/callback_helpers.h" |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/files/scoped_file.h" |
18 #include "base/logging.h" | 19 #include "base/logging.h" |
19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
20 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
21 #include "base/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
22 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
25 #include "content/common/sandbox_linux/sandbox_linux.h" | 26 #include "content/common/sandbox_linux/sandbox_linux.h" |
26 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" | 27 #include "content/common/sandbox_linux/sandbox_seccomp_bpf_linux.h" |
27 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
28 #include "content/public/common/sandbox_linux.h" | 29 #include "content/public/common/sandbox_linux.h" |
29 #include "sandbox/linux/services/credentials.h" | 30 #include "sandbox/linux/services/credentials.h" |
30 #include "sandbox/linux/services/thread_helpers.h" | 31 #include "sandbox/linux/services/thread_helpers.h" |
31 #include "sandbox/linux/services/yama.h" | 32 #include "sandbox/linux/services/yama.h" |
32 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" | 33 #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
33 | 34 |
34 using sandbox::Yama; | 35 using sandbox::Yama; |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
38 struct FDCloser { | 39 struct FDCloser { |
39 inline void operator()(int* fd) const { | 40 inline void operator()(int* fd) const { |
40 DCHECK(fd); | 41 DCHECK(fd); |
41 PCHECK(0 == IGNORE_EINTR(close(*fd))); | 42 PCHECK(0 == IGNORE_EINTR(close(*fd))); |
42 *fd = -1; | 43 *fd = -1; |
43 } | 44 } |
44 }; | 45 }; |
45 | 46 |
46 // Don't use base::ScopedFD since it doesn't CHECK that the file descriptor was | |
47 // closed. | |
48 typedef scoped_ptr<int, FDCloser> SafeScopedFD; | |
49 | |
50 void LogSandboxStarted(const std::string& sandbox_name) { | 47 void LogSandboxStarted(const std::string& sandbox_name) { |
51 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 48 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
52 const std::string process_type = | 49 const std::string process_type = |
53 command_line.GetSwitchValueASCII(switches::kProcessType); | 50 command_line.GetSwitchValueASCII(switches::kProcessType); |
54 const std::string activated_sandbox = | 51 const std::string activated_sandbox = |
55 "Activated " + sandbox_name + " sandbox for process type: " + | 52 "Activated " + sandbox_name + " sandbox for process type: " + |
56 process_type + "."; | 53 process_type + "."; |
57 #if defined(OS_CHROMEOS) | 54 #if defined(OS_CHROMEOS) |
58 LOG(WARNING) << activated_sandbox; | 55 LOG(WARNING) << activated_sandbox; |
59 #else | 56 #else |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 193 } |
197 | 194 |
198 return sandbox_status_flags_; | 195 return sandbox_status_flags_; |
199 } | 196 } |
200 | 197 |
201 // Threads are counted via /proc/self/task. This is a little hairy because of | 198 // Threads are counted via /proc/self/task. This is a little hairy because of |
202 // PID namespaces and existing sandboxes, so "self" must really be used instead | 199 // PID namespaces and existing sandboxes, so "self" must really be used instead |
203 // of using the pid. | 200 // of using the pid. |
204 bool LinuxSandbox::IsSingleThreaded() const { | 201 bool LinuxSandbox::IsSingleThreaded() const { |
205 bool is_single_threaded = false; | 202 bool is_single_threaded = false; |
206 int proc_self_task = OpenProcTaskFd(proc_fd_); | 203 base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_)); |
207 | 204 |
208 // In Debug mode, it's mandatory to be able to count threads to catch bugs. | 205 // In Debug mode, it's mandatory to be able to count threads to catch bugs. |
209 #if !defined(NDEBUG) | 206 #if !defined(NDEBUG) |
210 // Using CHECK here since we want to check all the cases where | 207 // Using CHECK here since we want to check all the cases where |
211 // !defined(NDEBUG) | 208 // !defined(NDEBUG) |
212 // gets built. | 209 // gets built. |
213 CHECK_LE(0, proc_self_task) << "Could not count threads, the sandbox was not " | 210 CHECK(proc_self_task.is_valid()) |
214 << "pre-initialized properly."; | 211 << "Could not count threads, the sandbox was not " |
| 212 << "pre-initialized properly."; |
215 #endif // !defined(NDEBUG) | 213 #endif // !defined(NDEBUG) |
216 | 214 |
217 if (proc_self_task < 0) { | 215 if (!proc_self_task.is_valid()) { |
218 // Pretend to be monothreaded if it can't be determined (for instance the | 216 // Pretend to be monothreaded if it can't be determined (for instance the |
219 // setuid sandbox is already engaged but no proc_fd_ is available). | 217 // setuid sandbox is already engaged but no proc_fd_ is available). |
220 is_single_threaded = true; | 218 is_single_threaded = true; |
221 } else { | 219 } else { |
222 SafeScopedFD task_closer(&proc_self_task); | |
223 is_single_threaded = | 220 is_single_threaded = |
224 sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task); | 221 sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task.get()); |
225 } | 222 } |
226 | 223 |
227 return is_single_threaded; | 224 return is_single_threaded; |
228 } | 225 } |
229 | 226 |
230 bool LinuxSandbox::seccomp_bpf_started() const { | 227 bool LinuxSandbox::seccomp_bpf_started() const { |
231 return seccomp_bpf_started_; | 228 return seccomp_bpf_started_; |
232 } | 229 } |
233 | 230 |
234 sandbox::SetuidSandboxClient* | 231 sandbox::SetuidSandboxClient* |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 (sandbox_status_flags_ != kSandboxLinuxInvalid) && | 381 (sandbox_status_flags_ != kSandboxLinuxInvalid) && |
385 (GetStatus() & kSandboxLinuxSeccompBPF); | 382 (GetStatus() & kSandboxLinuxSeccompBPF); |
386 } | 383 } |
387 if (promised_seccomp_bpf_would_start) { | 384 if (promised_seccomp_bpf_would_start) { |
388 CHECK(seccomp_bpf_started_); | 385 CHECK(seccomp_bpf_started_); |
389 } | 386 } |
390 } | 387 } |
391 | 388 |
392 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const { | 389 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const { |
393 DCHECK(thread); | 390 DCHECK(thread); |
394 int proc_self_task = OpenProcTaskFd(proc_fd_); | 391 base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_)); |
395 PCHECK(proc_self_task >= 0); | 392 PCHECK(proc_self_task.is_valid()); |
396 SafeScopedFD task_closer(&proc_self_task); | 393 CHECK(sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task.get(), |
397 CHECK( | 394 thread)); |
398 sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task, thread)); | |
399 } | 395 } |
400 | 396 |
401 } // namespace content | 397 } // namespace content |
OLD | NEW |