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

Side by Side Diff: sandbox/linux/suid/client/setuid_sandbox_host.cc

Issue 877153005: sandbox: extract SetuidSandboxHost code from SetuidSandboxClient (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to jln/hidehiko feedback; clang-format Created 5 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "sandbox/linux/suid/client/setuid_sandbox_client.h" 5 #include "sandbox/linux/suid/client/setuid_sandbox_host.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <sys/socket.h>
10 #include <sys/stat.h> 9 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <unistd.h> 10 #include <unistd.h>
14 11
12 #include <string>
13 #include <utility>
14
15 #include "base/command_line.h" 15 #include "base/command_line.h"
16 #include "base/environment.h" 16 #include "base/environment.h"
17 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h" 18 #include "base/files/file_util.h"
19 #include "base/files/scoped_file.h" 19 #include "base/files/scoped_file.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h" 21 #include "base/memory/scoped_ptr.h"
22 #include "base/path_service.h" 22 #include "base/path_service.h"
23 #include "base/posix/eintr_wrapper.h" 23 #include "base/posix/eintr_wrapper.h"
24 #include "base/process/launch.h" 24 #include "base/process/launch.h"
25 #include "base/process/process_metrics.h" 25 #include "base/process/process_metrics.h"
26 #include "base/strings/string_number_conversions.h" 26 #include "base/strings/string_number_conversions.h"
27 #include "sandbox/linux/services/init_process_reaper.h"
28 #include "sandbox/linux/suid/common/sandbox.h" 27 #include "sandbox/linux/suid/common/sandbox.h"
29 #include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h" 28 #include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h"
30 29
31 namespace { 30 namespace {
32 31
33 bool IsFileSystemAccessDenied() {
34 base::ScopedFD self_exe(HANDLE_EINTR(open("/", O_RDONLY)));
35 return !self_exe.is_valid();
36 }
37
38 // Set an environment variable that reflects the API version we expect from the 32 // Set an environment variable that reflects the API version we expect from the
39 // setuid sandbox. Old versions of the sandbox will ignore this. 33 // setuid sandbox. Old versions of the sandbox will ignore this.
40 void SetSandboxAPIEnvironmentVariable(base::Environment* env) { 34 void SetSandboxAPIEnvironmentVariable(base::Environment* env) {
41 env->SetVar(sandbox::kSandboxEnvironmentApiRequest, 35 env->SetVar(sandbox::kSandboxEnvironmentApiRequest,
42 base::IntToString(sandbox::kSUIDSandboxApiNumber)); 36 base::IntToString(sandbox::kSUIDSandboxApiNumber));
43 } 37 }
44 38
45 // Unset environment variables that are expected to be set by the setuid 39 // Unset environment variables that are expected to be set by the setuid
46 // sandbox. This is to allow nesting of one instance of the SUID sandbox 40 // sandbox. This is to allow nesting of one instance of the SUID sandbox
47 // inside another. 41 // inside another.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 continue; 82 continue;
89 83
90 std::string value; 84 std::string value;
91 if (env->GetVar(env_var, &value)) 85 if (env->GetVar(env_var, &value))
92 env->SetVar(saved_env_var->c_str(), value); 86 env->SetVar(saved_env_var->c_str(), value);
93 else 87 else
94 env->UnSetVar(saved_env_var->c_str()); 88 env->UnSetVar(saved_env_var->c_str());
95 } 89 }
96 } 90 }
97 91
98 int GetHelperApi(base::Environment* env) {
99 std::string api_string;
100 int api_number = 0; // Assume API version 0 if no environment was found.
101 if (env->GetVar(sandbox::kSandboxEnvironmentApiProvides, &api_string) &&
102 !base::StringToInt(api_string, &api_number)) {
103 // It's an error if we could not convert the API number.
104 api_number = -1;
105 }
106 return api_number;
107 }
108
109 // Convert |var_name| from the environment |env| to an int.
110 // Return -1 if the variable does not exist or the value cannot be converted.
111 int EnvToInt(base::Environment* env, const char* var_name) {
112 std::string var_string;
113 int var_value = -1;
114 if (env->GetVar(var_name, &var_string) &&
115 !base::StringToInt(var_string, &var_value)) {
116 var_value = -1;
117 }
118 return var_value;
119 }
120
121 pid_t GetHelperPID(base::Environment* env) {
122 return EnvToInt(env, sandbox::kSandboxHelperPidEnvironmentVarName);
123 }
124
125 // Get the IPC file descriptor used to communicate with the setuid helper.
126 int GetIPCDescriptor(base::Environment* env) {
127 return EnvToInt(env, sandbox::kSandboxDescriptorEnvironmentVarName);
128 }
129
130 const char* GetDevelSandboxPath() { 92 const char* GetDevelSandboxPath() {
131 return getenv("CHROME_DEVEL_SANDBOX"); 93 return getenv("CHROME_DEVEL_SANDBOX");
132 } 94 }
133 95
134 } // namespace 96 } // namespace
135 97
136 namespace sandbox { 98 namespace sandbox {
137 99
138 SetuidSandboxClient* SetuidSandboxClient::Create() { 100 SetuidSandboxHost* SetuidSandboxHost::Create() {
139 base::Environment* environment(base::Environment::Create()); 101 base::Environment* environment(base::Environment::Create());
140 SetuidSandboxClient* sandbox_client(new SetuidSandboxClient);
141
142 CHECK(environment); 102 CHECK(environment);
143 sandbox_client->env_ = environment; 103 return new SetuidSandboxHost(environment);
144 return sandbox_client;
145 } 104 }
146 105
147 SetuidSandboxClient::SetuidSandboxClient() 106 SetuidSandboxHost::SetuidSandboxHost(base::Environment* env) : env_(env) {
148 : env_(NULL),
149 sandboxed_(false) {
150 } 107 }
151 108
152 SetuidSandboxClient::~SetuidSandboxClient() { 109 SetuidSandboxHost::~SetuidSandboxHost() {
153 delete env_;
154 }
155
156 void SetuidSandboxClient::CloseDummyFile() {
157 // When we're launched through the setuid sandbox, SetupLaunchOptions
158 // arranges for kZygoteIdFd to be a dummy file descriptor to satisfy an
159 // ancient setuid sandbox ABI requirement. However, the descriptor is no
160 // longer needed, so we can simply close it right away now.
161 CHECK(IsSuidSandboxChild());
162
163 // Sanity check that kZygoteIdFd refers to a pipe.
164 struct stat st;
165 PCHECK(0 == fstat(kZygoteIdFd, &st));
166 CHECK(S_ISFIFO(st.st_mode));
167
168 PCHECK(0 == IGNORE_EINTR(close(kZygoteIdFd)));
169 }
170
171 bool SetuidSandboxClient::ChrootMe() {
172 int ipc_fd = GetIPCDescriptor(env_);
173
174 if (ipc_fd < 0) {
175 LOG(ERROR) << "Failed to obtain the sandbox IPC descriptor";
176 return false;
177 }
178
179 if (HANDLE_EINTR(write(ipc_fd, &kMsgChrootMe, 1)) != 1) {
180 PLOG(ERROR) << "Failed to write to chroot pipe";
181 return false;
182 }
183
184 // We need to reap the chroot helper process in any event.
185 pid_t helper_pid = GetHelperPID(env_);
186 // If helper_pid is -1 we wait for any child.
187 if (HANDLE_EINTR(waitpid(helper_pid, NULL, 0)) < 0) {
188 PLOG(ERROR) << "Failed to wait for setuid helper to die";
189 return false;
190 }
191
192 char reply;
193 if (HANDLE_EINTR(read(ipc_fd, &reply, 1)) != 1) {
194 PLOG(ERROR) << "Failed to read from chroot pipe";
195 return false;
196 }
197
198 if (reply != kMsgChrootSuccessful) {
199 LOG(ERROR) << "Error code reply from chroot helper";
200 return false;
201 }
202
203 // We now consider ourselves "fully sandboxed" as far as the
204 // setuid sandbox is concerned.
205 CHECK(IsFileSystemAccessDenied());
206 sandboxed_ = true;
207 return true;
208 }
209
210 bool SetuidSandboxClient::IsSuidSandboxUpToDate() const {
211 return GetHelperApi(env_) == kSUIDSandboxApiNumber;
212 }
213
214 bool SetuidSandboxClient::IsSuidSandboxChild() const {
215 return GetIPCDescriptor(env_) >= 0;
216 }
217
218 bool SetuidSandboxClient::IsInNewPIDNamespace() const {
219 return env_->HasVar(kSandboxPIDNSEnvironmentVarName);
220 }
221
222 bool SetuidSandboxClient::IsInNewNETNamespace() const {
223 return env_->HasVar(kSandboxNETNSEnvironmentVarName);
224 }
225
226 bool SetuidSandboxClient::IsSandboxed() const {
227 return sandboxed_;
228 } 110 }
229 111
230 // Check if CHROME_DEVEL_SANDBOX is set but empty. This currently disables 112 // Check if CHROME_DEVEL_SANDBOX is set but empty. This currently disables
231 // the setuid sandbox. TODO(jln): fix this (crbug.com/245376). 113 // the setuid sandbox. TODO(jln): fix this (crbug.com/245376).
232 bool SetuidSandboxClient::IsDisabledViaEnvironment() { 114 bool SetuidSandboxHost::IsDisabledViaEnvironment() {
233 const char* devel_sandbox_path = GetDevelSandboxPath(); 115 const char* devel_sandbox_path = GetDevelSandboxPath();
234 if (devel_sandbox_path && '\0' == *devel_sandbox_path) { 116 if (devel_sandbox_path && '\0' == *devel_sandbox_path) {
235 return true; 117 return true;
236 } 118 }
237 return false; 119 return false;
238 } 120 }
239 121
240 base::FilePath SetuidSandboxClient::GetSandboxBinaryPath() { 122 base::FilePath SetuidSandboxHost::GetSandboxBinaryPath() {
241 base::FilePath sandbox_binary; 123 base::FilePath sandbox_binary;
242 base::FilePath exe_dir; 124 base::FilePath exe_dir;
243 if (PathService::Get(base::DIR_EXE, &exe_dir)) { 125 if (PathService::Get(base::DIR_EXE, &exe_dir)) {
244 base::FilePath sandbox_candidate = exe_dir.AppendASCII("chrome-sandbox"); 126 base::FilePath sandbox_candidate = exe_dir.AppendASCII("chrome-sandbox");
245 if (base::PathExists(sandbox_candidate)) 127 if (base::PathExists(sandbox_candidate))
246 sandbox_binary = sandbox_candidate; 128 sandbox_binary = sandbox_candidate;
247 } 129 }
248 130
249 // In user-managed builds, including development builds, an environment 131 // In user-managed builds, including development builds, an environment
250 // variable is required to enable the sandbox. See 132 // variable is required to enable the sandbox. See
251 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment 133 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
252 struct stat st; 134 struct stat st;
253 if (sandbox_binary.empty() && stat(base::kProcSelfExe, &st) == 0 && 135 if (sandbox_binary.empty() && stat(base::kProcSelfExe, &st) == 0 &&
254 st.st_uid == getuid()) { 136 st.st_uid == getuid()) {
255 const char* devel_sandbox_path = GetDevelSandboxPath(); 137 const char* devel_sandbox_path = GetDevelSandboxPath();
256 if (devel_sandbox_path) { 138 if (devel_sandbox_path) {
257 sandbox_binary = base::FilePath(devel_sandbox_path); 139 sandbox_binary = base::FilePath(devel_sandbox_path);
258 } 140 }
259 } 141 }
260 142
261 return sandbox_binary; 143 return sandbox_binary;
262 } 144 }
263 145
264 void SetuidSandboxClient::PrependWrapper(base::CommandLine* cmd_line) { 146 void SetuidSandboxHost::PrependWrapper(base::CommandLine* cmd_line) {
265 std::string sandbox_binary(GetSandboxBinaryPath().value()); 147 std::string sandbox_binary(GetSandboxBinaryPath().value());
266 struct stat st; 148 struct stat st;
267 if (sandbox_binary.empty() || stat(sandbox_binary.c_str(), &st) != 0) { 149 if (sandbox_binary.empty() || stat(sandbox_binary.c_str(), &st) != 0) {
268 LOG(FATAL) << "The SUID sandbox helper binary is missing: " 150 LOG(FATAL) << "The SUID sandbox helper binary is missing: "
269 << sandbox_binary << " Aborting now. See " 151 << sandbox_binary << " Aborting now. See "
270 "https://code.google.com/p/chromium/wiki/" 152 "https://code.google.com/p/chromium/wiki/"
271 "LinuxSUIDSandboxDevelopment."; 153 "LinuxSUIDSandboxDevelopment.";
272 } 154 }
273 155
274 if (access(sandbox_binary.c_str(), X_OK) != 0 || (st.st_uid != 0) || 156 if (access(sandbox_binary.c_str(), X_OK) != 0 || (st.st_uid != 0) ||
275 ((st.st_mode & S_ISUID) == 0) || ((st.st_mode & S_IXOTH)) == 0) { 157 ((st.st_mode & S_ISUID) == 0) || ((st.st_mode & S_IXOTH)) == 0) {
276 LOG(FATAL) << "The SUID sandbox helper binary was found, but is not " 158 LOG(FATAL) << "The SUID sandbox helper binary was found, but is not "
277 "configured correctly. Rather than run without sandboxing " 159 "configured correctly. Rather than run without sandboxing "
278 "I'm aborting now. You need to make sure that " 160 "I'm aborting now. You need to make sure that "
279 << sandbox_binary << " is owned by root and has mode 4755."; 161 << sandbox_binary << " is owned by root and has mode 4755.";
280 } 162 }
281 163
282 cmd_line->PrependWrapper(sandbox_binary); 164 cmd_line->PrependWrapper(sandbox_binary);
283 } 165 }
284 166
285 void SetuidSandboxClient::SetupLaunchOptions( 167 void SetuidSandboxHost::SetupLaunchOptions(
286 base::LaunchOptions* options, 168 base::LaunchOptions* options,
287 base::FileHandleMappingVector* fds_to_remap, 169 base::FileHandleMappingVector* fds_to_remap,
288 base::ScopedFD* dummy_fd) { 170 base::ScopedFD* dummy_fd) {
289 DCHECK(options); 171 DCHECK(options);
290 DCHECK(fds_to_remap); 172 DCHECK(fds_to_remap);
291 173
292 // Launching a setuid binary requires PR_SET_NO_NEW_PRIVS to not be used. 174 // Launching a setuid binary requires PR_SET_NO_NEW_PRIVS to not be used.
293 options->allow_new_privs = true; 175 options->allow_new_privs = true;
294 UnsetExpectedEnvironmentVariables(&options->environ); 176 UnsetExpectedEnvironmentVariables(&options->environ);
295 177
296 // Set dummy_fd to the reading end of a closed pipe. 178 // Set dummy_fd to the reading end of a closed pipe.
297 int pipe_fds[2]; 179 int pipe_fds[2];
298 PCHECK(0 == pipe(pipe_fds)); 180 PCHECK(0 == pipe(pipe_fds));
299 PCHECK(0 == IGNORE_EINTR(close(pipe_fds[1]))); 181 PCHECK(0 == IGNORE_EINTR(close(pipe_fds[1])));
300 dummy_fd->reset(pipe_fds[0]); 182 dummy_fd->reset(pipe_fds[0]);
301 183
302 // We no longer need a dummy socket for discovering the child's PID, 184 // We no longer need a dummy socket for discovering the child's PID,
303 // but the sandbox is still hard-coded to expect a file descriptor at 185 // but the sandbox is still hard-coded to expect a file descriptor at
304 // kZygoteIdFd. Fixing this requires a sandbox API change. :( 186 // kZygoteIdFd. Fixing this requires a sandbox API change. :(
305 fds_to_remap->push_back(std::make_pair(dummy_fd->get(), kZygoteIdFd)); 187 fds_to_remap->push_back(std::make_pair(dummy_fd->get(), kZygoteIdFd));
306 } 188 }
307 189
308 void SetuidSandboxClient::SetupLaunchEnvironment() { 190 void SetuidSandboxHost::SetupLaunchEnvironment() {
309 SaveSUIDUnsafeEnvironmentVariables(env_); 191 SaveSUIDUnsafeEnvironmentVariables(env_.get());
310 SetSandboxAPIEnvironmentVariable(env_); 192 SetSandboxAPIEnvironmentVariable(env_.get());
311 } 193 }
312 194
313 } // namespace sandbox 195 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/linux/suid/client/setuid_sandbox_host.h ('k') | sandbox/linux/suid/client/setuid_sandbox_host_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698