Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 5 | #include "sandbox/linux/suid/client/setuid_sandbox_client.h" |
| 6 | |
| 7 | #include <sys/stat.h> |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/wait.h> |
| 10 | #include <unistd.h> |
| 11 | |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 12 | #include "base/command_line.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "base/environment.h" |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 14 | #include "base/file_util.h" |
| 15 | #include "base/files/file_path.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 16 | #include "base/logging.h" |
| 17 | #include "base/memory/scoped_ptr.h" |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 18 | #include "base/path_service.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | #include "base/posix/eintr_wrapper.h" |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 20 | #include "base/process/process_metrics.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 22 | #include "sandbox/linux/services/init_process_reaper.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | #include "sandbox/linux/suid/common/sandbox.h" |
| 24 | #include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 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. |
| 30 | void 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. |
| 38 | std::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. |
| 52 | void 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 | |
| 68 | int 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. |
| 81 | int 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 | |
| 91 | pid_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. |
| 96 | int GetIPCDescriptor(base::Environment* env) { |
| 97 | return EnvToInt(env, sandbox::kSandboxDescriptorEnvironmentVarName); |
| 98 | } |
| 99 | |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 100 | const char* GetDevelSandboxPath() { |
| 101 | return getenv("CHROME_DEVEL_SANDBOX"); |
| 102 | } |
| 103 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 104 | } // namespace |
| 105 | |
| 106 | namespace sandbox { |
| 107 | |
| 108 | SetuidSandboxClient* 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 | |
| 117 | SetuidSandboxClient::SetuidSandboxClient() |
| 118 | : env_(NULL), |
| 119 | sandboxed_(false) { |
| 120 | } |
| 121 | |
| 122 | SetuidSandboxClient::~SetuidSandboxClient() { |
| 123 | delete env_; |
| 124 | } |
| 125 | |
| 126 | bool 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) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 142 | if (HANDLE_EINTR(waitpid(helper_pid, NULL, 0)) < 0) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 143 | 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) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 164 | bool SetuidSandboxClient::CreateInitProcessReaper( |
| 165 | base::Closure* post_fork_parent_callback) { |
| 166 | return sandbox::CreateInitProcessReaper(post_fork_parent_callback); |
| 167 | } |
| 168 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 169 | bool SetuidSandboxClient::IsSuidSandboxUpToDate() const { |
| 170 | return GetHelperApi(env_) == kSUIDSandboxApiNumber; |
| 171 | } |
| 172 | |
| 173 | bool SetuidSandboxClient::IsSuidSandboxChild() const { |
| 174 | return GetIPCDescriptor(env_) >= 0; |
| 175 | } |
| 176 | |
| 177 | bool SetuidSandboxClient::IsInNewPIDNamespace() const { |
| 178 | return env_->HasVar(kSandboxPIDNSEnvironmentVarName); |
| 179 | } |
| 180 | |
| 181 | bool SetuidSandboxClient::IsInNewNETNamespace() const { |
| 182 | return env_->HasVar(kSandboxNETNSEnvironmentVarName); |
| 183 | } |
| 184 | |
| 185 | bool SetuidSandboxClient::IsSandboxed() const { |
| 186 | return sandboxed_; |
| 187 | } |
| 188 | |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame^] | 189 | // Check if CHROME_DEVEL_SANDBOX is set but empty. This currently disables |
| 190 | // the setuid sandbox. TODO(jln): fix this (crbug.com/245376). |
| 191 | bool 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 | |
| 199 | base::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 | |
| 223 | void 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 246 | void SetuidSandboxClient::SetupLaunchEnvironment() { |
| 247 | SaveSUIDUnsafeEnvironmentVariables(env_); |
| 248 | SetSandboxAPIEnvironmentVariable(env_); |
| 249 | } |
| 250 | |
| 251 | } // namespace sandbox |