forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile_remover.cc
294 lines (259 loc) · 11.1 KB
/
file_remover.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/os/file_remover.h"
#include <stdint.h>
#include <unordered_set>
#include <utility>
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/chrome_cleaner/mojom/zip_archiver.mojom.h"
#include "chrome/chrome_cleaner/logging/proto/removal_status.pb.h"
#include "chrome/chrome_cleaner/os/disk_util.h"
#include "chrome/chrome_cleaner/os/file_path_sanitization.h"
#include "chrome/chrome_cleaner/os/file_removal_status_updater.h"
#include "chrome/chrome_cleaner/os/pre_fetched_paths.h"
#include "chrome/chrome_cleaner/os/registry_util.h"
#include "chrome/chrome_cleaner/os/scoped_disable_wow64_redirection.h"
#include "chrome/chrome_cleaner/os/whitelisted_directory.h"
namespace chrome_cleaner {
namespace {
bool RegisterFileForPostRebootRemoval(const base::FilePath path) {
// Don't allow directories to be deleted post-reboot. The directory will only
// be deleted if it is empty, and we can't ensure it will be so don't worry
// about it.
if (base::DirectoryExists(path))
return false;
// MoveFileEx with MOVEFILE_DELAY_UNTIL_REBOOT flag and null destination
// registers |file_path| to be deleted on the next system restarts.
constexpr DWORD flags = MOVEFILE_DELAY_UNTIL_REBOOT;
return ::MoveFileEx(path.value().c_str(), nullptr, flags) != 0;
}
void DeleteEmptyDirectories(base::FilePath directory) {
while (
base::DirectoryExists(directory) && base::IsDirectoryEmpty(directory) &&
!WhitelistedDirectory::GetInstance()->IsWhitelistedDirectory(directory)) {
// Empty directories deleted in this cleanup are not logged in the matched
// folders list for the corresponding UwS, because they are not necessarily
// matched by any rule by the scanner.
LOG(INFO) << "Deleting empty directory " << SanitizePath(directory);
if (!base::DeleteFile(directory))
break;
directory = directory.DirName();
}
}
// Sanity checks file names to ensure they don't contain ".." or specify a
// drive root.
bool IsSafeNameForDeletion(const base::FilePath& path) {
// Empty path isn't allowed
if (path.empty())
return false;
const std::wstring& path_str = path.value();
// Disallow anything with "\..\".
if (path_str.find(L"\\..\\") != std::wstring::npos)
return false;
// Ensure the path does not specify a drive root: require a character other
// than \/:. after the last :
size_t last_colon_pos = path_str.rfind(L':');
if (last_colon_pos == std::wstring::npos)
return true;
for (size_t index = last_colon_pos + 1; index < path_str.size(); ++index) {
wchar_t character = path_str[index];
if (character != L'\\' && character != L'/' && character != L'.')
return true;
}
return false;
}
void OnArchiveDone(FileRemover::QuarantineResultCallback archival_done_callback,
mojom::ZipArchiverResultCode result_code) {
switch (result_code) {
// If the archive exists, |path| has already been quarantined.
case mojom::ZipArchiverResultCode::kSuccess:
case mojom::ZipArchiverResultCode::kZipFileExists:
std::move(archival_done_callback).Run(QUARANTINE_STATUS_QUARANTINED);
return;
case mojom::ZipArchiverResultCode::kIgnoredSourceFile:
std::move(archival_done_callback).Run(QUARANTINE_STATUS_SKIPPED);
return;
default:
LOG(ERROR) << "ZipArchiver returned an error code: " << result_code;
break;
}
std::move(archival_done_callback).Run(QUARANTINE_STATUS_ERROR);
}
} // namespace
FileRemover::FileRemover(scoped_refptr<DigestVerifier> digest_verifier,
std::unique_ptr<ZipArchiver> archiver,
const LayeredServiceProviderAPI& lsp,
base::RepeatingClosure reboot_needed_callback)
: digest_verifier_(digest_verifier),
archiver_(std::move(archiver)),
reboot_needed_callback_(reboot_needed_callback) {
LSPPathToGUIDs providers;
GetLayeredServiceProviders(lsp, &providers);
for (const auto& provider : providers)
deletion_forbidden_paths_.Insert(provider.first);
deletion_forbidden_paths_.Insert(
PreFetchedPaths::GetInstance()->GetExecutablePath());
}
FileRemover::~FileRemover() = default;
void FileRemover::RemoveNow(const base::FilePath& path,
DoneCallback callback) const {
ValidateAndQuarantineFile(
path, base::BindOnce(&FileRemover::RemoveFile, base::Unretained(this)),
std::move(callback));
}
void FileRemover::RegisterPostRebootRemoval(const base::FilePath& path,
DoneCallback callback) const {
ValidateAndQuarantineFile(
path,
base::BindOnce(&FileRemover::ScheduleRemoval, base::Unretained(this)),
std::move(callback));
}
FileRemoverAPI::DeletionValidationStatus FileRemover::CanRemove(
const base::FilePath& file) const {
if (!ValidateSandboxFilePath(file))
return DeletionValidationStatus::UNSAFE;
// Don't remove remote files. Do this before checking the digest so we don't
// read them unnecessarily.
if (base::PathExists(file) && !IsFilePresentLocally(file)) {
LOG(ERROR) << "Cannot remove remote file " << SanitizePath(file);
return DeletionValidationStatus::FORBIDDEN;
}
// Allow removing of all files if |digest_verifier_| is unavailable.
// Otherwise, allow removing only files unknown to |digest_verifier_|.
if (digest_verifier_ && digest_verifier_->IsKnownFile(file)) {
LOG(ERROR) << "Cannot remove known file " << SanitizePath(file);
return DeletionValidationStatus::FORBIDDEN;
}
if (WhitelistedDirectory::GetInstance()->IsWhitelistedDirectory(file)) {
// We are logging the path in both sanitized and non-sanitized form since
// this should never happen unless we are breaking something, in which
// case we will need precise information.
LOG(ERROR) << "Cannot remove a CSIDL path: " << file.value()
<< "', sanitized: '" << SanitizePath(file);
return DeletionValidationStatus::FORBIDDEN;
}
if (!IsSafeNameForDeletion(file) || !file.IsAbsolute() ||
deletion_forbidden_paths_.Contains(file)) {
return DeletionValidationStatus::FORBIDDEN;
}
chrome_cleaner::ScopedDisableWow64Redirection disable_wow64_redirection;
if (base::DirectoryExists(file))
return DeletionValidationStatus::FORBIDDEN;
return DeletionValidationStatus::ALLOWED;
}
void FileRemover::TryToQuarantine(const base::FilePath& path,
QuarantineResultCallback callback) const {
// Archiver may not be provided in tests.
if (archiver_ == nullptr) {
std::move(callback).Run(QUARANTINE_STATUS_DISABLED);
return;
}
archiver_->Archive(path, base::BindOnce(&OnArchiveDone, std::move(callback)));
}
void FileRemover::RemoveFile(const base::FilePath& path,
DoneCallback removal_done_callback,
QuarantineStatus quarantine_status) const {
FileRemovalStatusUpdater* removal_status_updater =
FileRemovalStatusUpdater::GetInstance();
removal_status_updater->UpdateQuarantineStatus(path, quarantine_status);
if (quarantine_status == QUARANTINE_STATUS_ERROR) {
removal_status_updater->UpdateRemovalStatus(
path, REMOVAL_STATUS_ERROR_IN_ARCHIVER);
std::move(removal_done_callback).Run(false);
return;
}
if (!base::DeleteFile(path)) {
// If the attempt to delete the file fails, propagate the failure as
// normal so that the engine knows about it and can try a backup action,
// but also register the file for post-reboot removal in case the engine
// doesn't have any effective backup action.
//
// A potential downside to this implementation is that if the file is
// later successfully deleted, we might ask users to reboot when no
// reboot is needed. See b/66944160 for more details.
if (RegisterFileForPostRebootRemoval(path)) {
reboot_needed_callback_.Run();
removal_status_updater->UpdateRemovalStatus(
path, REMOVAL_STATUS_SCHEDULED_FOR_REMOVAL_FALLBACK);
} else {
removal_status_updater->UpdateRemovalStatus(
path, REMOVAL_STATUS_FAILED_TO_REMOVE);
}
std::move(removal_done_callback).Run(false);
return;
}
removal_status_updater->UpdateRemovalStatus(path, REMOVAL_STATUS_REMOVED);
DeleteEmptyDirectories(path.DirName());
std::move(removal_done_callback).Run(true);
}
void FileRemover::ScheduleRemoval(
const base::FilePath& file_path,
FileRemover::DoneCallback removal_done_callback,
QuarantineStatus quarantine_status) const {
FileRemovalStatusUpdater* removal_status_updater =
FileRemovalStatusUpdater::GetInstance();
removal_status_updater->UpdateQuarantineStatus(file_path, quarantine_status);
if (quarantine_status == QUARANTINE_STATUS_ERROR) {
removal_status_updater->UpdateRemovalStatus(
file_path, REMOVAL_STATUS_ERROR_IN_ARCHIVER);
std::move(removal_done_callback).Run(false);
return;
}
if (!RegisterFileForPostRebootRemoval(file_path)) {
PLOG(ERROR) << "Failed to schedule delete file: "
<< chrome_cleaner::SanitizePath(file_path);
removal_status_updater->UpdateRemovalStatus(
file_path, REMOVAL_STATUS_FAILED_TO_SCHEDULE_FOR_REMOVAL);
std::move(removal_done_callback).Run(false);
return;
}
reboot_needed_callback_.Run();
removal_status_updater->UpdateRemovalStatus(
file_path, REMOVAL_STATUS_SCHEDULED_FOR_REMOVAL);
std::move(removal_done_callback).Run(true);
}
void FileRemover::ValidateAndQuarantineFile(
const base::FilePath& path,
FileRemover::RemovalCallback removal_callback,
FileRemover::DoneCallback done_callback) const {
DeletionValidationStatus status = CanRemove(path);
if (status == DeletionValidationStatus::UNSAFE) {
// Can't record the status of this removal because it's not even safe to
// normalize the path.
std::move(done_callback).Run(false);
return;
}
const base::FilePath normalized_path = NormalizePath(path);
FileRemovalStatusUpdater* removal_status_updater =
FileRemovalStatusUpdater::GetInstance();
switch (status) {
case DeletionValidationStatus::UNSAFE:
// Should be handled above.
NOTREACHED();
break;
case DeletionValidationStatus::FORBIDDEN:
removal_status_updater->UpdateRemovalStatus(
normalized_path, REMOVAL_STATUS_BLACKLISTED_FOR_REMOVAL);
std::move(done_callback).Run(false);
return;
case DeletionValidationStatus::ALLOWED:
// No-op. Proceed to removal.
break;
}
chrome_cleaner::ScopedDisableWow64Redirection disable_wow64_redirection;
if (!base::PathExists(normalized_path)) {
removal_status_updater->UpdateRemovalStatus(normalized_path,
REMOVAL_STATUS_NOT_FOUND);
std::move(done_callback).Run(true);
return;
}
TryToQuarantine(normalized_path,
base::BindOnce(std::move(removal_callback), normalized_path,
std::move(done_callback)));
}
} // namespace chrome_cleaner