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" |
17 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
18 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
19 #include "base/win/scoped_handle.h" | 21 #include "base/win/scoped_handle.h" |
20 | 22 |
21 namespace base { | 23 namespace base { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 struct PermissionInfo { | 27 struct PermissionInfo { |
26 PSECURITY_DESCRIPTOR security_descriptor; | 28 PSECURITY_DESCRIPTOR security_descriptor; |
27 ACL dacl; | 29 ACL dacl; |
28 }; | 30 }; |
29 | 31 |
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|. | 32 // Gets a blob indicating the permission information for |path|. |
67 // |length| is the length of the blob. Zero on failure. | 33 // |length| is the length of the blob. Zero on failure. |
68 // Returns the blob pointer, or NULL on failure. | 34 // Returns the blob pointer, or NULL on failure. |
69 void* GetPermissionInfo(const FilePath& path, size_t* length) { | 35 void* GetPermissionInfo(const FilePath& path, size_t* length) { |
70 DCHECK(length != NULL); | 36 DCHECK(length != NULL); |
71 *length = 0; | 37 *length = 0; |
72 PACL dacl = NULL; | 38 PACL dacl = NULL; |
73 PSECURITY_DESCRIPTOR security_descriptor; | 39 PSECURITY_DESCRIPTOR security_descriptor; |
74 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | 40 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
75 SE_FILE_OBJECT, | 41 SE_FILE_OBJECT, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 // local experimentation validates this simplified and *much* faster approach: | 108 // local experimentation validates this simplified and *much* faster approach: |
143 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. | 109 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. |
144 // [2] Telemetry performance test startup.cold.blank_page reports sane values. | 110 // [2] Telemetry performance test startup.cold.blank_page reports sane values. |
145 BY_HANDLE_FILE_INFORMATION bhi = {0}; | 111 BY_HANDLE_FILE_INFORMATION bhi = {0}; |
146 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); | 112 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); |
147 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, | 113 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, |
148 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); | 114 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); |
149 return true; | 115 return true; |
150 } | 116 } |
151 | 117 |
118 // Deny |permission| on the file |path|, for the current user. | |
119 bool DenyFilePermission(const FilePath& path, DWORD permission) { | |
120 PACL old_dacl; | |
121 PSECURITY_DESCRIPTOR security_descriptor; | |
122 | |
123 int path_size = path.value().size(); | |
Peter Kasting
2016/07/11 02:35:53
Should be size_t.
WC Leung
2016/07/18 09:41:35
Done.
| |
124 std::unique_ptr<TCHAR[]> path_ptr(new TCHAR[path_size + 1]); | |
Peter Kasting
2016/07/11 02:35:53
Nit: Prefer "= base::MakeUnique" to raw new.
WC Leung
2016/07/18 09:41:35
Done. Thanks for making base::MakeUnique known to
| |
125 wcsncpy(path_ptr.get(), path.value().c_str(), path_size + 1); | |
126 path_ptr[path_size] = L'\0'; | |
Peter Kasting
2016/07/11 02:35:53
I don't see why this line is needed, since the sou
WC Leung
2016/07/18 09:41:35
I'm super-paranoid here because a missing '\0' cau
Peter Kasting
2016/07/18 17:34:30
I'm opposed to adding something to account for (2)
WC Leung
2016/07/19 08:00:12
I see. I do buy in the readability part. So the li
| |
127 | |
128 if (GetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, | |
129 DACL_SECURITY_INFORMATION, nullptr, nullptr, | |
130 &old_dacl, nullptr, | |
131 &security_descriptor) != ERROR_SUCCESS) { | |
132 return false; | |
133 } | |
134 | |
135 LPTSTR current_user = L"CURRENT_USER"; | |
136 EXPLICIT_ACCESS new_access = { | |
137 permission, | |
138 DENY_ACCESS, | |
139 0, | |
140 {nullptr, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME, TRUSTEE_IS_USER, | |
141 current_user}}; | |
142 | |
143 PACL new_dacl; | |
144 if (SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl) != ERROR_SUCCESS) { | |
145 LocalFree(security_descriptor); | |
146 return false; | |
147 } | |
148 | |
149 DWORD rc = SetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, | |
150 DACL_SECURITY_INFORMATION, nullptr, nullptr, | |
151 new_dacl, nullptr); | |
152 LocalFree(security_descriptor); | |
153 LocalFree(new_dacl); | |
154 | |
155 return rc == ERROR_SUCCESS; | |
156 } | |
157 | |
152 // Checks if the volume supports Alternate Data Streams. This is required for | 158 // Checks if the volume supports Alternate Data Streams. This is required for |
153 // the Zone Identifier implementation. | 159 // the Zone Identifier implementation. |
154 bool VolumeSupportsADS(const FilePath& path) { | 160 bool VolumeSupportsADS(const FilePath& path) { |
155 wchar_t drive[MAX_PATH] = {0}; | 161 wchar_t drive[MAX_PATH] = {0}; |
156 wcscpy_s(drive, MAX_PATH, path.value().c_str()); | 162 wcscpy_s(drive, MAX_PATH, path.value().c_str()); |
157 | 163 |
158 if (!PathStripToRootW(drive)) | 164 if (!PathStripToRootW(drive)) |
159 return false; | 165 return false; |
160 | 166 |
161 DWORD fs_flags = 0; | 167 DWORD fs_flags = 0; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 DCHECK(info_ != NULL); | 212 DCHECK(info_ != NULL); |
207 DCHECK_NE(0u, length_); | 213 DCHECK_NE(0u, length_); |
208 } | 214 } |
209 | 215 |
210 FilePermissionRestorer::~FilePermissionRestorer() { | 216 FilePermissionRestorer::~FilePermissionRestorer() { |
211 if (!RestorePermissionInfo(path_, info_, length_)) | 217 if (!RestorePermissionInfo(path_, info_, length_)) |
212 NOTREACHED(); | 218 NOTREACHED(); |
213 } | 219 } |
214 | 220 |
215 } // namespace base | 221 } // namespace base |
OLD | NEW |