1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/installer/util/advanced_firewall_manager_win.h"
#include <objbase.h>
#include <stddef.h>
#include "base/check_op.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions_win.h"
#include "base/strings/utf_string_conversions.h"
#include "base/uuid.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_variant.h"
namespace installer {
AdvancedFirewallManager::AdvancedFirewallManager() = default;
AdvancedFirewallManager::~AdvancedFirewallManager() = default;
bool AdvancedFirewallManager::Init(const std::wstring& app_name,
const base::FilePath& app_path) {
firewall_rules_ = nullptr;
HRESULT hr = ::CoCreateInstance(CLSID_NetFwPolicy2, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&firewall_policy_));
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
firewall_policy_ = nullptr;
return false;
}
hr = firewall_policy_->get_Rules(&firewall_rules_);
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
firewall_rules_ = nullptr;
return false;
}
app_name_ = app_name;
app_path_ = app_path;
return true;
}
bool AdvancedFirewallManager::IsFirewallEnabled() {
long profile_types = 0;
HRESULT hr = firewall_policy_->get_CurrentProfileTypes(&profile_types);
if (FAILED(hr))
return false;
// The most-restrictive active profile takes precedence.
const NET_FW_PROFILE_TYPE2 kProfileTypes[] = {
NET_FW_PROFILE2_PUBLIC, NET_FW_PROFILE2_PRIVATE, NET_FW_PROFILE2_DOMAIN};
for (size_t i = 0; i < std::size(kProfileTypes); ++i) {
if ((profile_types & kProfileTypes[i]) != 0) {
VARIANT_BOOL enabled = VARIANT_TRUE;
hr = firewall_policy_->get_FirewallEnabled(kProfileTypes[i], &enabled);
// Assume the firewall is enabled if we can't determine.
if (FAILED(hr) || enabled != VARIANT_FALSE)
return true;
}
}
return false;
}
bool AdvancedFirewallManager::HasAnyRule() {
std::vector<Microsoft::WRL::ComPtr<INetFwRule>> rules;
GetAllRules(&rules);
return !rules.empty();
}
bool AdvancedFirewallManager::AddUDPRule(const std::wstring& rule_name,
const std::wstring& description,
uint16_t port) {
// Delete the rule. According MDSN |INetFwRules::Add| should replace rule with
// same "rule identifier". It's not clear what is "rule identifier", but it
// can successfully create many rule with same name.
DeleteRuleByName(rule_name);
// Create the rule and add it to the rule set (only succeeds if elevated).
Microsoft::WRL::ComPtr<INetFwRule> udp_rule =
CreateUDPRule(rule_name, description, port);
if (!udp_rule.Get())
return false;
HRESULT hr = firewall_rules_->Add(udp_rule.Get());
DLOG_IF(ERROR, FAILED(hr)) << logging::SystemErrorCodeToString(hr);
return SUCCEEDED(hr);
}
void AdvancedFirewallManager::DeleteRuleByName(const std::wstring& rule_name) {
std::vector<Microsoft::WRL::ComPtr<INetFwRule>> rules;
GetAllRules(&rules);
for (size_t i = 0; i < rules.size(); ++i) {
base::win::ScopedBstr name;
HRESULT hr = rules[i]->get_Name(name.Receive());
if (SUCCEEDED(hr) && name.Get() && std::wstring(name.Get()) == rule_name) {
DeleteRule(rules[i]);
}
}
}
void AdvancedFirewallManager::DeleteRule(
Microsoft::WRL::ComPtr<INetFwRule> rule) {
// Rename rule to unique name and delete by unique name. We can't just delete
// rule by name. Multiple rules with the same name and different app are
// possible.
base::win::ScopedBstr unique_name(
base::ASCIIToWide(base::Uuid::GenerateRandomV4().AsLowercaseString()));
rule->put_Name(unique_name.Get());
firewall_rules_->Remove(unique_name.Get());
}
void AdvancedFirewallManager::DeleteAllRules() {
std::vector<Microsoft::WRL::ComPtr<INetFwRule>> rules;
GetAllRules(&rules);
for (size_t i = 0; i < rules.size(); ++i) {
DeleteRule(rules[i]);
}
}
Microsoft::WRL::ComPtr<INetFwRule> AdvancedFirewallManager::CreateUDPRule(
const std::wstring& rule_name,
const std::wstring& description,
uint16_t port) {
Microsoft::WRL::ComPtr<INetFwRule> udp_rule;
HRESULT hr = ::CoCreateInstance(CLSID_NetFwRule, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&udp_rule));
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
return Microsoft::WRL::ComPtr<INetFwRule>();
}
udp_rule->put_Name(base::win::ScopedBstr(rule_name).Get());
udp_rule->put_Description(base::win::ScopedBstr(description).Get());
udp_rule->put_ApplicationName(base::win::ScopedBstr(app_path_.value()).Get());
udp_rule->put_Protocol(NET_FW_IP_PROTOCOL_UDP);
udp_rule->put_Direction(NET_FW_RULE_DIR_IN);
udp_rule->put_Enabled(VARIANT_TRUE);
udp_rule->put_LocalPorts(
base::win::ScopedBstr(base::NumberToWString(port)).Get());
udp_rule->put_Grouping(base::win::ScopedBstr(app_name_).Get());
udp_rule->put_Profiles(NET_FW_PROFILE2_ALL);
udp_rule->put_Action(NET_FW_ACTION_ALLOW);
return udp_rule;
}
void AdvancedFirewallManager::GetAllRules(
std::vector<Microsoft::WRL::ComPtr<INetFwRule>>* rules) {
Microsoft::WRL::ComPtr<IUnknown> rules_enum_unknown;
HRESULT hr = firewall_rules_->get__NewEnum(&rules_enum_unknown);
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
return;
}
Microsoft::WRL::ComPtr<IEnumVARIANT> rules_enum;
hr = rules_enum_unknown.As(&rules_enum);
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
return;
}
for (;;) {
base::win::ScopedVariant rule_var;
hr = rules_enum->Next(1, rule_var.Receive(), nullptr);
DLOG_IF(ERROR, FAILED(hr)) << logging::SystemErrorCodeToString(hr);
if (hr != S_OK)
break;
DCHECK_EQ(VT_DISPATCH, rule_var.type());
if (VT_DISPATCH != rule_var.type()) {
DLOG(ERROR) << "Unexpected type";
continue;
}
Microsoft::WRL::ComPtr<INetFwRule> rule;
hr = V_DISPATCH(rule_var.ptr())->QueryInterface(IID_PPV_ARGS(&rule));
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
continue;
}
base::win::ScopedBstr path;
hr = rule->get_ApplicationName(path.Receive());
if (FAILED(hr)) {
DLOG(ERROR) << logging::SystemErrorCodeToString(hr);
continue;
}
if (!path.Get() || !base::FilePath::CompareEqualIgnoreCase(
path.Get(), app_path_.value())) {
continue;
}
rules->push_back(rule);
}
}
} // namespace installer
|