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 | |
11 namespace sandbox { | |
12 | |
13 // These are undocumented, but readily found on the internet. | |
14 #define HEAP_CLASS_8 0x00008000 // CSR port heap | |
15 #define HEAP_CLASS_MASK 0x0000f000 | |
16 | |
17 // This structure is not documented, but we only care about the flags field. | |
18 struct _HEAP { | |
19 char reserved[0x70]; | |
20 DWORD flags; | |
21 }; | |
22 | |
23 DWORD HeapFlags(HANDLE handle) { | |
24 _HEAP* heap = reinterpret_cast<_HEAP*>(handle); | |
Will Harris
2017/03/22 19:21:49
is there any validation that can be done here that
liamjm (20p)
2017/04/14 17:27:20
I've added nullptr checks.
We could use ntdll!Rtl
| |
25 return heap->flags; | |
26 } | |
27 | |
28 HANDLE FindCsrPortHeap() { | |
29 DWORD number_of_heaps = ::GetProcessHeaps(0, NULL); | |
30 std::unique_ptr<HANDLE[]> all_heaps(new HANDLE[number_of_heaps]); | |
31 if (::GetProcessHeaps(number_of_heaps, all_heaps.get()) != number_of_heaps) | |
32 return nullptr; | |
33 | |
34 // Let's search for the CSR port heap handle, which we identify purely based | |
Will Harris
2017/03/22 19:21:49
nit: in chromium convention is to avoid use of 'us
liamjm (20p)
2017/04/14 17:27:20
Done.
| |
35 // on flags. | |
36 HANDLE csr_port_heap = nullptr; | |
37 for (size_t i = 0; i < number_of_heaps; ++i) { | |
38 HANDLE handle = all_heaps[i]; | |
39 DWORD flags = HeapFlags(handle); | |
40 if ((flags & HEAP_CLASS_MASK) == HEAP_CLASS_8) { | |
41 if (nullptr != csr_port_heap) { | |
42 LOG(ERROR) << "Found multiple suitable CSR Port heaps"; | |
43 return nullptr; | |
44 } | |
45 csr_port_heap = handle; | |
46 } | |
47 } | |
48 return csr_port_heap; | |
49 } | |
50 | |
51 } // namespace sandbox | |
OLD | NEW |