1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/win/src/sync_policy.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include <string> 1.9 + 1.10 +#include "sandbox/win/src/sync_policy.h" 1.11 + 1.12 +#include "base/logging.h" 1.13 +#include "sandbox/win/src/ipc_tags.h" 1.14 +#include "sandbox/win/src/policy_engine_opcodes.h" 1.15 +#include "sandbox/win/src/policy_params.h" 1.16 +#include "sandbox/win/src/sandbox_types.h" 1.17 +#include "sandbox/win/src/sandbox_utils.h" 1.18 + 1.19 +namespace sandbox { 1.20 + 1.21 +bool SyncPolicy::GenerateRules(const wchar_t* name, 1.22 + TargetPolicy::Semantics semantics, 1.23 + LowLevelPolicy* policy) { 1.24 + std::wstring mod_name(name); 1.25 + if (mod_name.empty()) { 1.26 + return false; 1.27 + } 1.28 + 1.29 + if (TargetPolicy::EVENTS_ALLOW_ANY != semantics && 1.30 + TargetPolicy::EVENTS_ALLOW_READONLY != semantics) { 1.31 + // Other flags are not valid for sync policy yet. 1.32 + NOTREACHED(); 1.33 + return false; 1.34 + } 1.35 + 1.36 + // Add the open rule. 1.37 + EvalResult result = ASK_BROKER; 1.38 + PolicyRule open(result); 1.39 + 1.40 + if (!open.AddStringMatch(IF, OpenEventParams::NAME, name, CASE_INSENSITIVE)) 1.41 + return false; 1.42 + 1.43 + if (TargetPolicy::EVENTS_ALLOW_READONLY == semantics) { 1.44 + // We consider all flags that are not known to be readonly as potentially 1.45 + // used for write. 1.46 + DWORD allowed_flags = SYNCHRONIZE | GENERIC_READ | READ_CONTROL; 1.47 + DWORD restricted_flags = ~allowed_flags; 1.48 + open.AddNumberMatch(IF_NOT, OpenEventParams::ACCESS, restricted_flags, AND); 1.49 + } 1.50 + 1.51 + if (!policy->AddRule(IPC_OPENEVENT_TAG, &open)) 1.52 + return false; 1.53 + 1.54 + // If it's not a read only, add the create rule. 1.55 + if (TargetPolicy::EVENTS_ALLOW_READONLY != semantics) { 1.56 + PolicyRule create(result); 1.57 + if (!create.AddStringMatch(IF, NameBased::NAME, name, CASE_INSENSITIVE)) 1.58 + return false; 1.59 + 1.60 + if (!policy->AddRule(IPC_CREATEEVENT_TAG, &create)) 1.61 + return false; 1.62 + } 1.63 + 1.64 + return true; 1.65 +} 1.66 + 1.67 +DWORD SyncPolicy::CreateEventAction(EvalResult eval_result, 1.68 + const ClientInfo& client_info, 1.69 + const std::wstring &event_name, 1.70 + uint32 manual_reset, 1.71 + uint32 initial_state, 1.72 + HANDLE *handle) { 1.73 + // The only action supported is ASK_BROKER which means create the requested 1.74 + // file as specified. 1.75 + if (ASK_BROKER != eval_result) 1.76 + return false; 1.77 + 1.78 + HANDLE local_handle = ::CreateEvent(NULL, manual_reset, initial_state, 1.79 + event_name.c_str()); 1.80 + if (NULL == local_handle) 1.81 + return ::GetLastError(); 1.82 + 1.83 + if (!::DuplicateHandle(::GetCurrentProcess(), local_handle, 1.84 + client_info.process, handle, 0, FALSE, 1.85 + DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { 1.86 + return ERROR_ACCESS_DENIED; 1.87 + } 1.88 + return ERROR_SUCCESS; 1.89 +} 1.90 + 1.91 +DWORD SyncPolicy::OpenEventAction(EvalResult eval_result, 1.92 + const ClientInfo& client_info, 1.93 + const std::wstring &event_name, 1.94 + uint32 desired_access, 1.95 + uint32 inherit_handle, 1.96 + HANDLE *handle) { 1.97 + // The only action supported is ASK_BROKER which means create the requested 1.98 + // file as specified. 1.99 + if (ASK_BROKER != eval_result) 1.100 + return false; 1.101 + 1.102 + HANDLE local_handle = ::OpenEvent(desired_access, FALSE, 1.103 + event_name.c_str()); 1.104 + if (NULL == local_handle) 1.105 + return ::GetLastError(); 1.106 + 1.107 + if (!::DuplicateHandle(::GetCurrentProcess(), local_handle, 1.108 + client_info.process, handle, 0, inherit_handle, 1.109 + DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) { 1.110 + return ERROR_ACCESS_DENIED; 1.111 + } 1.112 + return ERROR_SUCCESS; 1.113 +} 1.114 + 1.115 +} // namespace sandbox