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