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.
4 
5 #include "sandbox/win/src/policy_target.h"
6 
7 #include <stddef.h>
8 
9 #include "sandbox/win/src/crosscall_client.h"
10 #include "sandbox/win/src/ipc_tags.h"
11 #include "sandbox/win/src/policy_engine_processor.h"
12 #include "sandbox/win/src/policy_low_level.h"
13 #include "sandbox/win/src/policy_params.h"
14 #include "sandbox/win/src/sandbox_factory.h"
15 #include "sandbox/win/src/sandbox_nt_util.h"
16 #include "sandbox/win/src/sharedmem_ipc_client.h"
17 #include "sandbox/win/src/target_services.h"
18 
19 namespace sandbox {
20 
21 // Handle for our private heap.
22 extern void* g_heap;
23 
24 // This is the list of all imported symbols from ntdll.dll.
25 SANDBOX_INTERCEPT NtExports g_nt;
26 
27 // Policy data.
28 extern void* volatile g_shared_policy_memory;
29 SANDBOX_INTERCEPT size_t g_shared_policy_size;
30 
QueryBroker(IpcTag ipc_id,CountedParameterSetBase * params)31 bool QueryBroker(IpcTag ipc_id, CountedParameterSetBase* params) {
32   DCHECK_NT(static_cast<size_t>(ipc_id) < kMaxServiceCount);
33   DCHECK_NT(g_shared_policy_memory);
34   DCHECK_NT(g_shared_policy_size > 0);
35 
36   if (static_cast<size_t>(ipc_id) >= kMaxServiceCount)
37     return false;
38 
39   PolicyGlobal* global_policy =
40       reinterpret_cast<PolicyGlobal*>(g_shared_policy_memory);
41 
42   if (!global_policy->entry[static_cast<size_t>(ipc_id)])
43     return false;
44 
45   PolicyBuffer* policy = reinterpret_cast<PolicyBuffer*>(
46       reinterpret_cast<char*>(g_shared_policy_memory) +
47       reinterpret_cast<size_t>(
48           global_policy->entry[static_cast<size_t>(ipc_id)]));
49 
50   if ((reinterpret_cast<size_t>(
51            global_policy->entry[static_cast<size_t>(ipc_id)]) >
52        global_policy->data_size) ||
53       (g_shared_policy_size < global_policy->data_size)) {
54     NOTREACHED_NT();
55     return false;
56   }
57 
58   for (size_t i = 0; i < params->count; i++) {
59     if (!params->parameters[i].IsValid()) {
60       NOTREACHED_NT();
61       return false;
62     }
63   }
64 
65   PolicyProcessor processor(policy);
66   PolicyResult result =
67       processor.Evaluate(kShortEval, params->parameters, params->count);
68   DCHECK_NT(POLICY_ERROR != result);
69 
70   return POLICY_MATCH == result && ASK_BROKER == processor.GetAction();
71 }
72 
73 // -----------------------------------------------------------------------
74 
75 // Hooks NtSetInformationThread to block RevertToSelf from being
76 // called before the actual call to LowerToken.
TargetNtSetInformationThread(NtSetInformationThreadFunction orig_SetInformationThread,HANDLE thread,NT_THREAD_INFORMATION_CLASS thread_info_class,PVOID thread_information,ULONG thread_information_bytes)77 NTSTATUS WINAPI TargetNtSetInformationThread(
78     NtSetInformationThreadFunction orig_SetInformationThread,
79     HANDLE thread,
80     NT_THREAD_INFORMATION_CLASS thread_info_class,
81     PVOID thread_information,
82     ULONG thread_information_bytes) {
83   do {
84     if (SandboxFactory::GetTargetServices()->GetState()->RevertedToSelf())
85       break;
86     if (ThreadImpersonationToken != thread_info_class)
87       break;
88     // This is a revert to self.
89     return STATUS_SUCCESS;
90   } while (false);
91 
92   return orig_SetInformationThread(
93       thread, thread_info_class, thread_information, thread_information_bytes);
94 }
95 
96 // Hooks NtOpenThreadToken to force the open_as_self parameter to be set to
97 // false if we are still running with the impersonation token. open_as_self set
98 // to true means that the token will be open using the process token instead of
99 // the impersonation token. This is bad because the process token does not have
100 // access to open the thread token.
101 NTSTATUS WINAPI
TargetNtOpenThreadToken(NtOpenThreadTokenFunction orig_OpenThreadToken,HANDLE thread,ACCESS_MASK desired_access,BOOLEAN open_as_self,PHANDLE token)102 TargetNtOpenThreadToken(NtOpenThreadTokenFunction orig_OpenThreadToken,
103                         HANDLE thread,
104                         ACCESS_MASK desired_access,
105                         BOOLEAN open_as_self,
106                         PHANDLE token) {
107   if (!SandboxFactory::GetTargetServices()->GetState()->RevertedToSelf())
108     open_as_self = false;
109 
110   return orig_OpenThreadToken(thread, desired_access, open_as_self, token);
111 }
112 
113 // See comment for TargetNtOpenThreadToken
114 NTSTATUS WINAPI
TargetNtOpenThreadTokenEx(NtOpenThreadTokenExFunction orig_OpenThreadTokenEx,HANDLE thread,ACCESS_MASK desired_access,BOOLEAN open_as_self,ULONG handle_attributes,PHANDLE token)115 TargetNtOpenThreadTokenEx(NtOpenThreadTokenExFunction orig_OpenThreadTokenEx,
116                           HANDLE thread,
117                           ACCESS_MASK desired_access,
118                           BOOLEAN open_as_self,
119                           ULONG handle_attributes,
120                           PHANDLE token) {
121   if (!SandboxFactory::GetTargetServices()->GetState()->RevertedToSelf())
122     open_as_self = false;
123 
124   return orig_OpenThreadTokenEx(thread, desired_access, open_as_self,
125                                 handle_attributes, token);
126 }
127 
128 }  // namespace sandbox
129