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.
7 #include "sandbox/win/src/sync_policy.h"
9 #include "base/logging.h"
10 #include "base/strings/stringprintf.h"
11 #include "sandbox/win/src/ipc_tags.h"
12 #include "sandbox/win/src/nt_internals.h"
13 #include "sandbox/win/src/policy_engine_opcodes.h"
14 #include "sandbox/win/src/policy_params.h"
15 #include "sandbox/win/src/sandbox_types.h"
16 #include "sandbox/win/src/sandbox_utils.h"
17 #include "sandbox/win/src/sync_interception.h"
18 #include "sandbox/win/src/win_utils.h"
22 // Provides functionality to resolve a symbolic link within the object
23 // directory passed in.
24 NTSTATUS
ResolveSymbolicLink(const base::string16
& directory_name
,
25 const base::string16
& name
,
26 base::string16
* target
) {
27 NtOpenDirectoryObjectFunction NtOpenDirectoryObject
= NULL
;
28 ResolveNTFunctionPtr("NtOpenDirectoryObject", &NtOpenDirectoryObject
);
30 NtQuerySymbolicLinkObjectFunction NtQuerySymbolicLinkObject
= NULL
;
31 ResolveNTFunctionPtr("NtQuerySymbolicLinkObject",
32 &NtQuerySymbolicLinkObject
);
34 NtOpenSymbolicLinkObjectFunction NtOpenSymbolicLinkObject
= NULL
;
35 ResolveNTFunctionPtr("NtOpenSymbolicLinkObject", &NtOpenSymbolicLinkObject
);
37 NtCloseFunction NtClose
= NULL
;
38 ResolveNTFunctionPtr("NtClose", &NtClose
);
40 OBJECT_ATTRIBUTES symbolic_link_directory_attributes
= {};
41 UNICODE_STRING symbolic_link_directory_string
= {};
42 InitObjectAttribs(directory_name
, OBJ_CASE_INSENSITIVE
, NULL
,
43 &symbolic_link_directory_attributes
,
44 &symbolic_link_directory_string
, NULL
);
46 HANDLE symbolic_link_directory
= NULL
;
47 NTSTATUS status
= NtOpenDirectoryObject(&symbolic_link_directory
,
49 &symbolic_link_directory_attributes
);
50 if (status
!= STATUS_SUCCESS
) {
51 DLOG(ERROR
) << "Failed to open symbolic link directory. Error: "
56 OBJECT_ATTRIBUTES symbolic_link_attributes
= {};
57 UNICODE_STRING name_string
= {};
58 InitObjectAttribs(name
, OBJ_CASE_INSENSITIVE
, symbolic_link_directory
,
59 &symbolic_link_attributes
, &name_string
, NULL
);
61 HANDLE symbolic_link
= NULL
;
62 status
= NtOpenSymbolicLinkObject(&symbolic_link
, GENERIC_READ
,
63 &symbolic_link_attributes
);
64 NtClose(symbolic_link_directory
);
65 if (status
!= STATUS_SUCCESS
) {
66 DLOG(ERROR
) << "Failed to open symbolic link Error: " << status
;
70 UNICODE_STRING target_path
= {};
71 unsigned long target_length
= 0;
72 status
= NtQuerySymbolicLinkObject(symbolic_link
, &target_path
,
74 if (status
!= STATUS_BUFFER_TOO_SMALL
) {
75 NtClose(symbolic_link
);
76 DLOG(ERROR
) << "Failed to get length for symbolic link target. Error: "
81 target_path
.Length
= 0;
82 target_path
.MaximumLength
= static_cast<USHORT
>(target_length
);
83 target_path
.Buffer
= new wchar_t[target_path
.MaximumLength
+ 1];
84 status
= NtQuerySymbolicLinkObject(symbolic_link
, &target_path
,
86 if (status
== STATUS_SUCCESS
) {
87 target
->assign(target_path
.Buffer
, target_length
);
89 DLOG(ERROR
) << "Failed to resolve symbolic link. Error: " << status
;
92 NtClose(symbolic_link
);
93 delete[] target_path
.Buffer
;
97 NTSTATUS
GetBaseNamedObjectsDirectory(HANDLE
* directory
) {
98 static HANDLE base_named_objects_handle
= NULL
;
99 if (base_named_objects_handle
) {
100 *directory
= base_named_objects_handle
;
101 return STATUS_SUCCESS
;
104 NtOpenDirectoryObjectFunction NtOpenDirectoryObject
= NULL
;
105 ResolveNTFunctionPtr("NtOpenDirectoryObject", &NtOpenDirectoryObject
);
107 DWORD session_id
= 0;
108 ProcessIdToSessionId(::GetCurrentProcessId(), &session_id
);
110 base::string16 base_named_objects_path
;
112 NTSTATUS status
= ResolveSymbolicLink(L
"\\Sessions\\BNOLINKS",
113 base::StringPrintf(L
"%d", session_id
),
114 &base_named_objects_path
);
115 if (status
!= STATUS_SUCCESS
) {
116 DLOG(ERROR
) << "Failed to resolve BaseNamedObjects path. Error: "
121 UNICODE_STRING directory_name
= {};
122 OBJECT_ATTRIBUTES object_attributes
= {};
123 InitObjectAttribs(base_named_objects_path
, OBJ_CASE_INSENSITIVE
, NULL
,
124 &object_attributes
, &directory_name
, NULL
);
125 status
= NtOpenDirectoryObject(&base_named_objects_handle
,
126 DIRECTORY_ALL_ACCESS
,
128 if (status
== STATUS_SUCCESS
)
129 *directory
= base_named_objects_handle
;
133 bool SyncPolicy::GenerateRules(const wchar_t* name
,
134 TargetPolicy::Semantics semantics
,
135 LowLevelPolicy
* policy
) {
136 base::string16
mod_name(name
);
137 if (mod_name
.empty()) {
141 if (TargetPolicy::EVENTS_ALLOW_ANY
!= semantics
&&
142 TargetPolicy::EVENTS_ALLOW_READONLY
!= semantics
) {
143 // Other flags are not valid for sync policy yet.
148 // Add the open rule.
149 EvalResult result
= ASK_BROKER
;
150 PolicyRule
open(result
);
152 if (!open
.AddStringMatch(IF
, OpenEventParams::NAME
, name
, CASE_INSENSITIVE
))
155 if (TargetPolicy::EVENTS_ALLOW_READONLY
== semantics
) {
156 // We consider all flags that are not known to be readonly as potentially
158 uint32 allowed_flags
= SYNCHRONIZE
| GENERIC_READ
| READ_CONTROL
;
159 uint32 restricted_flags
= ~allowed_flags
;
160 open
.AddNumberMatch(IF_NOT
, OpenEventParams::ACCESS
, restricted_flags
, AND
);
163 if (!policy
->AddRule(IPC_OPENEVENT_TAG
, &open
))
166 // If it's not a read only, add the create rule.
167 if (TargetPolicy::EVENTS_ALLOW_READONLY
!= semantics
) {
168 PolicyRule
create(result
);
169 if (!create
.AddStringMatch(IF
, NameBased::NAME
, name
, CASE_INSENSITIVE
))
172 if (!policy
->AddRule(IPC_CREATEEVENT_TAG
, &create
))
179 NTSTATUS
SyncPolicy::CreateEventAction(EvalResult eval_result
,
180 const ClientInfo
& client_info
,
181 const base::string16
&event_name
,
183 uint32 initial_state
,
185 NtCreateEventFunction NtCreateEvent
= NULL
;
186 ResolveNTFunctionPtr("NtCreateEvent", &NtCreateEvent
);
188 // The only action supported is ASK_BROKER which means create the requested
189 // file as specified.
190 if (ASK_BROKER
!= eval_result
)
193 HANDLE object_directory
= NULL
;
194 NTSTATUS status
= GetBaseNamedObjectsDirectory(&object_directory
);
195 if (status
!= STATUS_SUCCESS
)
198 UNICODE_STRING unicode_event_name
= {};
199 OBJECT_ATTRIBUTES object_attributes
= {};
200 InitObjectAttribs(event_name
, OBJ_CASE_INSENSITIVE
, object_directory
,
201 &object_attributes
, &unicode_event_name
, NULL
);
203 HANDLE local_handle
= NULL
;
204 status
= NtCreateEvent(&local_handle
, EVENT_ALL_ACCESS
, &object_attributes
,
205 static_cast<EVENT_TYPE
>(event_type
),
206 static_cast<BOOLEAN
>(initial_state
));
207 if (NULL
== local_handle
)
210 if (!::DuplicateHandle(::GetCurrentProcess(), local_handle
,
211 client_info
.process
, handle
, 0, FALSE
,
212 DUPLICATE_CLOSE_SOURCE
| DUPLICATE_SAME_ACCESS
)) {
213 return STATUS_ACCESS_DENIED
;
218 NTSTATUS
SyncPolicy::OpenEventAction(EvalResult eval_result
,
219 const ClientInfo
& client_info
,
220 const base::string16
&event_name
,
221 uint32 desired_access
,
223 NtOpenEventFunction NtOpenEvent
= NULL
;
224 ResolveNTFunctionPtr("NtOpenEvent", &NtOpenEvent
);
226 // The only action supported is ASK_BROKER which means create the requested
227 // event as specified.
228 if (ASK_BROKER
!= eval_result
)
231 HANDLE object_directory
= NULL
;
232 NTSTATUS status
= GetBaseNamedObjectsDirectory(&object_directory
);
233 if (status
!= STATUS_SUCCESS
)
236 UNICODE_STRING unicode_event_name
= {};
237 OBJECT_ATTRIBUTES object_attributes
= {};
238 InitObjectAttribs(event_name
, OBJ_CASE_INSENSITIVE
, object_directory
,
239 &object_attributes
, &unicode_event_name
, NULL
);
241 HANDLE local_handle
= NULL
;
242 status
= NtOpenEvent(&local_handle
, desired_access
, &object_attributes
);
243 if (NULL
== local_handle
)
246 if (!::DuplicateHandle(::GetCurrentProcess(), local_handle
,
247 client_info
.process
, handle
, 0, FALSE
,
248 DUPLICATE_CLOSE_SOURCE
| DUPLICATE_SAME_ACCESS
)) {
249 return STATUS_ACCESS_DENIED
;
254 } // namespace sandbox