File: usb_blocklist.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (176 lines) | stat: -rw-r--r-- 6,107 bytes parent folder | download | duplicates (4)
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
// Copyright 2016 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/usb/usb_blocklist.h"

#include <algorithm>
#include <string>
#include <string_view>
#include <tuple>

#include "base/metrics/field_trial_params.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "services/device/public/cpp/device_features.h"
#include "services/device/public/mojom/usb_device.mojom.h"

namespace {

static base::LazyInstance<UsbBlocklist>::Leaky g_singleton =
    LAZY_INSTANCE_INITIALIZER;

constexpr uint16_t kMaxVersion = 0xffff;

// 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 UsbBlocklist::Entry& a, const UsbBlocklist::Entry& b) {
  return std::tie(a.vendor_id, a.product_id, a.max_version) <
         std::tie(b.vendor_id, b.product_id, b.max_version);
}

// Returns true if an entry in (begin, end] matches the vendor and product IDs
// of |entry| and has a device version greater than or equal to |entry|.
template <class Iterator>
bool EntryMatches(Iterator begin,
                  Iterator end,
                  const UsbBlocklist::Entry& entry) {
  auto it = std::lower_bound(begin, end, entry, CompareEntry);
  return it != end && it->vendor_id == entry.vendor_id &&
         it->product_id == entry.product_id;
}

// This list must be sorted according to CompareEntry.
constexpr UsbBlocklist::Entry kStaticEntries[] = {
    {0x096e, 0x0850, kMaxVersion},  // KEY-ID
    {0x096e, 0x0852, kMaxVersion},  // Feitian
    {0x096e, 0x0853, kMaxVersion},  // Feitian
    {0x096e, 0x0854, kMaxVersion},  // Feitian
    {0x096e, 0x0856, kMaxVersion},  // Feitian
    {0x096e, 0x0858, kMaxVersion},  // Feitian USB+NFC
    {0x096e, 0x085a, kMaxVersion},  // Feitian
    {0x096e, 0x085b, kMaxVersion},  // Feitian
    {0x096e, 0x0880, kMaxVersion},  // HyperFIDO

    {0x09c3, 0x0023, kMaxVersion},  // HID Global BlueTrust Token

    // Yubikey devices. https://crbug.com/818807
    {0x1050, 0x0010, kMaxVersion},
    {0x1050, 0x0018, kMaxVersion},
    {0x1050, 0x0030, kMaxVersion},
    {0x1050, 0x0110, kMaxVersion},
    {0x1050, 0x0111, kMaxVersion},
    {0x1050, 0x0112, kMaxVersion},
    {0x1050, 0x0113, kMaxVersion},
    {0x1050, 0x0114, kMaxVersion},
    {0x1050, 0x0115, kMaxVersion},
    {0x1050, 0x0116, kMaxVersion},
    {0x1050, 0x0120, kMaxVersion},
    {0x1050, 0x0200, kMaxVersion},
    {0x1050, 0x0211, kMaxVersion},
    {0x1050, 0x0401, kMaxVersion},
    {0x1050, 0x0402, kMaxVersion},
    {0x1050, 0x0403, kMaxVersion},
    {0x1050, 0x0404, kMaxVersion},
    {0x1050, 0x0405, kMaxVersion},
    {0x1050, 0x0406, kMaxVersion},
    {0x1050, 0x0407, kMaxVersion},
    {0x1050, 0x0410, kMaxVersion},

    {0x10c4, 0x8acf, kMaxVersion},  // U2F Zero
    {0x18d1, 0x5026, kMaxVersion},  // Titan
    {0x1a44, 0x00bb, kMaxVersion},  // VASCO
    {0x1d50, 0x60fc, kMaxVersion},  // OnlyKey
    {0x1e0d, 0xf1ae, kMaxVersion},  // Keydo AES
    {0x1e0d, 0xf1d0, kMaxVersion},  // Neowave Keydo
    {0x1ea8, 0xf025, kMaxVersion},  // Thetis
    {0x20a0, 0x4287, kMaxVersion},  // Nitrokey
    {0x24dc, 0x0101, kMaxVersion},  // JaCarta
    {0x2581, 0xf1d0, kMaxVersion},  // Happlink
    {0x2abe, 0x1002, kMaxVersion},  // Bluink
    {0x2ccf, 0x0880, kMaxVersion},  // Feitian USB, HyperFIDO
};

}  // namespace

UsbBlocklist::~UsbBlocklist() = default;

// static
UsbBlocklist& UsbBlocklist::Get() {
  return g_singleton.Get();
}

bool UsbBlocklist::IsExcluded(const Entry& entry) const {
  return EntryMatches(std::begin(kStaticEntries), std::end(kStaticEntries),
                      entry) ||
         EntryMatches(dynamic_entries_.begin(), dynamic_entries_.end(), entry);
}

bool UsbBlocklist::IsExcluded(
    const device::mojom::UsbDeviceInfo& device_info) const {
  uint16_t device_version = device_info.device_version_major << 8 |
                            device_info.device_version_minor << 4 |
                            device_info.device_version_subminor;
  return IsExcluded(
      Entry{device_info.vendor_id, device_info.product_id, device_version});
}

void UsbBlocklist::ResetToDefaultValuesForTest() {
  dynamic_entries_.clear();
  PopulateWithServerProvidedValues();
}

UsbBlocklist::UsbBlocklist() {
  DCHECK(std::is_sorted(std::begin(kStaticEntries), std::end(kStaticEntries),
                        CompareEntry));
  PopulateWithServerProvidedValues();
}

void UsbBlocklist::PopulateWithServerProvidedValues() {
  std::string blocklist_string = base::GetFieldTrialParamByFeatureAsString(
      /*feature=*/features::kWebUsbBlocklist,
      /*param_name=*/"blocklist_additions",
      /*default_value=*/"");

  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);
    if (components.size() != 3 || !IsHexComponent(components[0]) ||
        !IsHexComponent(components[1]) || !IsHexComponent(components[2])) {
      continue;
    }

    uint32_t vendor_id;
    uint32_t product_id;
    uint32_t max_version;
    if (!base::HexStringToUInt(components[0], &vendor_id) ||
        !base::HexStringToUInt(components[1], &product_id) ||
        !base::HexStringToUInt(components[2], &max_version)) {
      continue;
    }

    dynamic_entries_.emplace_back(vendor_id, product_id, max_version);
  }

  std::sort(dynamic_entries_.begin(), dynamic_entries_.end(), CompareEntry);
}