blob: 9600b44a5edbc8ef6294ad7a935187c46bdec040 [file] [log] [blame]
Ken Mixter03403162010-08-18 15:23:16 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "crash-reporter/crash_collector.h"
6
Ken Mixter04ec10f2010-08-26 16:02:02 -07007#include <dirent.h>
Ken Mixter9b346472010-11-07 13:45:45 -08008#include <fcntl.h> // For file creation modes.
Ken Mixter03403162010-08-18 15:23:16 -07009#include <pwd.h> // For struct passwd.
10#include <sys/types.h> // for mode_t.
Ken Mixter9b346472010-11-07 13:45:45 -080011#include <sys/wait.h> // For waitpid.
12#include <unistd.h> // For execv and fork.
Ken Mixter03403162010-08-18 15:23:16 -070013
Ken Mixteree849c52010-09-30 15:30:10 -070014#include <set>
15
Ken Mixter9b346472010-11-07 13:45:45 -080016#include "base/eintr_wrapper.h"
Ken Mixter03403162010-08-18 15:23:16 -070017#include "base/file_util.h"
18#include "base/logging.h"
19#include "base/string_util.h"
Ken Mixtera3249322011-03-03 08:47:38 -080020#include "chromeos/process.h"
Ken Mixter03403162010-08-18 15:23:16 -070021
22static const char kDefaultUserName[] = "chronos";
Ken Mixteree849c52010-09-30 15:30:10 -070023static const char kLsbRelease[] = "/etc/lsb-release";
Ken Mixterc49dbd42010-12-14 17:44:11 -080024static const char kShellPath[] = "/bin/sh";
Ken Mixter03403162010-08-18 15:23:16 -070025static const char kSystemCrashPath[] = "/var/spool/crash";
26static const char kUserCrashPath[] = "/home/chronos/user/crash";
27
28// Directory mode of the user crash spool directory.
29static const mode_t kUserCrashPathMode = 0755;
30
31// Directory mode of the system crash spool directory.
32static const mode_t kSystemCrashPathMode = 01755;
33
34static const uid_t kRootOwner = 0;
35static const uid_t kRootGroup = 0;
36
Ken Mixterda5db7a2010-09-17 13:50:42 -070037// Maximum crash reports per crash spool directory. Note that this is
38// a separate maximum from the maximum rate at which we upload these
39// diagnostics. The higher this rate is, the more space we allow for
40// core files, minidumps, and kcrash logs, and equivalently the more
41// processor and I/O bandwidth we dedicate to handling these crashes when
42// many occur at once. Also note that if core files are configured to
43// be left on the file system, we stop adding crashes when either the
44// number of core files or minidumps reaches this number.
45const int CrashCollector::kMaxCrashDirectorySize = 32;
Ken Mixter04ec10f2010-08-26 16:02:02 -070046
Ken Mixterafcf8082010-10-26 14:45:01 -070047CrashCollector::CrashCollector()
48 : forced_crash_directory_(NULL),
49 lsb_release_(kLsbRelease) {
Ken Mixter03403162010-08-18 15:23:16 -070050}
51
52CrashCollector::~CrashCollector() {
53}
54
55void CrashCollector::Initialize(
56 CrashCollector::CountCrashFunction count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080057 CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) {
Ken Mixter03403162010-08-18 15:23:16 -070058 CHECK(count_crash_function != NULL);
59 CHECK(is_feedback_allowed_function != NULL);
Ken Mixter03403162010-08-18 15:23:16 -070060
61 count_crash_function_ = count_crash_function;
62 is_feedback_allowed_function_ = is_feedback_allowed_function;
Ken Mixter03403162010-08-18 15:23:16 -070063}
64
Ken Mixter9b346472010-11-07 13:45:45 -080065int CrashCollector::WriteNewFile(const FilePath &filename,
66 const char *data,
67 int size) {
68 int fd = HANDLE_EINTR(open(filename.value().c_str(),
69 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666));
70 if (fd < 0) {
71 return -1;
72 }
73
74 int rv = file_util::WriteFileDescriptor(fd, data, size);
75 HANDLE_EINTR(close(fd));
76 return rv;
77}
78
Ken Mixteree849c52010-09-30 15:30:10 -070079std::string CrashCollector::Sanitize(const std::string &name) {
80 std::string result = name;
81 for (size_t i = 0; i < name.size(); ++i) {
82 if (!isalnum(result[i]) && result[i] != '_')
83 result[i] = '_';
84 }
85 return result;
86}
87
Ken Mixter03403162010-08-18 15:23:16 -070088std::string CrashCollector::FormatDumpBasename(const std::string &exec_name,
89 time_t timestamp,
90 pid_t pid) {
91 struct tm tm;
92 localtime_r(&timestamp, &tm);
Ken Mixteree849c52010-09-30 15:30:10 -070093 std::string sanitized_exec_name = Sanitize(exec_name);
Ken Mixter03403162010-08-18 15:23:16 -070094 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d",
Ken Mixteree849c52010-09-30 15:30:10 -070095 sanitized_exec_name.c_str(),
Ken Mixter03403162010-08-18 15:23:16 -070096 tm.tm_year + 1900,
97 tm.tm_mon + 1,
98 tm.tm_mday,
99 tm.tm_hour,
100 tm.tm_min,
101 tm.tm_sec,
102 pid);
103}
104
Ken Mixter207694d2010-10-28 15:42:37 -0700105FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory,
106 const std::string &basename,
107 const std::string &extension) {
108 return crash_directory.Append(StringPrintf("%s.%s",
109 basename.c_str(),
110 extension.c_str()));
111}
112
Ken Mixter03403162010-08-18 15:23:16 -0700113FilePath CrashCollector::GetCrashDirectoryInfo(
114 uid_t process_euid,
115 uid_t default_user_id,
116 gid_t default_user_group,
117 mode_t *mode,
118 uid_t *directory_owner,
119 gid_t *directory_group) {
120 if (process_euid == default_user_id) {
121 *mode = kUserCrashPathMode;
122 *directory_owner = default_user_id;
123 *directory_group = default_user_group;
124 return FilePath(kUserCrashPath);
125 } else {
126 *mode = kSystemCrashPathMode;
127 *directory_owner = kRootOwner;
128 *directory_group = kRootGroup;
129 return FilePath(kSystemCrashPath);
130 }
131}
132
133bool CrashCollector::GetUserInfoFromName(const std::string &name,
134 uid_t *uid,
135 gid_t *gid) {
136 char storage[256];
137 struct passwd passwd_storage;
138 struct passwd *passwd_result = NULL;
139
140 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage),
141 &passwd_result) != 0 || passwd_result == NULL) {
Ken Mixtera3249322011-03-03 08:47:38 -0800142 LOG(ERROR) << "Cannot find user named " << name;
Ken Mixter03403162010-08-18 15:23:16 -0700143 return false;
144 }
145
146 *uid = passwd_result->pw_uid;
147 *gid = passwd_result->pw_gid;
148 return true;
149}
150
151bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid,
Ken Mixter207694d2010-10-28 15:42:37 -0700152 FilePath *crash_directory,
153 bool *out_of_capacity) {
Ken Mixter03403162010-08-18 15:23:16 -0700154 uid_t default_user_id;
155 gid_t default_user_group;
156
Ken Mixter207694d2010-10-28 15:42:37 -0700157 if (out_of_capacity != NULL) *out_of_capacity = false;
158
Ken Mixter03403162010-08-18 15:23:16 -0700159 // For testing.
160 if (forced_crash_directory_ != NULL) {
161 *crash_directory = FilePath(forced_crash_directory_);
162 return true;
163 }
164
165 if (!GetUserInfoFromName(kDefaultUserName,
166 &default_user_id,
167 &default_user_group)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800168 LOG(ERROR) << "Could not find default user info";
Ken Mixter03403162010-08-18 15:23:16 -0700169 return false;
170 }
171 mode_t directory_mode;
172 uid_t directory_owner;
173 gid_t directory_group;
174 *crash_directory =
175 GetCrashDirectoryInfo(euid,
176 default_user_id,
177 default_user_group,
178 &directory_mode,
179 &directory_owner,
180 &directory_group);
181
182 if (!file_util::PathExists(*crash_directory)) {
183 // Create the spool directory with the appropriate mode (regardless of
184 // umask) and ownership.
185 mode_t old_mask = umask(0);
186 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 ||
187 chown(crash_directory->value().c_str(),
188 directory_owner,
189 directory_group) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800190 LOG(ERROR) << "Unable to create appropriate crash directory";
Ken Mixter03403162010-08-18 15:23:16 -0700191 return false;
192 }
193 umask(old_mask);
194 }
195
196 if (!file_util::PathExists(*crash_directory)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800197 LOG(ERROR) << "Unable to create crash directory "
198 << crash_directory->value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700199 return false;
200 }
201
Ken Mixter04ec10f2010-08-26 16:02:02 -0700202 if (!CheckHasCapacity(*crash_directory)) {
Ken Mixter207694d2010-10-28 15:42:37 -0700203 if (out_of_capacity != NULL) *out_of_capacity = true;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700204 return false;
205 }
206
Ken Mixter03403162010-08-18 15:23:16 -0700207 return true;
208}
Ken Mixter04ec10f2010-08-26 16:02:02 -0700209
210// Return true if the given crash directory has not already reached
211// maximum capacity.
212bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) {
213 DIR* dir = opendir(crash_directory.value().c_str());
214 if (!dir) {
215 return false;
216 }
217 struct dirent ent_buf;
218 struct dirent* ent;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700219 bool full = false;
Ken Mixteree849c52010-09-30 15:30:10 -0700220 std::set<std::string> basenames;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700221 while (readdir_r(dir, &ent_buf, &ent) == 0 && ent != NULL) {
222 if ((strcmp(ent->d_name, ".") == 0) ||
223 (strcmp(ent->d_name, "..") == 0))
224 continue;
225
Ken Mixteree849c52010-09-30 15:30:10 -0700226 std::string filename(ent->d_name);
227 size_t last_dot = filename.rfind(".");
228 std::string basename;
229 // If there is a valid looking extension, use the base part of the
230 // name. If the only dot is the first byte (aka a dot file), treat
231 // it as unique to avoid allowing a directory full of dot files
232 // from accumulating.
233 if (last_dot != std::string::npos && last_dot != 0)
234 basename = filename.substr(0, last_dot);
235 else
236 basename = filename;
237 basenames.insert(basename);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700238
Ken Mixteree849c52010-09-30 15:30:10 -0700239 if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800240 LOG(WARNING) << "Crash directory " << crash_directory.value()
241 << " already full with " << kMaxCrashDirectorySize
242 << " pending reports";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700243 full = true;
244 break;
245 }
246 }
247 closedir(dir);
248 return !full;
249}
Ken Mixteree849c52010-09-30 15:30:10 -0700250
Ken Mixterc49dbd42010-12-14 17:44:11 -0800251bool CrashCollector::IsCommentLine(const std::string &line) {
252 size_t found = line.find_first_not_of(" ");
253 return found != std::string::npos && line[found] == '#';
254}
255
Ken Mixteree849c52010-09-30 15:30:10 -0700256bool CrashCollector::ReadKeyValueFile(
257 const FilePath &path,
258 const char separator,
259 std::map<std::string, std::string> *dictionary) {
260 std::string contents;
261 if (!file_util::ReadFileToString(path, &contents)) {
262 return false;
263 }
264 typedef std::vector<std::string> StringVector;
265 StringVector lines;
266 SplitString(contents, '\n', &lines);
267 bool any_errors = false;
268 for (StringVector::iterator line = lines.begin(); line != lines.end();
269 ++line) {
270 // Allow empty strings.
271 if (line->empty())
272 continue;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800273 // Allow comment lines.
274 if (IsCommentLine(*line))
275 continue;
Ken Mixteree849c52010-09-30 15:30:10 -0700276 StringVector sides;
277 SplitString(*line, separator, &sides);
278 if (sides.size() != 2) {
279 any_errors = true;
280 continue;
281 }
282 dictionary->insert(std::pair<std::string, std::string>(sides[0], sides[1]));
283 }
284 return !any_errors;
285}
286
Ken Mixterc49dbd42010-12-14 17:44:11 -0800287bool CrashCollector::GetLogContents(const FilePath &config_path,
288 const std::string &exec_name,
289 const FilePath &output_file) {
290 std::map<std::string, std::string> log_commands;
291 if (!ReadKeyValueFile(config_path, ':', &log_commands)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800292 LOG(INFO) << "Unable to read log configuration file "
293 << config_path.value();
Ken Mixterc49dbd42010-12-14 17:44:11 -0800294 return false;
295 }
296
297 if (log_commands.find(exec_name) == log_commands.end())
298 return false;
299
Ken Mixtera3249322011-03-03 08:47:38 -0800300 chromeos::ProcessImpl diag_process;
301 diag_process.AddArg(kShellPath);
Ken Mixterc49dbd42010-12-14 17:44:11 -0800302 std::string shell_command = log_commands[exec_name];
Ken Mixtera3249322011-03-03 08:47:38 -0800303 diag_process.AddStringOption("-c", shell_command);
304 diag_process.RedirectOutput(output_file.value());
Ken Mixterc49dbd42010-12-14 17:44:11 -0800305
Ken Mixtera3249322011-03-03 08:47:38 -0800306 int result = diag_process.Run();
307 if (result != 0) {
308 LOG(INFO) << "Running shell command " << shell_command << "failed with: "
309 << result;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800310 return false;
311 }
312 return true;
313}
314
Ken Mixterafcf8082010-10-26 14:45:01 -0700315void CrashCollector::AddCrashMetaData(const std::string &key,
316 const std::string &value) {
317 extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str()));
318}
319
Ken Mixteree849c52010-09-30 15:30:10 -0700320void CrashCollector::WriteCrashMetaData(const FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700321 const std::string &exec_name,
322 const std::string &payload_path) {
Ken Mixteree849c52010-09-30 15:30:10 -0700323 std::map<std::string, std::string> contents;
Ken Mixterafcf8082010-10-26 14:45:01 -0700324 if (!ReadKeyValueFile(FilePath(std::string(lsb_release_)), '=', &contents)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800325 LOG(ERROR) << "Problem parsing " << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700326 // Even though there was some failure, take as much as we could read.
327 }
328 std::string version("unknown");
329 std::map<std::string, std::string>::iterator i;
330 if ((i = contents.find("CHROMEOS_RELEASE_VERSION")) != contents.end()) {
331 version = i->second;
332 }
Ken Mixterc909b692010-10-18 12:26:05 -0700333 int64 payload_size = -1;
334 file_util::GetFileSize(FilePath(payload_path), &payload_size);
Ken Mixterafcf8082010-10-26 14:45:01 -0700335 std::string meta_data = StringPrintf("%sexec_name=%s\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700336 "ver=%s\n"
Ken Mixter207694d2010-10-28 15:42:37 -0700337 "payload=%s\n"
Ken Mixterc909b692010-10-18 12:26:05 -0700338 "payload_size=%lld\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700339 "done=1\n",
Ken Mixterafcf8082010-10-26 14:45:01 -0700340 extra_metadata_.c_str(),
Ken Mixteree849c52010-09-30 15:30:10 -0700341 exec_name.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700342 version.c_str(),
Ken Mixter207694d2010-10-28 15:42:37 -0700343 payload_path.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700344 payload_size);
Ken Mixter9b346472010-11-07 13:45:45 -0800345 // We must use WriteNewFile instead of file_util::WriteFile as we
346 // do not want to write with root access to a symlink that an attacker
347 // might have created.
348 if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800349 LOG(ERROR) << "Unable to write " << meta_path.value();
Ken Mixteree849c52010-09-30 15:30:10 -0700350 }
351}