Record MTU discovery packets in net-internals log.
[chromium-blink-merge.git] / sandbox / win / src / sync_policy.cc
blob607b4465d68f296b0689f9ad134c50e83d68e203
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 <string>
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"
20 namespace sandbox {
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,
48 DIRECTORY_QUERY,
49 &symbolic_link_directory_attributes);
50 if (status != STATUS_SUCCESS) {
51 DLOG(ERROR) << "Failed to open symbolic link directory. Error: "
52 << status;
53 return status;
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;
67 return status;
70 UNICODE_STRING target_path = {};
71 unsigned long target_length = 0;
72 status = NtQuerySymbolicLinkObject(symbolic_link, &target_path,
73 &target_length);
74 if (status != STATUS_BUFFER_TOO_SMALL) {
75 NtClose(symbolic_link);
76 DLOG(ERROR) << "Failed to get length for symbolic link target. Error: "
77 << status;
78 return status;
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,
85 &target_length);
86 if (status == STATUS_SUCCESS) {
87 target->assign(target_path.Buffer, target_length);
88 } else {
89 DLOG(ERROR) << "Failed to resolve symbolic link. Error: " << status;
92 NtClose(symbolic_link);
93 delete[] target_path.Buffer;
94 return status;
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: "
117 << status;
118 return status;
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,
127 &object_attributes);
128 if (status == STATUS_SUCCESS)
129 *directory = base_named_objects_handle;
130 return status;
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()) {
138 return false;
141 if (TargetPolicy::EVENTS_ALLOW_ANY != semantics &&
142 TargetPolicy::EVENTS_ALLOW_READONLY != semantics) {
143 // Other flags are not valid for sync policy yet.
144 NOTREACHED();
145 return false;
148 // Add the open rule.
149 EvalResult result = ASK_BROKER;
150 PolicyRule open(result);
152 if (!open.AddStringMatch(IF, OpenEventParams::NAME, name, CASE_INSENSITIVE))
153 return false;
155 if (TargetPolicy::EVENTS_ALLOW_READONLY == semantics) {
156 // We consider all flags that are not known to be readonly as potentially
157 // used for write.
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))
164 return false;
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))
170 return false;
172 if (!policy->AddRule(IPC_CREATEEVENT_TAG, &create))
173 return false;
176 return true;
179 NTSTATUS SyncPolicy::CreateEventAction(EvalResult eval_result,
180 const ClientInfo& client_info,
181 const base::string16 &event_name,
182 uint32 event_type,
183 uint32 initial_state,
184 HANDLE *handle) {
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)
191 return false;
193 HANDLE object_directory = NULL;
194 NTSTATUS status = GetBaseNamedObjectsDirectory(&object_directory);
195 if (status != STATUS_SUCCESS)
196 return status;
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)
208 return status;
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;
215 return status;
218 NTSTATUS SyncPolicy::OpenEventAction(EvalResult eval_result,
219 const ClientInfo& client_info,
220 const base::string16 &event_name,
221 uint32 desired_access,
222 HANDLE *handle) {
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)
229 return false;
231 HANDLE object_directory = NULL;
232 NTSTATUS status = GetBaseNamedObjectsDirectory(&object_directory);
233 if (status != STATUS_SUCCESS)
234 return status;
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)
244 return status;
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;
251 return status;
254 } // namespace sandbox