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
|
// Copyright (c) 2012 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 "content/browser/download/base_file.h"
#include <windows.h>
#include <cguid.h>
#include <objbase.h>
#include <shellapi.h>
#include "base/file_util.h"
#include "base/guid.h"
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "content/browser/download/download_interrupt_reasons_impl.h"
#include "content/browser/download/download_stats.h"
#include "content/browser/safe_util_win.h"
#include "content/public/browser/browser_thread.h"
namespace content {
namespace {
const int kAllSpecialShFileOperationCodes[] = {
// Should be kept in sync with the case statement below.
ERROR_ACCESS_DENIED,
0x71,
0x72,
0x73,
0x74,
0x75,
0x76,
0x78,
0x79,
0x7A,
0x7C,
0x7D,
0x7E,
0x80,
0x81,
0x82,
0x83,
0x84,
0x85,
0x86,
0x87,
0x88,
0xB7,
0x402,
0x10000,
0x10074,
};
// Maps the result of a call to |SHFileOperation()| onto a
// |DownloadInterruptReason|.
//
// These return codes are *old* (as in, DOS era), and specific to
// |SHFileOperation()|.
// They do not appear in any windows header.
//
// See http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx.
DownloadInterruptReason MapShFileOperationCodes(int code) {
DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE;
// Check these pre-Win32 error codes first, then check for matches
// in Winerror.h.
// This switch statement should be kept in sync with the list of codes
// above.
switch (code) {
// Not a pre-Win32 error code; here so that this particular
// case shows up in our histograms. This is redundant with the
// mapping function net::MapSystemError used later.
case ERROR_ACCESS_DENIED: // Access is denied.
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The source and destination files are the same file.
// DE_SAMEFILE == 0x71
case 0x71:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The operation was canceled by the user, or silently canceled if the
// appropriate flags were supplied to SHFileOperation.
// DE_OPCANCELLED == 0x75
case 0x75:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// Security settings denied access to the source.
// DE_ACCESSDENIEDSRC == 0x78
case 0x78:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The source or destination path exceeded or would exceed MAX_PATH.
// DE_PATHTOODEEP == 0x79
case 0x79:
result = DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG;
break;
// The path in the source or destination or both was invalid.
// DE_INVALIDFILES == 0x7C
case 0x7C:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The destination path is an existing file.
// DE_FLDDESTISFILE == 0x7E
case 0x7E:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The destination path is an existing folder.
// DE_FILEDESTISFLD == 0x80
case 0x80:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The name of the file exceeds MAX_PATH.
// DE_FILENAMETOOLONG == 0x81
case 0x81:
result = DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG;
break;
// The destination is a read-only CD-ROM, possibly unformatted.
// DE_DEST_IS_CDROM == 0x82
case 0x82:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The destination is a read-only DVD, possibly unformatted.
// DE_DEST_IS_DVD == 0x83
case 0x83:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The destination is a writable CD-ROM, possibly unformatted.
// DE_DEST_IS_CDRECORD == 0x84
case 0x84:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The file involved in the operation is too large for the destination
// media or file system.
// DE_FILE_TOO_LARGE == 0x85
case 0x85:
result = DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE;
break;
// The source is a read-only CD-ROM, possibly unformatted.
// DE_SRC_IS_CDROM == 0x86
case 0x86:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The source is a read-only DVD, possibly unformatted.
// DE_SRC_IS_DVD == 0x87
case 0x87:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// The source is a writable CD-ROM, possibly unformatted.
// DE_SRC_IS_CDRECORD == 0x88
case 0x88:
result = DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
break;
// MAX_PATH was exceeded during the operation.
// DE_ERROR_MAX == 0xB7
case 0xB7:
result = DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG;
break;
// An unspecified error occurred on the destination.
// XE_ERRORONDEST == 0x10000
case 0x10000:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// Multiple file paths were specified in the source buffer, but only one
// destination file path.
// DE_MANYSRC1DEST == 0x72
case 0x72:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// Rename operation was specified but the destination path is
// a different directory. Use the move operation instead.
// DE_DIFFDIR == 0x73
case 0x73:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The source is a root directory, which cannot be moved or renamed.
// DE_ROOTDIR == 0x74
case 0x74:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The destination is a subtree of the source.
// DE_DESTSUBTREE == 0x76
case 0x76:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The operation involved multiple destination paths,
// which can fail in the case of a move operation.
// DE_MANYDEST == 0x7A
case 0x7A:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// The source and destination have the same parent folder.
// DE_DESTSAMETREE == 0x7D
case 0x7D:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// An unknown error occurred. This is typically due to an invalid path in
// the source or destination. This error does not occur on Windows Vista
// and later.
// DE_UNKNOWN_ERROR == 0x402
case 0x402:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
// Destination is a root directory and cannot be renamed.
// DE_ROOTDIR | ERRORONDEST == 0x10074
case 0x10074:
result = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
break;
}
// Narrow down on the reason we're getting some catch-all interrupt reasons.
if (result == DOWNLOAD_INTERRUPT_REASON_FILE_FAILED) {
UMA_HISTOGRAM_CUSTOM_ENUMERATION(
"Download.MapWinShErrorFileFailed", code,
base::CustomHistogram::ArrayToCustomRanges(
kAllSpecialShFileOperationCodes,
arraysize(kAllSpecialShFileOperationCodes)));
}
if (result == DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED) {
UMA_HISTOGRAM_CUSTOM_ENUMERATION(
"Download.MapWinShErrorAccessDenied", code,
base::CustomHistogram::ArrayToCustomRanges(
kAllSpecialShFileOperationCodes,
arraysize(kAllSpecialShFileOperationCodes)));
}
if (result != DOWNLOAD_INTERRUPT_REASON_NONE)
return result;
// If not one of the above codes, it should be a standard Windows error code.
return ConvertNetErrorToInterruptReason(
net::MapSystemError(code), DOWNLOAD_INTERRUPT_FROM_DISK);
}
// Maps a return code from ScanAndSaveDownloadedFile() to a
// DownloadInterruptReason. The return code in |result| is usually from the
// final IAttachmentExecute::Save() call.
DownloadInterruptReason MapScanAndSaveErrorCodeToInterruptReason(
HRESULT result) {
if (SUCCEEDED(result))
return DOWNLOAD_INTERRUPT_REASON_NONE;
switch (result) {
case INET_E_SECURITY_PROBLEM: // 0x800c000e
// This is returned if the download was blocked due to security
// restrictions. E.g. if the source URL was in the Restricted Sites zone
// and downloads are blocked on that zone, then the download would be
// deleted and this error code is returned.
return DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED;
case E_FAIL: // 0x80004005
// Returned if an anti-virus product reports an infection in the
// downloaded file during IAE::Save().
return DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED;
default:
// Any other error that occurs during IAttachmentExecute::Save() likely
// indicates a problem with the security check, but not necessarily the
// download. See http://crbug.com/153212.
return DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED;
}
}
} // namespace
// Renames a file using the SHFileOperation API to ensure that the target file
// gets the correct default security descriptor in the new path.
// Returns a network error, or net::OK for success.
DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions(
const base::FilePath& new_path) {
base::ThreadRestrictions::AssertIOAllowed();
// The parameters to SHFileOperation must be terminated with 2 NULL chars.
base::FilePath::StringType source = full_path_.value();
base::FilePath::StringType target = new_path.value();
source.append(1, L'\0');
target.append(1, L'\0');
SHFILEOPSTRUCT move_info = {0};
move_info.wFunc = FO_MOVE;
move_info.pFrom = source.c_str();
move_info.pTo = target.c_str();
move_info.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI |
FOF_NOCONFIRMMKDIR | FOF_NOCOPYSECURITYATTRIBS;
int result = SHFileOperation(&move_info);
DownloadInterruptReason interrupt_reason = DOWNLOAD_INTERRUPT_REASON_NONE;
if (result == 0 && move_info.fAnyOperationsAborted)
interrupt_reason = DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
else if (result != 0)
interrupt_reason = MapShFileOperationCodes(result);
if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE)
return LogInterruptReason("SHFileOperation", result, interrupt_reason);
return interrupt_reason;
}
DownloadInterruptReason BaseFile::AnnotateWithSourceInformation() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DCHECK(!detached_);
bound_net_log_.BeginEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ANNOTATED);
DownloadInterruptReason result = DOWNLOAD_INTERRUPT_REASON_NONE;
std::string braces_guid = "{" + client_guid_ + "}";
GUID guid = GUID_NULL;
if (base::IsValidGUID(client_guid_)) {
HRESULT hr = CLSIDFromString(
base::UTF8ToUTF16(braces_guid).c_str(), &guid);
if (FAILED(hr))
guid = GUID_NULL;
}
HRESULT hr = AVScanFile(full_path_, source_url_.spec(), guid);
// If the download file is missing after the call, then treat this as an
// interrupted download.
//
// If the ScanAndSaveDownloadedFile() call failed, but the downloaded file is
// still around, then don't interrupt the download. Attachment Execution
// Services deletes the submitted file if the downloaded file is blocked by
// policy or if it was found to be infected.
//
// If the file is still there, then the error could be due to AES not being
// available or some other error during the AES invocation. In either case,
// we don't surface the error to the user.
if (!base::PathExists(full_path_)) {
DCHECK(FAILED(hr));
result = MapScanAndSaveErrorCodeToInterruptReason(hr);
if (result == DOWNLOAD_INTERRUPT_REASON_NONE) {
RecordDownloadCount(FILE_MISSING_AFTER_SUCCESSFUL_SCAN_COUNT);
result = DOWNLOAD_INTERRUPT_REASON_FILE_SECURITY_CHECK_FAILED;
}
LogInterruptReason("ScanAndSaveDownloadedFile", hr, result);
}
bound_net_log_.EndEvent(net::NetLog::TYPE_DOWNLOAD_FILE_ANNOTATED);
return result;
}
} // namespace content
|