Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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/kernel_warning_collector.h" |
| 6 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 7 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
| 9 | #include <base/strings/string_number_conversions.h> |
| 10 | #include <base/strings/string_util.h> |
| 11 | #include <base/strings/stringprintf.h> |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | const char kExecName[] = "kernel-warning"; |
| 15 | const char kKernelWarningSignatureKey[] = "sig"; |
| 16 | const char kKernelWarningPath[] = "/var/run/kwarn/warning"; |
| 17 | const pid_t kKernelPid = 0; |
| 18 | const uid_t kRootUid = 0; |
| 19 | } // namespace |
| 20 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 21 | using base::FilePath; |
| 22 | using base::StringPrintf; |
| 23 | |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 24 | KernelWarningCollector::KernelWarningCollector() { |
| 25 | } |
| 26 | |
| 27 | KernelWarningCollector::~KernelWarningCollector() { |
| 28 | } |
| 29 | |
| 30 | bool KernelWarningCollector::LoadKernelWarning(std::string *content, |
Luigi Semenzato | b20c9ef | 2013-12-17 18:12:09 -0800 | [diff] [blame] | 31 | std::string *signature) { |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 32 | FilePath kernel_warning_path(kKernelWarningPath); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 33 | if (!base::ReadFileToString(kernel_warning_path, content)) { |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 34 | LOG(ERROR) << "Could not open " << kKernelWarningPath; |
| 35 | return false; |
| 36 | } |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 37 | // The signature is in the first line. |
Luigi Semenzato | b20c9ef | 2013-12-17 18:12:09 -0800 | [diff] [blame] | 38 | std::string::size_type end_position = content->find('\n'); |
| 39 | if (end_position == std::string::npos) { |
| 40 | LOG(ERROR) << "unexpected kernel warning format"; |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
Luigi Semenzato | b20c9ef | 2013-12-17 18:12:09 -0800 | [diff] [blame] | 43 | *signature = content->substr(0, end_position); |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool KernelWarningCollector::Collect() { |
| 48 | std::string reason = "normal collection"; |
| 49 | bool feedback = true; |
| 50 | if (IsDeveloperImage()) { |
| 51 | reason = "always collect from developer builds"; |
| 52 | feedback = true; |
| 53 | } else if (!is_feedback_allowed_function_()) { |
| 54 | reason = "no user consent"; |
| 55 | feedback = false; |
| 56 | } |
| 57 | |
| 58 | LOG(INFO) << "Processing kernel warning: " << reason; |
| 59 | |
| 60 | if (!feedback) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | std::string kernel_warning; |
Luigi Semenzato | b20c9ef | 2013-12-17 18:12:09 -0800 | [diff] [blame] | 65 | std::string warning_signature; |
| 66 | if (!LoadKernelWarning(&kernel_warning, &warning_signature)) { |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
| 70 | FilePath root_crash_directory; |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 71 | if (!GetCreatedCrashDirectoryByEuid(kRootUid, &root_crash_directory, |
| 72 | nullptr)) { |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
| 76 | std::string dump_basename = |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 77 | FormatDumpBasename(kExecName, time(nullptr), kKernelPid); |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 78 | FilePath kernel_crash_path = root_crash_directory.Append( |
| 79 | StringPrintf("%s.kcrash", dump_basename.c_str())); |
| 80 | |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 81 | // We must use WriteNewFile instead of base::WriteFile as we |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 82 | // do not want to write with root access to a symlink that an attacker |
| 83 | // might have created. |
| 84 | if (WriteNewFile(kernel_crash_path, |
| 85 | kernel_warning.data(), |
| 86 | kernel_warning.length()) != |
| 87 | static_cast<int>(kernel_warning.length())) { |
| 88 | LOG(INFO) << "Failed to write kernel warning to " |
| 89 | << kernel_crash_path.value().c_str(); |
| 90 | return true; |
| 91 | } |
| 92 | |
Luigi Semenzato | b20c9ef | 2013-12-17 18:12:09 -0800 | [diff] [blame] | 93 | AddCrashMetaData(kKernelWarningSignatureKey, warning_signature); |
Luigi Semenzato | 6fdc0b4 | 2013-04-11 17:22:13 -0700 | [diff] [blame] | 94 | WriteCrashMetaData( |
| 95 | root_crash_directory.Append( |
| 96 | StringPrintf("%s.meta", dump_basename.c_str())), |
| 97 | kExecName, kernel_crash_path.value()); |
| 98 | |
| 99 | LOG(INFO) << "Stored kernel warning into " << kernel_crash_path.value(); |
| 100 | return true; |
| 101 | } |