blob: 5b0bdb357a6f15dfacc2c344db9f5a8e97150cfa [file] [log] [blame]
Mike Frysinger57b261c2012-04-11 14:47:09 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosae4a86032010-06-16 17:08:34 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ken Mixter03403162010-08-18 15:23:16 -07005#include "crash-reporter/user_collector.h"
6
Ben Chan6e709a12012-02-29 12:10:44 -08007#include <bits/wordsize.h>
8#include <elf.h>
9#include <fcntl.h>
Ken Mixter777484c2010-07-23 16:22:44 -070010#include <grp.h> // For struct group.
Ken Mixter1b8fe012011-01-25 13:33:05 -080011#include <pcrecpp.h>
Ken Mixter777484c2010-07-23 16:22:44 -070012#include <pwd.h> // For struct passwd.
Ken Mixter2953c3a2010-10-18 14:42:20 -070013#include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS.
Ken Mixter777484c2010-07-23 16:22:44 -070014
Chris Sosae4a86032010-06-16 17:08:34 -070015#include <string>
Ken Mixter2953c3a2010-10-18 14:42:20 -070016#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070017
18#include "base/file_util.h"
19#include "base/logging.h"
Mike Frysinger1a8780d2013-02-14 22:39:57 -050020#include "base/posix/eintr_wrapper.h"
Michael Krebs2f3ed032012-08-21 20:17:03 -070021#include "base/stl_util.h"
Mike Frysingera557c112014-02-05 22:55:39 -050022#include "base/strings/string_split.h"
23#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
Ken Mixtera3249322011-03-03 08:47:38 -080025#include "chromeos/process.h"
26#include "chromeos/syslog_logging.h"
Ken Mixter207694d2010-10-28 15:42:37 -070027#include "gflags/gflags.h"
Chris Sosae4a86032010-06-16 17:08:34 -070028
Ken Mixterc6a58e02010-11-01 18:05:30 -070029#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Ken Mixter1b8fe012011-01-25 13:33:05 -080030DEFINE_bool(core2md_failure, false, "Core2md failure test");
31DEFINE_bool(directory_failure, false, "Spool directory failure test");
Ken Mixterc6a58e02010-11-01 18:05:30 -070032DEFINE_string(filter_in, "",
33 "Ignore all crashes but this for testing");
34#pragma GCC diagnostic error "-Wstrict-aliasing"
Ken Mixter207694d2010-10-28 15:42:37 -070035
36static const char kCollectionErrorSignature[] =
37 "crash_reporter-user-collection";
Chris Sosae4a86032010-06-16 17:08:34 -070038// This procfs file is used to cause kernel core file writing to
39// instead pipe the core file into a user space process. See
40// core(5) man page.
41static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern";
Ken Mixterc49dbd42010-12-14 17:44:11 -080042static const char kCorePipeLimitFile[] = "/proc/sys/kernel/core_pipe_limit";
43// Set core_pipe_limit to 4 so that we can catch a few unrelated concurrent
44// crashes, but finite to avoid infinitely recursing on crash handling.
45static const char kCorePipeLimit[] = "4";
Ken Mixter777484c2010-07-23 16:22:44 -070046static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md";
Ken Mixter777484c2010-07-23 16:22:44 -070047
Ben Chanf13bb582012-01-06 08:22:07 -080048static const char kStatePrefix[] = "State:\t";
Ken Mixterc49dbd42010-12-14 17:44:11 -080049
Michael Krebs1c57e9e2012-09-25 18:03:13 -070050// Define an otherwise invalid value that represents an unknown UID.
51static const uid_t kUnknownUid = -1;
52
Ken Mixter777484c2010-07-23 16:22:44 -070053const char *UserCollector::kUserId = "Uid:\t";
54const char *UserCollector::kGroupId = "Gid:\t";
Chris Sosae4a86032010-06-16 17:08:34 -070055
Simon Que9f90aca2013-02-19 17:19:52 -080056using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050057using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080058
Chris Sosae4a86032010-06-16 17:08:34 -070059UserCollector::UserCollector()
Ken Mixter777484c2010-07-23 16:22:44 -070060 : generate_diagnostics_(false),
61 core_pattern_file_(kCorePatternFile),
Ken Mixterc49dbd42010-12-14 17:44:11 -080062 core_pipe_limit_file_(kCorePipeLimitFile),
Ken Mixter03403162010-08-18 15:23:16 -070063 initialized_(false) {
Chris Sosae4a86032010-06-16 17:08:34 -070064}
65
66void UserCollector::Initialize(
67 UserCollector::CountCrashFunction count_crash_function,
68 const std::string &our_path,
69 UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function,
Ken Mixter777484c2010-07-23 16:22:44 -070070 bool generate_diagnostics) {
Ken Mixter03403162010-08-18 15:23:16 -070071 CrashCollector::Initialize(count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080072 is_feedback_allowed_function);
Chris Sosae4a86032010-06-16 17:08:34 -070073 our_path_ = our_path;
Chris Sosae4a86032010-06-16 17:08:34 -070074 initialized_ = true;
Ken Mixter777484c2010-07-23 16:22:44 -070075 generate_diagnostics_ = generate_diagnostics;
Chris Sosae4a86032010-06-16 17:08:34 -070076}
77
78UserCollector::~UserCollector() {
79}
80
Ben Chan6e709a12012-02-29 12:10:44 -080081std::string UserCollector::GetErrorTypeSignature(ErrorType error_type) const {
82 switch (error_type) {
83 case kErrorSystemIssue:
84 return "system-issue";
85 case kErrorReadCoreData:
86 return "read-core-data";
87 case kErrorUnusableProcFiles:
88 return "unusable-proc-files";
89 case kErrorInvalidCoreFile:
90 return "invalid-core-file";
91 case kErrorUnsupported32BitCoreFile:
92 return "unsupported-32bit-core-file";
93 case kErrorCore2MinidumpConversion:
94 return "core2md-conversion";
95 default:
96 return "";
97 }
98}
99
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700100// Return the string that should be used for the kernel's core_pattern file.
101// Note that if you change the format of the enabled pattern, you'll probably
102// also need to change the ParseCrashAttributes() function below, the
103// user_collector_test.cc unittest, and the logging_UserCrash.py autotest.
Chris Sosae4a86032010-06-16 17:08:34 -0700104std::string UserCollector::GetPattern(bool enabled) const {
105 if (enabled) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700106 // Combine the four crash attributes into one parameter to try to reduce
107 // the size of the invocation line for crash_reporter, since the kernel
108 // has a fixed-sized (128B) buffer for it (before parameter expansion).
109 // Note that the kernel does not support quoted arguments in core_pattern.
Mike Frysingerbfdf4a82014-03-14 09:31:33 -0400110 return StringPrintf("|%s --user=%%P:%%s:%%u:%%e", our_path_.c_str());
Chris Sosae4a86032010-06-16 17:08:34 -0700111 } else {
112 return "core";
113 }
114}
115
116bool UserCollector::SetUpInternal(bool enabled) {
117 CHECK(initialized_);
Ken Mixtera3249322011-03-03 08:47:38 -0800118 LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling";
119
Ken Mixterc49dbd42010-12-14 17:44:11 -0800120 if (file_util::WriteFile(FilePath(core_pipe_limit_file_),
121 kCorePipeLimit,
122 strlen(kCorePipeLimit)) !=
123 static_cast<int>(strlen(kCorePipeLimit))) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700124 PLOG(ERROR) << "Unable to write " << core_pipe_limit_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800125 return false;
126 }
Chris Sosae4a86032010-06-16 17:08:34 -0700127 std::string pattern = GetPattern(enabled);
128 if (file_util::WriteFile(FilePath(core_pattern_file_),
129 pattern.c_str(),
130 pattern.length()) !=
131 static_cast<int>(pattern.length())) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700132 PLOG(ERROR) << "Unable to write " << core_pattern_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700133 return false;
134 }
135 return true;
136}
137
Ben Chanf13bb582012-01-06 08:22:07 -0800138bool UserCollector::GetFirstLineWithPrefix(
139 const std::vector<std::string> &lines,
140 const char *prefix, std::string *line) {
141 std::vector<std::string>::const_iterator line_iterator;
142 for (line_iterator = lines.begin(); line_iterator != lines.end();
143 ++line_iterator) {
144 if (line_iterator->find(prefix) == 0) {
145 *line = *line_iterator;
146 return true;
147 }
148 }
149 return false;
150}
151
152bool UserCollector::GetIdFromStatus(
153 const char *prefix, IdKind kind,
154 const std::vector<std::string> &status_lines, int *id) {
Ken Mixter777484c2010-07-23 16:22:44 -0700155 // From fs/proc/array.c:task_state(), this file contains:
156 // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n
Ben Chanf13bb582012-01-06 08:22:07 -0800157 std::string id_line;
158 if (!GetFirstLineWithPrefix(status_lines, prefix, &id_line)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700159 return false;
160 }
Ben Chanf13bb582012-01-06 08:22:07 -0800161 std::string id_substring = id_line.substr(strlen(prefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700162 std::vector<std::string> ids;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700163 base::SplitString(id_substring, '\t', &ids);
Ken Mixter777484c2010-07-23 16:22:44 -0700164 if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) {
165 return false;
166 }
167 const char *number = ids[kind].c_str();
168 char *end_number = NULL;
169 *id = strtol(number, &end_number, 10);
Ben Chanf13bb582012-01-06 08:22:07 -0800170 if (*end_number != '\0') {
Ken Mixter777484c2010-07-23 16:22:44 -0700171 return false;
Ben Chanf13bb582012-01-06 08:22:07 -0800172 }
173 return true;
174}
175
176bool UserCollector::GetStateFromStatus(
177 const std::vector<std::string> &status_lines, std::string *state) {
178 std::string state_line;
179 if (!GetFirstLineWithPrefix(status_lines, kStatePrefix, &state_line)) {
180 return false;
181 }
182 *state = state_line.substr(strlen(kStatePrefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700183 return true;
184}
185
Ken Mixter207694d2010-10-28 15:42:37 -0700186void UserCollector::EnqueueCollectionErrorLog(pid_t pid,
Ben Chan6e709a12012-02-29 12:10:44 -0800187 ErrorType error_type,
Ken Mixter207694d2010-10-28 15:42:37 -0700188 const std::string &exec) {
189 FilePath crash_path;
Ken Mixtera3249322011-03-03 08:47:38 -0800190 LOG(INFO) << "Writing conversion problems as separate crash report.";
Ken Mixter207694d2010-10-28 15:42:37 -0700191 if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800192 LOG(ERROR) << "Could not even get log directory; out of space?";
Ken Mixter207694d2010-10-28 15:42:37 -0700193 return;
194 }
Thiemo Nagel8fce2852014-05-09 14:48:45 +0200195 AddCrashMetaData("sig", kCollectionErrorSignature);
196 AddCrashMetaData("error_type", GetErrorTypeSignature(error_type));
Ken Mixter207694d2010-10-28 15:42:37 -0700197 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
Ken Mixtera3249322011-03-03 08:47:38 -0800198 std::string error_log = chromeos::GetLog();
Ken Mixter1b8fe012011-01-25 13:33:05 -0800199 FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog");
Simon Queacc79382012-05-04 18:10:09 -0700200 if (GetLogContents(FilePath(log_config_path_), kCollectionErrorSignature,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800201 diag_log_path)) {
202 // We load the contents of diag_log into memory and append it to
203 // the error log. We cannot just append to files because we need
204 // to always create new files to prevent attack.
205 std::string diag_log_contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500206 base::ReadFileToString(diag_log_path, &diag_log_contents);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800207 error_log.append(diag_log_contents);
Mike Frysingera557c112014-02-05 22:55:39 -0500208 base::DeleteFile(diag_log_path, false);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800209 }
Ken Mixter207694d2010-10-28 15:42:37 -0700210 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
211 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
Ken Mixter9b346472010-11-07 13:45:45 -0800212 // We must use WriteNewFile instead of file_util::WriteFile as we do
213 // not want to write with root access to a symlink that an attacker
214 // might have created.
Thiemo Nagel8fce2852014-05-09 14:48:45 +0200215 if (WriteNewFile(log_path, error_log.data(), error_log.length()) < 0) {
216 LOG(ERROR) << "Error writing new file " << log_path.value();
217 return;
218 }
Ken Mixter207694d2010-10-28 15:42:37 -0700219 WriteCrashMetaData(meta_path, exec, log_path.value());
220}
221
Ken Mixter777484c2010-07-23 16:22:44 -0700222bool UserCollector::CopyOffProcFiles(pid_t pid,
223 const FilePath &container_dir) {
Mike Frysingera557c112014-02-05 22:55:39 -0500224 if (!base::CreateDirectory(container_dir)) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700225 PLOG(ERROR) << "Could not create " << container_dir.value().c_str();
Ken Mixter777484c2010-07-23 16:22:44 -0700226 return false;
227 }
228 FilePath process_path = GetProcessPath(pid);
Mike Frysingera557c112014-02-05 22:55:39 -0500229 if (!base::PathExists(process_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800230 LOG(ERROR) << "Path " << process_path.value() << " does not exist";
Ken Mixter777484c2010-07-23 16:22:44 -0700231 return false;
232 }
233 static const char *proc_files[] = {
234 "auxv",
235 "cmdline",
236 "environ",
237 "maps",
238 "status"
239 };
240 for (unsigned i = 0; i < arraysize(proc_files); ++i) {
Mike Frysingera557c112014-02-05 22:55:39 -0500241 if (!base::CopyFile(process_path.Append(proc_files[i]),
242 container_dir.Append(proc_files[i]))) {
Ken Mixtera3249322011-03-03 08:47:38 -0800243 LOG(ERROR) << "Could not copy " << proc_files[i] << " file";
Ken Mixter777484c2010-07-23 16:22:44 -0700244 return false;
245 }
246 }
Ben Chanec7d7832012-01-09 10:29:58 -0800247 return true;
Ben Chanf13bb582012-01-06 08:22:07 -0800248}
249
Ben Chan6e709a12012-02-29 12:10:44 -0800250bool UserCollector::ValidateProcFiles(const FilePath &container_dir) const {
Ben Chanf13bb582012-01-06 08:22:07 -0800251 // Check if the maps file is empty, which could be due to the crashed
252 // process being reaped by the kernel before finishing a core dump.
253 int64 file_size = 0;
Mike Frysingera557c112014-02-05 22:55:39 -0500254 if (!base::GetFileSize(container_dir.Append("maps"), &file_size)) {
Ben Chanf13bb582012-01-06 08:22:07 -0800255 LOG(ERROR) << "Could not get the size of maps file";
256 return false;
257 }
258 if (file_size == 0) {
259 LOG(ERROR) << "maps file is empty";
260 return false;
261 }
Ken Mixter777484c2010-07-23 16:22:44 -0700262 return true;
263}
264
Ben Chan6e709a12012-02-29 12:10:44 -0800265UserCollector::ErrorType UserCollector::ValidateCoreFile(
266 const FilePath &core_path) const {
267 int fd = HANDLE_EINTR(open(core_path.value().c_str(), O_RDONLY));
268 if (fd < 0) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700269 PLOG(ERROR) << "Could not open core file " << core_path.value();
Ben Chan6e709a12012-02-29 12:10:44 -0800270 return kErrorInvalidCoreFile;
271 }
272
273 char e_ident[EI_NIDENT];
Mike Frysingera557c112014-02-05 22:55:39 -0500274 bool read_ok = base::ReadFromFD(fd, e_ident, sizeof(e_ident));
Mike Frysingerf1a50142014-05-14 16:05:09 -0400275 IGNORE_EINTR(close(fd));
Ben Chan6e709a12012-02-29 12:10:44 -0800276 if (!read_ok) {
277 LOG(ERROR) << "Could not read header of core file";
278 return kErrorInvalidCoreFile;
279 }
280
281 if (e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 ||
282 e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3) {
283 LOG(ERROR) << "Invalid core file";
284 return kErrorInvalidCoreFile;
285 }
286
287#if __WORDSIZE == 64
288 // TODO(benchan, mkrebs): Remove this check once core2md can
289 // handles both 32-bit and 64-bit ELF on a 64-bit platform.
290 if (e_ident[EI_CLASS] == ELFCLASS32) {
291 LOG(ERROR) << "Conversion of 32-bit core file on 64-bit platform is "
292 << "currently not supported";
293 return kErrorUnsupported32BitCoreFile;
294 }
295#endif
296
297 return kErrorNone;
298}
299
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700300bool UserCollector::GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid,
Ken Mixter207694d2010-10-28 15:42:37 -0700301 FilePath *crash_file_path,
302 bool *out_of_capacity) {
Ken Mixter777484c2010-07-23 16:22:44 -0700303 FilePath process_path = GetProcessPath(pid);
304 std::string status;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800305 if (FLAGS_directory_failure) {
Ken Mixtera3249322011-03-03 08:47:38 -0800306 LOG(ERROR) << "Purposefully failing to create spool directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700307 return false;
308 }
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700309
310 uid_t uid;
Mike Frysingera557c112014-02-05 22:55:39 -0500311 if (base::ReadFileToString(process_path.Append("status"), &status)) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700312 std::vector<std::string> status_lines;
313 base::SplitString(status, '\n', &status_lines);
314
315 std::string process_state;
316 if (!GetStateFromStatus(status_lines, &process_state)) {
317 LOG(ERROR) << "Could not find process state in status file";
318 return false;
319 }
320 LOG(INFO) << "State of crashed process [" << pid << "]: " << process_state;
321
322 // Get effective UID of crashing process.
323 int id;
324 if (!GetIdFromStatus(kUserId, kIdEffective, status_lines, &id)) {
325 LOG(ERROR) << "Could not find euid in status file";
326 return false;
327 }
328 uid = id;
329 } else if (supplied_ruid != kUnknownUid) {
330 LOG(INFO) << "Using supplied UID " << supplied_ruid
331 << " for crashed process [" << pid
332 << "] due to error reading status file";
333 uid = supplied_ruid;
334 } else {
335 LOG(ERROR) << "Could not read status file and kernel did not supply UID";
Ken Mixtera3249322011-03-03 08:47:38 -0800336 LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500337 << base::DirectoryExists(process_path);
Ken Mixter777484c2010-07-23 16:22:44 -0700338 return false;
339 }
Ben Chanf13bb582012-01-06 08:22:07 -0800340
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700341 if (!GetCreatedCrashDirectoryByEuid(uid, crash_file_path, out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800342 LOG(ERROR) << "Could not create crash directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700343 return false;
344 }
345 return true;
Ken Mixter777484c2010-07-23 16:22:44 -0700346}
347
348bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) {
349 // Copy off all stdin to a core file.
350 FilePath stdin_path("/dev/fd/0");
Mike Frysingera557c112014-02-05 22:55:39 -0500351 if (base::CopyFile(stdin_path, core_path)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700352 return true;
353 }
354
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700355 PLOG(ERROR) << "Could not write core file";
Ken Mixter777484c2010-07-23 16:22:44 -0700356 // If the file system was full, make sure we remove any remnants.
Mike Frysingera557c112014-02-05 22:55:39 -0500357 base::DeleteFile(core_path, false);
Ken Mixter777484c2010-07-23 16:22:44 -0700358 return false;
359}
360
Ken Mixter207694d2010-10-28 15:42:37 -0700361bool UserCollector::RunCoreToMinidump(const FilePath &core_path,
362 const FilePath &procfs_directory,
363 const FilePath &minidump_path,
364 const FilePath &temp_directory) {
Ken Mixter777484c2010-07-23 16:22:44 -0700365 FilePath output_path = temp_directory.Append("output");
Ken Mixtera3249322011-03-03 08:47:38 -0800366 chromeos::ProcessImpl core2md;
367 core2md.RedirectOutput(output_path.value());
368 core2md.AddArg(kCoreToMinidumpConverterPath);
369 core2md.AddArg(core_path.value());
370 core2md.AddArg(procfs_directory.value());
Ken Mixter2953c3a2010-10-18 14:42:20 -0700371
Ken Mixtera3249322011-03-03 08:47:38 -0800372 if (!FLAGS_core2md_failure) {
373 core2md.AddArg(minidump_path.value());
374 } else {
Ken Mixter207694d2010-10-28 15:42:37 -0700375 // To test how core2md errors are propagaged, cause an error
376 // by forgetting a required argument.
Ken Mixter207694d2010-10-28 15:42:37 -0700377 }
378
Ken Mixtera3249322011-03-03 08:47:38 -0800379 int errorlevel = core2md.Run();
Ken Mixter777484c2010-07-23 16:22:44 -0700380
381 std::string output;
Mike Frysingera557c112014-02-05 22:55:39 -0500382 base::ReadFileToString(output_path, &output);
Ken Mixter777484c2010-07-23 16:22:44 -0700383 if (errorlevel != 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800384 LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath
385 << " [result=" << errorlevel << "]: " << output;
Ken Mixter777484c2010-07-23 16:22:44 -0700386 return false;
387 }
388
Mike Frysingera557c112014-02-05 22:55:39 -0500389 if (!base::PathExists(minidump_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800390 LOG(ERROR) << "Minidump file " << minidump_path.value()
391 << " was not created";
Ken Mixter777484c2010-07-23 16:22:44 -0700392 return false;
393 }
394 return true;
395}
396
Ben Chan6e709a12012-02-29 12:10:44 -0800397UserCollector::ErrorType UserCollector::ConvertCoreToMinidump(
398 pid_t pid,
399 const FilePath &container_dir,
400 const FilePath &core_path,
401 const FilePath &minidump_path) {
Ben Chanec7d7832012-01-09 10:29:58 -0800402 // If proc files are unuable, we continue to read the core file from stdin,
403 // but only skip the core-to-minidump conversion, so that we may still use
404 // the core file for debugging.
405 bool proc_files_usable =
406 CopyOffProcFiles(pid, container_dir) && ValidateProcFiles(container_dir);
407
408 if (!CopyStdinToCoreFile(core_path)) {
Ben Chan6e709a12012-02-29 12:10:44 -0800409 return kErrorReadCoreData;
Ken Mixter777484c2010-07-23 16:22:44 -0700410 }
411
Ben Chanec7d7832012-01-09 10:29:58 -0800412 if (!proc_files_usable) {
413 LOG(INFO) << "Skipped converting core file to minidump due to "
414 << "unusable proc files";
Ben Chan6e709a12012-02-29 12:10:44 -0800415 return kErrorUnusableProcFiles;
Ken Mixter777484c2010-07-23 16:22:44 -0700416 }
417
Ben Chan6e709a12012-02-29 12:10:44 -0800418 ErrorType error = ValidateCoreFile(core_path);
419 if (error != kErrorNone) {
420 return error;
Ken Mixter777484c2010-07-23 16:22:44 -0700421 }
422
Ben Chan6e709a12012-02-29 12:10:44 -0800423 if (!RunCoreToMinidump(core_path,
424 container_dir, // procfs directory
425 minidump_path,
426 container_dir)) { // temporary directory
427 return kErrorCore2MinidumpConversion;
428 }
429
430 LOG(INFO) << "Stored minidump to " << minidump_path.value();
431 return kErrorNone;
Ken Mixter207694d2010-10-28 15:42:37 -0700432}
433
Ben Chan6e709a12012-02-29 12:10:44 -0800434UserCollector::ErrorType UserCollector::ConvertAndEnqueueCrash(
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700435 pid_t pid, const std::string &exec, uid_t supplied_ruid,
436 bool *out_of_capacity) {
Ken Mixter207694d2010-10-28 15:42:37 -0700437 FilePath crash_path;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700438 if (!GetCreatedCrashDirectory(pid, supplied_ruid, &crash_path,
439 out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800440 LOG(ERROR) << "Unable to find/create process-specific crash path";
Ben Chan6e709a12012-02-29 12:10:44 -0800441 return kErrorSystemIssue;
Ken Mixter207694d2010-10-28 15:42:37 -0700442 }
443
Ben Chan294d5d12012-01-04 20:40:15 -0800444 // Directory like /tmp/crash_reporter/1234 which contains the
Ken Mixter207694d2010-10-28 15:42:37 -0700445 // procfs entries and other temporary files used during conversion.
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700446 FilePath container_dir(StringPrintf("/tmp/crash_reporter/%d", (int)pid));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800447 // Delete a pre-existing directory from crash reporter that may have
448 // been left around for diagnostics from a failed conversion attempt.
449 // If we don't, existing files can cause forking to fail.
Mike Frysingera557c112014-02-05 22:55:39 -0500450 base::DeleteFile(container_dir, true);
Ken Mixter207694d2010-10-28 15:42:37 -0700451 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
452 FilePath core_path = GetCrashPath(crash_path, dump_basename, "core");
453 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
454 FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp");
Ken Mixterc49dbd42010-12-14 17:44:11 -0800455 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
456
Simon Queacc79382012-05-04 18:10:09 -0700457 if (GetLogContents(FilePath(log_config_path_), exec, log_path))
Ken Mixterc49dbd42010-12-14 17:44:11 -0800458 AddCrashMetaData("log", log_path.value());
Ken Mixter207694d2010-10-28 15:42:37 -0700459
Ben Chan6e709a12012-02-29 12:10:44 -0800460 ErrorType error_type =
461 ConvertCoreToMinidump(pid, container_dir, core_path, minidump_path);
462 if (error_type != kErrorNone) {
Ken Mixtera3249322011-03-03 08:47:38 -0800463 LOG(INFO) << "Leaving core file at " << core_path.value()
464 << " due to conversion error";
Ben Chan6e709a12012-02-29 12:10:44 -0800465 return error_type;
Ken Mixter207694d2010-10-28 15:42:37 -0700466 }
467
468 // Here we commit to sending this file. We must not return false
469 // after this point or we will generate a log report as well as a
470 // crash report.
471 WriteCrashMetaData(meta_path,
472 exec,
473 minidump_path.value());
474
Michael Krebs538ecbf2011-07-27 14:13:22 -0700475 if (!IsDeveloperImage()) {
Mike Frysingera557c112014-02-05 22:55:39 -0500476 base::DeleteFile(core_path, false);
Ken Mixter777484c2010-07-23 16:22:44 -0700477 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800478 LOG(INFO) << "Leaving core file at " << core_path.value()
479 << " due to developer image";
Ken Mixter777484c2010-07-23 16:22:44 -0700480 }
481
Mike Frysingera557c112014-02-05 22:55:39 -0500482 base::DeleteFile(container_dir, true);
Ben Chan6e709a12012-02-29 12:10:44 -0800483 return kErrorNone;
Ken Mixter777484c2010-07-23 16:22:44 -0700484}
485
Ken Mixter1b8fe012011-01-25 13:33:05 -0800486bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes,
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700487 pid_t *pid, int *signal, uid_t *uid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800488 std::string *kernel_supplied_name) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700489 pcrecpp::RE re("(\\d+):(\\d+):(\\d+):(.*)");
490 if (re.FullMatch(crash_attributes, pid, signal, uid, kernel_supplied_name))
491 return true;
492
493 LOG(INFO) << "Falling back to parsing crash attributes '"
494 << crash_attributes << "' without UID";
495 pcrecpp::RE re_without_uid("(\\d+):(\\d+):(.*)");
496 *uid = kUnknownUid;
497 return re_without_uid.FullMatch(crash_attributes, pid, signal,
498 kernel_supplied_name);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800499}
500
Michael Krebs2f3ed032012-08-21 20:17:03 -0700501/* Returns true if the given executable name matches that of Chrome. This
502 * includes checks for threads that Chrome has renamed. */
503static bool IsChromeExecName(const std::string &exec) {
504 static const char *kChromeNames[] = {
505 "chrome",
Michael Krebsb1b91a52012-11-26 14:26:17 -0800506 /* These are additional thread names seen in http://crash/ */
507 "MediaPipeline",
Michael Krebs2f3ed032012-08-21 20:17:03 -0700508 /* These come from the use of base::PlatformThread::SetName() directly */
509 "CrBrowserMain", "CrRendererMain", "CrUtilityMain", "CrPPAPIMain",
510 "CrPPAPIBrokerMain", "CrPluginMain", "CrWorkerMain", "CrGpuMain",
511 "BrokerEvent", "CrVideoRenderer", "CrShutdownDetector",
512 "UsbEventHandler", "CrNaClMain", "CrServiceMain",
513 /* These thread names come from the use of base::Thread */
514 "Gamepad polling thread", "Chrome_InProcGpuThread",
515 "Chrome_DragDropThread", "Renderer::FILE", "VC manager",
516 "VideoCaptureModuleImpl", "JavaBridge", "VideoCaptureManagerThread",
517 "Geolocation", "Geolocation_wifi_provider",
518 "Device orientation polling thread", "Chrome_InProcRendererThread",
519 "NetworkChangeNotifier", "Watchdog", "inotify_reader",
520 "cf_iexplore_background_thread", "BrowserWatchdog",
521 "Chrome_HistoryThread", "Chrome_SyncThread", "Chrome_ShellDialogThread",
522 "Printing_Worker", "Chrome_SafeBrowsingThread", "SimpleDBThread",
523 "D-Bus thread", "AudioThread", "NullAudioThread", "V4L2Thread",
524 "ChromotingClientDecodeThread", "Profiling_Flush",
525 "worker_thread_ticker", "AudioMixerAlsa", "AudioMixerCras",
526 "FakeAudioRecordingThread", "CaptureThread",
527 "Chrome_WebSocketproxyThread", "ProcessWatcherThread",
528 "Chrome_CameraThread", "import_thread", "NaCl_IOThread",
529 "Chrome_CloudPrintJobPrintThread", "Chrome_CloudPrintProxyCoreThread",
530 "DaemonControllerFileIO", "ChromotingMainThread",
531 "ChromotingEncodeThread", "ChromotingDesktopThread",
532 "ChromotingIOThread", "ChromotingFileIOThread",
533 "Chrome_libJingle_WorkerThread", "Chrome_ChildIOThread",
534 "GLHelperThread", "RemotingHostPlugin",
535 // "PAC thread #%d", // not easy to check because of "%d"
536 "Chrome_DBThread", "Chrome_WebKitThread", "Chrome_FileThread",
537 "Chrome_FileUserBlockingThread", "Chrome_ProcessLauncherThread",
538 "Chrome_CacheThread", "Chrome_IOThread", "Cache Thread", "File Thread",
539 "ServiceProcess_IO", "ServiceProcess_File",
540 "extension_crash_uploader", "gpu-process_crash_uploader",
541 "plugin_crash_uploader", "renderer_crash_uploader",
542 /* These come from the use of webkit_glue::WebThreadImpl */
543 "Compositor", "Browser Compositor",
544 // "WorkerPool/%d", // not easy to check because of "%d"
545 /* These come from the use of base::Watchdog */
546 "Startup watchdog thread Watchdog", "Shutdown watchdog thread Watchdog",
547 /* These come from the use of AudioDeviceThread::Start */
Michael Krebsa1cc3832012-09-13 13:24:12 -0700548 "AudioDevice", "AudioInputDevice", "AudioOutputDevice",
Michael Krebs2f3ed032012-08-21 20:17:03 -0700549 /* These come from the use of MessageLoopFactory::GetMessageLoop */
550 "GpuVideoDecoder", "RtcVideoDecoderThread", "PipelineThread",
551 "AudioDecoderThread", "VideoDecoderThread",
552 /* These come from the use of MessageLoopFactory::GetMessageLoopProxy */
553 "CaptureVideoDecoderThread", "CaptureVideoDecoder",
554 /* These come from the use of base::SimpleThread */
555 "LocalInputMonitor/%d", // "%d" gets lopped off for kernel-supplied
556 /* These come from the use of base::DelegateSimpleThread */
557 "ipc_channel_nacl reader thread/%d", "plugin_audio_input_thread/%d",
558 "plugin_audio_thread/%d",
559 /* These come from the use of base::SequencedWorkerPool */
560 "BrowserBlockingWorker%d/%d", // "%d" gets lopped off for kernel-supplied
561 };
562 static std::set<std::string> chrome_names;
563
564 /* Initialize a set of chrome names, for efficient lookup */
565 if (chrome_names.empty()) {
566 for (size_t i = 0; i < arraysize(kChromeNames); i++) {
567 std::string check_name(kChromeNames[i]);
568 chrome_names.insert(check_name);
569 // When checking a kernel-supplied name, it should be truncated to 15
570 // chars. See PR_SET_NAME in
571 // http://www.kernel.org/doc/man-pages/online/pages/man2/prctl.2.html,
572 // although that page misleads by saying "16 bytes".
573 chrome_names.insert("supplied_" + std::string(check_name, 0, 15));
574 }
575 }
576
577 return ContainsKey(chrome_names, exec);
578}
579
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700580bool UserCollector::ShouldDump(bool has_owner_consent,
581 bool is_developer,
Michael Krebs4fe30db2011-08-05 13:54:52 -0700582 bool handle_chrome_crashes,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700583 const std::string &exec,
584 std::string *reason) {
585 reason->clear();
586
587 // Treat Chrome crashes as if the user opted-out. We stop counting Chrome
588 // crashes towards user crashes, so user crashes really mean non-Chrome
589 // user-space crashes.
Michael Krebs2f3ed032012-08-21 20:17:03 -0700590 if (!handle_chrome_crashes && IsChromeExecName(exec)) {
Mike Frysingerd2db5ff2013-10-08 19:03:23 -0400591 *reason = "ignoring call by kernel - chrome crash; "
592 "waiting for chrome to call us directly";
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700593 return false;
594 }
595
596 // For developer builds, we always want to keep the crash reports unless
597 // we're testing the crash facilities themselves. This overrides
598 // feedback. Crash sending still obeys consent.
Michael Krebs538ecbf2011-07-27 14:13:22 -0700599 if (is_developer) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700600 *reason = "developer build - not testing - always dumping";
601 return true;
602 }
603
604 if (!has_owner_consent) {
605 *reason = "ignoring - no consent";
606 return false;
607 }
608
609 *reason = "handling";
610 return true;
611}
612
Ken Mixter1b8fe012011-01-25 13:33:05 -0800613bool UserCollector::HandleCrash(const std::string &crash_attributes,
614 const char *force_exec) {
Chris Sosae4a86032010-06-16 17:08:34 -0700615 CHECK(initialized_);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700616 pid_t pid = 0;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800617 int signal = 0;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700618 uid_t supplied_ruid = kUnknownUid;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800619 std::string kernel_supplied_name;
620
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700621 if (!ParseCrashAttributes(crash_attributes, &pid, &signal, &supplied_ruid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800622 &kernel_supplied_name)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800623 LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800624 return false;
625 }
626
Ken Mixter777484c2010-07-23 16:22:44 -0700627 std::string exec;
628 if (force_exec) {
629 exec.assign(force_exec);
630 } else if (!GetExecutableBaseNameFromPid(pid, &exec)) {
Ken Mixter1b8fe012011-01-25 13:33:05 -0800631 // If we cannot find the exec name, use the kernel supplied name.
632 // We don't always use the kernel's since it truncates the name to
633 // 16 characters.
634 exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str());
Ken Mixter777484c2010-07-23 16:22:44 -0700635 }
Ken Mixterc6a58e02010-11-01 18:05:30 -0700636
637 // Allow us to test the crash reporting mechanism successfully even if
638 // other parts of the system crash.
639 if (!FLAGS_filter_in.empty() &&
640 (FLAGS_filter_in == "none" ||
641 FLAGS_filter_in != exec)) {
642 // We use a different format message to make it more obvious in tests
643 // which crashes are test generated and which are real.
Ken Mixtera3249322011-03-03 08:47:38 -0800644 LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while "
645 << "filter_in=" << FLAGS_filter_in << ".";
Ken Mixterc6a58e02010-11-01 18:05:30 -0700646 return true;
647 }
648
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700649 std::string reason;
650 bool dump = ShouldDump(is_feedback_allowed_function_(),
Michael Krebs538ecbf2011-07-27 14:13:22 -0700651 IsDeveloperImage(),
Michael Krebs4fe30db2011-08-05 13:54:52 -0700652 ShouldHandleChromeCrashes(),
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700653 exec,
654 &reason);
Ken Mixter2105b492010-11-09 16:14:38 -0800655
Ken Mixtera3249322011-03-03 08:47:38 -0800656 LOG(WARNING) << "Received crash notification for " << exec << "[" << pid
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700657 << "] sig " << signal << ", user " << supplied_ruid
658 << " (" << reason << ")";
Chris Sosae4a86032010-06-16 17:08:34 -0700659
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700660 if (dump) {
Chris Sosae4a86032010-06-16 17:08:34 -0700661 count_crash_function_();
Ken Mixter777484c2010-07-23 16:22:44 -0700662
Ken Mixter03403162010-08-18 15:23:16 -0700663 if (generate_diagnostics_) {
Ken Mixter207694d2010-10-28 15:42:37 -0700664 bool out_of_capacity = false;
Ben Chan6e709a12012-02-29 12:10:44 -0800665 ErrorType error_type =
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700666 ConvertAndEnqueueCrash(pid, exec, supplied_ruid, &out_of_capacity);
Ben Chan6e709a12012-02-29 12:10:44 -0800667 if (error_type != kErrorNone) {
Ken Mixter207694d2010-10-28 15:42:37 -0700668 if (!out_of_capacity)
Ben Chan6e709a12012-02-29 12:10:44 -0800669 EnqueueCollectionErrorLog(pid, error_type, exec);
Ken Mixter207694d2010-10-28 15:42:37 -0700670 return false;
671 }
Ken Mixter03403162010-08-18 15:23:16 -0700672 }
Ken Mixter777484c2010-07-23 16:22:44 -0700673 }
Ken Mixter207694d2010-10-28 15:42:37 -0700674
Ken Mixter777484c2010-07-23 16:22:44 -0700675 return true;
Chris Sosae4a86032010-06-16 17:08:34 -0700676}