Simon Que | f70060c | 2012-04-09 19:07:07 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "crash-reporter/crash_collector.h" |
| 6 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 7 | #include <dirent.h> |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 8 | #include <fcntl.h> // For file creation modes. |
Alex Vakulenko | 7589aff | 2014-07-30 10:07:35 -0700 | [diff] [blame] | 9 | #define __STDC_FORMAT_MACROS // PRId64 |
| 10 | #include <inttypes.h> |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 11 | #include <linux/limits.h> // PATH_MAX |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 12 | #include <pwd.h> // For struct passwd. |
| 13 | #include <sys/types.h> // for mode_t. |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 14 | #include <sys/wait.h> // For waitpid. |
| 15 | #include <unistd.h> // For execv and fork. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 17 | #include <set> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 18 | #include <utility> |
Simon Que | f70060c | 2012-04-09 19:07:07 -0700 | [diff] [blame] | 19 | #include <vector> |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 20 | |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 21 | #include <dbus/dbus-glib-lowlevel.h> |
| 22 | #include <glib.h> |
| 23 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 24 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 25 | #include <base/logging.h> |
| 26 | #include <base/posix/eintr_wrapper.h> |
| 27 | #include <base/strings/string_split.h> |
| 28 | #include <base/strings/string_util.h> |
| 29 | #include <base/strings/stringprintf.h> |
| 30 | #include <chromeos/cryptohome.h> |
| 31 | #include <chromeos/dbus/dbus.h> |
| 32 | #include <chromeos/dbus/service_constants.h> |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 33 | #include <chromeos/key_value_store.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 34 | #include <chromeos/process.h> |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 35 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 36 | namespace { |
| 37 | |
| 38 | const char kCollectChromeFile[] = |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 39 | "/mnt/stateful_partition/etc/collect_chrome_crashes"; |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 40 | const char kCrashTestInProgressPath[] = "/tmp/crash-test-in-progress"; |
| 41 | const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf"; |
| 42 | const char kDefaultUserName[] = "chronos"; |
| 43 | const char kLeaveCoreFile[] = "/root/.leave_core"; |
| 44 | const char kLsbRelease[] = "/etc/lsb-release"; |
| 45 | const char kShellPath[] = "/bin/sh"; |
| 46 | const char kSystemCrashPath[] = "/var/spool/crash"; |
| 47 | const char kUploadVarPrefix[] = "upload_var_"; |
| 48 | const char kUploadFilePrefix[] = "upload_file_"; |
| 49 | |
| 50 | // Key of the lsb-release entry containing the OS version. |
| 51 | const char kLsbVersionKey[] = "CHROMEOS_RELEASE_VERSION"; |
| 52 | |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 53 | // Normally this path is not used. Unfortunately, there are a few edge cases |
| 54 | // where we need this. Any process that runs as kDefaultUserName that crashes |
| 55 | // is consider a "user crash". That includes the initial Chrome browser that |
| 56 | // runs the login screen. If that blows up, there is no logged in user yet, |
| 57 | // so there is no per-user dir for us to stash things in. Instead we fallback |
| 58 | // to this path as it is at least encrypted on a per-system basis. |
| 59 | // |
| 60 | // This also comes up when running autotests. The GUI is sitting at the login |
| 61 | // screen while tests are sshing in, changing users, and triggering crashes as |
| 62 | // the user (purposefully). |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 63 | const char kFallbackUserCrashPath[] = "/home/chronos/crash"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 64 | |
| 65 | // Directory mode of the user crash spool directory. |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 66 | const mode_t kUserCrashPathMode = 0755; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 67 | |
| 68 | // Directory mode of the system crash spool directory. |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 69 | const mode_t kSystemCrashPathMode = 01755; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 70 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 71 | const uid_t kRootOwner = 0; |
| 72 | const uid_t kRootGroup = 0; |
| 73 | |
| 74 | } // namespace |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 75 | |
Ken Mixter | da5db7a | 2010-09-17 13:50:42 -0700 | [diff] [blame] | 76 | // Maximum crash reports per crash spool directory. Note that this is |
| 77 | // a separate maximum from the maximum rate at which we upload these |
| 78 | // diagnostics. The higher this rate is, the more space we allow for |
| 79 | // core files, minidumps, and kcrash logs, and equivalently the more |
| 80 | // processor and I/O bandwidth we dedicate to handling these crashes when |
| 81 | // many occur at once. Also note that if core files are configured to |
| 82 | // be left on the file system, we stop adding crashes when either the |
| 83 | // number of core files or minidumps reaches this number. |
| 84 | const int CrashCollector::kMaxCrashDirectorySize = 32; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 85 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 86 | using base::FilePath; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 87 | using base::StringPrintf; |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 88 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 89 | CrashCollector::CrashCollector() |
Lei Zhang | 9b1f300 | 2014-04-24 02:10:57 -0700 | [diff] [blame] | 90 | : lsb_release_(kLsbRelease), |
Simon Que | acc7938 | 2012-05-04 18:10:09 -0700 | [diff] [blame] | 91 | log_config_path_(kDefaultLogConfig) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | CrashCollector::~CrashCollector() { |
| 95 | } |
| 96 | |
| 97 | void CrashCollector::Initialize( |
| 98 | CrashCollector::CountCrashFunction count_crash_function, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 99 | CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) { |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 100 | CHECK(count_crash_function); |
| 101 | CHECK(is_feedback_allowed_function); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 102 | |
| 103 | count_crash_function_ = count_crash_function; |
| 104 | is_feedback_allowed_function_ = is_feedback_allowed_function; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 107 | int CrashCollector::WriteNewFile(const FilePath &filename, |
| 108 | const char *data, |
| 109 | int size) { |
| 110 | int fd = HANDLE_EINTR(open(filename.value().c_str(), |
| 111 | O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666)); |
| 112 | if (fd < 0) { |
| 113 | return -1; |
| 114 | } |
| 115 | |
Alex Vakulenko | 859ee45 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 116 | int rv = base::WriteFileDescriptor(fd, data, size) ? size : -1; |
Mike Frysinger | f1a5014 | 2014-05-14 16:05:09 -0400 | [diff] [blame] | 117 | IGNORE_EINTR(close(fd)); |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 118 | return rv; |
| 119 | } |
| 120 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 121 | std::string CrashCollector::Sanitize(const std::string &name) { |
Thiemo Nagel | 9895096 | 2014-05-13 19:48:32 +0200 | [diff] [blame] | 122 | // Make sure the sanitized name does not include any periods. |
| 123 | // The logic in crash_sender relies on this. |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 124 | std::string result = name; |
| 125 | for (size_t i = 0; i < name.size(); ++i) { |
| 126 | if (!isalnum(result[i]) && result[i] != '_') |
| 127 | result[i] = '_'; |
| 128 | } |
| 129 | return result; |
| 130 | } |
| 131 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 132 | std::string CrashCollector::FormatDumpBasename(const std::string &exec_name, |
| 133 | time_t timestamp, |
| 134 | pid_t pid) { |
| 135 | struct tm tm; |
| 136 | localtime_r(×tamp, &tm); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 137 | std::string sanitized_exec_name = Sanitize(exec_name); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 138 | return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d", |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 139 | sanitized_exec_name.c_str(), |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 140 | tm.tm_year + 1900, |
| 141 | tm.tm_mon + 1, |
| 142 | tm.tm_mday, |
| 143 | tm.tm_hour, |
| 144 | tm.tm_min, |
| 145 | tm.tm_sec, |
| 146 | pid); |
| 147 | } |
| 148 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 149 | FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory, |
| 150 | const std::string &basename, |
| 151 | const std::string &extension) { |
| 152 | return crash_directory.Append(StringPrintf("%s.%s", |
| 153 | basename.c_str(), |
| 154 | extension.c_str())); |
| 155 | } |
| 156 | |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 157 | namespace { |
| 158 | |
| 159 | const char *GetGErrorMessage(const GError *error) { |
| 160 | if (!error) |
| 161 | return "Unknown error."; |
| 162 | return error->message; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |
Ben Chan | efec0b3 | 2014-08-12 08:52:11 -0700 | [diff] [blame] | 167 | GHashTable *CrashCollector::GetActiveUserSessions() { |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 168 | GHashTable *active_sessions = nullptr; |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 169 | |
| 170 | chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection(); |
| 171 | if (!dbus.HasConnection()) { |
| 172 | LOG(ERROR) << "Error connecting to system D-Bus"; |
| 173 | return active_sessions; |
| 174 | } |
| 175 | chromeos::dbus::Proxy proxy(dbus, |
| 176 | login_manager::kSessionManagerServiceName, |
| 177 | login_manager::kSessionManagerServicePath, |
| 178 | login_manager::kSessionManagerInterface); |
| 179 | if (!proxy) { |
| 180 | LOG(ERROR) << "Error creating D-Bus proxy to interface " |
| 181 | << "'" << login_manager::kSessionManagerServiceName << "'"; |
| 182 | return active_sessions; |
| 183 | } |
| 184 | |
| 185 | // Request all the active sessions. |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 186 | GError *gerror = nullptr; |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 187 | if (!dbus_g_proxy_call(proxy.gproxy(), |
| 188 | login_manager::kSessionManagerRetrieveActiveSessions, |
| 189 | &gerror, G_TYPE_INVALID, |
| 190 | DBUS_TYPE_G_STRING_STRING_HASHTABLE, &active_sessions, |
| 191 | G_TYPE_INVALID)) { |
| 192 | LOG(ERROR) << "Error performing D-Bus proxy call " |
| 193 | << "'" |
| 194 | << login_manager::kSessionManagerRetrieveActiveSessions << "'" |
| 195 | << ": " << GetGErrorMessage(gerror); |
| 196 | return active_sessions; |
| 197 | } |
| 198 | |
| 199 | return active_sessions; |
| 200 | } |
| 201 | |
Ben Chan | efec0b3 | 2014-08-12 08:52:11 -0700 | [diff] [blame] | 202 | FilePath CrashCollector::GetUserCrashPath() { |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 203 | // In this multiprofile world, there is no one-specific user dir anymore. |
| 204 | // Ask the session manager for the active ones, then just run with the |
| 205 | // first result we get back. |
| 206 | FilePath user_path = FilePath(kFallbackUserCrashPath); |
| 207 | GHashTable *active_sessions = GetActiveUserSessions(); |
| 208 | if (!active_sessions) |
| 209 | return user_path; |
| 210 | |
| 211 | GList *list = g_hash_table_get_values(active_sessions); |
| 212 | if (list) { |
| 213 | const char *salted_path = static_cast<const char *>(list->data); |
Mike Frysinger | 37843a9 | 2013-06-11 17:03:59 -0400 | [diff] [blame] | 214 | user_path = chromeos::cryptohome::home::GetHashedUserPath(salted_path) |
| 215 | .Append("crash"); |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 216 | g_list_free(list); |
| 217 | } |
| 218 | |
| 219 | g_hash_table_destroy(active_sessions); |
| 220 | |
| 221 | return user_path; |
| 222 | } |
| 223 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 224 | FilePath CrashCollector::GetCrashDirectoryInfo( |
| 225 | uid_t process_euid, |
| 226 | uid_t default_user_id, |
| 227 | gid_t default_user_group, |
| 228 | mode_t *mode, |
| 229 | uid_t *directory_owner, |
| 230 | gid_t *directory_group) { |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 231 | // TODO(mkrebs): This can go away once Chrome crashes are handled |
| 232 | // normally (see crosbug.com/5872). |
| 233 | // Check if the user crash directory should be used. If we are |
| 234 | // collecting chrome crashes during autotesting, we want to put them in |
| 235 | // the system crash directory so they are outside the cryptohome -- in |
| 236 | // case we are being run during logout (see crosbug.com/18637). |
| 237 | if (process_euid == default_user_id && IsUserSpecificDirectoryEnabled()) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 238 | *mode = kUserCrashPathMode; |
| 239 | *directory_owner = default_user_id; |
| 240 | *directory_group = default_user_group; |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 241 | return GetUserCrashPath(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 242 | } else { |
| 243 | *mode = kSystemCrashPathMode; |
| 244 | *directory_owner = kRootOwner; |
| 245 | *directory_group = kRootGroup; |
| 246 | return FilePath(kSystemCrashPath); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | bool CrashCollector::GetUserInfoFromName(const std::string &name, |
| 251 | uid_t *uid, |
| 252 | gid_t *gid) { |
| 253 | char storage[256]; |
| 254 | struct passwd passwd_storage; |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 255 | struct passwd *passwd_result = nullptr; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 256 | |
| 257 | if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage), |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 258 | &passwd_result) != 0 || passwd_result == nullptr) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 259 | LOG(ERROR) << "Cannot find user named " << name; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
| 263 | *uid = passwd_result->pw_uid; |
| 264 | *gid = passwd_result->pw_gid; |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 269 | FilePath *crash_directory, |
| 270 | bool *out_of_capacity) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 271 | uid_t default_user_id; |
| 272 | gid_t default_user_group; |
| 273 | |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 274 | if (out_of_capacity) *out_of_capacity = false; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 275 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 276 | // For testing. |
Lei Zhang | 9b1f300 | 2014-04-24 02:10:57 -0700 | [diff] [blame] | 277 | if (!forced_crash_directory_.empty()) { |
| 278 | *crash_directory = forced_crash_directory_; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | |
| 282 | if (!GetUserInfoFromName(kDefaultUserName, |
| 283 | &default_user_id, |
| 284 | &default_user_group)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 285 | LOG(ERROR) << "Could not find default user info"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | mode_t directory_mode; |
| 289 | uid_t directory_owner; |
| 290 | gid_t directory_group; |
| 291 | *crash_directory = |
| 292 | GetCrashDirectoryInfo(euid, |
| 293 | default_user_id, |
| 294 | default_user_group, |
| 295 | &directory_mode, |
| 296 | &directory_owner, |
| 297 | &directory_group); |
| 298 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 299 | if (!base::PathExists(*crash_directory)) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 300 | // Create the spool directory with the appropriate mode (regardless of |
| 301 | // umask) and ownership. |
| 302 | mode_t old_mask = umask(0); |
| 303 | if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 || |
| 304 | chown(crash_directory->value().c_str(), |
| 305 | directory_owner, |
| 306 | directory_group) < 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 307 | LOG(ERROR) << "Unable to create appropriate crash directory"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | umask(old_mask); |
| 311 | } |
| 312 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 313 | if (!base::PathExists(*crash_directory)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 314 | LOG(ERROR) << "Unable to create crash directory " |
| 315 | << crash_directory->value().c_str(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 316 | return false; |
| 317 | } |
| 318 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 319 | if (!CheckHasCapacity(*crash_directory)) { |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 320 | if (out_of_capacity) *out_of_capacity = true; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 321 | return false; |
| 322 | } |
| 323 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 324 | return true; |
| 325 | } |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 326 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 327 | FilePath CrashCollector::GetProcessPath(pid_t pid) { |
| 328 | return FilePath(StringPrintf("/proc/%d", pid)); |
| 329 | } |
| 330 | |
| 331 | bool CrashCollector::GetSymlinkTarget(const FilePath &symlink, |
Ben Chan | 8563d20 | 2014-01-27 19:30:13 -0800 | [diff] [blame] | 332 | FilePath *target) { |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 333 | ssize_t max_size = 64; |
| 334 | std::vector<char> buffer; |
| 335 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 336 | while (true) { |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 337 | buffer.resize(max_size + 1); |
| 338 | ssize_t size = readlink(symlink.value().c_str(), buffer.data(), max_size); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 339 | if (size < 0) { |
| 340 | int saved_errno = errno; |
| 341 | LOG(ERROR) << "Readlink failed on " << symlink.value() << " with " |
| 342 | << saved_errno; |
| 343 | return false; |
| 344 | } |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 345 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 346 | buffer[size] = 0; |
| 347 | if (size == max_size) { |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 348 | max_size *= 2; |
| 349 | if (max_size > PATH_MAX) { |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 352 | continue; |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 353 | } |
| 354 | break; |
| 355 | } |
| 356 | |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 357 | *target = FilePath(buffer.data()); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
| 361 | bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid, |
| 362 | std::string *base_name) { |
| 363 | FilePath target; |
| 364 | FilePath process_path = GetProcessPath(pid); |
| 365 | FilePath exe_path = process_path.Append("exe"); |
| 366 | if (!GetSymlinkTarget(exe_path, &target)) { |
| 367 | LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value() |
| 368 | << " DirectoryExists: " |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 369 | << base::DirectoryExists(process_path); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 370 | // Try to further diagnose exe readlink failure cause. |
| 371 | struct stat buf; |
| 372 | int stat_result = stat(exe_path.value().c_str(), &buf); |
| 373 | int saved_errno = errno; |
| 374 | if (stat_result < 0) { |
| 375 | LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result |
| 376 | << " " << saved_errno; |
| 377 | } else { |
| 378 | LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode=" |
| 379 | << buf.st_mode; |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | *base_name = target.BaseName().value(); |
| 384 | return true; |
| 385 | } |
| 386 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 387 | // Return true if the given crash directory has not already reached |
| 388 | // maximum capacity. |
| 389 | bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) { |
| 390 | DIR* dir = opendir(crash_directory.value().c_str()); |
| 391 | if (!dir) { |
| 392 | return false; |
| 393 | } |
| 394 | struct dirent ent_buf; |
| 395 | struct dirent* ent; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 396 | bool full = false; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 397 | std::set<std::string> basenames; |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 398 | while (readdir_r(dir, &ent_buf, &ent) == 0 && ent) { |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 399 | if ((strcmp(ent->d_name, ".") == 0) || |
| 400 | (strcmp(ent->d_name, "..") == 0)) |
| 401 | continue; |
| 402 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 403 | std::string filename(ent->d_name); |
| 404 | size_t last_dot = filename.rfind("."); |
| 405 | std::string basename; |
| 406 | // If there is a valid looking extension, use the base part of the |
| 407 | // name. If the only dot is the first byte (aka a dot file), treat |
| 408 | // it as unique to avoid allowing a directory full of dot files |
| 409 | // from accumulating. |
| 410 | if (last_dot != std::string::npos && last_dot != 0) |
| 411 | basename = filename.substr(0, last_dot); |
| 412 | else |
| 413 | basename = filename; |
| 414 | basenames.insert(basename); |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 415 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 416 | if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 417 | LOG(WARNING) << "Crash directory " << crash_directory.value() |
| 418 | << " already full with " << kMaxCrashDirectorySize |
| 419 | << " pending reports"; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 420 | full = true; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | closedir(dir); |
| 425 | return !full; |
| 426 | } |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 427 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 428 | bool CrashCollector::GetLogContents(const FilePath &config_path, |
| 429 | const std::string &exec_name, |
| 430 | const FilePath &output_file) { |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 431 | chromeos::KeyValueStore store; |
| 432 | if (!store.Load(config_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 433 | LOG(INFO) << "Unable to read log configuration file " |
| 434 | << config_path.value(); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 435 | return false; |
| 436 | } |
| 437 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 438 | std::string command; |
| 439 | if (!store.GetString(exec_name, &command)) |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 440 | return false; |
| 441 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 442 | chromeos::ProcessImpl diag_process; |
| 443 | diag_process.AddArg(kShellPath); |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 444 | diag_process.AddStringOption("-c", command); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 445 | diag_process.RedirectOutput(output_file.value()); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 446 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 447 | const int result = diag_process.Run(); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 448 | if (result != 0) { |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 449 | LOG(INFO) << "Log command \"" << command << "\" exited with " << result; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | return true; |
| 453 | } |
| 454 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 455 | void CrashCollector::AddCrashMetaData(const std::string &key, |
| 456 | const std::string &value) { |
| 457 | extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str())); |
| 458 | } |
| 459 | |
Albert Chaulk | 33dfd47 | 2013-06-19 15:34:13 -0700 | [diff] [blame] | 460 | void CrashCollector::AddCrashMetaUploadFile(const std::string &key, |
| 461 | const std::string &path) { |
| 462 | if (!path.empty()) |
| 463 | AddCrashMetaData(kUploadFilePrefix + key, path); |
| 464 | } |
| 465 | |
| 466 | void CrashCollector::AddCrashMetaUploadData(const std::string &key, |
| 467 | const std::string &value) { |
| 468 | if (!value.empty()) |
| 469 | AddCrashMetaData(kUploadVarPrefix + key, value); |
| 470 | } |
| 471 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 472 | void CrashCollector::WriteCrashMetaData(const FilePath &meta_path, |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 473 | const std::string &exec_name, |
| 474 | const std::string &payload_path) { |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 475 | chromeos::KeyValueStore store; |
| 476 | if (!store.Load(FilePath(lsb_release_))) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 477 | LOG(ERROR) << "Problem parsing " << lsb_release_; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 478 | // Even though there was some failure, take as much as we could read. |
| 479 | } |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 480 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 481 | std::string version("unknown"); |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame^] | 482 | if (!store.GetString(kLsbVersionKey, &version)) { |
| 483 | LOG(ERROR) << "Unable to read " << kLsbVersionKey << " from " |
| 484 | << lsb_release_; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 485 | } |
Ben Chan | f84ea21 | 2014-08-06 17:27:48 -0700 | [diff] [blame] | 486 | int64_t payload_size = -1; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 487 | base::GetFileSize(FilePath(payload_path), &payload_size); |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 488 | std::string meta_data = StringPrintf("%sexec_name=%s\n" |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 489 | "ver=%s\n" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 490 | "payload=%s\n" |
Alex Vakulenko | eaf060c | 2014-08-08 09:36:48 -0700 | [diff] [blame] | 491 | "payload_size=%" PRId64 "\n" |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 492 | "done=1\n", |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 493 | extra_metadata_.c_str(), |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 494 | exec_name.c_str(), |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 495 | version.c_str(), |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 496 | payload_path.c_str(), |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 497 | payload_size); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 498 | // We must use WriteNewFile instead of base::WriteFile as we |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 499 | // do not want to write with root access to a symlink that an attacker |
| 500 | // might have created. |
| 501 | if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 502 | LOG(ERROR) << "Unable to write " << meta_path.value(); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
Thieu Le | 1652fb2 | 2011-03-03 12:14:43 -0800 | [diff] [blame] | 505 | |
| 506 | bool CrashCollector::IsCrashTestInProgress() { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 507 | return base::PathExists(FilePath(kCrashTestInProgressPath)); |
Thieu Le | 1652fb2 | 2011-03-03 12:14:43 -0800 | [diff] [blame] | 508 | } |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 509 | |
| 510 | bool CrashCollector::IsDeveloperImage() { |
| 511 | // If we're testing crash reporter itself, we don't want to special-case |
| 512 | // for developer images. |
| 513 | if (IsCrashTestInProgress()) |
| 514 | return false; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 515 | return base::PathExists(FilePath(kLeaveCoreFile)); |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | bool CrashCollector::ShouldHandleChromeCrashes() { |
| 519 | // If we're testing crash reporter itself, we don't want to allow an |
| 520 | // override for chrome crashes. And, let's be conservative and only |
| 521 | // allow an override for developer images. |
| 522 | if (!IsCrashTestInProgress() && IsDeveloperImage()) { |
| 523 | // Check if there's an override to indicate we should indeed collect |
| 524 | // chrome crashes. This allows the crashes to still be tracked when |
| 525 | // they occur in autotests. See "crosbug.com/17987". |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 526 | if (base::PathExists(FilePath(kCollectChromeFile))) |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 527 | return true; |
| 528 | } |
| 529 | // We default to ignoring chrome crashes. |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | bool CrashCollector::IsUserSpecificDirectoryEnabled() { |
| 534 | return !ShouldHandleChromeCrashes(); |
| 535 | } |