blob: a3c6f9c3cdd94783ad955c1939445a0fa2f351ef [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.
Ben Chanf84ea212014-08-06 17:27:48 -070013#include <stdint.h>
Ken Mixter2953c3a2010-10-18 14:42:20 -070014#include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS.
Ken Mixter777484c2010-07-23 16:22:44 -070015
Ben Chan7e776902014-06-18 13:19:51 -070016#include <set>
Chris Sosae4a86032010-06-16 17:08:34 -070017#include <string>
Ken Mixter2953c3a2010-10-18 14:42:20 -070018#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070019
Ben Chanab6cc902014-09-05 08:21:06 -070020#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070021#include <base/logging.h>
22#include <base/posix/eintr_wrapper.h>
23#include <base/stl_util.h>
24#include <base/strings/string_split.h>
25#include <base/strings/string_util.h>
26#include <base/strings/stringprintf.h>
27#include <chromeos/process.h>
28#include <chromeos/syslog_logging.h>
29#include <gflags/gflags.h>
Chris Sosae4a86032010-06-16 17:08:34 -070030
Ken Mixterc6a58e02010-11-01 18:05:30 -070031#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Ken Mixter1b8fe012011-01-25 13:33:05 -080032DEFINE_bool(core2md_failure, false, "Core2md failure test");
33DEFINE_bool(directory_failure, false, "Spool directory failure test");
Ken Mixterc6a58e02010-11-01 18:05:30 -070034DEFINE_string(filter_in, "",
35 "Ignore all crashes but this for testing");
36#pragma GCC diagnostic error "-Wstrict-aliasing"
Ken Mixter207694d2010-10-28 15:42:37 -070037
38static const char kCollectionErrorSignature[] =
39 "crash_reporter-user-collection";
Chris Sosae4a86032010-06-16 17:08:34 -070040// This procfs file is used to cause kernel core file writing to
41// instead pipe the core file into a user space process. See
42// core(5) man page.
43static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern";
Ken Mixterc49dbd42010-12-14 17:44:11 -080044static const char kCorePipeLimitFile[] = "/proc/sys/kernel/core_pipe_limit";
45// Set core_pipe_limit to 4 so that we can catch a few unrelated concurrent
46// crashes, but finite to avoid infinitely recursing on crash handling.
47static const char kCorePipeLimit[] = "4";
Ken Mixter777484c2010-07-23 16:22:44 -070048static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md";
Ken Mixter777484c2010-07-23 16:22:44 -070049
Ben Chanf13bb582012-01-06 08:22:07 -080050static const char kStatePrefix[] = "State:\t";
Ken Mixterc49dbd42010-12-14 17:44:11 -080051
Michael Krebs1c57e9e2012-09-25 18:03:13 -070052// Define an otherwise invalid value that represents an unknown UID.
53static const uid_t kUnknownUid = -1;
54
Ken Mixter777484c2010-07-23 16:22:44 -070055const char *UserCollector::kUserId = "Uid:\t";
56const char *UserCollector::kGroupId = "Gid:\t";
Chris Sosae4a86032010-06-16 17:08:34 -070057
Simon Que9f90aca2013-02-19 17:19:52 -080058using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050059using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080060
Chris Sosae4a86032010-06-16 17:08:34 -070061UserCollector::UserCollector()
Ken Mixter777484c2010-07-23 16:22:44 -070062 : generate_diagnostics_(false),
63 core_pattern_file_(kCorePatternFile),
Ken Mixterc49dbd42010-12-14 17:44:11 -080064 core_pipe_limit_file_(kCorePipeLimitFile),
Ken Mixter03403162010-08-18 15:23:16 -070065 initialized_(false) {
Chris Sosae4a86032010-06-16 17:08:34 -070066}
67
68void UserCollector::Initialize(
69 UserCollector::CountCrashFunction count_crash_function,
70 const std::string &our_path,
71 UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function,
Ken Mixter777484c2010-07-23 16:22:44 -070072 bool generate_diagnostics) {
Ken Mixter03403162010-08-18 15:23:16 -070073 CrashCollector::Initialize(count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080074 is_feedback_allowed_function);
Chris Sosae4a86032010-06-16 17:08:34 -070075 our_path_ = our_path;
Chris Sosae4a86032010-06-16 17:08:34 -070076 initialized_ = true;
Ken Mixter777484c2010-07-23 16:22:44 -070077 generate_diagnostics_ = generate_diagnostics;
Chris Sosae4a86032010-06-16 17:08:34 -070078}
79
80UserCollector::~UserCollector() {
81}
82
Ben Chan6e709a12012-02-29 12:10:44 -080083std::string UserCollector::GetErrorTypeSignature(ErrorType error_type) const {
84 switch (error_type) {
85 case kErrorSystemIssue:
86 return "system-issue";
87 case kErrorReadCoreData:
88 return "read-core-data";
89 case kErrorUnusableProcFiles:
90 return "unusable-proc-files";
91 case kErrorInvalidCoreFile:
92 return "invalid-core-file";
93 case kErrorUnsupported32BitCoreFile:
94 return "unsupported-32bit-core-file";
95 case kErrorCore2MinidumpConversion:
96 return "core2md-conversion";
97 default:
98 return "";
99 }
100}
101
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700102// Return the string that should be used for the kernel's core_pattern file.
103// Note that if you change the format of the enabled pattern, you'll probably
104// also need to change the ParseCrashAttributes() function below, the
105// user_collector_test.cc unittest, and the logging_UserCrash.py autotest.
Chris Sosae4a86032010-06-16 17:08:34 -0700106std::string UserCollector::GetPattern(bool enabled) const {
107 if (enabled) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700108 // Combine the four crash attributes into one parameter to try to reduce
109 // the size of the invocation line for crash_reporter, since the kernel
110 // has a fixed-sized (128B) buffer for it (before parameter expansion).
111 // Note that the kernel does not support quoted arguments in core_pattern.
Mike Frysingerbfdf4a82014-03-14 09:31:33 -0400112 return StringPrintf("|%s --user=%%P:%%s:%%u:%%e", our_path_.c_str());
Chris Sosae4a86032010-06-16 17:08:34 -0700113 } else {
114 return "core";
115 }
116}
117
118bool UserCollector::SetUpInternal(bool enabled) {
119 CHECK(initialized_);
Ken Mixtera3249322011-03-03 08:47:38 -0800120 LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling";
121
Ben Chanf30c6412014-05-22 23:09:01 -0700122 if (base::WriteFile(FilePath(core_pipe_limit_file_), kCorePipeLimit,
123 strlen(kCorePipeLimit)) !=
Ken Mixterc49dbd42010-12-14 17:44:11 -0800124 static_cast<int>(strlen(kCorePipeLimit))) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700125 PLOG(ERROR) << "Unable to write " << core_pipe_limit_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800126 return false;
127 }
Chris Sosae4a86032010-06-16 17:08:34 -0700128 std::string pattern = GetPattern(enabled);
Ben Chanf30c6412014-05-22 23:09:01 -0700129 if (base::WriteFile(FilePath(core_pattern_file_), pattern.c_str(),
130 pattern.length()) != static_cast<int>(pattern.length())) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700131 PLOG(ERROR) << "Unable to write " << core_pattern_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700132 return false;
133 }
134 return true;
135}
136
Ben Chanf13bb582012-01-06 08:22:07 -0800137bool UserCollector::GetFirstLineWithPrefix(
138 const std::vector<std::string> &lines,
139 const char *prefix, std::string *line) {
140 std::vector<std::string>::const_iterator line_iterator;
141 for (line_iterator = lines.begin(); line_iterator != lines.end();
142 ++line_iterator) {
143 if (line_iterator->find(prefix) == 0) {
144 *line = *line_iterator;
145 return true;
146 }
147 }
148 return false;
149}
150
151bool UserCollector::GetIdFromStatus(
152 const char *prefix, IdKind kind,
153 const std::vector<std::string> &status_lines, int *id) {
Ken Mixter777484c2010-07-23 16:22:44 -0700154 // From fs/proc/array.c:task_state(), this file contains:
155 // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n
Ben Chanf13bb582012-01-06 08:22:07 -0800156 std::string id_line;
157 if (!GetFirstLineWithPrefix(status_lines, prefix, &id_line)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700158 return false;
159 }
Ben Chanf13bb582012-01-06 08:22:07 -0800160 std::string id_substring = id_line.substr(strlen(prefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700161 std::vector<std::string> ids;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700162 base::SplitString(id_substring, '\t', &ids);
Ken Mixter777484c2010-07-23 16:22:44 -0700163 if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) {
164 return false;
165 }
166 const char *number = ids[kind].c_str();
167 char *end_number = NULL;
168 *id = strtol(number, &end_number, 10);
Ben Chanf13bb582012-01-06 08:22:07 -0800169 if (*end_number != '\0') {
Ken Mixter777484c2010-07-23 16:22:44 -0700170 return false;
Ben Chanf13bb582012-01-06 08:22:07 -0800171 }
172 return true;
173}
174
175bool UserCollector::GetStateFromStatus(
176 const std::vector<std::string> &status_lines, std::string *state) {
177 std::string state_line;
178 if (!GetFirstLineWithPrefix(status_lines, kStatePrefix, &state_line)) {
179 return false;
180 }
181 *state = state_line.substr(strlen(kStatePrefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700182 return true;
183}
184
Ken Mixter207694d2010-10-28 15:42:37 -0700185void UserCollector::EnqueueCollectionErrorLog(pid_t pid,
Ben Chan6e709a12012-02-29 12:10:44 -0800186 ErrorType error_type,
Ken Mixter207694d2010-10-28 15:42:37 -0700187 const std::string &exec) {
188 FilePath crash_path;
Ken Mixtera3249322011-03-03 08:47:38 -0800189 LOG(INFO) << "Writing conversion problems as separate crash report.";
Ken Mixter207694d2010-10-28 15:42:37 -0700190 if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800191 LOG(ERROR) << "Could not even get log directory; out of space?";
Ken Mixter207694d2010-10-28 15:42:37 -0700192 return;
193 }
Thiemo Nagel8fce2852014-05-09 14:48:45 +0200194 AddCrashMetaData("sig", kCollectionErrorSignature);
195 AddCrashMetaData("error_type", GetErrorTypeSignature(error_type));
Ken Mixter207694d2010-10-28 15:42:37 -0700196 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
Ken Mixtera3249322011-03-03 08:47:38 -0800197 std::string error_log = chromeos::GetLog();
Ken Mixter1b8fe012011-01-25 13:33:05 -0800198 FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog");
Simon Queacc79382012-05-04 18:10:09 -0700199 if (GetLogContents(FilePath(log_config_path_), kCollectionErrorSignature,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800200 diag_log_path)) {
201 // We load the contents of diag_log into memory and append it to
202 // the error log. We cannot just append to files because we need
203 // to always create new files to prevent attack.
204 std::string diag_log_contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500205 base::ReadFileToString(diag_log_path, &diag_log_contents);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800206 error_log.append(diag_log_contents);
Mike Frysingera557c112014-02-05 22:55:39 -0500207 base::DeleteFile(diag_log_path, false);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800208 }
Ken Mixter207694d2010-10-28 15:42:37 -0700209 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
210 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
Ben Chanf30c6412014-05-22 23:09:01 -0700211 // We must use WriteNewFile instead of base::WriteFile as we do
Ken Mixter9b346472010-11-07 13:45:45 -0800212 // not want to write with root access to a symlink that an attacker
213 // might have created.
Thiemo Nagel8fce2852014-05-09 14:48:45 +0200214 if (WriteNewFile(log_path, error_log.data(), error_log.length()) < 0) {
215 LOG(ERROR) << "Error writing new file " << log_path.value();
216 return;
217 }
Ken Mixter207694d2010-10-28 15:42:37 -0700218 WriteCrashMetaData(meta_path, exec, log_path.value());
219}
220
Ken Mixter777484c2010-07-23 16:22:44 -0700221bool UserCollector::CopyOffProcFiles(pid_t pid,
222 const FilePath &container_dir) {
Mike Frysingera557c112014-02-05 22:55:39 -0500223 if (!base::CreateDirectory(container_dir)) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700224 PLOG(ERROR) << "Could not create " << container_dir.value().c_str();
Ken Mixter777484c2010-07-23 16:22:44 -0700225 return false;
226 }
227 FilePath process_path = GetProcessPath(pid);
Mike Frysingera557c112014-02-05 22:55:39 -0500228 if (!base::PathExists(process_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800229 LOG(ERROR) << "Path " << process_path.value() << " does not exist";
Ken Mixter777484c2010-07-23 16:22:44 -0700230 return false;
231 }
232 static const char *proc_files[] = {
233 "auxv",
234 "cmdline",
235 "environ",
236 "maps",
237 "status"
238 };
239 for (unsigned i = 0; i < arraysize(proc_files); ++i) {
Mike Frysingera557c112014-02-05 22:55:39 -0500240 if (!base::CopyFile(process_path.Append(proc_files[i]),
241 container_dir.Append(proc_files[i]))) {
Ken Mixtera3249322011-03-03 08:47:38 -0800242 LOG(ERROR) << "Could not copy " << proc_files[i] << " file";
Ken Mixter777484c2010-07-23 16:22:44 -0700243 return false;
244 }
245 }
Ben Chanec7d7832012-01-09 10:29:58 -0800246 return true;
Ben Chanf13bb582012-01-06 08:22:07 -0800247}
248
Ben Chan6e709a12012-02-29 12:10:44 -0800249bool UserCollector::ValidateProcFiles(const FilePath &container_dir) const {
Ben Chanf13bb582012-01-06 08:22:07 -0800250 // Check if the maps file is empty, which could be due to the crashed
251 // process being reaped by the kernel before finishing a core dump.
Ben Chanf84ea212014-08-06 17:27:48 -0700252 int64_t file_size = 0;
Mike Frysingera557c112014-02-05 22:55:39 -0500253 if (!base::GetFileSize(container_dir.Append("maps"), &file_size)) {
Ben Chanf13bb582012-01-06 08:22:07 -0800254 LOG(ERROR) << "Could not get the size of maps file";
255 return false;
256 }
257 if (file_size == 0) {
258 LOG(ERROR) << "maps file is empty";
259 return false;
260 }
Ken Mixter777484c2010-07-23 16:22:44 -0700261 return true;
262}
263
Ben Chan6e709a12012-02-29 12:10:44 -0800264UserCollector::ErrorType UserCollector::ValidateCoreFile(
265 const FilePath &core_path) const {
266 int fd = HANDLE_EINTR(open(core_path.value().c_str(), O_RDONLY));
267 if (fd < 0) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700268 PLOG(ERROR) << "Could not open core file " << core_path.value();
Ben Chan6e709a12012-02-29 12:10:44 -0800269 return kErrorInvalidCoreFile;
270 }
271
272 char e_ident[EI_NIDENT];
Mike Frysingera557c112014-02-05 22:55:39 -0500273 bool read_ok = base::ReadFromFD(fd, e_ident, sizeof(e_ident));
Mike Frysingerf1a50142014-05-14 16:05:09 -0400274 IGNORE_EINTR(close(fd));
Ben Chan6e709a12012-02-29 12:10:44 -0800275 if (!read_ok) {
276 LOG(ERROR) << "Could not read header of core file";
277 return kErrorInvalidCoreFile;
278 }
279
280 if (e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 ||
281 e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3) {
282 LOG(ERROR) << "Invalid core file";
283 return kErrorInvalidCoreFile;
284 }
285
286#if __WORDSIZE == 64
287 // TODO(benchan, mkrebs): Remove this check once core2md can
288 // handles both 32-bit and 64-bit ELF on a 64-bit platform.
289 if (e_ident[EI_CLASS] == ELFCLASS32) {
290 LOG(ERROR) << "Conversion of 32-bit core file on 64-bit platform is "
291 << "currently not supported";
292 return kErrorUnsupported32BitCoreFile;
293 }
294#endif
295
296 return kErrorNone;
297}
298
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700299bool UserCollector::GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid,
Ken Mixter207694d2010-10-28 15:42:37 -0700300 FilePath *crash_file_path,
301 bool *out_of_capacity) {
Ken Mixter777484c2010-07-23 16:22:44 -0700302 FilePath process_path = GetProcessPath(pid);
303 std::string status;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800304 if (FLAGS_directory_failure) {
Ken Mixtera3249322011-03-03 08:47:38 -0800305 LOG(ERROR) << "Purposefully failing to create spool directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700306 return false;
307 }
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700308
309 uid_t uid;
Mike Frysingera557c112014-02-05 22:55:39 -0500310 if (base::ReadFileToString(process_path.Append("status"), &status)) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700311 std::vector<std::string> status_lines;
312 base::SplitString(status, '\n', &status_lines);
313
314 std::string process_state;
315 if (!GetStateFromStatus(status_lines, &process_state)) {
316 LOG(ERROR) << "Could not find process state in status file";
317 return false;
318 }
319 LOG(INFO) << "State of crashed process [" << pid << "]: " << process_state;
320
321 // Get effective UID of crashing process.
322 int id;
323 if (!GetIdFromStatus(kUserId, kIdEffective, status_lines, &id)) {
324 LOG(ERROR) << "Could not find euid in status file";
325 return false;
326 }
327 uid = id;
328 } else if (supplied_ruid != kUnknownUid) {
329 LOG(INFO) << "Using supplied UID " << supplied_ruid
330 << " for crashed process [" << pid
331 << "] due to error reading status file";
332 uid = supplied_ruid;
333 } else {
334 LOG(ERROR) << "Could not read status file and kernel did not supply UID";
Ken Mixtera3249322011-03-03 08:47:38 -0800335 LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500336 << base::DirectoryExists(process_path);
Ken Mixter777484c2010-07-23 16:22:44 -0700337 return false;
338 }
Ben Chanf13bb582012-01-06 08:22:07 -0800339
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700340 if (!GetCreatedCrashDirectoryByEuid(uid, crash_file_path, out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800341 LOG(ERROR) << "Could not create crash directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700342 return false;
343 }
344 return true;
Ken Mixter777484c2010-07-23 16:22:44 -0700345}
346
347bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) {
348 // Copy off all stdin to a core file.
349 FilePath stdin_path("/dev/fd/0");
Mike Frysingera557c112014-02-05 22:55:39 -0500350 if (base::CopyFile(stdin_path, core_path)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700351 return true;
352 }
353
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700354 PLOG(ERROR) << "Could not write core file";
Ken Mixter777484c2010-07-23 16:22:44 -0700355 // If the file system was full, make sure we remove any remnants.
Mike Frysingera557c112014-02-05 22:55:39 -0500356 base::DeleteFile(core_path, false);
Ken Mixter777484c2010-07-23 16:22:44 -0700357 return false;
358}
359
Ken Mixter207694d2010-10-28 15:42:37 -0700360bool UserCollector::RunCoreToMinidump(const FilePath &core_path,
361 const FilePath &procfs_directory,
362 const FilePath &minidump_path,
363 const FilePath &temp_directory) {
Ken Mixter777484c2010-07-23 16:22:44 -0700364 FilePath output_path = temp_directory.Append("output");
Ken Mixtera3249322011-03-03 08:47:38 -0800365 chromeos::ProcessImpl core2md;
366 core2md.RedirectOutput(output_path.value());
367 core2md.AddArg(kCoreToMinidumpConverterPath);
368 core2md.AddArg(core_path.value());
369 core2md.AddArg(procfs_directory.value());
Ken Mixter2953c3a2010-10-18 14:42:20 -0700370
Ken Mixtera3249322011-03-03 08:47:38 -0800371 if (!FLAGS_core2md_failure) {
372 core2md.AddArg(minidump_path.value());
373 } else {
Ken Mixter207694d2010-10-28 15:42:37 -0700374 // To test how core2md errors are propagaged, cause an error
375 // by forgetting a required argument.
Ken Mixter207694d2010-10-28 15:42:37 -0700376 }
377
Ken Mixtera3249322011-03-03 08:47:38 -0800378 int errorlevel = core2md.Run();
Ken Mixter777484c2010-07-23 16:22:44 -0700379
380 std::string output;
Mike Frysingera557c112014-02-05 22:55:39 -0500381 base::ReadFileToString(output_path, &output);
Ken Mixter777484c2010-07-23 16:22:44 -0700382 if (errorlevel != 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800383 LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath
384 << " [result=" << errorlevel << "]: " << output;
Ken Mixter777484c2010-07-23 16:22:44 -0700385 return false;
386 }
387
Mike Frysingera557c112014-02-05 22:55:39 -0500388 if (!base::PathExists(minidump_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800389 LOG(ERROR) << "Minidump file " << minidump_path.value()
390 << " was not created";
Ken Mixter777484c2010-07-23 16:22:44 -0700391 return false;
392 }
393 return true;
394}
395
Ben Chan6e709a12012-02-29 12:10:44 -0800396UserCollector::ErrorType UserCollector::ConvertCoreToMinidump(
397 pid_t pid,
398 const FilePath &container_dir,
399 const FilePath &core_path,
400 const FilePath &minidump_path) {
Ben Chanec7d7832012-01-09 10:29:58 -0800401 // If proc files are unuable, we continue to read the core file from stdin,
402 // but only skip the core-to-minidump conversion, so that we may still use
403 // the core file for debugging.
404 bool proc_files_usable =
405 CopyOffProcFiles(pid, container_dir) && ValidateProcFiles(container_dir);
406
407 if (!CopyStdinToCoreFile(core_path)) {
Ben Chan6e709a12012-02-29 12:10:44 -0800408 return kErrorReadCoreData;
Ken Mixter777484c2010-07-23 16:22:44 -0700409 }
410
Ben Chanec7d7832012-01-09 10:29:58 -0800411 if (!proc_files_usable) {
412 LOG(INFO) << "Skipped converting core file to minidump due to "
413 << "unusable proc files";
Ben Chan6e709a12012-02-29 12:10:44 -0800414 return kErrorUnusableProcFiles;
Ken Mixter777484c2010-07-23 16:22:44 -0700415 }
416
Ben Chan6e709a12012-02-29 12:10:44 -0800417 ErrorType error = ValidateCoreFile(core_path);
418 if (error != kErrorNone) {
419 return error;
Ken Mixter777484c2010-07-23 16:22:44 -0700420 }
421
Ben Chan6e709a12012-02-29 12:10:44 -0800422 if (!RunCoreToMinidump(core_path,
423 container_dir, // procfs directory
424 minidump_path,
425 container_dir)) { // temporary directory
426 return kErrorCore2MinidumpConversion;
427 }
428
429 LOG(INFO) << "Stored minidump to " << minidump_path.value();
430 return kErrorNone;
Ken Mixter207694d2010-10-28 15:42:37 -0700431}
432
Ben Chan6e709a12012-02-29 12:10:44 -0800433UserCollector::ErrorType UserCollector::ConvertAndEnqueueCrash(
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700434 pid_t pid, const std::string &exec, uid_t supplied_ruid,
435 bool *out_of_capacity) {
Ken Mixter207694d2010-10-28 15:42:37 -0700436 FilePath crash_path;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700437 if (!GetCreatedCrashDirectory(pid, supplied_ruid, &crash_path,
438 out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800439 LOG(ERROR) << "Unable to find/create process-specific crash path";
Ben Chan6e709a12012-02-29 12:10:44 -0800440 return kErrorSystemIssue;
Ken Mixter207694d2010-10-28 15:42:37 -0700441 }
442
Ben Chan294d5d12012-01-04 20:40:15 -0800443 // Directory like /tmp/crash_reporter/1234 which contains the
Ken Mixter207694d2010-10-28 15:42:37 -0700444 // procfs entries and other temporary files used during conversion.
Ben Chan7e776902014-06-18 13:19:51 -0700445 FilePath container_dir(StringPrintf("/tmp/crash_reporter/%d", pid));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800446 // Delete a pre-existing directory from crash reporter that may have
447 // been left around for diagnostics from a failed conversion attempt.
448 // If we don't, existing files can cause forking to fail.
Mike Frysingera557c112014-02-05 22:55:39 -0500449 base::DeleteFile(container_dir, true);
Ken Mixter207694d2010-10-28 15:42:37 -0700450 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
451 FilePath core_path = GetCrashPath(crash_path, dump_basename, "core");
452 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
453 FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp");
Ken Mixterc49dbd42010-12-14 17:44:11 -0800454 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
455
Simon Queacc79382012-05-04 18:10:09 -0700456 if (GetLogContents(FilePath(log_config_path_), exec, log_path))
Ken Mixterc49dbd42010-12-14 17:44:11 -0800457 AddCrashMetaData("log", log_path.value());
Ken Mixter207694d2010-10-28 15:42:37 -0700458
Ben Chan6e709a12012-02-29 12:10:44 -0800459 ErrorType error_type =
460 ConvertCoreToMinidump(pid, container_dir, core_path, minidump_path);
461 if (error_type != kErrorNone) {
Ken Mixtera3249322011-03-03 08:47:38 -0800462 LOG(INFO) << "Leaving core file at " << core_path.value()
463 << " due to conversion error";
Ben Chan6e709a12012-02-29 12:10:44 -0800464 return error_type;
Ken Mixter207694d2010-10-28 15:42:37 -0700465 }
466
467 // Here we commit to sending this file. We must not return false
468 // after this point or we will generate a log report as well as a
469 // crash report.
470 WriteCrashMetaData(meta_path,
471 exec,
472 minidump_path.value());
473
Michael Krebs538ecbf2011-07-27 14:13:22 -0700474 if (!IsDeveloperImage()) {
Mike Frysingera557c112014-02-05 22:55:39 -0500475 base::DeleteFile(core_path, false);
Ken Mixter777484c2010-07-23 16:22:44 -0700476 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800477 LOG(INFO) << "Leaving core file at " << core_path.value()
478 << " due to developer image";
Ken Mixter777484c2010-07-23 16:22:44 -0700479 }
480
Mike Frysingera557c112014-02-05 22:55:39 -0500481 base::DeleteFile(container_dir, true);
Ben Chan6e709a12012-02-29 12:10:44 -0800482 return kErrorNone;
Ken Mixter777484c2010-07-23 16:22:44 -0700483}
484
Ken Mixter1b8fe012011-01-25 13:33:05 -0800485bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes,
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700486 pid_t *pid, int *signal, uid_t *uid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800487 std::string *kernel_supplied_name) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700488 pcrecpp::RE re("(\\d+):(\\d+):(\\d+):(.*)");
489 if (re.FullMatch(crash_attributes, pid, signal, uid, kernel_supplied_name))
490 return true;
491
492 LOG(INFO) << "Falling back to parsing crash attributes '"
493 << crash_attributes << "' without UID";
494 pcrecpp::RE re_without_uid("(\\d+):(\\d+):(.*)");
495 *uid = kUnknownUid;
496 return re_without_uid.FullMatch(crash_attributes, pid, signal,
497 kernel_supplied_name);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800498}
499
Ben Chan7e776902014-06-18 13:19:51 -0700500// Returns true if the given executable name matches that of Chrome. This
501// includes checks for threads that Chrome has renamed.
Michael Krebs2f3ed032012-08-21 20:17:03 -0700502static bool IsChromeExecName(const std::string &exec) {
503 static const char *kChromeNames[] = {
504 "chrome",
Ben Chan7e776902014-06-18 13:19:51 -0700505 // These are additional thread names seen in http://crash/
Michael Krebsb1b91a52012-11-26 14:26:17 -0800506 "MediaPipeline",
Ben Chan7e776902014-06-18 13:19:51 -0700507 // These come from the use of base::PlatformThread::SetName() directly
Michael Krebs2f3ed032012-08-21 20:17:03 -0700508 "CrBrowserMain", "CrRendererMain", "CrUtilityMain", "CrPPAPIMain",
509 "CrPPAPIBrokerMain", "CrPluginMain", "CrWorkerMain", "CrGpuMain",
510 "BrokerEvent", "CrVideoRenderer", "CrShutdownDetector",
511 "UsbEventHandler", "CrNaClMain", "CrServiceMain",
Ben Chan7e776902014-06-18 13:19:51 -0700512 // These thread names come from the use of base::Thread
Michael Krebs2f3ed032012-08-21 20:17:03 -0700513 "Gamepad polling thread", "Chrome_InProcGpuThread",
514 "Chrome_DragDropThread", "Renderer::FILE", "VC manager",
515 "VideoCaptureModuleImpl", "JavaBridge", "VideoCaptureManagerThread",
516 "Geolocation", "Geolocation_wifi_provider",
517 "Device orientation polling thread", "Chrome_InProcRendererThread",
518 "NetworkChangeNotifier", "Watchdog", "inotify_reader",
519 "cf_iexplore_background_thread", "BrowserWatchdog",
520 "Chrome_HistoryThread", "Chrome_SyncThread", "Chrome_ShellDialogThread",
521 "Printing_Worker", "Chrome_SafeBrowsingThread", "SimpleDBThread",
522 "D-Bus thread", "AudioThread", "NullAudioThread", "V4L2Thread",
523 "ChromotingClientDecodeThread", "Profiling_Flush",
524 "worker_thread_ticker", "AudioMixerAlsa", "AudioMixerCras",
525 "FakeAudioRecordingThread", "CaptureThread",
526 "Chrome_WebSocketproxyThread", "ProcessWatcherThread",
527 "Chrome_CameraThread", "import_thread", "NaCl_IOThread",
528 "Chrome_CloudPrintJobPrintThread", "Chrome_CloudPrintProxyCoreThread",
529 "DaemonControllerFileIO", "ChromotingMainThread",
530 "ChromotingEncodeThread", "ChromotingDesktopThread",
531 "ChromotingIOThread", "ChromotingFileIOThread",
532 "Chrome_libJingle_WorkerThread", "Chrome_ChildIOThread",
533 "GLHelperThread", "RemotingHostPlugin",
534 // "PAC thread #%d", // not easy to check because of "%d"
535 "Chrome_DBThread", "Chrome_WebKitThread", "Chrome_FileThread",
536 "Chrome_FileUserBlockingThread", "Chrome_ProcessLauncherThread",
537 "Chrome_CacheThread", "Chrome_IOThread", "Cache Thread", "File Thread",
538 "ServiceProcess_IO", "ServiceProcess_File",
539 "extension_crash_uploader", "gpu-process_crash_uploader",
540 "plugin_crash_uploader", "renderer_crash_uploader",
Ben Chan7e776902014-06-18 13:19:51 -0700541 // These come from the use of webkit_glue::WebThreadImpl
Michael Krebs2f3ed032012-08-21 20:17:03 -0700542 "Compositor", "Browser Compositor",
543 // "WorkerPool/%d", // not easy to check because of "%d"
Ben Chan7e776902014-06-18 13:19:51 -0700544 // These come from the use of base::Watchdog
Michael Krebs2f3ed032012-08-21 20:17:03 -0700545 "Startup watchdog thread Watchdog", "Shutdown watchdog thread Watchdog",
Ben Chan7e776902014-06-18 13:19:51 -0700546 // These come from the use of AudioDeviceThread::Start
Michael Krebsa1cc3832012-09-13 13:24:12 -0700547 "AudioDevice", "AudioInputDevice", "AudioOutputDevice",
Ben Chan7e776902014-06-18 13:19:51 -0700548 // These come from the use of MessageLoopFactory::GetMessageLoop
Michael Krebs2f3ed032012-08-21 20:17:03 -0700549 "GpuVideoDecoder", "RtcVideoDecoderThread", "PipelineThread",
550 "AudioDecoderThread", "VideoDecoderThread",
Ben Chan7e776902014-06-18 13:19:51 -0700551 // These come from the use of MessageLoopFactory::GetMessageLoopProxy
Michael Krebs2f3ed032012-08-21 20:17:03 -0700552 "CaptureVideoDecoderThread", "CaptureVideoDecoder",
Ben Chan7e776902014-06-18 13:19:51 -0700553 // These come from the use of base::SimpleThread
Michael Krebs2f3ed032012-08-21 20:17:03 -0700554 "LocalInputMonitor/%d", // "%d" gets lopped off for kernel-supplied
Ben Chan7e776902014-06-18 13:19:51 -0700555 // These come from the use of base::DelegateSimpleThread
Michael Krebs2f3ed032012-08-21 20:17:03 -0700556 "ipc_channel_nacl reader thread/%d", "plugin_audio_input_thread/%d",
557 "plugin_audio_thread/%d",
Ben Chan7e776902014-06-18 13:19:51 -0700558 // These come from the use of base::SequencedWorkerPool
Michael Krebs2f3ed032012-08-21 20:17:03 -0700559 "BrowserBlockingWorker%d/%d", // "%d" gets lopped off for kernel-supplied
560 };
561 static std::set<std::string> chrome_names;
562
Ben Chan7e776902014-06-18 13:19:51 -0700563 // Initialize a set of chrome names, for efficient lookup
Michael Krebs2f3ed032012-08-21 20:17:03 -0700564 if (chrome_names.empty()) {
565 for (size_t i = 0; i < arraysize(kChromeNames); i++) {
566 std::string check_name(kChromeNames[i]);
567 chrome_names.insert(check_name);
568 // When checking a kernel-supplied name, it should be truncated to 15
569 // chars. See PR_SET_NAME in
570 // http://www.kernel.org/doc/man-pages/online/pages/man2/prctl.2.html,
571 // although that page misleads by saying "16 bytes".
572 chrome_names.insert("supplied_" + std::string(check_name, 0, 15));
573 }
574 }
575
576 return ContainsKey(chrome_names, exec);
577}
578
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700579bool UserCollector::ShouldDump(bool has_owner_consent,
580 bool is_developer,
Michael Krebs4fe30db2011-08-05 13:54:52 -0700581 bool handle_chrome_crashes,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700582 const std::string &exec,
583 std::string *reason) {
584 reason->clear();
585
586 // Treat Chrome crashes as if the user opted-out. We stop counting Chrome
587 // crashes towards user crashes, so user crashes really mean non-Chrome
588 // user-space crashes.
Michael Krebs2f3ed032012-08-21 20:17:03 -0700589 if (!handle_chrome_crashes && IsChromeExecName(exec)) {
Mike Frysingerd2db5ff2013-10-08 19:03:23 -0400590 *reason = "ignoring call by kernel - chrome crash; "
591 "waiting for chrome to call us directly";
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700592 return false;
593 }
594
595 // For developer builds, we always want to keep the crash reports unless
596 // we're testing the crash facilities themselves. This overrides
597 // feedback. Crash sending still obeys consent.
Michael Krebs538ecbf2011-07-27 14:13:22 -0700598 if (is_developer) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700599 *reason = "developer build - not testing - always dumping";
600 return true;
601 }
602
603 if (!has_owner_consent) {
604 *reason = "ignoring - no consent";
605 return false;
606 }
607
608 *reason = "handling";
609 return true;
610}
611
Ken Mixter1b8fe012011-01-25 13:33:05 -0800612bool UserCollector::HandleCrash(const std::string &crash_attributes,
613 const char *force_exec) {
Chris Sosae4a86032010-06-16 17:08:34 -0700614 CHECK(initialized_);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700615 pid_t pid = 0;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800616 int signal = 0;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700617 uid_t supplied_ruid = kUnknownUid;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800618 std::string kernel_supplied_name;
619
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700620 if (!ParseCrashAttributes(crash_attributes, &pid, &signal, &supplied_ruid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800621 &kernel_supplied_name)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800622 LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800623 return false;
624 }
625
Ken Mixter777484c2010-07-23 16:22:44 -0700626 std::string exec;
627 if (force_exec) {
628 exec.assign(force_exec);
629 } else if (!GetExecutableBaseNameFromPid(pid, &exec)) {
Ken Mixter1b8fe012011-01-25 13:33:05 -0800630 // If we cannot find the exec name, use the kernel supplied name.
631 // We don't always use the kernel's since it truncates the name to
632 // 16 characters.
633 exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str());
Ken Mixter777484c2010-07-23 16:22:44 -0700634 }
Ken Mixterc6a58e02010-11-01 18:05:30 -0700635
636 // Allow us to test the crash reporting mechanism successfully even if
637 // other parts of the system crash.
638 if (!FLAGS_filter_in.empty() &&
639 (FLAGS_filter_in == "none" ||
640 FLAGS_filter_in != exec)) {
641 // We use a different format message to make it more obvious in tests
642 // which crashes are test generated and which are real.
Ken Mixtera3249322011-03-03 08:47:38 -0800643 LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while "
644 << "filter_in=" << FLAGS_filter_in << ".";
Ken Mixterc6a58e02010-11-01 18:05:30 -0700645 return true;
646 }
647
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700648 std::string reason;
649 bool dump = ShouldDump(is_feedback_allowed_function_(),
Michael Krebs538ecbf2011-07-27 14:13:22 -0700650 IsDeveloperImage(),
Michael Krebs4fe30db2011-08-05 13:54:52 -0700651 ShouldHandleChromeCrashes(),
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700652 exec,
653 &reason);
Ken Mixter2105b492010-11-09 16:14:38 -0800654
Ken Mixtera3249322011-03-03 08:47:38 -0800655 LOG(WARNING) << "Received crash notification for " << exec << "[" << pid
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700656 << "] sig " << signal << ", user " << supplied_ruid
657 << " (" << reason << ")";
Chris Sosae4a86032010-06-16 17:08:34 -0700658
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700659 if (dump) {
Chris Sosae4a86032010-06-16 17:08:34 -0700660 count_crash_function_();
Ken Mixter777484c2010-07-23 16:22:44 -0700661
Ken Mixter03403162010-08-18 15:23:16 -0700662 if (generate_diagnostics_) {
Ken Mixter207694d2010-10-28 15:42:37 -0700663 bool out_of_capacity = false;
Ben Chan6e709a12012-02-29 12:10:44 -0800664 ErrorType error_type =
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700665 ConvertAndEnqueueCrash(pid, exec, supplied_ruid, &out_of_capacity);
Ben Chan6e709a12012-02-29 12:10:44 -0800666 if (error_type != kErrorNone) {
Ken Mixter207694d2010-10-28 15:42:37 -0700667 if (!out_of_capacity)
Ben Chan6e709a12012-02-29 12:10:44 -0800668 EnqueueCollectionErrorLog(pid, error_type, exec);
Ken Mixter207694d2010-10-28 15:42:37 -0700669 return false;
670 }
Ken Mixter03403162010-08-18 15:23:16 -0700671 }
Ken Mixter777484c2010-07-23 16:22:44 -0700672 }
Ken Mixter207694d2010-10-28 15:42:37 -0700673
Ken Mixter777484c2010-07-23 16:22:44 -0700674 return true;
Chris Sosae4a86032010-06-16 17:08:34 -0700675}