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

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

Issue 877153005: sandbox: extract SetuidSandboxHost code from SetuidSandboxClient (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to rickyz feedback 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 (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
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() {
hidehiko 2015/02/04 15:40:05 nit, and Just-FYI: This pattern usually can be sim
mdempsky 2015/02/05 03:02:07 Yep, I was intending to do that in a followup CL,
139 base::Environment* environment(base::Environment::Create()); 65 base::Environment* environment(base::Environment::Create());
140 SetuidSandboxClient* sandbox_client(new SetuidSandboxClient); 66 SetuidSandboxClient* sandbox_client(new SetuidSandboxClient);
141 67
142 CHECK(environment); 68 CHECK(environment);
143 sandbox_client->env_ = environment; 69 sandbox_client->env_ = environment;
144 return sandbox_client; 70 return sandbox_client;
145 } 71 }
146 72
147 SetuidSandboxClient::SetuidSandboxClient() 73 SetuidSandboxClient::SetuidSandboxClient()
148 : env_(NULL), 74 : env_(NULL),
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 146 }
221 147
222 bool SetuidSandboxClient::IsInNewNETNamespace() const { 148 bool SetuidSandboxClient::IsInNewNETNamespace() const {
223 return env_->HasVar(kSandboxNETNSEnvironmentVarName); 149 return env_->HasVar(kSandboxNETNSEnvironmentVarName);
224 } 150 }
225 151
226 bool SetuidSandboxClient::IsSandboxed() const { 152 bool SetuidSandboxClient::IsSandboxed() const {
227 return sandboxed_; 153 return sandboxed_;
228 } 154 }
229 155
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 156 } // namespace sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698