1 // Copyright (c) 2006-2008 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.
5 #include "sandbox/win/src/target_interceptions.h"
7 #include "sandbox/win/src/interception_agent.h"
8 #include "sandbox/win/src/sandbox_factory.h"
9 #include "sandbox/win/src/sandbox_nt_util.h"
10 #include "sandbox/win/src/target_services.h"
14 SANDBOX_INTERCEPT NtExports g_nt
;
16 // Hooks NtMapViewOfSection to detect the load of DLLs. If hot patching is
17 // required for this dll, this functions patches it.
18 NTSTATUS WINAPI
TargetNtMapViewOfSection(
19 NtMapViewOfSectionFunction orig_MapViewOfSection
, HANDLE section
,
20 HANDLE process
, PVOID
*base
, ULONG_PTR zero_bits
, SIZE_T commit_size
,
21 PLARGE_INTEGER offset
, PSIZE_T view_size
, SECTION_INHERIT inherit
,
22 ULONG allocation_type
, ULONG protect
) {
23 NTSTATUS ret
= orig_MapViewOfSection(section
, process
, base
, zero_bits
,
24 commit_size
, offset
, view_size
, inherit
,
25 allocation_type
, protect
);
27 static int s_load_count
= 0;
28 if (1 == s_load_count
) {
29 SandboxFactory::GetTargetServices()->GetState()->SetKernel32Loaded();
40 if (!IsSameProcess(process
))
43 if (!IsValidImageSection(section
, base
, offset
, view_size
))
47 UNICODE_STRING
* module_name
=
48 GetImageInfoFromModule(reinterpret_cast<HMODULE
>(*base
), &image_flags
);
49 UNICODE_STRING
* file_name
= GetBackingFilePath(*base
);
51 if ((!module_name
) && (image_flags
& MODULE_HAS_CODE
)) {
52 // If the module has no exports we retrieve the module name from the
53 // full path of the mapped section.
54 module_name
= ExtractModuleName(file_name
);
57 InterceptionAgent
* agent
= InterceptionAgent::GetInterceptionAgent();
60 if (!agent
->OnDllLoad(file_name
, module_name
, *base
)) {
61 // Interception agent is demanding to un-map the module.
62 g_nt
.UnmapViewOfSection(process
, *base
);
63 ret
= STATUS_UNSUCCESSFUL
;
68 operator delete(module_name
, NT_ALLOC
);
71 operator delete(file_name
, NT_ALLOC
);
81 NTSTATUS WINAPI
TargetNtUnmapViewOfSection(
82 NtUnmapViewOfSectionFunction orig_UnmapViewOfSection
, HANDLE process
,
84 NTSTATUS ret
= orig_UnmapViewOfSection(process
, base
);
89 if (!IsSameProcess(process
))
92 InterceptionAgent
* agent
= InterceptionAgent::GetInterceptionAgent();
95 agent
->OnDllUnload(base
);
100 } // namespace sandbox