| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/test/test_file_util.h" | 5 #include "base/test/test_file_util.h" |
| 6 | 6 |
| 7 #include <windows.h> | |
| 8 #include <aclapi.h> | 7 #include <aclapi.h> |
| 9 #include <shlwapi.h> | 8 #include <shlwapi.h> |
| 10 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <wchar.h> |
| 11 #include <windows.h> |
| 11 | 12 |
| 13 #include <memory> |
| 12 #include <vector> | 14 #include <vector> |
| 13 | 15 |
| 14 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 15 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 16 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 18 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
| 19 #include "base/win/scoped_handle.h" | 22 #include "base/win/scoped_handle.h" |
| 20 | 23 |
| 21 namespace base { | 24 namespace base { |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 struct PermissionInfo { | 28 struct PermissionInfo { |
| 26 PSECURITY_DESCRIPTOR security_descriptor; | 29 PSECURITY_DESCRIPTOR security_descriptor; |
| 27 ACL dacl; | 30 ACL dacl; |
| 28 }; | 31 }; |
| 29 | 32 |
| 30 // Deny |permission| on the file |path|, for the current user. | |
| 31 bool DenyFilePermission(const FilePath& path, DWORD permission) { | |
| 32 PACL old_dacl; | |
| 33 PSECURITY_DESCRIPTOR security_descriptor; | |
| 34 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | |
| 35 SE_FILE_OBJECT, | |
| 36 DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, | |
| 37 NULL, &security_descriptor) != ERROR_SUCCESS) { | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 EXPLICIT_ACCESS change; | |
| 42 change.grfAccessPermissions = permission; | |
| 43 change.grfAccessMode = DENY_ACCESS; | |
| 44 change.grfInheritance = 0; | |
| 45 change.Trustee.pMultipleTrustee = NULL; | |
| 46 change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; | |
| 47 change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; | |
| 48 change.Trustee.TrusteeType = TRUSTEE_IS_USER; | |
| 49 change.Trustee.ptstrName = const_cast<wchar_t*>(L"CURRENT_USER"); | |
| 50 | |
| 51 PACL new_dacl; | |
| 52 if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { | |
| 53 LocalFree(security_descriptor); | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | |
| 58 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, | |
| 59 NULL, NULL, new_dacl, NULL); | |
| 60 LocalFree(security_descriptor); | |
| 61 LocalFree(new_dacl); | |
| 62 | |
| 63 return rc == ERROR_SUCCESS; | |
| 64 } | |
| 65 | |
| 66 // Gets a blob indicating the permission information for |path|. | 33 // Gets a blob indicating the permission information for |path|. |
| 67 // |length| is the length of the blob. Zero on failure. | 34 // |length| is the length of the blob. Zero on failure. |
| 68 // Returns the blob pointer, or NULL on failure. | 35 // Returns the blob pointer, or NULL on failure. |
| 69 void* GetPermissionInfo(const FilePath& path, size_t* length) { | 36 void* GetPermissionInfo(const FilePath& path, size_t* length) { |
| 70 DCHECK(length != NULL); | 37 DCHECK(length != NULL); |
| 71 *length = 0; | 38 *length = 0; |
| 72 PACL dacl = NULL; | 39 PACL dacl = NULL; |
| 73 PSECURITY_DESCRIPTOR security_descriptor; | 40 PSECURITY_DESCRIPTOR security_descriptor; |
| 74 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | 41 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
| 75 SE_FILE_OBJECT, | 42 SE_FILE_OBJECT, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 102 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, | 69 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, |
| 103 NULL, NULL, &perm->dacl, NULL); | 70 NULL, NULL, &perm->dacl, NULL); |
| 104 LocalFree(perm->security_descriptor); | 71 LocalFree(perm->security_descriptor); |
| 105 | 72 |
| 106 char* char_array = reinterpret_cast<char*>(info); | 73 char* char_array = reinterpret_cast<char*>(info); |
| 107 delete [] char_array; | 74 delete [] char_array; |
| 108 | 75 |
| 109 return rc == ERROR_SUCCESS; | 76 return rc == ERROR_SUCCESS; |
| 110 } | 77 } |
| 111 | 78 |
| 79 std::unique_ptr<wchar_t[]> ToCStr(const std::basic_string<wchar_t>& str) { |
| 80 size_t size = str.size() + 1; |
| 81 std::unique_ptr<wchar_t[]> ptr = base::MakeUnique<wchar_t[]>(size); |
| 82 wcsncpy(ptr.get(), str.c_str(), size); |
| 83 return ptr; |
| 84 } |
| 85 |
| 112 } // namespace | 86 } // namespace |
| 113 | 87 |
| 114 bool DieFileDie(const FilePath& file, bool recurse) { | 88 bool DieFileDie(const FilePath& file, bool recurse) { |
| 115 // It turns out that to not induce flakiness a long timeout is needed. | 89 // It turns out that to not induce flakiness a long timeout is needed. |
| 116 const int kIterations = 25; | 90 const int kIterations = 25; |
| 117 const TimeDelta kTimeout = TimeDelta::FromSeconds(10) / kIterations; | 91 const TimeDelta kTimeout = TimeDelta::FromSeconds(10) / kIterations; |
| 118 | 92 |
| 119 if (!PathExists(file)) | 93 if (!PathExists(file)) |
| 120 return true; | 94 return true; |
| 121 | 95 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 142 // local experimentation validates this simplified and *much* faster approach: | 116 // local experimentation validates this simplified and *much* faster approach: |
| 143 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. | 117 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. |
| 144 // [2] Telemetry performance test startup.cold.blank_page reports sane values. | 118 // [2] Telemetry performance test startup.cold.blank_page reports sane values. |
| 145 BY_HANDLE_FILE_INFORMATION bhi = {0}; | 119 BY_HANDLE_FILE_INFORMATION bhi = {0}; |
| 146 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); | 120 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); |
| 147 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, | 121 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, |
| 148 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); | 122 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); |
| 149 return true; | 123 return true; |
| 150 } | 124 } |
| 151 | 125 |
| 126 // Deny |permission| on the file |path|, for the current user. |
| 127 bool DenyFilePermission(const FilePath& path, DWORD permission) { |
| 128 PACL old_dacl; |
| 129 PSECURITY_DESCRIPTOR security_descriptor; |
| 130 |
| 131 std::unique_ptr<TCHAR[]> path_ptr = ToCStr(path.value()); |
| 132 if (GetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, |
| 133 DACL_SECURITY_INFORMATION, nullptr, nullptr, |
| 134 &old_dacl, nullptr, |
| 135 &security_descriptor) != ERROR_SUCCESS) { |
| 136 return false; |
| 137 } |
| 138 |
| 139 std::unique_ptr<TCHAR[]> current_user = ToCStr(std::wstring(L"CURRENT_USER")); |
| 140 EXPLICIT_ACCESS new_access = { |
| 141 permission, |
| 142 DENY_ACCESS, |
| 143 0, |
| 144 {nullptr, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME, TRUSTEE_IS_USER, |
| 145 current_user.get()}}; |
| 146 |
| 147 PACL new_dacl; |
| 148 if (SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl) != ERROR_SUCCESS) { |
| 149 LocalFree(security_descriptor); |
| 150 return false; |
| 151 } |
| 152 |
| 153 DWORD rc = SetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, |
| 154 DACL_SECURITY_INFORMATION, nullptr, nullptr, |
| 155 new_dacl, nullptr); |
| 156 LocalFree(security_descriptor); |
| 157 LocalFree(new_dacl); |
| 158 |
| 159 return rc == ERROR_SUCCESS; |
| 160 } |
| 161 |
| 152 // Checks if the volume supports Alternate Data Streams. This is required for | 162 // Checks if the volume supports Alternate Data Streams. This is required for |
| 153 // the Zone Identifier implementation. | 163 // the Zone Identifier implementation. |
| 154 bool VolumeSupportsADS(const FilePath& path) { | 164 bool VolumeSupportsADS(const FilePath& path) { |
| 155 wchar_t drive[MAX_PATH] = {0}; | 165 wchar_t drive[MAX_PATH] = {0}; |
| 156 wcscpy_s(drive, MAX_PATH, path.value().c_str()); | 166 wcscpy_s(drive, MAX_PATH, path.value().c_str()); |
| 157 | 167 |
| 158 if (!PathStripToRootW(drive)) | 168 if (!PathStripToRootW(drive)) |
| 159 return false; | 169 return false; |
| 160 | 170 |
| 161 DWORD fs_flags = 0; | 171 DWORD fs_flags = 0; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 DCHECK(info_ != NULL); | 216 DCHECK(info_ != NULL); |
| 207 DCHECK_NE(0u, length_); | 217 DCHECK_NE(0u, length_); |
| 208 } | 218 } |
| 209 | 219 |
| 210 FilePermissionRestorer::~FilePermissionRestorer() { | 220 FilePermissionRestorer::~FilePermissionRestorer() { |
| 211 if (!RestorePermissionInfo(path_, info_, length_)) | 221 if (!RestorePermissionInfo(path_, info_, length_)) |
| 212 NOTREACHED(); | 222 NOTREACHED(); |
| 213 } | 223 } |
| 214 | 224 |
| 215 } // namespace base | 225 } // namespace base |
| OLD | NEW |