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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/serial/serial_blocklist.h"
#include <algorithm>
#include <string>
#include <string_view>
#include <tuple>
#include "base/metrics/field_trial_params.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
#include "services/device/public/mojom/serial.mojom.h"
namespace {
const char kEntryKeyUsb[] = "usb";
const char kEntryKeyBluetooth[] = "bluetooth";
const char kBluetoothStandardUUID[] = "0000-1000-8000-00805f9b34fb";
// Returns true if the passed string is exactly 4 digits long and only contains
// valid hexadecimal characters (no leading 0x).
bool IsHexComponent(std::string_view string) {
if (string.length() != 4)
return false;
// This is necessary because base::HexStringToUInt allows whitespace and the
// "0x" prefix in its input.
for (char c : string) {
if (c >= '0' && c <= '9')
continue;
if (c >= 'a' && c <= 'f')
continue;
if (c >= 'A' && c <= 'F')
continue;
return false;
}
return true;
}
bool CompareEntry(const SerialBlocklist::Entry& a,
const SerialBlocklist::Entry& b) {
return std::tie(a.usb_vendor_id, a.usb_product_id,
a.bluetooth_service_class_id) <
std::tie(b.usb_vendor_id, b.usb_product_id,
b.bluetooth_service_class_id);
}
// Returns true if an entry in [begin, end) matches |entry|.
template <class Iterator>
bool EntryMatches(Iterator begin,
Iterator end,
const SerialBlocklist::Entry& entry) {
auto it = std::lower_bound(begin, end, entry, CompareEntry);
if (it == end) {
return false;
}
// USB devices
if (it->bluetooth_service_class_id.empty()) {
return it->usb_vendor_id == entry.usb_vendor_id &&
it->usb_product_id == entry.usb_product_id;
}
// Bluetooth devices
return it->bluetooth_service_class_id == entry.bluetooth_service_class_id;
}
} // namespace
BASE_FEATURE(kWebSerialBlocklist,
"WebSerialBlocklist",
base::FEATURE_ENABLED_BY_DEFAULT);
constexpr base::FeatureParam<std::string> kWebSerialBlocklistAdditions{
&kWebSerialBlocklist, "BlocklistAdditions", /*default_value=*/""};
SerialBlocklist::~SerialBlocklist() = default;
// static
SerialBlocklist& SerialBlocklist::Get() {
static base::NoDestructor<SerialBlocklist> blocklist;
return *blocklist;
}
bool SerialBlocklist::IsExcluded(
const device::mojom::SerialPortInfo& port_info) const {
if (!((port_info.has_vendor_id && port_info.has_product_id) ||
port_info.bluetooth_service_class_id.has_value())) {
return false;
}
// We exclude all Bluetooth specified services except Serial Port Profile
// regardless of the blocklist for security.
if (port_info.bluetooth_service_class_id &&
port_info.bluetooth_service_class_id->IsValid() &&
*port_info.bluetooth_service_class_id !=
device::GetSerialPortProfileUUID() &&
port_info.bluetooth_service_class_id->canonical_value().substr(9, 27) ==
kBluetoothStandardUUID) {
return true;
}
Entry entry(port_info.has_vendor_id ? port_info.vendor_id : 0x0,
port_info.has_product_id ? port_info.product_id : 0x0,
port_info.bluetooth_service_class_id
? port_info.bluetooth_service_class_id->canonical_value()
: "");
return EntryMatches(std::begin(static_entries_), std::end(static_entries_),
entry) ||
EntryMatches(dynamic_entries_.begin(), dynamic_entries_.end(), entry);
}
void SerialBlocklist::ResetToDefaultValuesForTesting() {
dynamic_entries_.clear();
PopulateWithServerProvidedValues();
}
SerialBlocklist::SerialBlocklist() {
CHECK(std::is_sorted(std::begin(static_entries_), std::end(static_entries_),
CompareEntry));
PopulateWithServerProvidedValues();
}
void SerialBlocklist::PopulateWithServerProvidedValues() {
std::string blocklist_string = kWebSerialBlocklistAdditions.Get();
for (const auto& entry :
base::SplitStringPiece(blocklist_string, ",", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY)) {
std::vector<std::string_view> components = base::SplitStringPiece(
entry, ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
// Entry must at least indicate the type
if (components.empty()) {
continue;
}
if (components[0] == kEntryKeyUsb) {
if (components.size() != 3 || !IsHexComponent(components[1]) ||
!IsHexComponent(components[2])) {
continue;
}
uint32_t vendor_id;
uint32_t product_id;
if (!base::HexStringToUInt(components[1], &vendor_id) ||
!base::HexStringToUInt(components[2], &product_id)) {
continue;
}
dynamic_entries_.emplace_back(vendor_id, product_id,
/*bluetooth_service_class_id=*/"");
} else if (components[0] == kEntryKeyBluetooth) {
if (components.size() != 2) {
continue;
}
std::string uuid_as_string(components[1]);
device::BluetoothUUID uuid(uuid_as_string);
if (!uuid.IsValid()) {
continue;
}
dynamic_entries_.emplace_back(/*usb_vendor_id=*/0, /*usb_product_id=*/0,
uuid.canonical_value());
}
}
std::sort(dynamic_entries_.begin(), dynamic_entries_.end(), CompareEntry);
}
|