1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/kasko/loader_lock.h"
16 :
17 : #include <stdint.h>
18 : #include <winternl.h>
19 :
20 : namespace kasko {
21 :
22 : namespace {
23 :
24 E : void* AddOffset(void* ptr, int offset) {
25 E : return reinterpret_cast<uint8_t*>(ptr) + offset;
26 E : }
27 :
28 : } // namespace
29 :
30 E : CRITICAL_SECTION* GetLoaderLock() {
31 : // The offset to the loader lock in the PEB structure. This value
32 : // is undocumented but appears to never change.
33 : static const uint32_t kLoaderLockOffset = 0xa0;
34 : static_assert(4 == sizeof(void*), "Only supported in 32 bit.");
35 : // In 64 bit processes, the offset is 0x110.
36 :
37 E : PEB* peb = NtCurrentTeb()->ProcessEnvironmentBlock;
38 : CRITICAL_SECTION* loader_lock =
39 E : *reinterpret_cast<CRITICAL_SECTION**>(AddOffset(peb, kLoaderLockOffset));
40 :
41 E : return loader_lock;
42 E : }
43 :
44 : } // namespace kasko
|