OLD | NEW |
---|---|
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/policy/system_log_uploader.h" | 5 #include "system_log_uploader.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/location.h" | |
14 #include "base/macros.h" | |
15 #include "base/metrics/histogram_macros.h" | |
16 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_split.h" | |
18 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
19 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
20 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/chromeos/policy/upload_job_impl.h" | 17 #include "chrome/browser/chromeos/policy/upload_job_impl.h" |
22 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 18 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
23 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " | 19 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " |
24 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "components/feedback/anonymizer_tool.h" | |
25 #include "components/policy/core/browser/browser_policy_connector.h" | 22 #include "components/policy/core/browser/browser_policy_connector.h" |
26 #include "components/policy/core/common/cloud/enterprise_metrics.h" | |
27 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
28 #include "net/http/http_request_headers.h" | 24 #include "net/http/http_request_headers.h" |
29 #include "third_party/re2/src/re2/re2.h" | |
30 | 25 |
31 namespace { | 26 namespace { |
32 // The maximum number of successive retries. | 27 // The maximum number of successive retries. |
33 const int kMaxNumRetries = 1; | 28 const int kMaxNumRetries = 1; |
34 | 29 |
35 // String constant defining the url tail we upload system logs to. | 30 // String constant defining the url tail we upload system logs to. |
36 const char* kSystemLogUploadUrlTail = "/upload"; | 31 const char* kSystemLogUploadUrlTail = "/upload"; |
37 | 32 |
38 // The file names of the system logs to upload. | 33 // The file names of the system logs to upload. |
39 // Note: do not add anything to this list without checking for PII in the file. | 34 // Note: do not add anything to this list without checking for PII in the file. |
40 const char* const kSystemLogFileNames[] = { | 35 const char* const kSystemLogFileNames[] = { |
41 "/var/log/bios_info.txt", "/var/log/chrome/chrome", | 36 "/var/log/bios_info.txt", "/var/log/chrome/chrome", |
42 "/var/log/eventlog.txt", "/var/log/platform_info.txt", | 37 "/var/log/eventlog.txt", "/var/log/platform_info.txt", |
43 "/var/log/messages", "/var/log/messages.1", | 38 "/var/log/messages", "/var/log/messages.1", |
44 "/var/log/net.log", "/var/log/net.1.log", | 39 "/var/log/net.log", "/var/log/net.1.log", |
45 "/var/log/ui/ui.LATEST", "/var/log/update_engine.log"}; | 40 "/var/log/ui/ui.LATEST", "/var/log/update_engine.log"}; |
46 | 41 |
47 const char kEmailAddress[] = | 42 // Reads the system log files as binary files, anonymizes data, stores the files |
48 "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}\\@" | 43 // as pairs (file name, data) and returns. Called on blocking thread. |
49 "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+"; | |
50 const char kIPAddress[] = | |
51 "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])" | |
52 "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2" | |
53 "[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]" | |
54 "[0-9]{2}|[1-9][0-9]|[0-9]))"; | |
55 const char kIPv6Address[] = | |
56 "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|" | |
57 "([0-9a-fA-F]{1,4}:){1,7}:|" | |
58 "([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|" | |
59 "([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|" | |
60 "([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|" | |
61 "([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|" | |
62 "([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|" | |
63 "[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|" | |
64 ":((:[0-9a-fA-F]{1,4}){1,7}|:)|" | |
65 "fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|" | |
66 "::(ffff(:0{1,4}){0,1}:){0,1}" | |
67 "((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}" | |
68 "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|" | |
69 "([0-9a-fA-F]{1,4}:){1,4}:" | |
70 "((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}" | |
71 "(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"; | |
72 | |
73 const char kWebUrl[] = "(http|https|Http|Https|rtsp|Rtsp):\\/\\/"; | |
74 | |
75 // Reads the system log files as binary files, stores the files as pairs | |
76 // (file name, data) and returns. Called on blocking thread. | |
77 scoped_ptr<policy::SystemLogUploader::SystemLogs> ReadFiles() { | 44 scoped_ptr<policy::SystemLogUploader::SystemLogs> ReadFiles() { |
78 scoped_ptr<policy::SystemLogUploader::SystemLogs> system_logs( | 45 scoped_ptr<policy::SystemLogUploader::SystemLogs> system_logs( |
79 new policy::SystemLogUploader::SystemLogs()); | 46 new policy::SystemLogUploader::SystemLogs()); |
47 feedback::AnonymizerTool anonymizer; | |
80 for (auto const file_path : kSystemLogFileNames) { | 48 for (auto const file_path : kSystemLogFileNames) { |
81 if (!base::PathExists(base::FilePath(file_path))) | 49 if (!base::PathExists(base::FilePath(file_path))) |
82 continue; | 50 continue; |
83 std::string data = std::string(); | 51 std::string data = std::string(); |
84 if (!base::ReadFileToString(base::FilePath(file_path), &data)) { | 52 if (!base::ReadFileToString(base::FilePath(file_path), &data)) { |
85 LOG(ERROR) << "Failed to read the system log file from the disk " | 53 LOG(ERROR) << "Failed to read the system log file from the disk " |
86 << file_path << std::endl; | 54 << file_path << std::endl; |
87 } | 55 } |
88 system_logs->push_back(std::make_pair( | 56 system_logs->push_back(std::make_pair( |
89 file_path, policy::SystemLogUploader::RemoveSensitiveData(data))); | 57 file_path, |
58 policy::SystemLogUploader::RemoveSensitiveData(anonymizer, data))); | |
Andrew T Wilson (Slow)
2016/01/25 11:06:42
Pass |anonymizer| as const object, or as ptr.
Polina Bondarenko
2016/01/25 15:08:33
Passed as const ptr.
| |
90 } | 59 } |
91 return system_logs; | 60 return system_logs; |
92 } | 61 } |
93 | 62 |
94 // An implementation of the |SystemLogUploader::Delegate|, that is used to | 63 // An implementation of the |SystemLogUploader::Delegate|, that is used to |
95 // create an upload job and load system logs from the disk. | 64 // create an upload job and load system logs from the disk. |
96 class SystemLogDelegate : public policy::SystemLogUploader::Delegate { | 65 class SystemLogDelegate : public policy::SystemLogUploader::Delegate { |
97 public: | 66 public: |
98 SystemLogDelegate(); | 67 SystemLogDelegate(); |
99 ~SystemLogDelegate() override; | 68 ~SystemLogDelegate() override; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 117 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
149 switches::kSystemLogUploadFrequency); | 118 switches::kSystemLogUploadFrequency); |
150 int frequency; | 119 int frequency; |
151 if (base::StringToInt(string_value, &frequency)) { | 120 if (base::StringToInt(string_value, &frequency)) { |
152 upload_frequency = base::TimeDelta::FromMilliseconds(frequency); | 121 upload_frequency = base::TimeDelta::FromMilliseconds(frequency); |
153 } | 122 } |
154 } | 123 } |
155 return upload_frequency; | 124 return upload_frequency; |
156 } | 125 } |
157 | 126 |
158 void RecordSystemLogPIILeak(policy::SystemLogPIIType type) { | |
159 UMA_HISTOGRAM_ENUMERATION(policy::kMetricSystemLogPII, type, | |
160 policy::SYSTEM_LOG_PII_TYPE_SIZE); | |
161 } | |
162 | |
163 std::string GetUploadUrl() { | 127 std::string GetUploadUrl() { |
164 return policy::BrowserPolicyConnector::GetDeviceManagementUrl() + | 128 return policy::BrowserPolicyConnector::GetDeviceManagementUrl() + |
165 kSystemLogUploadUrlTail; | 129 kSystemLogUploadUrlTail; |
166 } | 130 } |
167 | 131 |
168 } // namespace | 132 } // namespace |
169 | 133 |
170 namespace policy { | 134 namespace policy { |
171 | 135 |
172 // Determines the time between log uploads. | 136 // Determines the time between log uploads. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 ScheduleNextSystemLogUpload( | 205 ScheduleNextSystemLogUpload( |
242 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); | 206 base::TimeDelta::FromMilliseconds(kErrorUploadDelayMs)); |
243 } else { | 207 } else { |
244 // No more retries. | 208 // No more retries. |
245 retry_count_ = 0; | 209 retry_count_ = 0; |
246 ScheduleNextSystemLogUpload(upload_frequency_); | 210 ScheduleNextSystemLogUpload(upload_frequency_); |
247 } | 211 } |
248 } | 212 } |
249 | 213 |
250 // static | 214 // static |
251 std::string SystemLogUploader::RemoveSensitiveData(const std::string& data) { | 215 std::string SystemLogUploader::RemoveSensitiveData( |
252 std::string result = ""; | 216 feedback::AnonymizerTool& anonymizer, |
253 RE2 email_pattern(kEmailAddress), ipv4_pattern(kIPAddress), | 217 const std::string& data) { |
254 ipv6_pattern(kIPv6Address), url_pattern(kWebUrl); | 218 return anonymizer.Anonymize(data); |
Andrew T Wilson (Slow)
2016/01/25 11:06:42
Is there really value to have this broken-out stat
Polina Bondarenko
2016/01/25 15:08:33
It exists only for unit testing.
| |
255 | |
256 for (const std::string& line : base::SplitString( | |
257 data, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL)) { | |
258 // Email. | |
259 if (RE2::PartialMatch(line, email_pattern)) { | |
260 RecordSystemLogPIILeak(SYSTEM_LOG_PII_TYPE_EMAIL_ADDRESS); | |
261 continue; | |
262 } | |
263 | |
264 // IPv4 address. | |
265 if (RE2::PartialMatch(line, ipv4_pattern)) { | |
266 RecordSystemLogPIILeak(SYSTEM_LOG_PII_TYPE_IP_ADDRESS); | |
267 continue; | |
268 } | |
269 | |
270 // IPv6 address. | |
271 if (RE2::PartialMatch(line, ipv6_pattern)) { | |
272 RecordSystemLogPIILeak(SYSTEM_LOG_PII_TYPE_IP_ADDRESS); | |
273 continue; | |
274 } | |
275 | |
276 // URL. | |
277 if (RE2::PartialMatch(line, url_pattern)) { | |
278 RecordSystemLogPIILeak(SYSTEM_LOG_PII_TYPE_WEB_URL); | |
279 continue; | |
280 } | |
281 | |
282 // SSID. | |
283 if (line.find("SSID=") != std::string::npos) { | |
284 RecordSystemLogPIILeak(SYSTEM_LOG_PII_TYPE_SSID); | |
285 continue; | |
286 } | |
287 | |
288 result += line + "\n"; | |
289 } | |
290 return result; | |
291 } | 219 } |
292 | |
293 void SystemLogUploader::RefreshUploadSettings() { | 220 void SystemLogUploader::RefreshUploadSettings() { |
294 // Attempt to fetch the current value of the reporting settings. | 221 // Attempt to fetch the current value of the reporting settings. |
295 // If trusted values are not available, register this function to be called | 222 // If trusted values are not available, register this function to be called |
296 // back when they are available. | 223 // back when they are available. |
297 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); | 224 chromeos::CrosSettings* settings = chromeos::CrosSettings::Get(); |
298 if (chromeos::CrosSettingsProvider::TRUSTED != | 225 if (chromeos::CrosSettingsProvider::TRUSTED != |
299 settings->PrepareTrustedValues( | 226 settings->PrepareTrustedValues( |
300 base::Bind(&SystemLogUploader::RefreshUploadSettings, | 227 base::Bind(&SystemLogUploader::RefreshUploadSettings, |
301 weak_factory_.GetWeakPtr()))) { | 228 weak_factory_.GetWeakPtr()))) { |
302 return; | 229 return; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 base::TimeDelta()); | 284 base::TimeDelta()); |
358 // Ensure that we never have more than one pending delayed task. | 285 // Ensure that we never have more than one pending delayed task. |
359 weak_factory_.InvalidateWeakPtrs(); | 286 weak_factory_.InvalidateWeakPtrs(); |
360 task_runner_->PostDelayedTask(FROM_HERE, | 287 task_runner_->PostDelayedTask(FROM_HERE, |
361 base::Bind(&SystemLogUploader::StartLogUpload, | 288 base::Bind(&SystemLogUploader::StartLogUpload, |
362 weak_factory_.GetWeakPtr()), | 289 weak_factory_.GetWeakPtr()), |
363 delay); | 290 delay); |
364 } | 291 } |
365 | 292 |
366 } // namespace policy | 293 } // namespace policy |
OLD | NEW |