blob: fc03cdd099e3dc16fc8459fc3d87e0a95e9f64f2 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
Ben Murdocha02191e2014-04-16 11:17:03 +01005#include "sandbox/linux/suid/client/setuid_sandbox_client.h"
6
Ben Murdoch0529e5d2014-04-24 10:50:13 +01007#include <fcntl.h>
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +01008#include <stdlib.h>
9#include <sys/socket.h>
Ben Murdocha02191e2014-04-16 11:17:03 +010010#include <sys/stat.h>
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include <sys/types.h>
12#include <sys/wait.h>
13#include <unistd.h>
14
Ben Murdocha02191e2014-04-16 11:17:03 +010015#include "base/command_line.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "base/environment.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010017#include "base/file_util.h"
18#include "base/files/file_path.h"
Ben Murdoch0529e5d2014-04-24 10:50:13 +010019#include "base/files/scoped_file.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020#include "base/logging.h"
Ben Murdoch0529e5d2014-04-24 10:50:13 +010021#include "base/macros.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000022#include "base/memory/scoped_ptr.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010023#include "base/path_service.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000024#include "base/posix/eintr_wrapper.h"
Ben Murdoch0529e5d2014-04-24 10:50:13 +010025#include "base/process/launch.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010026#include "base/process/process_metrics.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010027#include "base/strings/string_number_conversions.h"
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +000028#include "sandbox/linux/services/init_process_reaper.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029#include "sandbox/linux/suid/common/sandbox.h"
30#include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031
32namespace {
33
Ben Murdoch0529e5d2014-04-24 10:50:13 +010034bool IsFileSystemAccessDenied() {
35 base::ScopedFD self_exe(HANDLE_EINTR(open(base::kProcSelfExe, O_RDONLY)));
36 return !self_exe.is_valid();
37}
38
Torne (Richard Coles)58218062012-11-14 11:43:16 +000039// Set an environment variable that reflects the API version we expect from the
40// setuid sandbox. Old versions of the sandbox will ignore this.
41void SetSandboxAPIEnvironmentVariable(base::Environment* env) {
42 env->SetVar(sandbox::kSandboxEnvironmentApiRequest,
43 base::IntToString(sandbox::kSUIDSandboxApiNumber));
44}
45
Ben Murdoch0529e5d2014-04-24 10:50:13 +010046// Unset environment variables that are expected to be set by the setuid
47// sandbox. This is to allow nesting of one instance of the SUID sandbox
48// inside another.
49void UnsetExpectedEnvironmentVariables(base::EnvironmentMap* env_map) {
50 DCHECK(env_map);
51 const base::NativeEnvironmentString environment_vars[] = {
52 sandbox::kSandboxDescriptorEnvironmentVarName,
53 sandbox::kSandboxHelperPidEnvironmentVarName,
54 sandbox::kSandboxEnvironmentApiProvides,
55 sandbox::kSandboxPIDNSEnvironmentVarName,
56 sandbox::kSandboxNETNSEnvironmentVarName,
57 };
58
59 for (size_t i = 0; i < arraysize(environment_vars); ++i) {
60 // Setting values in EnvironmentMap to an empty-string will make
61 // sure that they get unset from the environment via AlterEnvironment().
62 (*env_map)[environment_vars[i]] = base::NativeEnvironmentString();
63 }
64}
65
Torne (Richard Coles)58218062012-11-14 11:43:16 +000066// Wrapper around a shared C function.
67// Returns the "saved" environment variable name corresponding to |envvar|
68// in a new string or NULL.
69std::string* CreateSavedVariableName(const char* env_var) {
70 char* const saved_env_var = SandboxSavedEnvironmentVariable(env_var);
71 if (!saved_env_var)
72 return NULL;
73 std::string* saved_env_var_copy = new std::string(saved_env_var);
74 // SandboxSavedEnvironmentVariable is the C function that we wrap and uses
75 // malloc() to allocate memory.
76 free(saved_env_var);
77 return saved_env_var_copy;
78}
79
80// The ELF loader will clear many environment variables so we save them to
81// different names here so that the SUID sandbox can resolve them for the
82// renderer.
83void SaveSUIDUnsafeEnvironmentVariables(base::Environment* env) {
84 for (unsigned i = 0; kSUIDUnsafeEnvironmentVariables[i]; ++i) {
85 const char* env_var = kSUIDUnsafeEnvironmentVariables[i];
86 // Get the saved environment variable corresponding to envvar.
87 scoped_ptr<std::string> saved_env_var(CreateSavedVariableName(env_var));
88 if (saved_env_var == NULL)
89 continue;
90
91 std::string value;
92 if (env->GetVar(env_var, &value))
93 env->SetVar(saved_env_var->c_str(), value);
94 else
95 env->UnSetVar(saved_env_var->c_str());
96 }
97}
98
99int GetHelperApi(base::Environment* env) {
100 std::string api_string;
101 int api_number = 0; // Assume API version 0 if no environment was found.
102 if (env->GetVar(sandbox::kSandboxEnvironmentApiProvides, &api_string) &&
103 !base::StringToInt(api_string, &api_number)) {
104 // It's an error if we could not convert the API number.
105 api_number = -1;
106 }
107 return api_number;
108}
109
110// Convert |var_name| from the environment |env| to an int.
111// Return -1 if the variable does not exist or the value cannot be converted.
112int EnvToInt(base::Environment* env, const char* var_name) {
113 std::string var_string;
114 int var_value = -1;
115 if (env->GetVar(var_name, &var_string) &&
116 !base::StringToInt(var_string, &var_value)) {
117 var_value = -1;
118 }
119 return var_value;
120}
121
122pid_t GetHelperPID(base::Environment* env) {
123 return EnvToInt(env, sandbox::kSandboxHelperPidEnvironmentVarName);
124}
125
126// Get the IPC file descriptor used to communicate with the setuid helper.
127int GetIPCDescriptor(base::Environment* env) {
128 return EnvToInt(env, sandbox::kSandboxDescriptorEnvironmentVarName);
129}
130
Ben Murdocha02191e2014-04-16 11:17:03 +0100131const char* GetDevelSandboxPath() {
132 return getenv("CHROME_DEVEL_SANDBOX");
133}
134
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000135} // namespace
136
137namespace sandbox {
138
139SetuidSandboxClient* SetuidSandboxClient::Create() {
140 base::Environment* environment(base::Environment::Create());
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100141 SetuidSandboxClient* sandbox_client(new SetuidSandboxClient);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000142
143 CHECK(environment);
144 sandbox_client->env_ = environment;
145 return sandbox_client;
146}
147
148SetuidSandboxClient::SetuidSandboxClient()
149 : env_(NULL),
150 sandboxed_(false) {
151}
152
153SetuidSandboxClient::~SetuidSandboxClient() {
154 delete env_;
155}
156
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100157void SetuidSandboxClient::CloseDummyFile() {
158 // When we're launched through the setuid sandbox, SetupLaunchOptions
159 // arranges for kZygoteIdFd to be a dummy file descriptor to satisfy an
160 // ancient setuid sandbox ABI requirement. However, the descriptor is no
161 // longer needed, so we can simply close it right away now.
162 CHECK(IsSuidSandboxChild());
163
164 // Sanity check that kZygoteIdFd refers to a pipe.
165 struct stat st;
166 PCHECK(0 == fstat(kZygoteIdFd, &st));
167 CHECK(S_ISFIFO(st.st_mode));
168
169 PCHECK(0 == IGNORE_EINTR(close(kZygoteIdFd)));
170}
171
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000172bool SetuidSandboxClient::ChrootMe() {
173 int ipc_fd = GetIPCDescriptor(env_);
174
175 if (ipc_fd < 0) {
176 LOG(ERROR) << "Failed to obtain the sandbox IPC descriptor";
177 return false;
178 }
179
180 if (HANDLE_EINTR(write(ipc_fd, &kMsgChrootMe, 1)) != 1) {
181 PLOG(ERROR) << "Failed to write to chroot pipe";
182 return false;
183 }
184
185 // We need to reap the chroot helper process in any event.
186 pid_t helper_pid = GetHelperPID(env_);
187 // If helper_pid is -1 we wait for any child.
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000188 if (HANDLE_EINTR(waitpid(helper_pid, NULL, 0)) < 0) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000189 PLOG(ERROR) << "Failed to wait for setuid helper to die";
190 return false;
191 }
192
193 char reply;
194 if (HANDLE_EINTR(read(ipc_fd, &reply, 1)) != 1) {
195 PLOG(ERROR) << "Failed to read from chroot pipe";
196 return false;
197 }
198
199 if (reply != kMsgChrootSuccessful) {
200 LOG(ERROR) << "Error code reply from chroot helper";
201 return false;
202 }
203
204 // We now consider ourselves "fully sandboxed" as far as the
205 // setuid sandbox is concerned.
Ben Murdoch0529e5d2014-04-24 10:50:13 +0100206 CHECK(IsFileSystemAccessDenied());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000207 sandboxed_ = true;
208 return true;
209}
210
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +0000211bool SetuidSandboxClient::CreateInitProcessReaper(
212 base::Closure* post_fork_parent_callback) {
213 return sandbox::CreateInitProcessReaper(post_fork_parent_callback);
214}
215
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000216bool SetuidSandboxClient::IsSuidSandboxUpToDate() const {
217 return GetHelperApi(env_) == kSUIDSandboxApiNumber;
218}
219
220bool SetuidSandboxClient::IsSuidSandboxChild() const {
221 return GetIPCDescriptor(env_) >= 0;
222}
223
224bool SetuidSandboxClient::IsInNewPIDNamespace() const {
225 return env_->HasVar(kSandboxPIDNSEnvironmentVarName);
226}
227
228bool SetuidSandboxClient::IsInNewNETNamespace() const {
229 return env_->HasVar(kSandboxNETNSEnvironmentVarName);
230}
231
232bool SetuidSandboxClient::IsSandboxed() const {
233 return sandboxed_;
234}
235
Ben Murdocha02191e2014-04-16 11:17:03 +0100236// Check if CHROME_DEVEL_SANDBOX is set but empty. This currently disables
237// the setuid sandbox. TODO(jln): fix this (crbug.com/245376).
238bool SetuidSandboxClient::IsDisabledViaEnvironment() {
239 const char* devel_sandbox_path = GetDevelSandboxPath();
240 if (devel_sandbox_path && '\0' == *devel_sandbox_path) {
241 return true;
242 }
243 return false;
244}
245
246base::FilePath SetuidSandboxClient::GetSandboxBinaryPath() {
247 base::FilePath sandbox_binary;
248 base::FilePath exe_dir;
249 if (PathService::Get(base::DIR_EXE, &exe_dir)) {
250 base::FilePath sandbox_candidate = exe_dir.AppendASCII("chrome-sandbox");
251 if (base::PathExists(sandbox_candidate))
252 sandbox_binary = sandbox_candidate;
253 }
254
255 // In user-managed builds, including development builds, an environment
256 // variable is required to enable the sandbox. See
257 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
258 struct stat st;
259 if (sandbox_binary.empty() && stat(base::kProcSelfExe, &st) == 0 &&
260 st.st_uid == getuid()) {
261 const char* devel_sandbox_path = GetDevelSandboxPath();
262 if (devel_sandbox_path) {
263 sandbox_binary = base::FilePath(devel_sandbox_path);
264 }
265 }
266
267 return sandbox_binary;
268}
269
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100270void SetuidSandboxClient::PrependWrapper(base::CommandLine* cmd_line) {
Ben Murdocha02191e2014-04-16 11:17:03 +0100271 std::string sandbox_binary(GetSandboxBinaryPath().value());
272 struct stat st;
273 if (sandbox_binary.empty() || stat(sandbox_binary.c_str(), &st) != 0) {
274 LOG(FATAL) << "The SUID sandbox helper binary is missing: "
275 << sandbox_binary << " Aborting now. See "
276 "https://code.google.com/p/chromium/wiki/"
277 "LinuxSUIDSandboxDevelopment.";
278 }
279
280 if (access(sandbox_binary.c_str(), X_OK) != 0 || (st.st_uid != 0) ||
281 ((st.st_mode & S_ISUID) == 0) || ((st.st_mode & S_IXOTH)) == 0) {
282 LOG(FATAL) << "The SUID sandbox helper binary was found, but is not "
283 "configured correctly. Rather than run without sandboxing "
284 "I'm aborting now. You need to make sure that "
285 << sandbox_binary << " is owned by root and has mode 4755.";
Ben Murdoch0529e5d2014-04-24 10:50:13 +0100286 }
Ben Murdocha02191e2014-04-16 11:17:03 +0100287
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100288 cmd_line->PrependWrapper(sandbox_binary);
289}
Ben Murdoch0529e5d2014-04-24 10:50:13 +0100290
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100291void SetuidSandboxClient::SetupLaunchOptions(
292 base::LaunchOptions* options,
293 base::FileHandleMappingVector* fds_to_remap,
294 base::ScopedFD* dummy_fd) {
295 DCHECK(options);
296 DCHECK(fds_to_remap);
297
298 // Launching a setuid binary requires PR_SET_NO_NEW_PRIVS to not be used.
299 options->allow_new_privs = true;
300 UnsetExpectedEnvironmentVariables(&options->environ);
301
302 // Set dummy_fd to the reading end of a closed pipe.
303 int pipe_fds[2];
304 PCHECK(0 == pipe(pipe_fds));
305 PCHECK(0 == IGNORE_EINTR(close(pipe_fds[1])));
306 dummy_fd->reset(pipe_fds[0]);
307
308 // We no longer need a dummy socket for discovering the child's PID,
309 // but the sandbox is still hard-coded to expect a file descriptor at
310 // kZygoteIdFd. Fixing this requires a sandbox API change. :(
311 fds_to_remap->push_back(std::make_pair(dummy_fd->get(), kZygoteIdFd));
Ben Murdocha02191e2014-04-16 11:17:03 +0100312}
313
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000314void SetuidSandboxClient::SetupLaunchEnvironment() {
315 SaveSUIDUnsafeEnvironmentVariables(env_);
316 SetSandboxAPIEnvironmentVariable(env_);
317}
318
319} // namespace sandbox