OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/posix/process_info.h" |
| 16 |
| 17 #include <string.h> |
| 18 |
| 19 #include "base/logging.h" |
| 20 #include "base/mac/mach_logging.h" |
| 21 |
| 22 namespace crashpad { |
| 23 |
| 24 ProcessInfo::ProcessInfo() : kern_proc_info_(), initialized_() { |
| 25 } |
| 26 |
| 27 ProcessInfo::~ProcessInfo() { |
| 28 } |
| 29 |
| 30 bool ProcessInfo::Initialize(pid_t pid) { |
| 31 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
| 32 |
| 33 int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; |
| 34 size_t len = sizeof(kern_proc_info_); |
| 35 if (sysctl(mib, arraysize(mib), &kern_proc_info_, &len, nullptr, 0) != 0) { |
| 36 PLOG(ERROR) << "sysctl for pid " << pid; |
| 37 return false; |
| 38 } |
| 39 |
| 40 // This sysctl does not return an error if the pid was not found. 10.9.5 |
| 41 // xnu-2422.115.4/bsd/kern/kern_sysctl.c sysctl_prochandle() calls |
| 42 // xnu-2422.115.4/bsd/kern/kern_proc.c proc_iterate(), which provides no |
| 43 // indication of whether anything was done. To catch this, check that the PID |
| 44 // has changed from the 0 value it was given when initialized by the |
| 45 // constructor. |
| 46 if (kern_proc_info_.kp_proc.p_pid == 0) { |
| 47 LOG(WARNING) << "pid " << pid << " not found"; |
| 48 return false; |
| 49 } |
| 50 |
| 51 DCHECK_EQ(kern_proc_info_.kp_proc.p_pid, pid); |
| 52 |
| 53 INITIALIZATION_STATE_SET_VALID(initialized_); |
| 54 return true; |
| 55 } |
| 56 |
| 57 bool ProcessInfo::InitializeFromTask(task_t task) { |
| 58 pid_t pid; |
| 59 kern_return_t kr = pid_for_task(task, &pid); |
| 60 if (kr != KERN_SUCCESS) { |
| 61 MACH_LOG(ERROR, kr) << "pid_for_task"; |
| 62 return false; |
| 63 } |
| 64 |
| 65 return Initialize(pid); |
| 66 } |
| 67 |
| 68 pid_t ProcessInfo::ProcessID() const { |
| 69 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 70 return kern_proc_info_.kp_proc.p_pid; |
| 71 } |
| 72 |
| 73 pid_t ProcessInfo::ParentProcessID() const { |
| 74 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 75 return kern_proc_info_.kp_eproc.e_ppid; |
| 76 } |
| 77 |
| 78 uid_t ProcessInfo::RealUserID() const { |
| 79 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 80 return kern_proc_info_.kp_eproc.e_pcred.p_ruid; |
| 81 } |
| 82 |
| 83 uid_t ProcessInfo::EffectiveUserID() const { |
| 84 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 85 return kern_proc_info_.kp_eproc.e_ucred.cr_uid; |
| 86 } |
| 87 |
| 88 uid_t ProcessInfo::SavedUserID() const { |
| 89 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 90 return kern_proc_info_.kp_eproc.e_pcred.p_svuid; |
| 91 } |
| 92 |
| 93 gid_t ProcessInfo::RealGroupID() const { |
| 94 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 95 return kern_proc_info_.kp_eproc.e_pcred.p_rgid; |
| 96 } |
| 97 |
| 98 gid_t ProcessInfo::EffectiveGroupID() const { |
| 99 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 100 return kern_proc_info_.kp_eproc.e_ucred.cr_gid; |
| 101 } |
| 102 |
| 103 gid_t ProcessInfo::SavedGroupID() const { |
| 104 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 105 return kern_proc_info_.kp_eproc.e_pcred.p_svgid; |
| 106 } |
| 107 |
| 108 std::set<gid_t> ProcessInfo::SupplementaryGroups() const { |
| 109 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 110 |
| 111 const short ngroups = kern_proc_info_.kp_eproc.e_ucred.cr_ngroups; |
| 112 DCHECK_GE(ngroups, 0); |
| 113 DCHECK_LT(static_cast<size_t>(ngroups), |
| 114 arraysize(kern_proc_info_.kp_eproc.e_ucred.cr_groups)); |
| 115 |
| 116 const gid_t* groups = kern_proc_info_.kp_eproc.e_ucred.cr_groups; |
| 117 return std::set<gid_t>(&groups[0], &groups[ngroups]); |
| 118 } |
| 119 |
| 120 std::set<gid_t> ProcessInfo::AllGroups() const { |
| 121 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 122 |
| 123 std::set<gid_t> all_groups = SupplementaryGroups(); |
| 124 all_groups.insert(RealGroupID()); |
| 125 all_groups.insert(EffectiveGroupID()); |
| 126 all_groups.insert(SavedGroupID()); |
| 127 return all_groups; |
| 128 } |
| 129 |
| 130 bool ProcessInfo::DidChangePrivileges() const { |
| 131 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 132 return kern_proc_info_.kp_proc.p_flag & P_SUGID; |
| 133 } |
| 134 |
| 135 bool ProcessInfo::Is64Bit() const { |
| 136 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 137 return kern_proc_info_.kp_proc.p_flag & P_LP64; |
| 138 } |
| 139 |
| 140 void ProcessInfo::StartTime(timeval* start_time) const { |
| 141 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 142 *start_time = kern_proc_info_.kp_proc.p_starttime; |
| 143 } |
| 144 |
| 145 bool ProcessInfo::Arguments(std::vector<std::string>* argv) const { |
| 146 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
| 147 |
| 148 // The format of KERN_PROCARGS2 is explained in 10.9.2 adv_cmds-153/ps/print.c |
| 149 // getproclline(). It is an int (argc) followed by the executable’s string |
| 150 // area. The string area consists of NUL-terminated strings, beginning with |
| 151 // the executable path, and then starting on an aligned boundary, all of the |
| 152 // elements of argv, envp, and applev. |
| 153 |
| 154 // It is possible for a process to exec() in between the two sysctl() calls |
| 155 // below. If that happens, and the string area of the new program is larger |
| 156 // than that of the old one, args_size_estimate will be too small. To detect |
| 157 // this situation, the second sysctl() attempts to fetch args_size_estimate + |
| 158 // 1 bytes, expecting to only receive args_size_estimate. If it gets the extra |
| 159 // byte, it indicates that the string area has grown, and the sysctl() pair |
| 160 // will be retried a limited number of times. |
| 161 |
| 162 size_t args_size_estimate; |
| 163 size_t args_size; |
| 164 std::string args; |
| 165 int tries = 3; |
| 166 const pid_t pid = ProcessID(); |
| 167 do { |
| 168 int mib[] = {CTL_KERN, KERN_PROCARGS2, pid}; |
| 169 int rv = |
| 170 sysctl(mib, arraysize(mib), nullptr, &args_size_estimate, nullptr, 0); |
| 171 if (rv != 0) { |
| 172 PLOG(ERROR) << "sysctl (size) for pid " << pid; |
| 173 return false; |
| 174 } |
| 175 |
| 176 args_size = args_size_estimate + 1; |
| 177 args.resize(args_size); |
| 178 rv = sysctl(mib, arraysize(mib), &args[0], &args_size, nullptr, 0); |
| 179 if (rv != 0) { |
| 180 PLOG(ERROR) << "sysctl (data) for pid " << pid; |
| 181 return false; |
| 182 } |
| 183 } while (args_size == args_size_estimate + 1 && tries--); |
| 184 |
| 185 if (args_size == args_size_estimate + 1) { |
| 186 LOG(ERROR) << "unexpected args_size"; |
| 187 return false; |
| 188 } |
| 189 |
| 190 // KERN_PROCARGS2 needs to at least contain argc. |
| 191 if (args_size < sizeof(int)) { |
| 192 LOG(ERROR) << "tiny args_size"; |
| 193 return false; |
| 194 } |
| 195 args.resize(args_size); |
| 196 |
| 197 // Get argc. |
| 198 int argc; |
| 199 memcpy(&argc, &args[0], sizeof(argc)); |
| 200 |
| 201 // Find the end of the executable path. |
| 202 size_t start_pos = sizeof(argc); |
| 203 size_t nul_pos = args.find('\0', start_pos); |
| 204 if (nul_pos == std::string::npos) { |
| 205 LOG(ERROR) << "unterminated executable path"; |
| 206 return false; |
| 207 } |
| 208 |
| 209 // Find the beginning of the string area. |
| 210 start_pos = args.find_first_not_of('\0', nul_pos); |
| 211 if (start_pos == std::string::npos) { |
| 212 LOG(ERROR) << "no string area"; |
| 213 return false; |
| 214 } |
| 215 |
| 216 std::vector<std::string> local_argv; |
| 217 while (argc-- && nul_pos != std::string::npos) { |
| 218 nul_pos = args.find('\0', start_pos); |
| 219 local_argv.push_back(args.substr(start_pos, nul_pos - start_pos)); |
| 220 start_pos = nul_pos + 1; |
| 221 } |
| 222 |
| 223 if (argc >= 0) { |
| 224 // Not every argument was recovered. |
| 225 LOG(ERROR) << "did not recover all arguments"; |
| 226 return false; |
| 227 } |
| 228 |
| 229 argv->swap(local_argv); |
| 230 return true; |
| 231 } |
| 232 |
| 233 } // namespace crashpad |
OLD | NEW |