OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sandbox/win/src/heap_helper.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/win/windows_version.h" | |
11 | |
12 namespace sandbox { | |
13 | |
14 // These are undocumented, but readily found on the internet. | |
15 #define HEAP_CLASS_8 0x00008000 // CSR port heap | |
16 #define HEAP_CLASS_MASK 0x0000f000 | |
17 | |
18 // This structure is not documented, but the flags field is the only relevant | |
19 // field. | |
20 struct _HEAP { | |
21 char reserved[0x70]; | |
22 DWORD flags; | |
23 }; | |
24 | |
25 bool HeapFlags(HANDLE handle, DWORD* flags) { | |
26 if (!handle || !flags) { | |
27 // This is an error. | |
28 return false; | |
29 } | |
30 _HEAP* heap = reinterpret_cast<_HEAP*>(handle); | |
31 *flags = heap->flags; | |
32 return true; | |
33 } | |
34 | |
35 HANDLE FindCsrPortHeap() { | |
36 if (base::win::GetVersion() < base::win::VERSION_WIN10) { | |
37 // This functionality has not been verified on versions before Win10. | |
38 return nullptr; | |
39 } | |
40 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); | |
41 std::unique_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]); | |
42 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps) | |
43 return nullptr; | |
44 | |
45 // Search for the CSR port heap handle, identified purely based on flags. | |
46 HANDLE csr_port_heap = nullptr; | |
47 for (size_t i = 0; i < number_of_heaps; ++i) { | |
48 HANDLE handle = all_heaps[i]; | |
49 DWORD flags = 0; | |
50 if (!HeapFlags(handle, &flags)) { | |
51 LOG(ERROR) << "Unable to get flags for this heap"; | |
dcheng
2017/05/03 23:26:04
Is there a reason these are LOG(ERROR) instead of
| |
52 continue; | |
53 } | |
54 if ((flags & HEAP_CLASS_MASK) == HEAP_CLASS_8) { | |
55 if (nullptr != csr_port_heap) { | |
56 LOG(ERROR) << "Found multiple suitable CSR Port heaps"; | |
57 return nullptr; | |
58 } | |
59 csr_port_heap = handle; | |
60 } | |
61 } | |
62 return csr_port_heap; | |
63 } | |
64 | |
65 } // namespace sandbox | |
OLD | NEW |