Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // derived from process_util_linux.cc and process_util_mac.cc |
michael@0 | 6 | |
michael@0 | 7 | #include "base/process_util.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <sys/param.h> |
michael@0 | 10 | #include <sys/sysctl.h> |
michael@0 | 11 | #include <sys/wait.h> |
michael@0 | 12 | #if defined(OS_DRAGONFLY) || defined(OS_FREEBSD) |
michael@0 | 13 | #include <sys/user.h> |
michael@0 | 14 | #endif |
michael@0 | 15 | |
michael@0 | 16 | #include <ctype.h> |
michael@0 | 17 | #include <fcntl.h> |
michael@0 | 18 | #include <kvm.h> |
michael@0 | 19 | #include <unistd.h> |
michael@0 | 20 | |
michael@0 | 21 | #include <string> |
michael@0 | 22 | |
michael@0 | 23 | #include "base/debug_util.h" |
michael@0 | 24 | #include "base/eintr_wrapper.h" |
michael@0 | 25 | #include "base/file_util.h" |
michael@0 | 26 | #include "base/logging.h" |
michael@0 | 27 | #include "base/string_tokenizer.h" |
michael@0 | 28 | #include "base/string_util.h" |
michael@0 | 29 | |
michael@0 | 30 | #if defined(_POSIX_SPAWN) && _POSIX_SPAWN > 0 |
michael@0 | 31 | #define HAVE_POSIX_SPAWN 1 |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | /* |
michael@0 | 35 | * On platforms that are not gonk based, we fall back to an arbitrary |
michael@0 | 36 | * UID. This is generally the UID for user `nobody', albeit it is not |
michael@0 | 37 | * always the case. |
michael@0 | 38 | */ |
michael@0 | 39 | |
michael@0 | 40 | #if defined(OS_NETBSD) || defined(OS_OPENBSD) |
michael@0 | 41 | # define CHILD_UNPRIVILEGED_UID 32767 |
michael@0 | 42 | # define CHILD_UNPRIVILEGED_GID 32767 |
michael@0 | 43 | #else |
michael@0 | 44 | # define CHILD_UNPRIVILEGED_UID 65534 |
michael@0 | 45 | # define CHILD_UNPRIVILEGED_GID 65534 |
michael@0 | 46 | #endif |
michael@0 | 47 | |
michael@0 | 48 | #ifndef __dso_public |
michael@0 | 49 | # ifdef __exported |
michael@0 | 50 | # define __dso_public __exported |
michael@0 | 51 | # else |
michael@0 | 52 | # define __dso_public __attribute__((__visibility__("default"))) |
michael@0 | 53 | # endif |
michael@0 | 54 | #endif |
michael@0 | 55 | |
michael@0 | 56 | #ifdef HAVE_POSIX_SPAWN |
michael@0 | 57 | #include <spawn.h> |
michael@0 | 58 | extern "C" char **environ __dso_public; |
michael@0 | 59 | #endif |
michael@0 | 60 | |
michael@0 | 61 | namespace { |
michael@0 | 62 | |
michael@0 | 63 | enum ParsingState { |
michael@0 | 64 | KEY_NAME, |
michael@0 | 65 | KEY_VALUE |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | static mozilla::EnvironmentLog gProcessLog("MOZ_PROCESS_LOG"); |
michael@0 | 69 | |
michael@0 | 70 | } // namespace |
michael@0 | 71 | |
michael@0 | 72 | namespace base { |
michael@0 | 73 | |
michael@0 | 74 | #ifdef HAVE_POSIX_SPAWN |
michael@0 | 75 | |
michael@0 | 76 | void FreeEnvVarsArray(char* array[], int length) |
michael@0 | 77 | { |
michael@0 | 78 | for (int i = 0; i < length; i++) { |
michael@0 | 79 | free(array[i]); |
michael@0 | 80 | } |
michael@0 | 81 | delete[] array; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | bool LaunchApp(const std::vector<std::string>& argv, |
michael@0 | 85 | const file_handle_mapping_vector& fds_to_remap, |
michael@0 | 86 | bool wait, ProcessHandle* process_handle) { |
michael@0 | 87 | return LaunchApp(argv, fds_to_remap, environment_map(), |
michael@0 | 88 | wait, process_handle); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | bool LaunchApp(const std::vector<std::string>& argv, |
michael@0 | 92 | const file_handle_mapping_vector& fds_to_remap, |
michael@0 | 93 | const environment_map& env_vars_to_set, |
michael@0 | 94 | bool wait, ProcessHandle* process_handle, |
michael@0 | 95 | ProcessArchitecture arch) { |
michael@0 | 96 | return LaunchApp(argv, fds_to_remap, env_vars_to_set, |
michael@0 | 97 | PRIVILEGES_INHERIT, |
michael@0 | 98 | wait, process_handle); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | bool LaunchApp(const std::vector<std::string>& argv, |
michael@0 | 102 | const file_handle_mapping_vector& fds_to_remap, |
michael@0 | 103 | const environment_map& env_vars_to_set, |
michael@0 | 104 | ChildPrivileges privs, |
michael@0 | 105 | bool wait, ProcessHandle* process_handle, |
michael@0 | 106 | ProcessArchitecture arch) { |
michael@0 | 107 | bool retval = true; |
michael@0 | 108 | |
michael@0 | 109 | char* argv_copy[argv.size() + 1]; |
michael@0 | 110 | for (size_t i = 0; i < argv.size(); i++) { |
michael@0 | 111 | argv_copy[i] = const_cast<char*>(argv[i].c_str()); |
michael@0 | 112 | } |
michael@0 | 113 | argv_copy[argv.size()] = NULL; |
michael@0 | 114 | |
michael@0 | 115 | // Make sure we don't leak any FDs to the child process by marking all FDs |
michael@0 | 116 | // as close-on-exec. |
michael@0 | 117 | SetAllFDsToCloseOnExec(); |
michael@0 | 118 | |
michael@0 | 119 | // Copy environment to a new char array and add the variables |
michael@0 | 120 | // in env_vars_to_set. |
michael@0 | 121 | // Existing variables are overwritten by env_vars_to_set. |
michael@0 | 122 | int pos = 0; |
michael@0 | 123 | environment_map combined_env_vars = env_vars_to_set; |
michael@0 | 124 | while(environ[pos] != NULL) { |
michael@0 | 125 | std::string varString = environ[pos]; |
michael@0 | 126 | std::string varName = varString.substr(0, varString.find_first_of('=')); |
michael@0 | 127 | std::string varValue = varString.substr(varString.find_first_of('=') + 1); |
michael@0 | 128 | if (combined_env_vars.find(varName) == combined_env_vars.end()) { |
michael@0 | 129 | combined_env_vars[varName] = varValue; |
michael@0 | 130 | } |
michael@0 | 131 | pos++; |
michael@0 | 132 | } |
michael@0 | 133 | int varsLen = combined_env_vars.size() + 1; |
michael@0 | 134 | |
michael@0 | 135 | char** vars = new char*[varsLen]; |
michael@0 | 136 | int i = 0; |
michael@0 | 137 | for (environment_map::const_iterator it = combined_env_vars.begin(); |
michael@0 | 138 | it != combined_env_vars.end(); ++it) { |
michael@0 | 139 | std::string entry(it->first); |
michael@0 | 140 | entry += "="; |
michael@0 | 141 | entry += it->second; |
michael@0 | 142 | vars[i] = strdup(entry.c_str()); |
michael@0 | 143 | i++; |
michael@0 | 144 | } |
michael@0 | 145 | vars[i] = NULL; |
michael@0 | 146 | |
michael@0 | 147 | posix_spawn_file_actions_t file_actions; |
michael@0 | 148 | if (posix_spawn_file_actions_init(&file_actions) != 0) { |
michael@0 | 149 | FreeEnvVarsArray(vars, varsLen); |
michael@0 | 150 | return false; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | // Turn fds_to_remap array into a set of dup2 calls. |
michael@0 | 154 | for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin(); |
michael@0 | 155 | it != fds_to_remap.end(); |
michael@0 | 156 | ++it) { |
michael@0 | 157 | int src_fd = it->first; |
michael@0 | 158 | int dest_fd = it->second; |
michael@0 | 159 | |
michael@0 | 160 | if (src_fd == dest_fd) { |
michael@0 | 161 | int flags = fcntl(src_fd, F_GETFD); |
michael@0 | 162 | if (flags != -1) { |
michael@0 | 163 | fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC); |
michael@0 | 164 | } |
michael@0 | 165 | } else { |
michael@0 | 166 | if (posix_spawn_file_actions_adddup2(&file_actions, src_fd, dest_fd) != 0) { |
michael@0 | 167 | posix_spawn_file_actions_destroy(&file_actions); |
michael@0 | 168 | FreeEnvVarsArray(vars, varsLen); |
michael@0 | 169 | return false; |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | pid_t pid = 0; |
michael@0 | 175 | int spawn_succeeded = (posix_spawnp(&pid, |
michael@0 | 176 | argv_copy[0], |
michael@0 | 177 | &file_actions, |
michael@0 | 178 | NULL, |
michael@0 | 179 | argv_copy, |
michael@0 | 180 | vars) == 0); |
michael@0 | 181 | |
michael@0 | 182 | FreeEnvVarsArray(vars, varsLen); |
michael@0 | 183 | |
michael@0 | 184 | posix_spawn_file_actions_destroy(&file_actions); |
michael@0 | 185 | |
michael@0 | 186 | bool process_handle_valid = pid > 0; |
michael@0 | 187 | if (!spawn_succeeded || !process_handle_valid) { |
michael@0 | 188 | retval = false; |
michael@0 | 189 | } else { |
michael@0 | 190 | if (wait) |
michael@0 | 191 | HANDLE_EINTR(waitpid(pid, 0, 0)); |
michael@0 | 192 | |
michael@0 | 193 | if (process_handle) |
michael@0 | 194 | *process_handle = pid; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | return retval; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | bool LaunchApp(const CommandLine& cl, |
michael@0 | 201 | bool wait, bool start_hidden, ProcessHandle* process_handle) { |
michael@0 | 202 | // TODO(playmobil): Do we need to respect the start_hidden flag? |
michael@0 | 203 | file_handle_mapping_vector no_files; |
michael@0 | 204 | return LaunchApp(cl.argv(), no_files, wait, process_handle); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | void SetCurrentProcessPrivileges(ChildPrivileges privs) { |
michael@0 | 208 | |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | #else // no posix_spawn, use fork/exec |
michael@0 | 212 | |
michael@0 | 213 | bool LaunchApp(const std::vector<std::string>& argv, |
michael@0 | 214 | const file_handle_mapping_vector& fds_to_remap, |
michael@0 | 215 | bool wait, ProcessHandle* process_handle) { |
michael@0 | 216 | return LaunchApp(argv, fds_to_remap, environment_map(), |
michael@0 | 217 | wait, process_handle); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | bool LaunchApp(const std::vector<std::string>& argv, |
michael@0 | 221 | const file_handle_mapping_vector& fds_to_remap, |
michael@0 | 222 | const environment_map& env_vars_to_set, |
michael@0 | 223 | bool wait, ProcessHandle* process_handle, |
michael@0 | 224 | ProcessArchitecture arch) { |
michael@0 | 225 | return LaunchApp(argv, fds_to_remap, env_vars_to_set, |
michael@0 | 226 | PRIVILEGES_INHERIT, |
michael@0 | 227 | wait, process_handle); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | bool LaunchApp(const std::vector<std::string>& argv, |
michael@0 | 231 | const file_handle_mapping_vector& fds_to_remap, |
michael@0 | 232 | const environment_map& env_vars_to_set, |
michael@0 | 233 | ChildPrivileges privs, |
michael@0 | 234 | bool wait, ProcessHandle* process_handle, |
michael@0 | 235 | ProcessArchitecture arch) { |
michael@0 | 236 | scoped_array<char*> argv_cstr(new char*[argv.size() + 1]); |
michael@0 | 237 | // Illegal to allocate memory after fork and before execvp |
michael@0 | 238 | InjectiveMultimap fd_shuffle1, fd_shuffle2; |
michael@0 | 239 | fd_shuffle1.reserve(fds_to_remap.size()); |
michael@0 | 240 | fd_shuffle2.reserve(fds_to_remap.size()); |
michael@0 | 241 | |
michael@0 | 242 | pid_t pid = fork(); |
michael@0 | 243 | if (pid < 0) |
michael@0 | 244 | return false; |
michael@0 | 245 | |
michael@0 | 246 | if (pid == 0) { |
michael@0 | 247 | for (file_handle_mapping_vector::const_iterator |
michael@0 | 248 | it = fds_to_remap.begin(); it != fds_to_remap.end(); ++it) { |
michael@0 | 249 | fd_shuffle1.push_back(InjectionArc(it->first, it->second, false)); |
michael@0 | 250 | fd_shuffle2.push_back(InjectionArc(it->first, it->second, false)); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | if (!ShuffleFileDescriptors(&fd_shuffle1)) |
michael@0 | 254 | _exit(127); |
michael@0 | 255 | |
michael@0 | 256 | CloseSuperfluousFds(fd_shuffle2); |
michael@0 | 257 | |
michael@0 | 258 | for (size_t i = 0; i < argv.size(); i++) |
michael@0 | 259 | argv_cstr[i] = const_cast<char*>(argv[i].c_str()); |
michael@0 | 260 | argv_cstr[argv.size()] = NULL; |
michael@0 | 261 | |
michael@0 | 262 | SetCurrentProcessPrivileges(privs); |
michael@0 | 263 | |
michael@0 | 264 | for (environment_map::const_iterator it = env_vars_to_set.begin(); |
michael@0 | 265 | it != env_vars_to_set.end(); ++it) { |
michael@0 | 266 | if (setenv(it->first.c_str(), it->second.c_str(), 1/*overwrite*/)) |
michael@0 | 267 | _exit(127); |
michael@0 | 268 | } |
michael@0 | 269 | execv(argv_cstr[0], argv_cstr.get()); |
michael@0 | 270 | // if we get here, we're in serious trouble and should complain loudly |
michael@0 | 271 | DLOG(ERROR) << "FAILED TO exec() CHILD PROCESS, path: " << argv_cstr[0]; |
michael@0 | 272 | _exit(127); |
michael@0 | 273 | } else { |
michael@0 | 274 | gProcessLog.print("==> process %d launched child process %d\n", |
michael@0 | 275 | GetCurrentProcId(), pid); |
michael@0 | 276 | if (wait) |
michael@0 | 277 | HANDLE_EINTR(waitpid(pid, 0, 0)); |
michael@0 | 278 | |
michael@0 | 279 | if (process_handle) |
michael@0 | 280 | *process_handle = pid; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | return true; |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | bool LaunchApp(const CommandLine& cl, |
michael@0 | 287 | bool wait, bool start_hidden, |
michael@0 | 288 | ProcessHandle* process_handle) { |
michael@0 | 289 | file_handle_mapping_vector no_files; |
michael@0 | 290 | return LaunchApp(cl.argv(), no_files, wait, process_handle); |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | void SetCurrentProcessPrivileges(ChildPrivileges privs) { |
michael@0 | 294 | if (privs == PRIVILEGES_INHERIT) { |
michael@0 | 295 | return; |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | gid_t gid = CHILD_UNPRIVILEGED_GID; |
michael@0 | 299 | uid_t uid = CHILD_UNPRIVILEGED_UID; |
michael@0 | 300 | if (setgid(gid) != 0) { |
michael@0 | 301 | DLOG(ERROR) << "FAILED TO setgid() CHILD PROCESS"; |
michael@0 | 302 | _exit(127); |
michael@0 | 303 | } |
michael@0 | 304 | if (setuid(uid) != 0) { |
michael@0 | 305 | DLOG(ERROR) << "FAILED TO setuid() CHILD PROCESS"; |
michael@0 | 306 | _exit(127); |
michael@0 | 307 | } |
michael@0 | 308 | if (chdir("/") != 0) |
michael@0 | 309 | gProcessLog.print("==> could not chdir()\n"); |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | #endif |
michael@0 | 313 | |
michael@0 | 314 | NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name, |
michael@0 | 315 | const ProcessFilter* filter) |
michael@0 | 316 | { |
michael@0 | 317 | int numEntries; |
michael@0 | 318 | kvm_t *kvm; |
michael@0 | 319 | std::string exe(WideToASCII(executable_name)); |
michael@0 | 320 | |
michael@0 | 321 | #if defined(OS_DRAGONFLY) || defined(OS_FREEBSD) |
michael@0 | 322 | kvm = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); |
michael@0 | 323 | struct kinfo_proc* procs = kvm_getprocs(kvm, KERN_PROC_UID, getuid(), &numEntries); |
michael@0 | 324 | if (procs != NULL && numEntries > 0) { |
michael@0 | 325 | for (int i = 0; i < numEntries; i++) { |
michael@0 | 326 | # if defined(OS_DRAGONFLY) |
michael@0 | 327 | if (exe != procs[i].kp_comm) continue; |
michael@0 | 328 | if (filter && !filter->Includes(procs[i].kp_pid, procs[i].kp_ppid)) continue; |
michael@0 | 329 | ProcessEntry e; |
michael@0 | 330 | e.pid = procs[i].kp_pid; |
michael@0 | 331 | e.ppid = procs[i].kp_ppid; |
michael@0 | 332 | strlcpy(e.szExeFile, procs[i].kp_comm, sizeof e.szExeFile); |
michael@0 | 333 | content.push_back(e); |
michael@0 | 334 | # elif defined(OS_FREEBSD) |
michael@0 | 335 | if (exe != procs[i].ki_comm) continue; |
michael@0 | 336 | if (filter && !filter->Includes(procs[i].ki_pid, procs[i].ki_ppid)) continue; |
michael@0 | 337 | ProcessEntry e; |
michael@0 | 338 | e.pid = procs[i].ki_pid; |
michael@0 | 339 | e.ppid = procs[i].ki_ppid; |
michael@0 | 340 | strlcpy(e.szExeFile, procs[i].ki_comm, sizeof e.szExeFile); |
michael@0 | 341 | content.push_back(e); |
michael@0 | 342 | # endif |
michael@0 | 343 | #else |
michael@0 | 344 | kvm = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL); |
michael@0 | 345 | #if defined(OS_OPENBSD) |
michael@0 | 346 | struct kinfo_proc* procs = kvm_getprocs(kvm, KERN_PROC_UID, getuid(), sizeof(struct kinfo_proc), &numEntries); |
michael@0 | 347 | #else |
michael@0 | 348 | struct kinfo_proc2* procs = kvm_getproc2(kvm, KERN_PROC_UID, getuid(), sizeof(struct kinfo_proc2), &numEntries); |
michael@0 | 349 | #endif |
michael@0 | 350 | if (procs != NULL && numEntries > 0) { |
michael@0 | 351 | for (int i = 0; i < numEntries; i++) { |
michael@0 | 352 | if (exe != procs[i].p_comm) continue; |
michael@0 | 353 | if (filter && !filter->Includes(procs[i].p_pid, procs[i].p_ppid)) continue; |
michael@0 | 354 | ProcessEntry e; |
michael@0 | 355 | e.pid = procs[i].p_pid; |
michael@0 | 356 | e.ppid = procs[i].p_ppid; |
michael@0 | 357 | strlcpy(e.szExeFile, procs[i].p_comm, sizeof e.szExeFile); |
michael@0 | 358 | content.push_back(e); |
michael@0 | 359 | #endif |
michael@0 | 360 | } |
michael@0 | 361 | } |
michael@0 | 362 | nextEntry = 0; |
michael@0 | 363 | kvm_close(kvm); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | NamedProcessIterator::~NamedProcessIterator() { |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | const ProcessEntry* NamedProcessIterator::NextProcessEntry() { |
michael@0 | 370 | if (nextEntry >= content.size()) return NULL; |
michael@0 | 371 | return &content[nextEntry++]; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | } // namespace base |