blob: 48b64e9171b165b242eac1489a568f5bdc69e8fa [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Chris Sosae4a86032010-06-16 17:08:34 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "user_collector.h"
Ken Mixter03403162010-08-18 15:23:16 -070018
Ben Chan6e709a12012-02-29 12:10:44 -080019#include <elf.h>
20#include <fcntl.h>
Ken Mixter777484c2010-07-23 16:22:44 -070021#include <grp.h> // For struct group.
Ken Mixter1b8fe012011-01-25 13:33:05 -080022#include <pcrecpp.h>
Ken Mixter777484c2010-07-23 16:22:44 -070023#include <pwd.h> // For struct passwd.
Ben Chanf84ea212014-08-06 17:27:48 -070024#include <stdint.h>
Steve Fung8bafb3d2015-08-07 13:22:46 -070025#include <sys/cdefs.h> // For __WORDSIZE
Steve Fung6db7cd72015-10-06 16:43:56 -070026#include <sys/fsuid.h>
Ken Mixter2953c3a2010-10-18 14:42:20 -070027#include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS.
Steve Fungb440e502015-08-21 02:12:34 -070028#include <unistd.h> // For setgroups
Ken Mixter777484c2010-07-23 16:22:44 -070029
Steve Fung6db7cd72015-10-06 16:43:56 -070030#include <iostream> // For std::oct
Chris Sosae4a86032010-06-16 17:08:34 -070031#include <string>
Ken Mixter2953c3a2010-10-18 14:42:20 -070032#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070033
Ben Chanab6cc902014-09-05 08:21:06 -070034#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070035#include <base/logging.h>
36#include <base/posix/eintr_wrapper.h>
Ben Chan7e776902014-06-18 13:19:51 -070037#include <base/strings/string_split.h>
38#include <base/strings/string_util.h>
39#include <base/strings/stringprintf.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070040#include <brillo/process.h>
41#include <brillo/syslog_logging.h>
Steve Fungab2ac7d2015-08-14 17:58:05 -070042#include <cutils/properties.h>
Steve Fungb440e502015-08-21 02:12:34 -070043#include <private/android_filesystem_config.h>
Ken Mixter207694d2010-10-28 15:42:37 -070044
45static const char kCollectionErrorSignature[] =
46 "crash_reporter-user-collection";
Steve Fungab2ac7d2015-08-14 17:58:05 -070047static const char kCorePatternProperty[] = "crash_reporter.coredump.enabled";
48static const char kCoreToMinidumpConverterPath[] = "/system/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
Steve Fungda981332015-08-23 17:18:23 -070052static const char kCoreTempFolder[] = "/data/misc/crash_reporter/tmp";
Steve Fungab2ac7d2015-08-14 17:58:05 -070053
Steve Fung773fd3c2015-10-09 17:01:35 -070054// Define an otherwise invalid value that represents an unknown UID and GID.
Michael Krebs1c57e9e2012-09-25 18:03:13 -070055static const uid_t kUnknownUid = -1;
Steve Fung773fd3c2015-10-09 17:01:35 -070056static const gid_t kUnknownGid = -1;
Michael Krebs1c57e9e2012-09-25 18:03:13 -070057
Ken Mixter777484c2010-07-23 16:22:44 -070058const char *UserCollector::kUserId = "Uid:\t";
59const char *UserCollector::kGroupId = "Gid:\t";
Chris Sosae4a86032010-06-16 17:08:34 -070060
Steve Fung48180112015-09-30 16:27:56 -070061
Simon Que9f90aca2013-02-19 17:19:52 -080062using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050063using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080064
Chris Sosae4a86032010-06-16 17:08:34 -070065UserCollector::UserCollector()
Ken Mixter777484c2010-07-23 16:22:44 -070066 : generate_diagnostics_(false),
Ken Mixter03403162010-08-18 15:23:16 -070067 initialized_(false) {
Chris Sosae4a86032010-06-16 17:08:34 -070068}
69
70void UserCollector::Initialize(
71 UserCollector::CountCrashFunction count_crash_function,
72 const std::string &our_path,
73 UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function,
Steve Fungd6169a22014-08-11 15:52:23 -070074 bool generate_diagnostics,
75 bool core2md_failure,
76 bool directory_failure,
77 const std::string &filter_in) {
Ken Mixter03403162010-08-18 15:23:16 -070078 CrashCollector::Initialize(count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080079 is_feedback_allowed_function);
Chris Sosae4a86032010-06-16 17:08:34 -070080 our_path_ = our_path;
Chris Sosae4a86032010-06-16 17:08:34 -070081 initialized_ = true;
Ken Mixter777484c2010-07-23 16:22:44 -070082 generate_diagnostics_ = generate_diagnostics;
Steve Fungd6169a22014-08-11 15:52:23 -070083 core2md_failure_ = core2md_failure;
84 directory_failure_ = directory_failure;
85 filter_in_ = filter_in;
Steve Fungb440e502015-08-21 02:12:34 -070086
Steve Funga76ba852015-11-11 17:50:37 -080087 gid_t groups[] = { AID_ROOT, AID_SYSTEM, AID_DBUS, AID_READPROC };
Steve Fungb440e502015-08-21 02:12:34 -070088 if (setgroups(arraysize(groups), groups) != 0) {
Steve Funga76ba852015-11-11 17:50:37 -080089 PLOG(FATAL) << "Unable to set groups to root, system, dbus, and readproc";
Steve Fungb440e502015-08-21 02:12:34 -070090 }
Chris Sosae4a86032010-06-16 17:08:34 -070091}
92
93UserCollector::~UserCollector() {
94}
95
Ben Chan6e709a12012-02-29 12:10:44 -080096std::string UserCollector::GetErrorTypeSignature(ErrorType error_type) const {
97 switch (error_type) {
98 case kErrorSystemIssue:
99 return "system-issue";
100 case kErrorReadCoreData:
101 return "read-core-data";
102 case kErrorUnusableProcFiles:
103 return "unusable-proc-files";
104 case kErrorInvalidCoreFile:
105 return "invalid-core-file";
106 case kErrorUnsupported32BitCoreFile:
107 return "unsupported-32bit-core-file";
108 case kErrorCore2MinidumpConversion:
109 return "core2md-conversion";
110 default:
111 return "";
112 }
113}
114
Chris Sosae4a86032010-06-16 17:08:34 -0700115bool UserCollector::SetUpInternal(bool enabled) {
116 CHECK(initialized_);
Ken Mixtera3249322011-03-03 08:47:38 -0800117 LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling";
118
Steve Fungab2ac7d2015-08-14 17:58:05 -0700119 property_set(kCorePatternProperty, enabled ? "1" : "0");
120
Chris Sosae4a86032010-06-16 17:08:34 -0700121 return true;
122}
123
Ben Chanf13bb582012-01-06 08:22:07 -0800124bool UserCollector::GetFirstLineWithPrefix(
125 const std::vector<std::string> &lines,
126 const char *prefix, std::string *line) {
127 std::vector<std::string>::const_iterator line_iterator;
128 for (line_iterator = lines.begin(); line_iterator != lines.end();
129 ++line_iterator) {
130 if (line_iterator->find(prefix) == 0) {
131 *line = *line_iterator;
132 return true;
133 }
134 }
135 return false;
136}
137
138bool UserCollector::GetIdFromStatus(
139 const char *prefix, IdKind kind,
140 const std::vector<std::string> &status_lines, int *id) {
Ken Mixter777484c2010-07-23 16:22:44 -0700141 // From fs/proc/array.c:task_state(), this file contains:
142 // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n
Ben Chanf13bb582012-01-06 08:22:07 -0800143 std::string id_line;
144 if (!GetFirstLineWithPrefix(status_lines, prefix, &id_line)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700145 return false;
146 }
Ben Chanf13bb582012-01-06 08:22:07 -0800147 std::string id_substring = id_line.substr(strlen(prefix), std::string::npos);
Alex Vakulenkoea05ff92016-01-20 07:53:57 -0800148 std::vector<std::string> ids = base::SplitString(
149 id_substring, "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
Ken Mixter777484c2010-07-23 16:22:44 -0700150 if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) {
151 return false;
152 }
153 const char *number = ids[kind].c_str();
Ben Chan262d7982014-09-18 08:05:20 -0700154 char *end_number = nullptr;
Ken Mixter777484c2010-07-23 16:22:44 -0700155 *id = strtol(number, &end_number, 10);
Ben Chanf13bb582012-01-06 08:22:07 -0800156 if (*end_number != '\0') {
Ken Mixter777484c2010-07-23 16:22:44 -0700157 return false;
Ben Chanf13bb582012-01-06 08:22:07 -0800158 }
159 return true;
160}
161
162bool UserCollector::GetStateFromStatus(
163 const std::vector<std::string> &status_lines, std::string *state) {
164 std::string state_line;
165 if (!GetFirstLineWithPrefix(status_lines, kStatePrefix, &state_line)) {
166 return false;
167 }
168 *state = state_line.substr(strlen(kStatePrefix), std::string::npos);
Ken Mixter777484c2010-07-23 16:22:44 -0700169 return true;
170}
171
Ken Mixter207694d2010-10-28 15:42:37 -0700172void UserCollector::EnqueueCollectionErrorLog(pid_t pid,
Ben Chan6e709a12012-02-29 12:10:44 -0800173 ErrorType error_type,
Ken Mixter207694d2010-10-28 15:42:37 -0700174 const std::string &exec) {
175 FilePath crash_path;
Ken Mixtera3249322011-03-03 08:47:38 -0800176 LOG(INFO) << "Writing conversion problems as separate crash report.";
Ben Chan262d7982014-09-18 08:05:20 -0700177 if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, nullptr)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800178 LOG(ERROR) << "Could not even get log directory; out of space?";
Ken Mixter207694d2010-10-28 15:42:37 -0700179 return;
180 }
Thiemo Nagel8fce2852014-05-09 14:48:45 +0200181 AddCrashMetaData("sig", kCollectionErrorSignature);
182 AddCrashMetaData("error_type", GetErrorTypeSignature(error_type));
Ben Chan262d7982014-09-18 08:05:20 -0700183 std::string dump_basename = FormatDumpBasename(exec, time(nullptr), pid);
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700184 std::string error_log = brillo::GetLog();
Ken Mixter1b8fe012011-01-25 13:33:05 -0800185 FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog");
Simon Queacc79382012-05-04 18:10:09 -0700186 if (GetLogContents(FilePath(log_config_path_), kCollectionErrorSignature,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800187 diag_log_path)) {
188 // We load the contents of diag_log into memory and append it to
189 // the error log. We cannot just append to files because we need
190 // to always create new files to prevent attack.
191 std::string diag_log_contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500192 base::ReadFileToString(diag_log_path, &diag_log_contents);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800193 error_log.append(diag_log_contents);
Mike Frysingera557c112014-02-05 22:55:39 -0500194 base::DeleteFile(diag_log_path, false);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800195 }
Ken Mixter207694d2010-10-28 15:42:37 -0700196 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
197 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
Ben Chanf30c6412014-05-22 23:09:01 -0700198 // We must use WriteNewFile instead of base::WriteFile as we do
Ken Mixter9b346472010-11-07 13:45:45 -0800199 // not want to write with root access to a symlink that an attacker
200 // might have created.
Thiemo Nagel8fce2852014-05-09 14:48:45 +0200201 if (WriteNewFile(log_path, error_log.data(), error_log.length()) < 0) {
202 LOG(ERROR) << "Error writing new file " << log_path.value();
203 return;
204 }
Ken Mixter207694d2010-10-28 15:42:37 -0700205 WriteCrashMetaData(meta_path, exec, log_path.value());
206}
207
Ken Mixter777484c2010-07-23 16:22:44 -0700208bool UserCollector::CopyOffProcFiles(pid_t pid,
209 const FilePath &container_dir) {
Mike Frysingera557c112014-02-05 22:55:39 -0500210 if (!base::CreateDirectory(container_dir)) {
Steve Fung6db7cd72015-10-06 16:43:56 -0700211 PLOG(ERROR) << "Could not create " << container_dir.value();
212 return false;
213 }
214 int dir_mask = base::FILE_PERMISSION_READ_BY_USER
215 | base::FILE_PERMISSION_WRITE_BY_USER
216 | base::FILE_PERMISSION_EXECUTE_BY_USER
217 | base::FILE_PERMISSION_READ_BY_GROUP
218 | base::FILE_PERMISSION_WRITE_BY_GROUP;
219 if (!base::SetPosixFilePermissions(container_dir,
220 base::FILE_PERMISSION_MASK & dir_mask)) {
221 PLOG(ERROR) << "Could not set permissions for " << container_dir.value()
222 << " to " << std::oct
223 << (base::FILE_PERMISSION_MASK & dir_mask);
Ken Mixter777484c2010-07-23 16:22:44 -0700224 return false;
225 }
226 FilePath process_path = GetProcessPath(pid);
Mike Frysingera557c112014-02-05 22:55:39 -0500227 if (!base::PathExists(process_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800228 LOG(ERROR) << "Path " << process_path.value() << " does not exist";
Ken Mixter777484c2010-07-23 16:22:44 -0700229 return false;
230 }
231 static const char *proc_files[] = {
232 "auxv",
233 "cmdline",
234 "environ",
235 "maps",
236 "status"
237 };
238 for (unsigned i = 0; i < arraysize(proc_files); ++i) {
Mike Frysingera557c112014-02-05 22:55:39 -0500239 if (!base::CopyFile(process_path.Append(proc_files[i]),
240 container_dir.Append(proc_files[i]))) {
Ken Mixtera3249322011-03-03 08:47:38 -0800241 LOG(ERROR) << "Could not copy " << proc_files[i] << " file";
Ken Mixter777484c2010-07-23 16:22:44 -0700242 return false;
243 }
244 }
Ben Chanec7d7832012-01-09 10:29:58 -0800245 return true;
Ben Chanf13bb582012-01-06 08:22:07 -0800246}
247
Ben Chan6e709a12012-02-29 12:10:44 -0800248bool UserCollector::ValidateProcFiles(const FilePath &container_dir) const {
Ben Chanf13bb582012-01-06 08:22:07 -0800249 // Check if the maps file is empty, which could be due to the crashed
250 // process being reaped by the kernel before finishing a core dump.
Ben Chanf84ea212014-08-06 17:27:48 -0700251 int64_t file_size = 0;
Mike Frysingera557c112014-02-05 22:55:39 -0500252 if (!base::GetFileSize(container_dir.Append("maps"), &file_size)) {
Ben Chanf13bb582012-01-06 08:22:07 -0800253 LOG(ERROR) << "Could not get the size of maps file";
254 return false;
255 }
256 if (file_size == 0) {
257 LOG(ERROR) << "maps file is empty";
258 return false;
259 }
Ken Mixter777484c2010-07-23 16:22:44 -0700260 return true;
261}
262
Ben Chan6e709a12012-02-29 12:10:44 -0800263UserCollector::ErrorType UserCollector::ValidateCoreFile(
264 const FilePath &core_path) const {
265 int fd = HANDLE_EINTR(open(core_path.value().c_str(), O_RDONLY));
266 if (fd < 0) {
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700267 PLOG(ERROR) << "Could not open core file " << core_path.value();
Ben Chan6e709a12012-02-29 12:10:44 -0800268 return kErrorInvalidCoreFile;
269 }
270
271 char e_ident[EI_NIDENT];
Mike Frysingera557c112014-02-05 22:55:39 -0500272 bool read_ok = base::ReadFromFD(fd, e_ident, sizeof(e_ident));
Mike Frysingerf1a50142014-05-14 16:05:09 -0400273 IGNORE_EINTR(close(fd));
Ben Chan6e709a12012-02-29 12:10:44 -0800274 if (!read_ok) {
275 LOG(ERROR) << "Could not read header of core file";
276 return kErrorInvalidCoreFile;
277 }
278
279 if (e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 ||
280 e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3) {
281 LOG(ERROR) << "Invalid core file";
282 return kErrorInvalidCoreFile;
283 }
284
285#if __WORDSIZE == 64
286 // TODO(benchan, mkrebs): Remove this check once core2md can
287 // handles both 32-bit and 64-bit ELF on a 64-bit platform.
288 if (e_ident[EI_CLASS] == ELFCLASS32) {
289 LOG(ERROR) << "Conversion of 32-bit core file on 64-bit platform is "
290 << "currently not supported";
291 return kErrorUnsupported32BitCoreFile;
292 }
293#endif
294
295 return kErrorNone;
296}
297
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700298bool UserCollector::GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid,
Ken Mixter207694d2010-10-28 15:42:37 -0700299 FilePath *crash_file_path,
300 bool *out_of_capacity) {
Ken Mixter777484c2010-07-23 16:22:44 -0700301 FilePath process_path = GetProcessPath(pid);
302 std::string status;
Steve Fungd6169a22014-08-11 15:52:23 -0700303 if (directory_failure_) {
Ken Mixtera3249322011-03-03 08:47:38 -0800304 LOG(ERROR) << "Purposefully failing to create spool directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700305 return false;
306 }
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700307
308 uid_t uid;
Mike Frysingera557c112014-02-05 22:55:39 -0500309 if (base::ReadFileToString(process_path.Append("status"), &status)) {
Alex Vakulenkoea05ff92016-01-20 07:53:57 -0800310 std::vector<std::string> status_lines = base::SplitString(
311 status, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700312
313 std::string process_state;
314 if (!GetStateFromStatus(status_lines, &process_state)) {
315 LOG(ERROR) << "Could not find process state in status file";
316 return false;
317 }
318 LOG(INFO) << "State of crashed process [" << pid << "]: " << process_state;
319
320 // Get effective UID of crashing process.
321 int id;
322 if (!GetIdFromStatus(kUserId, kIdEffective, status_lines, &id)) {
323 LOG(ERROR) << "Could not find euid in status file";
324 return false;
325 }
326 uid = id;
327 } else if (supplied_ruid != kUnknownUid) {
328 LOG(INFO) << "Using supplied UID " << supplied_ruid
329 << " for crashed process [" << pid
330 << "] due to error reading status file";
331 uid = supplied_ruid;
332 } else {
333 LOG(ERROR) << "Could not read status file and kernel did not supply UID";
Ken Mixtera3249322011-03-03 08:47:38 -0800334 LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500335 << base::DirectoryExists(process_path);
Ken Mixter777484c2010-07-23 16:22:44 -0700336 return false;
337 }
Ben Chanf13bb582012-01-06 08:22:07 -0800338
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700339 if (!GetCreatedCrashDirectoryByEuid(uid, crash_file_path, out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800340 LOG(ERROR) << "Could not create crash directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700341 return false;
342 }
343 return true;
Ken Mixter777484c2010-07-23 16:22:44 -0700344}
345
346bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) {
347 // Copy off all stdin to a core file.
Steve Fungab2ac7d2015-08-14 17:58:05 -0700348 FilePath stdin_path("/proc/self/fd/0");
Mike Frysingera557c112014-02-05 22:55:39 -0500349 if (base::CopyFile(stdin_path, core_path)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700350 return true;
351 }
352
Chris Masoneb3fe6c32013-05-31 09:37:33 -0700353 PLOG(ERROR) << "Could not write core file";
Ken Mixter777484c2010-07-23 16:22:44 -0700354 // If the file system was full, make sure we remove any remnants.
Mike Frysingera557c112014-02-05 22:55:39 -0500355 base::DeleteFile(core_path, false);
Ken Mixter777484c2010-07-23 16:22:44 -0700356 return false;
357}
358
Ken Mixter207694d2010-10-28 15:42:37 -0700359bool UserCollector::RunCoreToMinidump(const FilePath &core_path,
360 const FilePath &procfs_directory,
361 const FilePath &minidump_path,
362 const FilePath &temp_directory) {
Ken Mixter777484c2010-07-23 16:22:44 -0700363 FilePath output_path = temp_directory.Append("output");
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700364 brillo::ProcessImpl core2md;
Ken Mixtera3249322011-03-03 08:47:38 -0800365 core2md.RedirectOutput(output_path.value());
366 core2md.AddArg(kCoreToMinidumpConverterPath);
367 core2md.AddArg(core_path.value());
368 core2md.AddArg(procfs_directory.value());
Ken Mixter2953c3a2010-10-18 14:42:20 -0700369
Steve Fungd6169a22014-08-11 15:52:23 -0700370 if (!core2md_failure_) {
Ken Mixtera3249322011-03-03 08:47:38 -0800371 core2md.AddArg(minidump_path.value());
372 } else {
Ken Mixter207694d2010-10-28 15:42:37 -0700373 // To test how core2md errors are propagaged, cause an error
374 // by forgetting a required argument.
Ken Mixter207694d2010-10-28 15:42:37 -0700375 }
376
Ken Mixtera3249322011-03-03 08:47:38 -0800377 int errorlevel = core2md.Run();
Ken Mixter777484c2010-07-23 16:22:44 -0700378
379 std::string output;
Mike Frysingera557c112014-02-05 22:55:39 -0500380 base::ReadFileToString(output_path, &output);
Ken Mixter777484c2010-07-23 16:22:44 -0700381 if (errorlevel != 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800382 LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath
383 << " [result=" << errorlevel << "]: " << output;
Ken Mixter777484c2010-07-23 16:22:44 -0700384 return false;
385 }
386
Mike Frysingera557c112014-02-05 22:55:39 -0500387 if (!base::PathExists(minidump_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800388 LOG(ERROR) << "Minidump file " << minidump_path.value()
389 << " was not created";
Ken Mixter777484c2010-07-23 16:22:44 -0700390 return false;
391 }
392 return true;
393}
394
Ben Chan6e709a12012-02-29 12:10:44 -0800395UserCollector::ErrorType UserCollector::ConvertCoreToMinidump(
396 pid_t pid,
397 const FilePath &container_dir,
398 const FilePath &core_path,
399 const FilePath &minidump_path) {
Ben Chanec7d7832012-01-09 10:29:58 -0800400 // If proc files are unuable, we continue to read the core file from stdin,
401 // but only skip the core-to-minidump conversion, so that we may still use
402 // the core file for debugging.
403 bool proc_files_usable =
404 CopyOffProcFiles(pid, container_dir) && ValidateProcFiles(container_dir);
405
Steve Fung6db7cd72015-10-06 16:43:56 -0700406 // Switch back to the original UID/GID.
407 gid_t rgid, egid, sgid;
408 if (getresgid(&rgid, &egid, &sgid) != 0) {
409 PLOG(FATAL) << "Unable to read saved gid";
410 }
411 if (setresgid(sgid, sgid, -1) != 0) {
412 PLOG(FATAL) << "Unable to set real group ID back to saved gid";
413 } else {
414 if (getresgid(&rgid, &egid, &sgid) != 0) {
415 // If the groups cannot be read at this point, the rgid variable will
416 // contain the previously read group ID from before changing it. This
417 // will cause the chown call below to set the incorrect group for
418 // non-root crashes. But do not treat this as a fatal error, so that
419 // the rest of the collection will continue for potential manual
420 // collection by a developer.
421 PLOG(ERROR) << "Unable to read real group ID after setting it";
422 }
423 }
424
425 uid_t ruid, euid, suid;
426 if (getresuid(&ruid, &euid, &suid) != 0) {
427 PLOG(FATAL) << "Unable to read saved uid";
428 }
429 if (setresuid(suid, suid, -1) != 0) {
430 PLOG(FATAL) << "Unable to set real user ID back to saved uid";
431 } else {
432 if (getresuid(&ruid, &euid, &suid) != 0) {
433 // If the user ID cannot be read at this point, the ruid variable will
434 // contain the previously read user ID from before changing it. This
435 // will cause the chown call below to set the incorrect user for
436 // non-root crashes. But do not treat this as a fatal error, so that
437 // the rest of the collection will continue for potential manual
438 // collection by a developer.
439 PLOG(ERROR) << "Unable to read real user ID after setting it";
440 }
441 }
442
Ben Chanec7d7832012-01-09 10:29:58 -0800443 if (!CopyStdinToCoreFile(core_path)) {
Ben Chan6e709a12012-02-29 12:10:44 -0800444 return kErrorReadCoreData;
Ken Mixter777484c2010-07-23 16:22:44 -0700445 }
446
Ben Chanec7d7832012-01-09 10:29:58 -0800447 if (!proc_files_usable) {
448 LOG(INFO) << "Skipped converting core file to minidump due to "
449 << "unusable proc files";
Ben Chan6e709a12012-02-29 12:10:44 -0800450 return kErrorUnusableProcFiles;
Ken Mixter777484c2010-07-23 16:22:44 -0700451 }
452
Ben Chan6e709a12012-02-29 12:10:44 -0800453 ErrorType error = ValidateCoreFile(core_path);
454 if (error != kErrorNone) {
455 return error;
Ken Mixter777484c2010-07-23 16:22:44 -0700456 }
457
Steve Fung6db7cd72015-10-06 16:43:56 -0700458 // Chown the temp container directory back to the original user/group that
459 // crash_reporter is run as, so that additional files can be written to
460 // the temp folder.
461 if (chown(container_dir.value().c_str(), ruid, rgid) < 0) {
462 PLOG(ERROR) << "Could not set owner for " << container_dir.value();
463 }
464
Ben Chan6e709a12012-02-29 12:10:44 -0800465 if (!RunCoreToMinidump(core_path,
466 container_dir, // procfs directory
467 minidump_path,
468 container_dir)) { // temporary directory
469 return kErrorCore2MinidumpConversion;
470 }
471
472 LOG(INFO) << "Stored minidump to " << minidump_path.value();
473 return kErrorNone;
Ken Mixter207694d2010-10-28 15:42:37 -0700474}
475
Ben Chan6e709a12012-02-29 12:10:44 -0800476UserCollector::ErrorType UserCollector::ConvertAndEnqueueCrash(
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700477 pid_t pid, const std::string &exec, uid_t supplied_ruid,
478 bool *out_of_capacity) {
Ken Mixter207694d2010-10-28 15:42:37 -0700479 FilePath crash_path;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700480 if (!GetCreatedCrashDirectory(pid, supplied_ruid, &crash_path,
481 out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800482 LOG(ERROR) << "Unable to find/create process-specific crash path";
Ben Chan6e709a12012-02-29 12:10:44 -0800483 return kErrorSystemIssue;
Ken Mixter207694d2010-10-28 15:42:37 -0700484 }
485
Ben Chan294d5d12012-01-04 20:40:15 -0800486 // Directory like /tmp/crash_reporter/1234 which contains the
Ken Mixter207694d2010-10-28 15:42:37 -0700487 // procfs entries and other temporary files used during conversion.
Steve Fungab2ac7d2015-08-14 17:58:05 -0700488 FilePath container_dir(StringPrintf("%s/%d", kCoreTempFolder, pid));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800489 // Delete a pre-existing directory from crash reporter that may have
490 // been left around for diagnostics from a failed conversion attempt.
491 // If we don't, existing files can cause forking to fail.
Mike Frysingera557c112014-02-05 22:55:39 -0500492 base::DeleteFile(container_dir, true);
Ben Chan262d7982014-09-18 08:05:20 -0700493 std::string dump_basename = FormatDumpBasename(exec, time(nullptr), pid);
Ken Mixter207694d2010-10-28 15:42:37 -0700494 FilePath core_path = GetCrashPath(crash_path, dump_basename, "core");
495 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
496 FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp");
Ken Mixterc49dbd42010-12-14 17:44:11 -0800497 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
498
Simon Queacc79382012-05-04 18:10:09 -0700499 if (GetLogContents(FilePath(log_config_path_), exec, log_path))
Ken Mixterc49dbd42010-12-14 17:44:11 -0800500 AddCrashMetaData("log", log_path.value());
Ken Mixter207694d2010-10-28 15:42:37 -0700501
Ben Chan6e709a12012-02-29 12:10:44 -0800502 ErrorType error_type =
503 ConvertCoreToMinidump(pid, container_dir, core_path, minidump_path);
504 if (error_type != kErrorNone) {
Ken Mixtera3249322011-03-03 08:47:38 -0800505 LOG(INFO) << "Leaving core file at " << core_path.value()
506 << " due to conversion error";
Ben Chan6e709a12012-02-29 12:10:44 -0800507 return error_type;
Ken Mixter207694d2010-10-28 15:42:37 -0700508 }
509
510 // Here we commit to sending this file. We must not return false
511 // after this point or we will generate a log report as well as a
512 // crash report.
513 WriteCrashMetaData(meta_path,
514 exec,
515 minidump_path.value());
516
Michael Krebs538ecbf2011-07-27 14:13:22 -0700517 if (!IsDeveloperImage()) {
Mike Frysingera557c112014-02-05 22:55:39 -0500518 base::DeleteFile(core_path, false);
Ken Mixter777484c2010-07-23 16:22:44 -0700519 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800520 LOG(INFO) << "Leaving core file at " << core_path.value()
521 << " due to developer image";
Ken Mixter777484c2010-07-23 16:22:44 -0700522 }
523
Mike Frysingera557c112014-02-05 22:55:39 -0500524 base::DeleteFile(container_dir, true);
Ben Chan6e709a12012-02-29 12:10:44 -0800525 return kErrorNone;
Ken Mixter777484c2010-07-23 16:22:44 -0700526}
527
Ken Mixter1b8fe012011-01-25 13:33:05 -0800528bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes,
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700529 pid_t *pid, int *signal, uid_t *uid,
Steve Fung773fd3c2015-10-09 17:01:35 -0700530 gid_t *gid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800531 std::string *kernel_supplied_name) {
Steve Fung773fd3c2015-10-09 17:01:35 -0700532 pcrecpp::RE re("(\\d+):(\\d+):(\\d+):(\\d+):(.*)");
533 if (re.FullMatch(crash_attributes, pid, signal, uid, gid,
534 kernel_supplied_name))
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700535 return true;
536
537 LOG(INFO) << "Falling back to parsing crash attributes '"
Steve Fung773fd3c2015-10-09 17:01:35 -0700538 << crash_attributes << "' without UID and GID";
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700539 pcrecpp::RE re_without_uid("(\\d+):(\\d+):(.*)");
540 *uid = kUnknownUid;
Steve Fung773fd3c2015-10-09 17:01:35 -0700541 *gid = kUnknownGid;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700542 return re_without_uid.FullMatch(crash_attributes, pid, signal,
543 kernel_supplied_name);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800544}
545
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700546bool UserCollector::ShouldDump(bool has_owner_consent,
547 bool is_developer,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700548 std::string *reason) {
549 reason->clear();
550
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700551 // For developer builds, we always want to keep the crash reports unless
552 // we're testing the crash facilities themselves. This overrides
553 // feedback. Crash sending still obeys consent.
Michael Krebs538ecbf2011-07-27 14:13:22 -0700554 if (is_developer) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700555 *reason = "developer build - not testing - always dumping";
556 return true;
557 }
558
559 if (!has_owner_consent) {
560 *reason = "ignoring - no consent";
561 return false;
562 }
563
564 *reason = "handling";
565 return true;
566}
567
Ken Mixter1b8fe012011-01-25 13:33:05 -0800568bool UserCollector::HandleCrash(const std::string &crash_attributes,
569 const char *force_exec) {
Chris Sosae4a86032010-06-16 17:08:34 -0700570 CHECK(initialized_);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700571 pid_t pid = 0;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800572 int signal = 0;
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700573 uid_t supplied_ruid = kUnknownUid;
Steve Fung773fd3c2015-10-09 17:01:35 -0700574 gid_t supplied_rgid = kUnknownGid;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800575 std::string kernel_supplied_name;
576
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700577 if (!ParseCrashAttributes(crash_attributes, &pid, &signal, &supplied_ruid,
Steve Fung773fd3c2015-10-09 17:01:35 -0700578 &supplied_rgid, &kernel_supplied_name)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800579 LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800580 return false;
581 }
582
Steve Fung6db7cd72015-10-06 16:43:56 -0700583 // Switch to the group and user that ran the crashing binary in order to
584 // access their /proc files. Do not set suid/sgid, so that we can switch
585 // back after copying the necessary files.
Steve Fung773fd3c2015-10-09 17:01:35 -0700586 if (setresgid(supplied_rgid, supplied_rgid, -1) != 0) {
Steve Fung6db7cd72015-10-06 16:43:56 -0700587 PLOG(FATAL) << "Unable to set real group ID to access process files";
588 }
589 if (setresuid(supplied_ruid, supplied_ruid, -1) != 0) {
590 PLOG(FATAL) << "Unable to set real user ID to access process files";
591 }
592
Ken Mixter777484c2010-07-23 16:22:44 -0700593 std::string exec;
594 if (force_exec) {
595 exec.assign(force_exec);
596 } else if (!GetExecutableBaseNameFromPid(pid, &exec)) {
Ken Mixter1b8fe012011-01-25 13:33:05 -0800597 // If we cannot find the exec name, use the kernel supplied name.
598 // We don't always use the kernel's since it truncates the name to
599 // 16 characters.
600 exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str());
Ken Mixter777484c2010-07-23 16:22:44 -0700601 }
Ken Mixterc6a58e02010-11-01 18:05:30 -0700602
603 // Allow us to test the crash reporting mechanism successfully even if
604 // other parts of the system crash.
Steve Fungd6169a22014-08-11 15:52:23 -0700605 if (!filter_in_.empty() &&
606 (filter_in_ == "none" ||
607 filter_in_ != exec)) {
Ken Mixterc6a58e02010-11-01 18:05:30 -0700608 // We use a different format message to make it more obvious in tests
609 // which crashes are test generated and which are real.
Ken Mixtera3249322011-03-03 08:47:38 -0800610 LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while "
Steve Fungd6169a22014-08-11 15:52:23 -0700611 << "filter_in=" << filter_in_ << ".";
Ken Mixterc6a58e02010-11-01 18:05:30 -0700612 return true;
613 }
614
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700615 std::string reason;
616 bool dump = ShouldDump(is_feedback_allowed_function_(),
Michael Krebs538ecbf2011-07-27 14:13:22 -0700617 IsDeveloperImage(),
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700618 &reason);
Ken Mixter2105b492010-11-09 16:14:38 -0800619
Ken Mixtera3249322011-03-03 08:47:38 -0800620 LOG(WARNING) << "Received crash notification for " << exec << "[" << pid
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700621 << "] sig " << signal << ", user " << supplied_ruid
622 << " (" << reason << ")";
Chris Sosae4a86032010-06-16 17:08:34 -0700623
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700624 if (dump) {
Chris Sosae4a86032010-06-16 17:08:34 -0700625 count_crash_function_();
Ken Mixter777484c2010-07-23 16:22:44 -0700626
Ken Mixter03403162010-08-18 15:23:16 -0700627 if (generate_diagnostics_) {
Ken Mixter207694d2010-10-28 15:42:37 -0700628 bool out_of_capacity = false;
Ben Chan6e709a12012-02-29 12:10:44 -0800629 ErrorType error_type =
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700630 ConvertAndEnqueueCrash(pid, exec, supplied_ruid, &out_of_capacity);
Ben Chan6e709a12012-02-29 12:10:44 -0800631 if (error_type != kErrorNone) {
Ken Mixter207694d2010-10-28 15:42:37 -0700632 if (!out_of_capacity)
Ben Chan6e709a12012-02-29 12:10:44 -0800633 EnqueueCollectionErrorLog(pid, error_type, exec);
Ken Mixter207694d2010-10-28 15:42:37 -0700634 return false;
635 }
Ken Mixter03403162010-08-18 15:23:16 -0700636 }
Ken Mixter777484c2010-07-23 16:22:44 -0700637 }
Ken Mixter207694d2010-10-28 15:42:37 -0700638
Ken Mixter777484c2010-07-23 16:22:44 -0700639 return true;
Chris Sosae4a86032010-06-16 17:08:34 -0700640}