Chromium Code Reviews| 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 template <class T> | |
| 80 std::unique_ptr<T[]> ToCStr(const std::basic_string<T>& str) { | |
|
Peter Kasting
2016/07/28 20:09:49
Don't templatize this. Use "std::wstring" directl
WC Leung
2016/08/01 18:03:06
Done.
| |
| 81 size_t size = str.size() + 1; | |
| 82 std::unique_ptr<T[]> ptr = base::MakeUnique<T[]>(size); | |
| 83 wcsncpy(ptr.get(), str.c_str(), size); | |
| 84 return ptr; | |
| 85 } | |
| 86 | |
| 112 } // namespace | 87 } // namespace |
| 113 | 88 |
| 114 bool DieFileDie(const FilePath& file, bool recurse) { | 89 bool DieFileDie(const FilePath& file, bool recurse) { |
| 115 // It turns out that to not induce flakiness a long timeout is needed. | 90 // It turns out that to not induce flakiness a long timeout is needed. |
| 116 const int kIterations = 25; | 91 const int kIterations = 25; |
| 117 const TimeDelta kTimeout = TimeDelta::FromSeconds(10) / kIterations; | 92 const TimeDelta kTimeout = TimeDelta::FromSeconds(10) / kIterations; |
| 118 | 93 |
| 119 if (!PathExists(file)) | 94 if (!PathExists(file)) |
| 120 return true; | 95 return true; |
| 121 | 96 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 142 // local experimentation validates this simplified and *much* faster approach: | 117 // local experimentation validates this simplified and *much* faster approach: |
| 143 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. | 118 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. |
| 144 // [2] Telemetry performance test startup.cold.blank_page reports sane values. | 119 // [2] Telemetry performance test startup.cold.blank_page reports sane values. |
| 145 BY_HANDLE_FILE_INFORMATION bhi = {0}; | 120 BY_HANDLE_FILE_INFORMATION bhi = {0}; |
| 146 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); | 121 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); |
| 147 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, | 122 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, |
| 148 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); | 123 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); |
| 149 return true; | 124 return true; |
| 150 } | 125 } |
| 151 | 126 |
| 127 // Deny |permission| on the file |path|, for the current user. | |
| 128 bool DenyFilePermission(const FilePath& path, DWORD permission) { | |
| 129 PACL old_dacl; | |
| 130 PSECURITY_DESCRIPTOR security_descriptor; | |
| 131 | |
| 132 std::unique_ptr<TCHAR[]> path_ptr = ToCStr(path.value()); | |
| 133 if (GetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, | |
| 134 DACL_SECURITY_INFORMATION, nullptr, nullptr, | |
| 135 &old_dacl, nullptr, | |
| 136 &security_descriptor) != ERROR_SUCCESS) { | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 std::unique_ptr<TCHAR[]> current_user = ToCStr(std::wstring(L"CURRENT_USER")); | |
| 141 EXPLICIT_ACCESS new_access = { | |
| 142 permission, | |
| 143 DENY_ACCESS, | |
| 144 0, | |
| 145 {nullptr, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME, TRUSTEE_IS_USER, | |
| 146 current_user.get()}}; | |
| 147 | |
| 148 PACL new_dacl; | |
| 149 if (SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl) != ERROR_SUCCESS) { | |
| 150 LocalFree(security_descriptor); | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 DWORD rc = SetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, | |
| 155 DACL_SECURITY_INFORMATION, nullptr, nullptr, | |
| 156 new_dacl, nullptr); | |
| 157 LocalFree(security_descriptor); | |
| 158 LocalFree(new_dacl); | |
| 159 | |
| 160 return rc == ERROR_SUCCESS; | |
| 161 } | |
| 162 | |
| 152 // Checks if the volume supports Alternate Data Streams. This is required for | 163 // Checks if the volume supports Alternate Data Streams. This is required for |
| 153 // the Zone Identifier implementation. | 164 // the Zone Identifier implementation. |
| 154 bool VolumeSupportsADS(const FilePath& path) { | 165 bool VolumeSupportsADS(const FilePath& path) { |
| 155 wchar_t drive[MAX_PATH] = {0}; | 166 wchar_t drive[MAX_PATH] = {0}; |
| 156 wcscpy_s(drive, MAX_PATH, path.value().c_str()); | 167 wcscpy_s(drive, MAX_PATH, path.value().c_str()); |
| 157 | 168 |
| 158 if (!PathStripToRootW(drive)) | 169 if (!PathStripToRootW(drive)) |
| 159 return false; | 170 return false; |
| 160 | 171 |
| 161 DWORD fs_flags = 0; | 172 DWORD fs_flags = 0; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 DCHECK(info_ != NULL); | 217 DCHECK(info_ != NULL); |
| 207 DCHECK_NE(0u, length_); | 218 DCHECK_NE(0u, length_); |
| 208 } | 219 } |
| 209 | 220 |
| 210 FilePermissionRestorer::~FilePermissionRestorer() { | 221 FilePermissionRestorer::~FilePermissionRestorer() { |
| 211 if (!RestorePermissionInfo(path_, info_, length_)) | 222 if (!RestorePermissionInfo(path_, info_, length_)) |
| 212 NOTREACHED(); | 223 NOTREACHED(); |
| 213 } | 224 } |
| 214 | 225 |
| 215 } // namespace base | 226 } // namespace base |
| OLD | NEW |