blob: e28e8fdea97110a3a45d96307e6edc8e8ae26d7f [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 */
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "kernel_warning_collector.h"
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070018
Ben Chanab6cc902014-09-05 08:21:06 -070019#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070020#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 Semenzato6fdc0b42013-04-11 17:22:13 -070024
25namespace {
26const char kExecName[] = "kernel-warning";
27const char kKernelWarningSignatureKey[] = "sig";
28const char kKernelWarningPath[] = "/var/run/kwarn/warning";
29const pid_t kKernelPid = 0;
30const uid_t kRootUid = 0;
31} // namespace
32
Mike Frysingera557c112014-02-05 22:55:39 -050033using base::FilePath;
34using base::StringPrintf;
35
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070036KernelWarningCollector::KernelWarningCollector() {
37}
38
39KernelWarningCollector::~KernelWarningCollector() {
40}
41
42bool KernelWarningCollector::LoadKernelWarning(std::string *content,
Luigi Semenzatob20c9ef2013-12-17 18:12:09 -080043 std::string *signature) {
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070044 FilePath kernel_warning_path(kKernelWarningPath);
Mike Frysingera557c112014-02-05 22:55:39 -050045 if (!base::ReadFileToString(kernel_warning_path, content)) {
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070046 LOG(ERROR) << "Could not open " << kKernelWarningPath;
47 return false;
48 }
Ben Chan7e776902014-06-18 13:19:51 -070049 // The signature is in the first line.
Luigi Semenzatob20c9ef2013-12-17 18:12:09 -080050 std::string::size_type end_position = content->find('\n');
51 if (end_position == std::string::npos) {
52 LOG(ERROR) << "unexpected kernel warning format";
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070053 return false;
54 }
Luigi Semenzatob20c9ef2013-12-17 18:12:09 -080055 *signature = content->substr(0, end_position);
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070056 return true;
57}
58
59bool 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 Semenzatob20c9ef2013-12-17 18:12:09 -080077 std::string warning_signature;
78 if (!LoadKernelWarning(&kernel_warning, &warning_signature)) {
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070079 return true;
80 }
81
82 FilePath root_crash_directory;
Ben Chan262d7982014-09-18 08:05:20 -070083 if (!GetCreatedCrashDirectoryByEuid(kRootUid, &root_crash_directory,
84 nullptr)) {
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070085 return true;
86 }
87
88 std::string dump_basename =
Ben Chan262d7982014-09-18 08:05:20 -070089 FormatDumpBasename(kExecName, time(nullptr), kKernelPid);
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070090 FilePath kernel_crash_path = root_crash_directory.Append(
91 StringPrintf("%s.kcrash", dump_basename.c_str()));
92
Ben Chanf30c6412014-05-22 23:09:01 -070093 // We must use WriteNewFile instead of base::WriteFile as we
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070094 // 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 Semenzatob20c9ef2013-12-17 18:12:09 -0800105 AddCrashMetaData(kKernelWarningSignatureKey, warning_signature);
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700106 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}