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