1 //===-- ProcessLauncherLinux.cpp --------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 10 #include "lldb/Host/Host.h" 11 #include "lldb/Host/HostProcess.h" 12 #include "lldb/Host/Pipe.h" 13 #include "lldb/Host/ProcessLaunchInfo.h" 14 #include "lldb/Utility/FileSpec.h" 15 #include "lldb/Utility/Log.h" 16 #include "llvm/Support/Errno.h" 17 18 #include <limits.h> 19 #include <sys/ptrace.h> 20 #include <sys/wait.h> 21 #include <unistd.h> 22 23 #include <sstream> 24 #include <csignal> 25 26 #ifdef __ANDROID__ 27 #include <android/api-level.h> 28 #define PT_TRACE_ME PTRACE_TRACEME 29 #endif 30 31 #if defined(__ANDROID_API__) && __ANDROID_API__ < 15 32 #include <linux/personality.h> 33 #elif defined(__linux__) 34 #include <sys/personality.h> 35 #endif 36 37 using namespace lldb; 38 using namespace lldb_private; 39 40 static void FixupEnvironment(Environment &env) { 41 #ifdef __ANDROID__ 42 // If there is no PATH variable specified inside the environment then set the 43 // path to /system/bin. It is required because the default path used by 44 // execve() is wrong on android. 45 env.try_emplace("PATH", "/system/bin"); 46 #endif 47 } 48 49 static void LLVM_ATTRIBUTE_NORETURN ExitWithError(int error_fd, 50 const char *operation) { 51 int err = errno; 52 llvm::raw_fd_ostream os(error_fd, true); 53 os << operation << " failed: " << llvm::sys::StrError(err); 54 os.flush(); 55 _exit(1); 56 } 57 58 static void DisableASLRIfRequested(int error_fd, const ProcessLaunchInfo &info) { 59 #if defined(__linux__) 60 if (info.GetFlags().Test(lldb::eLaunchFlagDisableASLR)) { 61 const unsigned long personality_get_current = 0xffffffff; 62 int value = personality(personality_get_current); 63 if (value == -1) 64 ExitWithError(error_fd, "personality get"); 65 66 value = personality(ADDR_NO_RANDOMIZE | value); 67 if (value == -1) 68 ExitWithError(error_fd, "personality set"); 69 } 70 #endif 71 } 72 73 static void DupDescriptor(int error_fd, const FileSpec &file_spec, int fd, 74 int flags) { 75 int target_fd = ::open(file_spec.GetCString(), flags, 0666); 76 77 if (target_fd == -1) 78 ExitWithError(error_fd, "DupDescriptor-open"); 79 80 if (target_fd == fd) 81 return; 82 83 if (::dup2(target_fd, fd) == -1) 84 ExitWithError(error_fd, "DupDescriptor-dup2"); 85 86 ::close(target_fd); 87 return; 88 } 89 90 static void LLVM_ATTRIBUTE_NORETURN ChildFunc(int error_fd, 91 const ProcessLaunchInfo &info) { 92 if (info.GetFlags().Test(eLaunchFlagLaunchInSeparateProcessGroup)) { 93 if (setpgid(0, 0) != 0) 94 ExitWithError(error_fd, "setpgid"); 95 } 96 97 for (size_t i = 0; i < info.GetNumFileActions(); ++i) { 98 const FileAction &action = *info.GetFileActionAtIndex(i); 99 switch (action.GetAction()) { 100 case FileAction::eFileActionClose: 101 if (close(action.GetFD()) != 0) 102 ExitWithError(error_fd, "close"); 103 break; 104 case FileAction::eFileActionDuplicate: 105 if (dup2(action.GetFD(), action.GetActionArgument()) == -1) 106 ExitWithError(error_fd, "dup2"); 107 break; 108 case FileAction::eFileActionOpen: 109 DupDescriptor(error_fd, action.GetFileSpec(), action.GetFD(), 110 action.GetActionArgument()); 111 break; 112 case FileAction::eFileActionNone: 113 break; 114 } 115 } 116 117 const char **argv = info.GetArguments().GetConstArgumentVector(); 118 119 // Change working directory 120 if (info.GetWorkingDirectory() && 121 0 != ::chdir(info.GetWorkingDirectory().GetCString())) 122 ExitWithError(error_fd, "chdir"); 123 124 DisableASLRIfRequested(error_fd, info); 125 Environment env = info.GetEnvironment(); 126 FixupEnvironment(env); 127 Environment::Envp envp = env.getEnvp(); 128 129 // Clear the signal mask to prevent the child from being affected by any 130 // masking done by the parent. 131 sigset_t set; 132 if (sigemptyset(&set) != 0 || 133 pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0) 134 ExitWithError(error_fd, "pthread_sigmask"); 135 136 if (info.GetFlags().Test(eLaunchFlagDebug)) { 137 // Do not inherit setgid powers. 138 if (setgid(getgid()) != 0) 139 ExitWithError(error_fd, "setgid"); 140 141 // HACK: 142 // Close everything besides stdin, stdout, and stderr that has no file 143 // action to avoid leaking. Only do this when debugging, as elsewhere we 144 // actually rely on passing open descriptors to child processes. 145 for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) 146 if (!info.GetFileActionForFD(fd) && fd != error_fd) 147 close(fd); 148 149 // Start tracing this child that is about to exec. 150 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1) 151 ExitWithError(error_fd, "ptrace"); 152 } 153 154 // Execute. We should never return... 155 execve(argv[0], const_cast<char *const *>(argv), envp); 156 157 #if defined(__linux__) 158 if (errno == ETXTBSY) { 159 // On android M and earlier we can get this error because the adb daemon 160 // can hold a write handle on the executable even after it has finished 161 // uploading it. This state lasts only a short time and happens only when 162 // there are many concurrent adb commands being issued, such as when 163 // running the test suite. (The file remains open when someone does an "adb 164 // shell" command in the fork() child before it has had a chance to exec.) 165 // Since this state should clear up quickly, wait a while and then give it 166 // one more go. 167 usleep(50000); 168 execve(argv[0], const_cast<char *const *>(argv), envp); 169 } 170 #endif 171 172 // ...unless exec fails. In which case we definitely need to end the child 173 // here. 174 ExitWithError(error_fd, "execve"); 175 } 176 177 HostProcess 178 ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info, 179 Status &error) { 180 char exe_path[PATH_MAX]; 181 launch_info.GetExecutableFile().GetPath(exe_path, sizeof(exe_path)); 182 183 // A pipe used by the child process to report errors. 184 PipePosix pipe; 185 const bool child_processes_inherit = false; 186 error = pipe.CreateNew(child_processes_inherit); 187 if (error.Fail()) 188 return HostProcess(); 189 190 ::pid_t pid = ::fork(); 191 if (pid == -1) { 192 // Fork failed 193 error.SetErrorStringWithFormatv("Fork failed with error message: {0}", 194 llvm::sys::StrError()); 195 return HostProcess(LLDB_INVALID_PROCESS_ID); 196 } 197 if (pid == 0) { 198 // child process 199 pipe.CloseReadFileDescriptor(); 200 ChildFunc(pipe.ReleaseWriteFileDescriptor(), launch_info); 201 } 202 203 // parent process 204 205 pipe.CloseWriteFileDescriptor(); 206 char buf[1000]; 207 int r = read(pipe.GetReadFileDescriptor(), buf, sizeof buf); 208 209 if (r == 0) 210 return HostProcess(pid); // No error. We're done. 211 212 error.SetErrorString(buf); 213 214 waitpid(pid, nullptr, 0); 215 216 return HostProcess(); 217 } 218