blob: cc4beead21fdbdec195863cd6fce27bc6b4e481f [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"
Michael Krebs2f3ed032012-08-21 20:17:03 -070020#include "base/stl_util.h"
Chris Masone8a68c7c2011-05-14 11:44:04 -070021#include "base/string_split.h"
Chris Sosae4a86032010-06-16 17:08:34 -070022#include "base/string_util.h"
Mike Frysinger57b261c2012-04-11 14:47:09 -040023#include "base/stringprintf.h"
Ken Mixtera3249322011-03-03 08:47:38 -080024#include "chromeos/process.h"
25#include "chromeos/syslog_logging.h"
Ken Mixter207694d2010-10-28 15:42:37 -070026#include "gflags/gflags.h"
Chris Sosae4a86032010-06-16 17:08:34 -070027
Ken Mixterc6a58e02010-11-01 18:05:30 -070028#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Ken Mixter1b8fe012011-01-25 13:33:05 -080029DEFINE_bool(core2md_failure, false, "Core2md failure test");
30DEFINE_bool(directory_failure, false, "Spool directory failure test");
Ken Mixterc6a58e02010-11-01 18:05:30 -070031DEFINE_string(filter_in, "",
32 "Ignore all crashes but this for testing");
33#pragma GCC diagnostic error "-Wstrict-aliasing"
Ken Mixter207694d2010-10-28 15:42:37 -070034
35static const char kCollectionErrorSignature[] =
36 "crash_reporter-user-collection";
Chris Sosae4a86032010-06-16 17:08:34 -070037// This procfs file is used to cause kernel core file writing to
38// instead pipe the core file into a user space process. See
39// core(5) man page.
40static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern";
Ken Mixterc49dbd42010-12-14 17:44:11 -080041static const char kCorePipeLimitFile[] = "/proc/sys/kernel/core_pipe_limit";
42// Set core_pipe_limit to 4 so that we can catch a few unrelated concurrent
43// crashes, but finite to avoid infinitely recursing on crash handling.
44static const char kCorePipeLimit[] = "4";
Ken Mixter777484c2010-07-23 16:22:44 -070045static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md";
Ken Mixter777484c2010-07-23 16:22:44 -070046
Ben Chanf13bb582012-01-06 08:22:07 -080047static const char kStatePrefix[] = "State:\t";
Ken Mixterc49dbd42010-12-14 17:44:11 -080048
Michael Krebs1c57e9e2012-09-25 18:03:13 -070049// Define an otherwise invalid value that represents an unknown UID.
50static const uid_t kUnknownUid = -1;
51
Ken Mixter777484c2010-07-23 16:22:44 -070052const char *UserCollector::kUserId = "Uid:\t";
53const char *UserCollector::kGroupId = "Gid:\t";
Chris Sosae4a86032010-06-16 17:08:34 -070054
55UserCollector::UserCollector()
Ken Mixter777484c2010-07-23 16:22:44 -070056 : generate_diagnostics_(false),
57 core_pattern_file_(kCorePatternFile),
Ken Mixterc49dbd42010-12-14 17:44:11 -080058 core_pipe_limit_file_(kCorePipeLimitFile),
Ken Mixter03403162010-08-18 15:23:16 -070059 initialized_(false) {
Chris Sosae4a86032010-06-16 17:08:34 -070060}
61
62void UserCollector::Initialize(
63 UserCollector::CountCrashFunction count_crash_function,
64 const std::string &our_path,
65 UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function,
Ken Mixter777484c2010-07-23 16:22:44 -070066 bool generate_diagnostics) {
Ken Mixter03403162010-08-18 15:23:16 -070067 CrashCollector::Initialize(count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080068 is_feedback_allowed_function);
Chris Sosae4a86032010-06-16 17:08:34 -070069 our_path_ = our_path;
Chris Sosae4a86032010-06-16 17:08:34 -070070 initialized_ = true;
Ken Mixter777484c2010-07-23 16:22:44 -070071 generate_diagnostics_ = generate_diagnostics;
Chris Sosae4a86032010-06-16 17:08:34 -070072}
73
74UserCollector::~UserCollector() {
75}
76
Ben Chan6e709a12012-02-29 12:10:44 -080077std::string UserCollector::GetErrorTypeSignature(ErrorType error_type) const {
78 switch (error_type) {
79 case kErrorSystemIssue:
80 return "system-issue";
81 case kErrorReadCoreData:
82 return "read-core-data";
83 case kErrorUnusableProcFiles:
84 return "unusable-proc-files";
85 case kErrorInvalidCoreFile:
86 return "invalid-core-file";
87 case kErrorUnsupported32BitCoreFile:
88 return "unsupported-32bit-core-file";
89 case kErrorCore2MinidumpConversion:
90 return "core2md-conversion";
91 default:
92 return "";
93 }
94}
95
Michael Krebs1c57e9e2012-09-25 18:03:13 -070096// Return the string that should be used for the kernel's core_pattern file.
97// Note that if you change the format of the enabled pattern, you'll probably
98// also need to change the ParseCrashAttributes() function below, the
99// user_collector_test.cc unittest, and the logging_UserCrash.py autotest.
Chris Sosae4a86032010-06-16 17:08:34 -0700100std::string UserCollector::GetPattern(bool enabled) const {
101 if (enabled) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700102 // Combine the four crash attributes into one parameter to try to reduce
103 // the size of the invocation line for crash_reporter, since the kernel
104 // has a fixed-sized (128B) buffer for it (before parameter expansion).
105 // Note that the kernel does not support quoted arguments in core_pattern.
106 return StringPrintf("|%s --user=%%p:%%s:%%u:%%e", our_path_.c_str());
Chris Sosae4a86032010-06-16 17:08:34 -0700107 } else {
108 return "core";
109 }
110}
111
112bool UserCollector::SetUpInternal(bool enabled) {
113 CHECK(initialized_);
Ken Mixtera3249322011-03-03 08:47:38 -0800114 LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling";
115
Ken Mixterc49dbd42010-12-14 17:44:11 -0800116 if (file_util::WriteFile(FilePath(core_pipe_limit_file_),
117 kCorePipeLimit,
118 strlen(kCorePipeLimit)) !=
119 static_cast<int>(strlen(kCorePipeLimit))) {
Ken Mixtera3249322011-03-03 08:47:38 -0800120 LOG(ERROR) << "Unable to write " << core_pipe_limit_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800121 return false;
122 }
Chris Sosae4a86032010-06-16 17:08:34 -0700123 std::string pattern = GetPattern(enabled);
124 if (file_util::WriteFile(FilePath(core_pattern_file_),
125 pattern.c_str(),
126 pattern.length()) !=
127 static_cast<int>(pattern.length())) {
Ken Mixtera3249322011-03-03 08:47:38 -0800128 LOG(ERROR) << "Unable to write " << core_pattern_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700129 return false;
130 }
131 return true;
132}
133
Ken Mixter777484c2010-07-23 16:22:44 -0700134FilePath UserCollector::GetProcessPath(pid_t pid) {
135 return FilePath(StringPrintf("/proc/%d", pid));
136}
137
138bool UserCollector::GetSymlinkTarget(const FilePath &symlink,
139 FilePath *target) {
140 int max_size = 32;
141 scoped_array<char> buffer;
142 while (true) {
143 buffer.reset(new char[max_size + 1]);
144 ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size);
145 if (size < 0) {
Ken Mixterd49d3622011-02-09 18:23:00 -0800146 int saved_errno = errno;
Ken Mixtera3249322011-03-03 08:47:38 -0800147 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
148 << saved_errno;
Ken Mixter777484c2010-07-23 16:22:44 -0700149 return false;
150 }
151 buffer[size] = 0;
152 if (size == max_size) {
153 // Avoid overflow when doubling.
154 if (max_size * 2 > max_size) {
155 max_size *= 2;
156 continue;
157 } else {
158 return false;
159 }
160 }
161 break;
162 }
163
164 *target = FilePath(buffer.get());
165 return true;
166}
167
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700168bool UserCollector::GetExecutableBaseNameFromPid(pid_t pid,
Ken Mixter777484c2010-07-23 16:22:44 -0700169 std::string *base_name) {
170 FilePath target;
Ken Mixterd49d3622011-02-09 18:23:00 -0800171 FilePath process_path = GetProcessPath(pid);
172 FilePath exe_path = process_path.Append("exe");
173 if (!GetSymlinkTarget(exe_path, &target)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800174 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
175 << " DirectoryExists: "
176 << file_util::DirectoryExists(process_path);
Ken Mixterd49d3622011-02-09 18:23:00 -0800177 // Try to further diagnose exe readlink failure cause.
178 struct stat buf;
179 int stat_result = stat(exe_path.value().c_str(), &buf);
180 int saved_errno = errno;
181 if (stat_result < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800182 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
183 << " " << saved_errno;
Ken Mixterd49d3622011-02-09 18:23:00 -0800184 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800185 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
186 << buf.st_mode;
Ken Mixterd49d3622011-02-09 18:23:00 -0800187 }
Ken Mixter777484c2010-07-23 16:22:44 -0700188 return false;
Ken Mixterd49d3622011-02-09 18:23:00 -0800189 }
Ken Mixter777484c2010-07-23 16:22:44 -0700190 *base_name = target.BaseName().value();
191 return true;
192}
193
Ben Chanf13bb582012-01-06 08:22:07 -0800194bool UserCollector::GetFirstLineWithPrefix(
195 const std::vector<std::string> &lines,
196 const char *prefix, std::string *line) {
197 std::vector<std::string>::const_iterator line_iterator;
198 for (line_iterator = lines.begin(); line_iterator != lines.end();
199 ++line_iterator) {
200 if (line_iterator->find(prefix) == 0) {
201 *line = *line_iterator;
202 return true;
203 }
204 }
205 return false;
206}
207
208bool UserCollector::GetIdFromStatus(
209 const char *prefix, IdKind kind,
210 const std::vector<std::string> &status_lines, int *id) {
Ken Mixter777484c2010-07-23 16:22:44 -0700211 // From fs/proc/array.c:task_state(), this file contains:
212 // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n
Ben Chanf13bb582012-01-06 08:22:07 -0800213 std::string id_line;
214 if (!GetFirstLineWithPrefix(status_lines, prefix, &id_line)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700215 return false;
216 }
Ben Chanf13bb582012-01-06 08:22:07 -0800217 std::string id_substring = id_line.substr(strlen(prefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700218 std::vector<std::string> ids;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700219 base::SplitString(id_substring, '\t', &ids);
Ken Mixter777484c2010-07-23 16:22:44 -0700220 if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) {
221 return false;
222 }
223 const char *number = ids[kind].c_str();
224 char *end_number = NULL;
225 *id = strtol(number, &end_number, 10);
Ben Chanf13bb582012-01-06 08:22:07 -0800226 if (*end_number != '\0') {
Ken Mixter777484c2010-07-23 16:22:44 -0700227 return false;
Ben Chanf13bb582012-01-06 08:22:07 -0800228 }
229 return true;
230}
231
232bool UserCollector::GetStateFromStatus(
233 const std::vector<std::string> &status_lines, std::string *state) {
234 std::string state_line;
235 if (!GetFirstLineWithPrefix(status_lines, kStatePrefix, &state_line)) {
236 return false;
237 }
238 *state = state_line.substr(strlen(kStatePrefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700239 return true;
240}
241
Ken Mixter207694d2010-10-28 15:42:37 -0700242void UserCollector::EnqueueCollectionErrorLog(pid_t pid,
Ben Chan6e709a12012-02-29 12:10:44 -0800243 ErrorType error_type,
Ken Mixter207694d2010-10-28 15:42:37 -0700244 const std::string &exec) {
245 FilePath crash_path;
Ken Mixtera3249322011-03-03 08:47:38 -0800246 LOG(INFO) << "Writing conversion problems as separate crash report.";
Ken Mixter207694d2010-10-28 15:42:37 -0700247 if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800248 LOG(ERROR) << "Could not even get log directory; out of space?";
Ken Mixter207694d2010-10-28 15:42:37 -0700249 return;
250 }
251 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
Ken Mixtera3249322011-03-03 08:47:38 -0800252 std::string error_log = chromeos::GetLog();
Ken Mixter1b8fe012011-01-25 13:33:05 -0800253 FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog");
Simon Queacc79382012-05-04 18:10:09 -0700254 if (GetLogContents(FilePath(log_config_path_), kCollectionErrorSignature,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800255 diag_log_path)) {
256 // We load the contents of diag_log into memory and append it to
257 // the error log. We cannot just append to files because we need
258 // to always create new files to prevent attack.
259 std::string diag_log_contents;
260 file_util::ReadFileToString(diag_log_path, &diag_log_contents);
261 error_log.append(diag_log_contents);
262 file_util::Delete(diag_log_path, false);
263 }
Ken Mixter207694d2010-10-28 15:42:37 -0700264 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
265 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
Ken Mixter9b346472010-11-07 13:45:45 -0800266 // We must use WriteNewFile instead of file_util::WriteFile as we do
267 // not want to write with root access to a symlink that an attacker
268 // might have created.
Ken Mixter1b8fe012011-01-25 13:33:05 -0800269 WriteNewFile(log_path, error_log.data(), error_log.length());
Ken Mixter207694d2010-10-28 15:42:37 -0700270 AddCrashMetaData("sig", kCollectionErrorSignature);
Ben Chan6e709a12012-02-29 12:10:44 -0800271 AddCrashMetaData("error_type", GetErrorTypeSignature(error_type));
Ken Mixter207694d2010-10-28 15:42:37 -0700272 WriteCrashMetaData(meta_path, exec, log_path.value());
273}
274
Ken Mixter777484c2010-07-23 16:22:44 -0700275bool UserCollector::CopyOffProcFiles(pid_t pid,
276 const FilePath &container_dir) {
277 if (!file_util::CreateDirectory(container_dir)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800278 LOG(ERROR) << "Could not create " << container_dir.value().c_str();
Ken Mixter777484c2010-07-23 16:22:44 -0700279 return false;
280 }
281 FilePath process_path = GetProcessPath(pid);
282 if (!file_util::PathExists(process_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800283 LOG(ERROR) << "Path " << process_path.value() << " does not exist";
Ken Mixter777484c2010-07-23 16:22:44 -0700284 return false;
285 }
286 static const char *proc_files[] = {
287 "auxv",
288 "cmdline",
289 "environ",
290 "maps",
291 "status"
292 };
293 for (unsigned i = 0; i < arraysize(proc_files); ++i) {
294 if (!file_util::CopyFile(process_path.Append(proc_files[i]),
295 container_dir.Append(proc_files[i]))) {
Ken Mixtera3249322011-03-03 08:47:38 -0800296 LOG(ERROR) << "Could not copy " << proc_files[i] << " file";
Ken Mixter777484c2010-07-23 16:22:44 -0700297 return false;
298 }
299 }
Ben Chanec7d7832012-01-09 10:29:58 -0800300 return true;
Ben Chanf13bb582012-01-06 08:22:07 -0800301}
302
Ben Chan6e709a12012-02-29 12:10:44 -0800303bool UserCollector::ValidateProcFiles(const FilePath &container_dir) const {
Ben Chanf13bb582012-01-06 08:22:07 -0800304 // Check if the maps file is empty, which could be due to the crashed
305 // process being reaped by the kernel before finishing a core dump.
306 int64 file_size = 0;
307 if (!file_util::GetFileSize(container_dir.Append("maps"), &file_size)) {
308 LOG(ERROR) << "Could not get the size of maps file";
309 return false;
310 }
311 if (file_size == 0) {
312 LOG(ERROR) << "maps file is empty";
313 return false;
314 }
Ken Mixter777484c2010-07-23 16:22:44 -0700315 return true;
316}
317
Ben Chan6e709a12012-02-29 12:10:44 -0800318UserCollector::ErrorType UserCollector::ValidateCoreFile(
319 const FilePath &core_path) const {
320 int fd = HANDLE_EINTR(open(core_path.value().c_str(), O_RDONLY));
321 if (fd < 0) {
322 LOG(ERROR) << "Could not open core file " << core_path.value();
323 return kErrorInvalidCoreFile;
324 }
325
326 char e_ident[EI_NIDENT];
327 bool read_ok = file_util::ReadFromFD(fd, e_ident, sizeof(e_ident));
328 HANDLE_EINTR(close(fd));
329 if (!read_ok) {
330 LOG(ERROR) << "Could not read header of core file";
331 return kErrorInvalidCoreFile;
332 }
333
334 if (e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 ||
335 e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3) {
336 LOG(ERROR) << "Invalid core file";
337 return kErrorInvalidCoreFile;
338 }
339
340#if __WORDSIZE == 64
341 // TODO(benchan, mkrebs): Remove this check once core2md can
342 // handles both 32-bit and 64-bit ELF on a 64-bit platform.
343 if (e_ident[EI_CLASS] == ELFCLASS32) {
344 LOG(ERROR) << "Conversion of 32-bit core file on 64-bit platform is "
345 << "currently not supported";
346 return kErrorUnsupported32BitCoreFile;
347 }
348#endif
349
350 return kErrorNone;
351}
352
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700353bool UserCollector::GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid,
Ken Mixter207694d2010-10-28 15:42:37 -0700354 FilePath *crash_file_path,
355 bool *out_of_capacity) {
Ken Mixter777484c2010-07-23 16:22:44 -0700356 FilePath process_path = GetProcessPath(pid);
357 std::string status;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800358 if (FLAGS_directory_failure) {
Ken Mixtera3249322011-03-03 08:47:38 -0800359 LOG(ERROR) << "Purposefully failing to create spool directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700360 return false;
361 }
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700362
363 uid_t uid;
364 if (file_util::ReadFileToString(process_path.Append("status"), &status)) {
365 std::vector<std::string> status_lines;
366 base::SplitString(status, '\n', &status_lines);
367
368 std::string process_state;
369 if (!GetStateFromStatus(status_lines, &process_state)) {
370 LOG(ERROR) << "Could not find process state in status file";
371 return false;
372 }
373 LOG(INFO) << "State of crashed process [" << pid << "]: " << process_state;
374
375 // Get effective UID of crashing process.
376 int id;
377 if (!GetIdFromStatus(kUserId, kIdEffective, status_lines, &id)) {
378 LOG(ERROR) << "Could not find euid in status file";
379 return false;
380 }
381 uid = id;
382 } else if (supplied_ruid != kUnknownUid) {
383 LOG(INFO) << "Using supplied UID " << supplied_ruid
384 << " for crashed process [" << pid
385 << "] due to error reading status file";
386 uid = supplied_ruid;
387 } else {
388 LOG(ERROR) << "Could not read status file and kernel did not supply UID";
Ken Mixtera3249322011-03-03 08:47:38 -0800389 LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: "
390 << file_util::DirectoryExists(process_path);
Ken Mixter777484c2010-07-23 16:22:44 -0700391 return false;
392 }
Ben Chanf13bb582012-01-06 08:22:07 -0800393
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700394 if (!GetCreatedCrashDirectoryByEuid(uid, crash_file_path, out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800395 LOG(ERROR) << "Could not create crash directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700396 return false;
397 }
398 return true;
Ken Mixter777484c2010-07-23 16:22:44 -0700399}
400
401bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) {
402 // Copy off all stdin to a core file.
403 FilePath stdin_path("/dev/fd/0");
404 if (file_util::CopyFile(stdin_path, core_path)) {
405 return true;
406 }
407
Ken Mixtera3249322011-03-03 08:47:38 -0800408 LOG(ERROR) << "Could not write core file";
Ken Mixter777484c2010-07-23 16:22:44 -0700409 // If the file system was full, make sure we remove any remnants.
410 file_util::Delete(core_path, false);
411 return false;
412}
413
Ken Mixter207694d2010-10-28 15:42:37 -0700414bool UserCollector::RunCoreToMinidump(const FilePath &core_path,
415 const FilePath &procfs_directory,
416 const FilePath &minidump_path,
417 const FilePath &temp_directory) {
Ken Mixter777484c2010-07-23 16:22:44 -0700418 FilePath output_path = temp_directory.Append("output");
Ken Mixtera3249322011-03-03 08:47:38 -0800419 chromeos::ProcessImpl core2md;
420 core2md.RedirectOutput(output_path.value());
421 core2md.AddArg(kCoreToMinidumpConverterPath);
422 core2md.AddArg(core_path.value());
423 core2md.AddArg(procfs_directory.value());
Ken Mixter2953c3a2010-10-18 14:42:20 -0700424
Ken Mixtera3249322011-03-03 08:47:38 -0800425 if (!FLAGS_core2md_failure) {
426 core2md.AddArg(minidump_path.value());
427 } else {
Ken Mixter207694d2010-10-28 15:42:37 -0700428 // To test how core2md errors are propagaged, cause an error
429 // by forgetting a required argument.
Ken Mixter207694d2010-10-28 15:42:37 -0700430 }
431
Ken Mixtera3249322011-03-03 08:47:38 -0800432 int errorlevel = core2md.Run();
Ken Mixter777484c2010-07-23 16:22:44 -0700433
434 std::string output;
435 file_util::ReadFileToString(output_path, &output);
436 if (errorlevel != 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800437 LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath
438 << " [result=" << errorlevel << "]: " << output;
Ken Mixter777484c2010-07-23 16:22:44 -0700439 return false;
440 }
441
442 if (!file_util::PathExists(minidump_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800443 LOG(ERROR) << "Minidump file " << minidump_path.value()
444 << " was not created";
Ken Mixter777484c2010-07-23 16:22:44 -0700445 return false;
446 }
447 return true;
448}
449
Ben Chan6e709a12012-02-29 12:10:44 -0800450UserCollector::ErrorType UserCollector::ConvertCoreToMinidump(
451 pid_t pid,
452 const FilePath &container_dir,
453 const FilePath &core_path,
454 const FilePath &minidump_path) {
Ben Chanec7d7832012-01-09 10:29:58 -0800455 // If proc files are unuable, we continue to read the core file from stdin,
456 // but only skip the core-to-minidump conversion, so that we may still use
457 // the core file for debugging.
458 bool proc_files_usable =
459 CopyOffProcFiles(pid, container_dir) && ValidateProcFiles(container_dir);
460
461 if (!CopyStdinToCoreFile(core_path)) {
Ben Chan6e709a12012-02-29 12:10:44 -0800462 return kErrorReadCoreData;
Ken Mixter777484c2010-07-23 16:22:44 -0700463 }
464
Ben Chanec7d7832012-01-09 10:29:58 -0800465 if (!proc_files_usable) {
466 LOG(INFO) << "Skipped converting core file to minidump due to "
467 << "unusable proc files";
Ben Chan6e709a12012-02-29 12:10:44 -0800468 return kErrorUnusableProcFiles;
Ken Mixter777484c2010-07-23 16:22:44 -0700469 }
470
Ben Chan6e709a12012-02-29 12:10:44 -0800471 ErrorType error = ValidateCoreFile(core_path);
472 if (error != kErrorNone) {
473 return error;
Ken Mixter777484c2010-07-23 16:22:44 -0700474 }
475
Ben Chan6e709a12012-02-29 12:10:44 -0800476 if (!RunCoreToMinidump(core_path,
477 container_dir, // procfs directory
478 minidump_path,
479 container_dir)) { // temporary directory
480 return kErrorCore2MinidumpConversion;
481 }
482
483 LOG(INFO) << "Stored minidump to " << minidump_path.value();
484 return kErrorNone;
Ken Mixter207694d2010-10-28 15:42:37 -0700485}
486
Ben Chan6e709a12012-02-29 12:10:44 -0800487UserCollector::ErrorType UserCollector::ConvertAndEnqueueCrash(
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700488 pid_t pid, const std::string &exec, uid_t supplied_ruid,
489 bool *out_of_capacity) {
Ken Mixter207694d2010-10-28 15:42:37 -0700490 FilePath crash_path;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700491 if (!GetCreatedCrashDirectory(pid, supplied_ruid, &crash_path,
492 out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800493 LOG(ERROR) << "Unable to find/create process-specific crash path";
Ben Chan6e709a12012-02-29 12:10:44 -0800494 return kErrorSystemIssue;
Ken Mixter207694d2010-10-28 15:42:37 -0700495 }
496
Ben Chan294d5d12012-01-04 20:40:15 -0800497 // Directory like /tmp/crash_reporter/1234 which contains the
Ken Mixter207694d2010-10-28 15:42:37 -0700498 // procfs entries and other temporary files used during conversion.
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700499 FilePath container_dir(StringPrintf("/tmp/crash_reporter/%d", (int)pid));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800500 // Delete a pre-existing directory from crash reporter that may have
501 // been left around for diagnostics from a failed conversion attempt.
502 // If we don't, existing files can cause forking to fail.
503 file_util::Delete(container_dir, true);
Ken Mixter207694d2010-10-28 15:42:37 -0700504 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
505 FilePath core_path = GetCrashPath(crash_path, dump_basename, "core");
506 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
507 FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp");
Ken Mixterc49dbd42010-12-14 17:44:11 -0800508 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
509
Simon Queacc79382012-05-04 18:10:09 -0700510 if (GetLogContents(FilePath(log_config_path_), exec, log_path))
Ken Mixterc49dbd42010-12-14 17:44:11 -0800511 AddCrashMetaData("log", log_path.value());
Ken Mixter207694d2010-10-28 15:42:37 -0700512
Ben Chan6e709a12012-02-29 12:10:44 -0800513 ErrorType error_type =
514 ConvertCoreToMinidump(pid, container_dir, core_path, minidump_path);
515 if (error_type != kErrorNone) {
Ken Mixtera3249322011-03-03 08:47:38 -0800516 LOG(INFO) << "Leaving core file at " << core_path.value()
517 << " due to conversion error";
Ben Chan6e709a12012-02-29 12:10:44 -0800518 return error_type;
Ken Mixter207694d2010-10-28 15:42:37 -0700519 }
520
521 // Here we commit to sending this file. We must not return false
522 // after this point or we will generate a log report as well as a
523 // crash report.
524 WriteCrashMetaData(meta_path,
525 exec,
526 minidump_path.value());
527
Michael Krebs538ecbf2011-07-27 14:13:22 -0700528 if (!IsDeveloperImage()) {
Ken Mixter777484c2010-07-23 16:22:44 -0700529 file_util::Delete(core_path, false);
530 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800531 LOG(INFO) << "Leaving core file at " << core_path.value()
532 << " due to developer image";
Ken Mixter777484c2010-07-23 16:22:44 -0700533 }
534
Ken Mixter207694d2010-10-28 15:42:37 -0700535 file_util::Delete(container_dir, true);
Ben Chan6e709a12012-02-29 12:10:44 -0800536 return kErrorNone;
Ken Mixter777484c2010-07-23 16:22:44 -0700537}
538
Ken Mixter1b8fe012011-01-25 13:33:05 -0800539bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes,
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700540 pid_t *pid, int *signal, uid_t *uid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800541 std::string *kernel_supplied_name) {
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700542 pcrecpp::RE re("(\\d+):(\\d+):(\\d+):(.*)");
543 if (re.FullMatch(crash_attributes, pid, signal, uid, kernel_supplied_name))
544 return true;
545
546 LOG(INFO) << "Falling back to parsing crash attributes '"
547 << crash_attributes << "' without UID";
548 pcrecpp::RE re_without_uid("(\\d+):(\\d+):(.*)");
549 *uid = kUnknownUid;
550 return re_without_uid.FullMatch(crash_attributes, pid, signal,
551 kernel_supplied_name);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800552}
553
Michael Krebs2f3ed032012-08-21 20:17:03 -0700554/* Returns true if the given executable name matches that of Chrome. This
555 * includes checks for threads that Chrome has renamed. */
556static bool IsChromeExecName(const std::string &exec) {
557 static const char *kChromeNames[] = {
558 "chrome",
Michael Krebsb1b91a52012-11-26 14:26:17 -0800559 /* These are additional thread names seen in http://crash/ */
560 "MediaPipeline",
Michael Krebs2f3ed032012-08-21 20:17:03 -0700561 /* These come from the use of base::PlatformThread::SetName() directly */
562 "CrBrowserMain", "CrRendererMain", "CrUtilityMain", "CrPPAPIMain",
563 "CrPPAPIBrokerMain", "CrPluginMain", "CrWorkerMain", "CrGpuMain",
564 "BrokerEvent", "CrVideoRenderer", "CrShutdownDetector",
565 "UsbEventHandler", "CrNaClMain", "CrServiceMain",
566 /* These thread names come from the use of base::Thread */
567 "Gamepad polling thread", "Chrome_InProcGpuThread",
568 "Chrome_DragDropThread", "Renderer::FILE", "VC manager",
569 "VideoCaptureModuleImpl", "JavaBridge", "VideoCaptureManagerThread",
570 "Geolocation", "Geolocation_wifi_provider",
571 "Device orientation polling thread", "Chrome_InProcRendererThread",
572 "NetworkChangeNotifier", "Watchdog", "inotify_reader",
573 "cf_iexplore_background_thread", "BrowserWatchdog",
574 "Chrome_HistoryThread", "Chrome_SyncThread", "Chrome_ShellDialogThread",
575 "Printing_Worker", "Chrome_SafeBrowsingThread", "SimpleDBThread",
576 "D-Bus thread", "AudioThread", "NullAudioThread", "V4L2Thread",
577 "ChromotingClientDecodeThread", "Profiling_Flush",
578 "worker_thread_ticker", "AudioMixerAlsa", "AudioMixerCras",
579 "FakeAudioRecordingThread", "CaptureThread",
580 "Chrome_WebSocketproxyThread", "ProcessWatcherThread",
581 "Chrome_CameraThread", "import_thread", "NaCl_IOThread",
582 "Chrome_CloudPrintJobPrintThread", "Chrome_CloudPrintProxyCoreThread",
583 "DaemonControllerFileIO", "ChromotingMainThread",
584 "ChromotingEncodeThread", "ChromotingDesktopThread",
585 "ChromotingIOThread", "ChromotingFileIOThread",
586 "Chrome_libJingle_WorkerThread", "Chrome_ChildIOThread",
587 "GLHelperThread", "RemotingHostPlugin",
588 // "PAC thread #%d", // not easy to check because of "%d"
589 "Chrome_DBThread", "Chrome_WebKitThread", "Chrome_FileThread",
590 "Chrome_FileUserBlockingThread", "Chrome_ProcessLauncherThread",
591 "Chrome_CacheThread", "Chrome_IOThread", "Cache Thread", "File Thread",
592 "ServiceProcess_IO", "ServiceProcess_File",
593 "extension_crash_uploader", "gpu-process_crash_uploader",
594 "plugin_crash_uploader", "renderer_crash_uploader",
595 /* These come from the use of webkit_glue::WebThreadImpl */
596 "Compositor", "Browser Compositor",
597 // "WorkerPool/%d", // not easy to check because of "%d"
598 /* These come from the use of base::Watchdog */
599 "Startup watchdog thread Watchdog", "Shutdown watchdog thread Watchdog",
600 /* These come from the use of AudioDeviceThread::Start */
Michael Krebsa1cc3832012-09-13 13:24:12 -0700601 "AudioDevice", "AudioInputDevice", "AudioOutputDevice",
Michael Krebs2f3ed032012-08-21 20:17:03 -0700602 /* These come from the use of MessageLoopFactory::GetMessageLoop */
603 "GpuVideoDecoder", "RtcVideoDecoderThread", "PipelineThread",
604 "AudioDecoderThread", "VideoDecoderThread",
605 /* These come from the use of MessageLoopFactory::GetMessageLoopProxy */
606 "CaptureVideoDecoderThread", "CaptureVideoDecoder",
607 /* These come from the use of base::SimpleThread */
608 "LocalInputMonitor/%d", // "%d" gets lopped off for kernel-supplied
609 /* These come from the use of base::DelegateSimpleThread */
610 "ipc_channel_nacl reader thread/%d", "plugin_audio_input_thread/%d",
611 "plugin_audio_thread/%d",
612 /* These come from the use of base::SequencedWorkerPool */
613 "BrowserBlockingWorker%d/%d", // "%d" gets lopped off for kernel-supplied
614 };
615 static std::set<std::string> chrome_names;
616
617 /* Initialize a set of chrome names, for efficient lookup */
618 if (chrome_names.empty()) {
619 for (size_t i = 0; i < arraysize(kChromeNames); i++) {
620 std::string check_name(kChromeNames[i]);
621 chrome_names.insert(check_name);
622 // When checking a kernel-supplied name, it should be truncated to 15
623 // chars. See PR_SET_NAME in
624 // http://www.kernel.org/doc/man-pages/online/pages/man2/prctl.2.html,
625 // although that page misleads by saying "16 bytes".
626 chrome_names.insert("supplied_" + std::string(check_name, 0, 15));
627 }
628 }
629
630 return ContainsKey(chrome_names, exec);
631}
632
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700633bool UserCollector::ShouldDump(bool has_owner_consent,
634 bool is_developer,
Michael Krebs4fe30db2011-08-05 13:54:52 -0700635 bool handle_chrome_crashes,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700636 const std::string &exec,
637 std::string *reason) {
638 reason->clear();
639
640 // Treat Chrome crashes as if the user opted-out. We stop counting Chrome
641 // crashes towards user crashes, so user crashes really mean non-Chrome
642 // user-space crashes.
Michael Krebs2f3ed032012-08-21 20:17:03 -0700643 if (!handle_chrome_crashes && IsChromeExecName(exec)) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700644 *reason = "ignoring - chrome crash";
645 return false;
646 }
647
648 // For developer builds, we always want to keep the crash reports unless
649 // we're testing the crash facilities themselves. This overrides
650 // feedback. Crash sending still obeys consent.
Michael Krebs538ecbf2011-07-27 14:13:22 -0700651 if (is_developer) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700652 *reason = "developer build - not testing - always dumping";
653 return true;
654 }
655
656 if (!has_owner_consent) {
657 *reason = "ignoring - no consent";
658 return false;
659 }
660
661 *reason = "handling";
662 return true;
663}
664
Ken Mixter1b8fe012011-01-25 13:33:05 -0800665bool UserCollector::HandleCrash(const std::string &crash_attributes,
666 const char *force_exec) {
Chris Sosae4a86032010-06-16 17:08:34 -0700667 CHECK(initialized_);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700668 pid_t pid = 0;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800669 int signal = 0;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700670 uid_t supplied_ruid = kUnknownUid;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800671 std::string kernel_supplied_name;
672
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700673 if (!ParseCrashAttributes(crash_attributes, &pid, &signal, &supplied_ruid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800674 &kernel_supplied_name)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800675 LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800676 return false;
677 }
678
Ken Mixter777484c2010-07-23 16:22:44 -0700679 std::string exec;
680 if (force_exec) {
681 exec.assign(force_exec);
682 } else if (!GetExecutableBaseNameFromPid(pid, &exec)) {
Ken Mixter1b8fe012011-01-25 13:33:05 -0800683 // If we cannot find the exec name, use the kernel supplied name.
684 // We don't always use the kernel's since it truncates the name to
685 // 16 characters.
686 exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str());
Ken Mixter777484c2010-07-23 16:22:44 -0700687 }
Ken Mixterc6a58e02010-11-01 18:05:30 -0700688
689 // Allow us to test the crash reporting mechanism successfully even if
690 // other parts of the system crash.
691 if (!FLAGS_filter_in.empty() &&
692 (FLAGS_filter_in == "none" ||
693 FLAGS_filter_in != exec)) {
694 // We use a different format message to make it more obvious in tests
695 // which crashes are test generated and which are real.
Ken Mixtera3249322011-03-03 08:47:38 -0800696 LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while "
697 << "filter_in=" << FLAGS_filter_in << ".";
Ken Mixterc6a58e02010-11-01 18:05:30 -0700698 return true;
699 }
700
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700701 std::string reason;
702 bool dump = ShouldDump(is_feedback_allowed_function_(),
Michael Krebs538ecbf2011-07-27 14:13:22 -0700703 IsDeveloperImage(),
Michael Krebs4fe30db2011-08-05 13:54:52 -0700704 ShouldHandleChromeCrashes(),
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700705 exec,
706 &reason);
Ken Mixter2105b492010-11-09 16:14:38 -0800707
Ken Mixtera3249322011-03-03 08:47:38 -0800708 LOG(WARNING) << "Received crash notification for " << exec << "[" << pid
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700709 << "] sig " << signal << ", user " << supplied_ruid
710 << " (" << reason << ")";
Chris Sosae4a86032010-06-16 17:08:34 -0700711
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700712 if (dump) {
Chris Sosae4a86032010-06-16 17:08:34 -0700713 count_crash_function_();
Ken Mixter777484c2010-07-23 16:22:44 -0700714
Ken Mixter03403162010-08-18 15:23:16 -0700715 if (generate_diagnostics_) {
Ken Mixter207694d2010-10-28 15:42:37 -0700716 bool out_of_capacity = false;
Ben Chan6e709a12012-02-29 12:10:44 -0800717 ErrorType error_type =
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700718 ConvertAndEnqueueCrash(pid, exec, supplied_ruid, &out_of_capacity);
Ben Chan6e709a12012-02-29 12:10:44 -0800719 if (error_type != kErrorNone) {
Ken Mixter207694d2010-10-28 15:42:37 -0700720 if (!out_of_capacity)
Ben Chan6e709a12012-02-29 12:10:44 -0800721 EnqueueCollectionErrorLog(pid, error_type, exec);
Ken Mixter207694d2010-10-28 15:42:37 -0700722 return false;
723 }
Ken Mixter03403162010-08-18 15:23:16 -0700724 }
Ken Mixter777484c2010-07-23 16:22:44 -0700725 }
Ken Mixter207694d2010-10-28 15:42:37 -0700726
Ken Mixter777484c2010-07-23 16:22:44 -0700727 return true;
Chris Sosae4a86032010-06-16 17:08:34 -0700728}