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