blob: 8feec040e09152214666432401f1e8887318f5e6 [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
7#include <sys/stat.h>
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include <sys/types.h>
9#include <sys/wait.h>
10#include <unistd.h>
11
Ben Murdocha02191e2014-04-16 11:17:03 +010012#include "base/command_line.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "base/environment.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010014#include "base/file_util.h"
15#include "base/files/file_path.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "base/logging.h"
17#include "base/memory/scoped_ptr.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010018#include "base/path_service.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019#include "base/posix/eintr_wrapper.h"
Ben Murdocha02191e2014-04-16 11:17:03 +010020#include "base/process/process_metrics.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010021#include "base/strings/string_number_conversions.h"
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +000022#include "sandbox/linux/services/init_process_reaper.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023#include "sandbox/linux/suid/common/sandbox.h"
24#include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025
26namespace {
27
28// Set an environment variable that reflects the API version we expect from the
29// setuid sandbox. Old versions of the sandbox will ignore this.
30void SetSandboxAPIEnvironmentVariable(base::Environment* env) {
31 env->SetVar(sandbox::kSandboxEnvironmentApiRequest,
32 base::IntToString(sandbox::kSUIDSandboxApiNumber));
33}
34
35// Wrapper around a shared C function.
36// Returns the "saved" environment variable name corresponding to |envvar|
37// in a new string or NULL.
38std::string* CreateSavedVariableName(const char* env_var) {
39 char* const saved_env_var = SandboxSavedEnvironmentVariable(env_var);
40 if (!saved_env_var)
41 return NULL;
42 std::string* saved_env_var_copy = new std::string(saved_env_var);
43 // SandboxSavedEnvironmentVariable is the C function that we wrap and uses
44 // malloc() to allocate memory.
45 free(saved_env_var);
46 return saved_env_var_copy;
47}
48
49// The ELF loader will clear many environment variables so we save them to
50// different names here so that the SUID sandbox can resolve them for the
51// renderer.
52void SaveSUIDUnsafeEnvironmentVariables(base::Environment* env) {
53 for (unsigned i = 0; kSUIDUnsafeEnvironmentVariables[i]; ++i) {
54 const char* env_var = kSUIDUnsafeEnvironmentVariables[i];
55 // Get the saved environment variable corresponding to envvar.
56 scoped_ptr<std::string> saved_env_var(CreateSavedVariableName(env_var));
57 if (saved_env_var == NULL)
58 continue;
59
60 std::string value;
61 if (env->GetVar(env_var, &value))
62 env->SetVar(saved_env_var->c_str(), value);
63 else
64 env->UnSetVar(saved_env_var->c_str());
65 }
66}
67
68int GetHelperApi(base::Environment* env) {
69 std::string api_string;
70 int api_number = 0; // Assume API version 0 if no environment was found.
71 if (env->GetVar(sandbox::kSandboxEnvironmentApiProvides, &api_string) &&
72 !base::StringToInt(api_string, &api_number)) {
73 // It's an error if we could not convert the API number.
74 api_number = -1;
75 }
76 return api_number;
77}
78
79// Convert |var_name| from the environment |env| to an int.
80// Return -1 if the variable does not exist or the value cannot be converted.
81int EnvToInt(base::Environment* env, const char* var_name) {
82 std::string var_string;
83 int var_value = -1;
84 if (env->GetVar(var_name, &var_string) &&
85 !base::StringToInt(var_string, &var_value)) {
86 var_value = -1;
87 }
88 return var_value;
89}
90
91pid_t GetHelperPID(base::Environment* env) {
92 return EnvToInt(env, sandbox::kSandboxHelperPidEnvironmentVarName);
93}
94
95// Get the IPC file descriptor used to communicate with the setuid helper.
96int GetIPCDescriptor(base::Environment* env) {
97 return EnvToInt(env, sandbox::kSandboxDescriptorEnvironmentVarName);
98}
99
Ben Murdocha02191e2014-04-16 11:17:03 +0100100const char* GetDevelSandboxPath() {
101 return getenv("CHROME_DEVEL_SANDBOX");
102}
103
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000104} // namespace
105
106namespace sandbox {
107
108SetuidSandboxClient* SetuidSandboxClient::Create() {
109 base::Environment* environment(base::Environment::Create());
110 SetuidSandboxClient* sandbox_client(new(SetuidSandboxClient));
111
112 CHECK(environment);
113 sandbox_client->env_ = environment;
114 return sandbox_client;
115}
116
117SetuidSandboxClient::SetuidSandboxClient()
118 : env_(NULL),
119 sandboxed_(false) {
120}
121
122SetuidSandboxClient::~SetuidSandboxClient() {
123 delete env_;
124}
125
126bool SetuidSandboxClient::ChrootMe() {
127 int ipc_fd = GetIPCDescriptor(env_);
128
129 if (ipc_fd < 0) {
130 LOG(ERROR) << "Failed to obtain the sandbox IPC descriptor";
131 return false;
132 }
133
134 if (HANDLE_EINTR(write(ipc_fd, &kMsgChrootMe, 1)) != 1) {
135 PLOG(ERROR) << "Failed to write to chroot pipe";
136 return false;
137 }
138
139 // We need to reap the chroot helper process in any event.
140 pid_t helper_pid = GetHelperPID(env_);
141 // If helper_pid is -1 we wait for any child.
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000142 if (HANDLE_EINTR(waitpid(helper_pid, NULL, 0)) < 0) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000143 PLOG(ERROR) << "Failed to wait for setuid helper to die";
144 return false;
145 }
146
147 char reply;
148 if (HANDLE_EINTR(read(ipc_fd, &reply, 1)) != 1) {
149 PLOG(ERROR) << "Failed to read from chroot pipe";
150 return false;
151 }
152
153 if (reply != kMsgChrootSuccessful) {
154 LOG(ERROR) << "Error code reply from chroot helper";
155 return false;
156 }
157
158 // We now consider ourselves "fully sandboxed" as far as the
159 // setuid sandbox is concerned.
160 sandboxed_ = true;
161 return true;
162}
163
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +0000164bool SetuidSandboxClient::CreateInitProcessReaper(
165 base::Closure* post_fork_parent_callback) {
166 return sandbox::CreateInitProcessReaper(post_fork_parent_callback);
167}
168
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000169bool SetuidSandboxClient::IsSuidSandboxUpToDate() const {
170 return GetHelperApi(env_) == kSUIDSandboxApiNumber;
171}
172
173bool SetuidSandboxClient::IsSuidSandboxChild() const {
174 return GetIPCDescriptor(env_) >= 0;
175}
176
177bool SetuidSandboxClient::IsInNewPIDNamespace() const {
178 return env_->HasVar(kSandboxPIDNSEnvironmentVarName);
179}
180
181bool SetuidSandboxClient::IsInNewNETNamespace() const {
182 return env_->HasVar(kSandboxNETNSEnvironmentVarName);
183}
184
185bool SetuidSandboxClient::IsSandboxed() const {
186 return sandboxed_;
187}
188
Ben Murdocha02191e2014-04-16 11:17:03 +0100189// Check if CHROME_DEVEL_SANDBOX is set but empty. This currently disables
190// the setuid sandbox. TODO(jln): fix this (crbug.com/245376).
191bool SetuidSandboxClient::IsDisabledViaEnvironment() {
192 const char* devel_sandbox_path = GetDevelSandboxPath();
193 if (devel_sandbox_path && '\0' == *devel_sandbox_path) {
194 return true;
195 }
196 return false;
197}
198
199base::FilePath SetuidSandboxClient::GetSandboxBinaryPath() {
200 base::FilePath sandbox_binary;
201 base::FilePath exe_dir;
202 if (PathService::Get(base::DIR_EXE, &exe_dir)) {
203 base::FilePath sandbox_candidate = exe_dir.AppendASCII("chrome-sandbox");
204 if (base::PathExists(sandbox_candidate))
205 sandbox_binary = sandbox_candidate;
206 }
207
208 // In user-managed builds, including development builds, an environment
209 // variable is required to enable the sandbox. See
210 // http://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment
211 struct stat st;
212 if (sandbox_binary.empty() && stat(base::kProcSelfExe, &st) == 0 &&
213 st.st_uid == getuid()) {
214 const char* devel_sandbox_path = GetDevelSandboxPath();
215 if (devel_sandbox_path) {
216 sandbox_binary = base::FilePath(devel_sandbox_path);
217 }
218 }
219
220 return sandbox_binary;
221}
222
223void SetuidSandboxClient::PrependWrapper(base::CommandLine* cmd_line) {
224 DCHECK(cmd_line);
225 std::string sandbox_binary(GetSandboxBinaryPath().value());
226 struct stat st;
227 if (sandbox_binary.empty() || stat(sandbox_binary.c_str(), &st) != 0) {
228 LOG(FATAL) << "The SUID sandbox helper binary is missing: "
229 << sandbox_binary << " Aborting now. See "
230 "https://code.google.com/p/chromium/wiki/"
231 "LinuxSUIDSandboxDevelopment.";
232 }
233
234 if (access(sandbox_binary.c_str(), X_OK) != 0 || (st.st_uid != 0) ||
235 ((st.st_mode & S_ISUID) == 0) || ((st.st_mode & S_IXOTH)) == 0) {
236 LOG(FATAL) << "The SUID sandbox helper binary was found, but is not "
237 "configured correctly. Rather than run without sandboxing "
238 "I'm aborting now. You need to make sure that "
239 << sandbox_binary << " is owned by root and has mode 4755.";
240
241 } else {
242 cmd_line->PrependWrapper(sandbox_binary);
243 }
244}
245
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000246void SetuidSandboxClient::SetupLaunchEnvironment() {
247 SaveSUIDUnsafeEnvironmentVariables(env_);
248 SetSandboxAPIEnvironmentVariable(env_);
249}
250
251} // namespace sandbox