File: shill_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 (424 lines) | stat: -rw-r--r-- 16,487 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// 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/shill_data_collector.h"

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/system_logs/shill_log_pii_identifiers.h"
#include "chrome/browser/support_tool/data_collector_utils.h"
#include "chromeos/ash/components/dbus/shill/shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_ipconfig_client.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/components/onc/onc_utils.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace {

constexpr char kNetworkDevices[] = "network_devices";
constexpr char kNetworkServices[] = "network_services";
constexpr char kServicePrefix[] = "/service/";
constexpr char kDevicePrefix[] = "/device/";

std::string GetString(const base::Value& value) {
  if (!value.is_string()) {
    LOG(ERROR) << "Bad string value: " << value;
    return std::string();
  }
  return value.GetString();
}

// Check the contents of `value` and returns true if its contents are empty. If
// `value` contains literal types like int, bool, it'll return false.
bool HasEmptyContents(const base::Value& value) {
  // Check non-literal types to see if they have empty contents.
  if (value.is_string())
    return value.GetString().empty();
  if (value.is_list())
    return value.GetList().empty();
  if (value.is_dict())
    return value.GetDict().empty();
  // The literal types can't be empty.
  return false;
}

constexpr char kMaskedString[] = "*** MASKED ***";

// Converts `shill_log` into std::string and detects PII sensitive data it
// contains. Returns the detected PII map.
PIIMap DetectPII(
    base::Value::Dict shill_log,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container) {
  redaction::RedactionTool* redaction_tool = redaction_tool_container->Get();
  PIIMap detected_pii;
  // Detect PII in `shill_log` and add the detected PII to `detected_pii`.
  std::string json;
  base::JSONWriter::WriteWithOptions(
      shill_log, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
  PIIMap pii_in_logs = redaction_tool->Detect(std::move(json));
  MergePIIMaps(detected_pii, pii_in_logs);
  return detected_pii;
}

// Converts `shill_log` into std::string and redacts PII sensitive data it
// contains.
std::string RedactAndKeepSelectedPII(
    const std::set<redaction::PIIType>& pii_types_to_keep,
    const base::Value::Dict& shill_log,
    scoped_refptr<redaction::RedactionToolContainer> redaction_tool_container) {
  redaction::RedactionTool* redaction_tool = redaction_tool_container->Get();
  std::string property_str;
  base::JSONWriter::WriteWithOptions(
      shill_log, base::JSONWriter::OPTIONS_PRETTY_PRINT, &property_str);
  property_str = redaction_tool->RedactAndKeepSelected(std::move(property_str),
                                                       pii_types_to_keep);
  return property_str;
}

// Creates `shill_properties.txt` at `target_directory` and writes the contents
// of `shill_property` into it. Returns true on success.
bool WriteOutputFiles(std::string shill_property,
                      base::FilePath target_directory) {
  return base::WriteFile(
      target_directory.Append(FILE_PATH_LITERAL("shill_properties.txt")),
      shill_property);
}

// Recursively traverses `dict`. Detects and adds PII listed in
// `system_logs::kShillPIIMaskedMap` in `dict` to `pii_map` if `scrub` is false;
// otherwise replace PII with `kMaskedString`.
void DetectOrScrubPIIInDictionary(
    base::Value::Dict& dict,
    bool scrub,
    std::set<redaction::PIIType>& pii_types_to_keep,
    PIIMap& pii_map) {
  for (auto entry : dict) {
    if (entry.second.is_dict()) {
      DetectOrScrubPIIInDictionary(entry.second.GetDict(), scrub,
                                   pii_types_to_keep, pii_map);
      continue;
    }
    if (!system_logs::kShillPIIMaskedMap.contains(entry.first))
      continue;
    // We don't add empty values to `pii_map` nor mask them because empty
    // values don't contain PII anyway.
    if (HasEmptyContents(entry.second))
      continue;
    if (scrub &&
        !base::Contains(pii_types_to_keep,
                        system_logs::kShillPIIMaskedMap.at(entry.first))) {
      entry.second = base::Value(kMaskedString);
      continue;
    }
    std::string value_as_string;
    base::JSONWriter::WriteWithOptions(
        entry.second, base::JSONWriter::OPTIONS_PRETTY_PRINT, &value_as_string);
    pii_map[system_logs::kShillPIIMaskedMap.at(entry.first)].emplace(
        value_as_string);
  }
}

}  // namespace

ShillDataCollector::ShillDataCollector() {
  shill_log_.Set(kNetworkDevices, base::Value::Dict());
  shill_log_.Set(kNetworkServices, base::Value::Dict());
  collector_err_ = {{"Device", {}}, {"Service", {}}, {"IPConfig", {}}};
}

ShillDataCollector::~ShillDataCollector() = default;

std::string ShillDataCollector::GetName() const {
  return "Shill";
}

std::string ShillDataCollector::GetDescription() const {
  return "Collects Shill data and exports the data into file named shill.";
}

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

void ShillDataCollector::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_);
  data_collector_done_callback_ = std::move(on_data_collected_callback);
  task_runner_for_redaction_tool_ = std::move(task_runner_for_redaction_tool);
  redaction_tool_container_ = std::move(redaction_tool_container);
  ash::ShillManagerClient::Get()->GetProperties(
      base::BindOnce(&ShillDataCollector::OnGetManagerProperties,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ShillDataCollector::OnGetManagerProperties(
    std::optional<base::Value::Dict> result) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!result) {
    SupportToolError error = {
        SupportToolErrorCode::kDataCollectorError,
        "ShillDataCollector: ManagerPropertiesCallback failed"};
    std::move(data_collector_done_callback_).Run(/*error=*/error);
    return;
  }

  // Records how many entries are pending to be processed. Adds 1 to guard
  // against the case where `num_entries_left_` drops to 0 before all entries
  // are retrieved.
  num_entries_left_ = 1;
  const base::Value::List* devices = result->FindList(shill::kDevicesProperty);
  if (devices) {
    for (const base::Value& device : *devices) {
      std::string path = GetString(device);
      if (path.empty())
        continue;
      ++num_entries_left_;
      ash::ShillDeviceClient::Get()->GetProperties(
          dbus::ObjectPath(path),
          base::BindOnce(&ShillDataCollector::OnGetDevice,
                         weak_ptr_factory_.GetWeakPtr(), path));
    }
  }

  const base::Value::List* services =
      result->FindList(shill::kServicesProperty);
  if (services) {
    for (const base::Value& service : *services) {
      std::string path = GetString(service);
      if (path.empty())
        continue;
      ++num_entries_left_;
      ash::ShillServiceClient::Get()->GetProperties(
          dbus::ObjectPath(path),
          base::BindOnce(&ShillDataCollector::OnGetService,
                         weak_ptr_factory_.GetWeakPtr(), path));
    }
  }
  // Removes the protective "1" so that `num_entries_left_` can become 0.
  --num_entries_left_;
  CheckIfDone();
}

void ShillDataCollector::OnGetDevice(
    const std::string& device_path,
    std::optional<base::Value::Dict> properties) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!properties) {
    collector_err_["Device"].emplace_back(device_path);
  } else {
    AddDeviceAndRequestIPConfigs(device_path, *properties);
  }
  --num_entries_left_;
  CheckIfDone();
}

void ShillDataCollector::AddDeviceAndRequestIPConfigs(
    const std::string& device_path,
    const base::Value::Dict& properties) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  shill_log_.FindDict(kNetworkDevices)
      ->Set(device_path, ExpandProperties(device_path, properties));

  const base::Value::List* ip_configs =
      properties.FindList(shill::kIPConfigsProperty);
  if (!ip_configs)
    return;

  for (const base::Value& ip_config : *ip_configs) {
    std::string ip_config_path = GetString(ip_config);
    if (ip_config_path.empty())
      continue;
    ++num_entries_left_;
    ash::ShillIPConfigClient::Get()->GetProperties(
        dbus::ObjectPath(ip_config_path),
        base::BindOnce(&ShillDataCollector::OnGetIPConfig,
                       weak_ptr_factory_.GetWeakPtr(), device_path,
                       ip_config_path));
  }
}

void ShillDataCollector::OnGetIPConfig(
    const std::string& device_path,
    const std::string& ip_config_path,
    std::optional<base::Value::Dict> properties) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!properties) {
    collector_err_["IPConfig"].emplace_back(
        base::StrCat({device_path, ": ", ip_config_path}));
  } else {
    AddIPConfig(device_path, ip_config_path, *properties);
  }
  --num_entries_left_;
  CheckIfDone();
}

void ShillDataCollector::AddIPConfig(const std::string& device_path,
                                     const std::string& ip_config_path,
                                     const base::Value::Dict& properties) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  base::Value::Dict& device =
      shill_log_.FindDict(kNetworkDevices)->Find(device_path)->GetDict();
  base::Value::Dict* ip_configs = device.FindDict(shill::kIPConfigsProperty);
  if (!ip_configs) {
    ip_configs =
        device.Set(shill::kIPConfigsProperty, base::Value::Dict())->GetIfDict();
  }
  DCHECK(ip_configs);
  ip_configs->Set(ip_config_path, ExpandProperties(ip_config_path, properties));
}

void ShillDataCollector::OnGetService(
    const std::string& service_path,
    std::optional<base::Value::Dict> properties) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!properties) {
    collector_err_["Service"].emplace_back(service_path);
  } else {
    shill_log_.FindDict(kNetworkServices)
        ->Set(service_path, ExpandProperties(service_path, properties.value()));
  }
  --num_entries_left_;
  CheckIfDone();
}

base::Value::Dict ShillDataCollector::ExpandProperties(
    const std::string& object_path,
    const base::Value::Dict& properties) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  base::Value::Dict dict = properties.Clone();
  // Converts UIData from a string to a dictionary.
  std::string* ui_data = dict.FindString(shill::kUIDataProperty);
  if (ui_data) {
    std::optional<base::Value::Dict> ui_data_dict =
        chromeos::onc::ReadDictionaryFromJson(*ui_data);
    if (ui_data_dict.has_value()) {
      dict.Set(shill::kUIDataProperty, base::Value(std::move(*ui_data_dict)));
    }
  }

  if (base::StartsWith(object_path, kServicePrefix,
                       base::CompareCase::SENSITIVE)) {
    pii_map_[redaction::PIIType::kSSID].insert(
        *dict.FindString(shill::kNameProperty));
  } else if (base::StartsWith(object_path, kDevicePrefix,
                              base::CompareCase::SENSITIVE)) {
    pii_map_[redaction::PIIType::kSSID].insert(
        *dict.FindString(shill::kNameProperty));
    // Only detects "Address" in the top level Device dictionary, not globally
    // (which would mask IPConfigs which get anonymized separately).
    pii_map_[redaction::PIIType::kMACAddress].insert(
        *dict.FindString(shill::kAddressProperty));
  }
  std::set<redaction::PIIType> empty = {};
  DetectOrScrubPIIInDictionary(dict, /*scrub=*/false,
                               /*pii_types_to_keep=*/empty, pii_map_);
  return dict;
}

void ShillDataCollector::CheckIfDone() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (num_entries_left_)
    return;
  task_runner_for_redaction_tool_->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&DetectPII, shill_log_.Clone(), redaction_tool_container_),
      base::BindOnce(&ShillDataCollector::OnPIIDetected,
                     weak_ptr_factory_.GetWeakPtr()));
}

void ShillDataCollector::OnPIIDetected(PIIMap detected_pii) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  for (auto& entry : detected_pii)
    pii_map_[entry.first].insert(entry.second.begin(), entry.second.end());
  // Generates error message, if any.
  std::string collector_errors;
  for (const auto& err : collector_err_) {
    if (err.second.size()) {
      base::StrAppend(&collector_errors,
                      {"Get ", err.first, " Properties Failed for : ",
                       base::JoinString(err.second, ", "), "\n"});
    }
  }
  if (collector_errors.size()) {
    SupportToolError error = {
        SupportToolErrorCode::kDataCollectorError,
        base::StrCat({"ShillDataCollector had errors collecting data: ",
                      collector_errors})};
    std::move(data_collector_done_callback_).Run(/*error=*/error);
  } else {
    std::move(data_collector_done_callback_).Run(/*error=*/std::nullopt);
  }
}

void ShillDataCollector::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_);
  // Only masks shill::kNameProperty in the top levels of devices and services.
  if (!pii_types_to_keep.count(redaction::PIIType::kSSID)) {
    for (auto entry : *shill_log_.FindDict(kNetworkServices)) {
      std::string log_name = ash::NetworkPathId(entry.first);  // Not PII
      entry.second.GetDict().Set(shill::kNameProperty, log_name);
    }
    for (auto entry : *shill_log_.FindDict(kNetworkDevices))
      entry.second.GetDict().Set(shill::kNameProperty, kMaskedString);
  }
  // Masks PII listed in `system_logs::kShillPIIMaskedMap`.
  DetectOrScrubPIIInDictionary(shill_log_, /*scrub=*/true, pii_types_to_keep,
                               pii_map_);
  // Runs RedactionTool to further remove PII.
  task_runner_for_redaction_tool->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&RedactAndKeepSelectedPII, pii_types_to_keep,
                     shill_log_.Clone(), redaction_tool_container),
      base::BindOnce(&ShillDataCollector::OnPIIRedacted,
                     weak_ptr_factory_.GetWeakPtr(), target_directory,
                     std::move(on_exported_callback)));
}

void ShillDataCollector::OnPIIRedacted(
    base::FilePath target_directory,
    DataCollectorDoneCallback on_exported_callback,
    std::string shill_property) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&WriteOutputFiles, std::move(shill_property),
                     target_directory),
      base::BindOnce(&ShillDataCollector::OnFilesWritten,
                     weak_ptr_factory_.GetWeakPtr(), target_directory,
                     std::move(on_exported_callback)));
}

void ShillDataCollector::OnFilesWritten(
    base::FilePath target_directory,
    DataCollectorDoneCallback on_exported_callback,
    bool success) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!success) {
    SupportToolError error = {SupportToolErrorCode::kDataCollectorError,
                              "ShillDataCollector failed on data export."};
    std::move(on_exported_callback).Run(error);
    return;
  }
  shill_log_.clear();
  std::move(on_exported_callback).Run(/*error=*/std::nullopt);
}