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

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

Issue 10843059: Create a LinuxSandbox class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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
« no previous file with comments | « content/common/sandbox_linux.h ('k') | content/common/sandbox_methods_linux.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <fcntl.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8
9 #include "base/command_line.h"
10 #include "base/eintr_wrapper.h"
11 #include "base/logging.h"
12 #include "base/memory/singleton.h"
13 #include "content/common/sandbox_linux.h"
14 #include "content/common/seccomp_sandbox.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/common/sandbox_linux.h"
17 #include "sandbox/linux/suid/client/setuid_sandbox_client.h"
18
19 #if defined(SECCOMP_BPF_SANDBOX)
20 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
21 #endif
22
23 namespace {
24
25 // Implement the command line enabling logic for seccomp-legacy.
26 bool IsSeccompLegacyDesired() {
27 #if defined(SECCOMP_SANDBOX)
28 #if defined(NDEBUG)
29 // Off by default; allow turning on with a switch.
30 return CommandLine::ForCurrentProcess()->HasSwitch(
31 switches::kEnableSeccompSandbox);
32 #else
33 // On by default; allow turning off with a switch.
34 return !CommandLine::ForCurrentProcess()->HasSwitch(
35 switches::kDisableSeccompSandbox);
36 #endif // NDEBUG
37 #endif // SECCOMP_SANDBOX
38 return false;
39 }
40
41 } // namespace
42
43 namespace content {
44
45 LinuxSandbox::LinuxSandbox()
46 : proc_fd_(-1),
47 pre_initialized_(false),
48 seccomp_legacy_supported_(false),
49 setuid_sandbox_client_(sandbox::SetuidSandboxClient::Create()) {
50 if (setuid_sandbox_client_ == NULL) {
51 LOG(FATAL) << "Failed to instantiate the setuid sandbox client.";
52 }
53 }
54
55 LinuxSandbox::~LinuxSandbox() {
56 }
57
58 LinuxSandbox* LinuxSandbox::GetInstance() {
59 LinuxSandbox* instance = Singleton<LinuxSandbox>::get();
60 CHECK(instance);
61 return instance;
62 }
63
64 void LinuxSandbox::PreinitializeSandboxBegin() {
65 CHECK(!pre_initialized_);
66 seccomp_legacy_supported_ = false;
67 #if defined(SECCOMP_SANDBOX)
68 if (IsSeccompLegacyDesired()) {
69 proc_fd_ = open("/proc", O_DIRECTORY | O_RDONLY);
70 if (proc_fd_ < 0) {
71 LOG(ERROR) << "Cannot access \"/proc\". Disabling seccomp-legacy "
72 "sandboxing.";
73 // Now is a good time to figure out if we can support seccomp sandboxing
74 // at all. We will call SupportsSeccompSandbox again later, when actually
75 // enabling it, but we allow the implementation to cache some information.
76 // This is the only place where we will log full lack of seccomp-legacy
77 // support.
78 } else if (!SupportsSeccompSandbox(proc_fd_)) {
79 VLOG(1) << "Lacking support for seccomp-legacy sandbox.";
80 CHECK_EQ(HANDLE_EINTR(close(proc_fd_)), 0);
81 proc_fd_ = -1;
82 } else {
83 seccomp_legacy_supported_ = true;
84 }
85 }
86 #endif // SECCOMP_SANDBOX
87 #if defined(SECCOMP_BPF_SANDBOX)
88 // Similarly, we "pre-warm" the code that detects supports for seccomp BPF.
89 // TODO(jln): Use proc_fd_ here too once we're comfortable it does not create
90 // an additional security risk.
91 if (playground2::Sandbox::supportsSeccompSandbox(-1) !=
92 playground2::Sandbox::STATUS_AVAILABLE) {
93 VLOG(1) << "Lacking support for seccomp-bpf sandbox.";
94 }
95 #endif // SECCOMP_BPF_SANDBOX
96 pre_initialized_ = true;
97 }
98
99 // Once we finally know our process type, we can cleanup proc_fd_
100 // or pass it to seccomp-legacy.
101 void LinuxSandbox::PreinitializeSandboxFinish(
102 const std::string& process_type) {
103 CHECK(pre_initialized_);
104 if (proc_fd_ >= 0) {
105 if (ShouldEnableSeccompLegacy(process_type)) {
106 #if defined(SECCOMP_SANDBOX)
107 SeccompSandboxSetProcFd(proc_fd_);
108 #endif
109 } else {
110 DCHECK_GE(proc_fd_, 0);
111 CHECK_EQ(HANDLE_EINTR(close(proc_fd_)), 0);
112 }
113 proc_fd_ = -1;
114 }
115 }
116
117 void LinuxSandbox::PreinitializeSandbox(const std::string& process_type) {
118 PreinitializeSandboxBegin();
119 PreinitializeSandboxFinish(process_type);
120 }
121
122 int LinuxSandbox::GetStatus() {
123 CHECK(pre_initialized_);
124 int sandbox_flags = 0;
125 if (setuid_sandbox_client_->IsSandboxed()) {
126 sandbox_flags |= kSandboxLinuxSUID;
127 if (setuid_sandbox_client_->IsInNewPIDNamespace())
128 sandbox_flags |= kSandboxLinuxPIDNS;
129 if (setuid_sandbox_client_->IsInNewNETNamespace())
130 sandbox_flags |= kSandboxLinuxNetNS;
131 }
132 if (seccomp_legacy_supported_) {
133 sandbox_flags |= kSandboxLinuxSeccomp;
134 }
135 return sandbox_flags;
136 }
137
138 sandbox::SetuidSandboxClient*
139 LinuxSandbox::setuid_sandbox_client() const {
140 return setuid_sandbox_client_.get();
141 }
142
143 // For seccomp-legacy, we implement the policy inline, here.
144 bool LinuxSandbox::StartSeccompLegacy(const std::string& process_type) {
145 if (!pre_initialized_)
146 PreinitializeSandbox(process_type);
147 if (ShouldEnableSeccompLegacy(process_type)) {
148 // SupportsSeccompSandbox() returns a cached result, as we already
149 // called it earlier in the PreinitializeSandbox(). Thus, it is OK for us
150 // to not pass in a file descriptor for "/proc".
151 #if defined(SECCOMP_SANDBOX)
152 if (SupportsSeccompSandbox(-1)) {
153 StartSeccompSandbox();
154 return true;
155 }
156 #endif
157 }
158 return false;
159 }
160
161 // For seccomp-bpf, we will use the seccomp-bpf policy class.
162 // TODO(jln): implement this.
163 bool LinuxSandbox::StartSeccompBpf(const std::string& process_type) {
164 CHECK(pre_initialized_);
165 NOTREACHED();
166 return false;
167 }
168
169 // Our "policy" on whether or not to enable seccomp-legacy. Only renderers are
170 // supported.
171 bool LinuxSandbox::ShouldEnableSeccompLegacy(
172 const std::string& process_type) {
173 CHECK(pre_initialized_);
174 if (IsSeccompLegacyDesired() &&
175 seccomp_legacy_supported_ &&
176 process_type == switches::kRendererProcess) {
177 return true;
178 } else {
179 return false;
180 }
181 }
182
183 } // namespace content
184
OLDNEW
« no previous file with comments | « content/common/sandbox_linux.h ('k') | content/common/sandbox_methods_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698