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

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

Issue 191673003: Implement ScopedFD in terms of ScopedGeneric. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months 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
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
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) << "Could not count threads, the sandbox was not "
214 << "pre-initialized properly."; 211 << "pre-initialized properly.";
215 #endif // !defined(NDEBUG) 212 #endif // !defined(NDEBUG)
216 213
217 if (proc_self_task < 0) { 214 if (!proc_self_task) {
218 // Pretend to be monothreaded if it can't be determined (for instance the 215 // 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). 216 // setuid sandbox is already engaged but no proc_fd_ is available).
220 is_single_threaded = true; 217 is_single_threaded = true;
221 } else { 218 } else {
222 SafeScopedFD task_closer(&proc_self_task);
223 is_single_threaded = 219 is_single_threaded =
224 sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task); 220 sandbox::ThreadHelpers::IsSingleThreaded(proc_self_task.get());
225 } 221 }
226 222
227 return is_single_threaded; 223 return is_single_threaded;
228 } 224 }
229 225
230 bool LinuxSandbox::seccomp_bpf_started() const { 226 bool LinuxSandbox::seccomp_bpf_started() const {
231 return seccomp_bpf_started_; 227 return seccomp_bpf_started_;
232 } 228 }
233 229
234 sandbox::SetuidSandboxClient* 230 sandbox::SetuidSandboxClient*
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 (sandbox_status_flags_ != kSandboxLinuxInvalid) && 368 (sandbox_status_flags_ != kSandboxLinuxInvalid) &&
373 (GetStatus() & kSandboxLinuxSeccompBPF); 369 (GetStatus() & kSandboxLinuxSeccompBPF);
374 } 370 }
375 if (promised_seccomp_bpf_would_start) { 371 if (promised_seccomp_bpf_would_start) {
376 CHECK(seccomp_bpf_started_); 372 CHECK(seccomp_bpf_started_);
377 } 373 }
378 } 374 }
379 375
380 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const { 376 void LinuxSandbox::StopThreadAndEnsureNotCounted(base::Thread* thread) const {
381 DCHECK(thread); 377 DCHECK(thread);
382 int proc_self_task = OpenProcTaskFd(proc_fd_); 378 base::ScopedFD proc_self_task(OpenProcTaskFd(proc_fd_));
383 PCHECK(proc_self_task >= 0); 379 PCHECK(proc_self_task);
384 SafeScopedFD task_closer(&proc_self_task); 380 CHECK(sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task.get(),
385 CHECK( 381 thread));
386 sandbox::ThreadHelpers::StopThreadAndWatchProcFS(proc_self_task, thread));
387 } 382 }
388 383
389 } // namespace content 384 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698