File: system_logs_data_collector.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (267 lines) | stat: -rw-r--r-- 11,074 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/support_tool/ash/system_logs_data_collector.h"

#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/containers/fixed_flat_map.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/support_tool/data_collector.h"
#include "chrome/browser/support_tool/data_collector_utils.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/dbus/debug_daemon/debug_daemon_client.h"
#include "components/feedback/redaction_tool/pii_types.h"
#include "components/feedback/redaction_tool/redaction_tool.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "third_party/cros_system_api/dbus/debugd/dbus-constants.h"

namespace {

// Return the name debugd sends the logs in the `log_file_name` file
// in the file system.
base::FilePath GetDebugdPathOfLog(const base::FilePath& log_file_name) {
  // Most of the logs in debugd are stored with their file's base name. However,
  // there are some exceptions. We map the log file's base name in file system
  // and their name in debugd for these exceptional cases.
  static constexpr auto kDebugdLogNames =
      base::MakeFixedFlatMap<std::string_view, std::string_view>(
          {{"arc.log", "cheets_log"},
           {"chrome", "chrome_system_log"},
           {"chrome.PREVIOUS", "chrome_system_log.PREVIOUS"},
           {"memd.clip", "memd clips"},
           {"net.log", "netlog"},
           {"messages", "syslog"},
           {"ui.LATEST", "ui_log"},
           {"debug_vboot_noisy.log", "verified boot"}});
  auto log_name = kDebugdLogNames.find(log_file_name.value());
  return log_name == kDebugdLogNames.end() ? log_file_name
                                           : base::FilePath(log_name->second);
}

// Filters the logs in `logs` and returns the map containing only the logs
// specified in `requested_logs`. Returns the `logs` without any filtering if
// `requested_logs` is empty.
std::map<std::string, std::string> GetOnlyRequestedLogs(
    const std::map<std::string, std::string>& logs,
    const std::set<base::FilePath>& requested_logs) {
  // Return all logs without filtering if `requested_logs` is not specified.
  if (requested_logs.empty())
    return std::move(logs);

  std::map<std::string, std::string> filtered_logs;
  for (const base::FilePath& requested_log : requested_logs) {
    base::FilePath requested_log_name = GetDebugdPathOfLog(requested_log);
    // We check if `logs` contain `requested_log_name` first.
    auto search_result = logs.find(requested_log_name.value());
    if (search_result != logs.end()) {
      filtered_logs.emplace(search_result->first, search_result->second);
      continue;
    }
    // If `logs` doesn't contain `requested_log_name`, we also check if it
    // contains the version that doesn't have extension since debugd may remove
    // extension of log file's name when returning the logs.
    search_result = logs.find(requested_log_name.RemoveExtension().value());
    if (search_result != logs.end()) {
      filtered_logs.emplace(search_result->first, search_result->second);
      continue;
    }
  }
  return filtered_logs;
}

// Detects PII sensitive data that `system_logs` contains and returns
// the detected PII map.
PIIMap DetectPII(
    std::map<std::string, std::string> system_logs,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container) {
  redaction::RedactionTool* redaction_tool = redaction_tool_container->Get();
  PIIMap detected_pii;
  // Detect PII in all entries in `system_logs` and add the detected
  // PII to `detected_pii`.
  for (const auto& entry : system_logs) {
    PIIMap pii_in_logs = redaction_tool->Detect(entry.second);
    MergePIIMaps(detected_pii, pii_in_logs);
  }
  return detected_pii;
}

// Redacts PII from `system_logs` except `pii_types_to_keep` and returns the map
// containing redacted logs.
std::map<std::string, std::string> RedactPII(
    std::map<std::string, std::string> system_logs,
    const std::set<redaction::PIIType>& pii_types_to_keep,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container) {
  redaction::RedactionTool* redaction_tool = redaction_tool_container->Get();
  for (auto& [log_name, log_contents] : system_logs) {
    log_contents =
        redaction_tool->RedactAndKeepSelected(log_contents, pii_types_to_keep);
  }
  return system_logs;
}

// Opens a "var_log_files" file under `target_directory` and writes
// `system_logs` into it.
bool WriteSystemLogFiles(std::map<std::string, std::string> system_logs,
                         base::FilePath target_directory) {
  base::FilePath target_path =
      target_directory.Append(FILE_PATH_LITERAL("var_log_files"));
  if (!base::CreateDirectory(target_path))
    return false;
  bool success = true;
  for (const auto& [log_name, log_contents] : system_logs) {
    if (!base::WriteFile(target_path.AppendASCII(log_name), log_contents))
      success = false;
  }
  return success;
}

}  // namespace

SystemLogsDataCollector::SystemLogsDataCollector(
    std::set<base::FilePath> requested_logs)
    : requested_logs_(requested_logs) {}
SystemLogsDataCollector::~SystemLogsDataCollector() = default;

std::string SystemLogsDataCollector::GetName() const {
  return "System Logs Data Collector";
}

std::string SystemLogsDataCollector::GetDescription() const {
  return "Collect various system logs under /var/log in ChromeOS devices";
}

const PIIMap& SystemLogsDataCollector::GetDetectedPII() {
  return pii_map_;
}

void SystemLogsDataCollector::CollectDataAndDetectPII(
    DataCollectorDoneCallback on_data_collected_callback,
    scoped_refptr<base::SequencedTaskRunner> task_runner_for_redaction_tool,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  ash::DebugDaemonClient* debugd_client = ash::DebugDaemonClient::Get();

  const user_manager::User* user =
      user_manager::UserManager::Get()->GetActiveUser();

  // We will only request /var/log files.
  const std::vector<debugd::FeedbackLogType> included_log_types = {
      debugd::FeedbackLogType::VAR_LOG_FILES};

  // `debugd_client` will run the callback on original thread (see
  // dbus/object_proxy.h for more details).
  debugd_client->GetFeedbackLogs(
      cryptohome::CreateAccountIdentifierFromAccountId(
          user ? user->GetAccountId() : EmptyAccountId()),
      included_log_types,
      base::BindOnce(&SystemLogsDataCollector::OnGetFeedbackLogs,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(on_data_collected_callback),
                     task_runner_for_redaction_tool, redaction_tool_container));
}

void SystemLogsDataCollector::OnGetFeedbackLogs(
    DataCollectorDoneCallback on_data_collected_callback,
    scoped_refptr<base::SequencedTaskRunner> task_runner_for_redaction_tool,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container,
    bool success,
    const std::map<std::string, std::string>& logs) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  system_logs_ = GetOnlyRequestedLogs(logs, requested_logs_);

  // If `system_logs_` are empty, it means the data collector couldn't get any
  // of the requested logs. Return the error message.
  if (system_logs_.empty()) {
    SupportToolError error = {
        SupportToolErrorCode::kDataCollectorError,
        "SystemLogsDataCollector couldn't retrieve requested logs."};
    std::move(on_data_collected_callback).Run(error);
    return;
  }

  // There might be some logs missing if `success` is not true. Document it in
  // error message even though some of the logs could be retrieved successfully.
  std::optional<SupportToolError> error =
      success ? std::nullopt
              : std::make_optional(
                    SupportToolError(SupportToolErrorCode::kDataCollectorError,
                                     "SystemLogsDataCollector got error from "
                                     "debugd when requesting logs."));

  task_runner_for_redaction_tool->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&DetectPII, system_logs_, redaction_tool_container),
      base::BindOnce(&SystemLogsDataCollector::OnPIIDetected,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(on_data_collected_callback), error));
}

void SystemLogsDataCollector::OnPIIDetected(
    DataCollectorDoneCallback on_data_collected_callback,
    std::optional<SupportToolError> error,
    PIIMap detected_pii) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  pii_map_ = detected_pii;
  std::move(on_data_collected_callback).Run(error);
}

void SystemLogsDataCollector::ExportCollectedDataWithPII(
    std::set<redaction::PIIType> pii_types_to_keep,
    base::FilePath target_directory,
    scoped_refptr<base::SequencedTaskRunner> task_runner_for_redaction_tool,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container,
    DataCollectorDoneCallback on_exported_callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_runner_for_redaction_tool->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&RedactPII, std::move(system_logs_), pii_types_to_keep,
                     redaction_tool_container),
      base::BindOnce(&SystemLogsDataCollector::OnPIIRedacted,
                     weak_ptr_factory_.GetWeakPtr(), target_directory,
                     std::move(on_exported_callback)));
}

void SystemLogsDataCollector::OnPIIRedacted(
    base::FilePath target_directory,
    DataCollectorDoneCallback on_exported_callback,
    std::map<std::string, std::string> redacted_logs) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&WriteSystemLogFiles, redacted_logs, target_directory),
      base::BindOnce(&SystemLogsDataCollector::OnFilesWritten,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(on_exported_callback)));
}

void SystemLogsDataCollector::OnFilesWritten(
    DataCollectorDoneCallback on_exported_callback,
    bool success) {
  if (!success) {
    SupportToolError error = {
        SupportToolErrorCode::kDataCollectorError,
        "SystemLogsDataCollector failed to write system log files."};
    std::move(on_exported_callback).Run(error);
    return;
  }
  std::move(on_exported_callback).Run(/*error=*/std::nullopt);
}