Skip to content

Commit

Permalink
Ignore non-smart-card USB interfaces (#843)
Browse files Browse the repository at this point in the history
Only let the PC/SC see those USB device interfaces that have suitable
properties. The exact criteria is replicated from what the CCID
driver does internally: either the interface class should be "11"
("Smart Card") or both it should be "255" ("Vendor Specific") and the
extra data length should be "54" (which is the standard length of the
CCID descriptor).

The primary goal is to workaround issues with specific composite devices
on modern ChromeOS versions. Before this commit, PC/SC would see all
interfaces and attempt to connect via each of them. This used to cause
no harm, except spurious errors (which we suppressed heuristically).

However, ChromeOS version 105 introduced behavior change, not allowing
to connect to such composite devices more than once. In combination with
our previously built heuristic "if connection fails, simulate that the
whole device reappears under a different bus number" this results in
some devices not being able to initialize at all. This commit fixes such
issues (we're still discussing if ChromeOS permission_broker could
behave better in this case). As a bonus, we reduce log spam for all
composite devices.

This fixes #849. More context in https://b.corp.google.com/issues/291810618.
  • Loading branch information
emaxx-google committed Jul 26, 2023
1 parent 9f485e9 commit cfb267e
Show file tree
Hide file tree
Showing 5 changed files with 582 additions and 7 deletions.
10 changes: 10 additions & 0 deletions smart_card_connector_app/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ goog.require('GoogleSmartCard.PcscLiteServerClientsManagement.ClientHandler');
goog.require('GoogleSmartCard.PcscLiteServerClientsManagement.ReadinessTracker');
goog.require('GoogleSmartCard.PortMessageChannel');
goog.require('GoogleSmartCard.SingleMessageBasedChannel');
goog.require('GoogleSmartCard.SmartCardFilterLibusbHook');
goog.require('goog.asserts');
goog.require('goog.log');
goog.require('goog.log.Logger');
Expand Down Expand Up @@ -117,7 +118,16 @@ if (logBufferForwarderToNaclModule) {

const libusbProxyReceiver =
new GSC.LibusbProxyReceiver(executableModule.getMessageChannel());
// Disable USB communication for the in-session instance of our extension
// whenever the ChromeOS Lock Screen is on, to avoid the access conflict with
// the lock-screen instance of ourselves that might be needed for user
// authentication.
libusbProxyReceiver.addHook(new GSC.LibusbLoginStateHook());
// Ignore non-smart-card USB devices and interfaces. This avoid spurious
// initialization failures in the CCID driver. Also it's a workaround for the
// case when attempts to connect to non-smart-card interfaces fail and cause the
// smart card interface connections to break as well.
libusbProxyReceiver.addHook(new GSC.SmartCardFilterLibusbHook());

const pcscLiteReadinessTracker =
new GSC.PcscLiteServerClientsManagement.ReadinessTracker(
Expand Down
330 changes: 330 additions & 0 deletions smart_card_connector_app/src/smart-card-filter-libusb-hook-unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
/**
* @license
* Copyright 2023 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.require('GoogleSmartCard.LibusbProxyDataModel');
goog.require('GoogleSmartCard.LibusbToJsApiAdaptor');
goog.require('GoogleSmartCard.SmartCardFilterLibusbHook');
goog.require('goog.asserts');
goog.require('goog.testing');
goog.require('goog.testing.asserts');
goog.require('goog.testing.jsunit');

goog.setTestOnly();

goog.scope(function() {

const GSC = GoogleSmartCard;
const LibusbJsConfigurationDescriptor =
GSC.LibusbProxyDataModel.LibusbJsConfigurationDescriptor;

const FAKE_DEVICE_ID = 123;

class FakeLibusbProxyHookDelegate extends GSC.LibusbToJsApiAdaptor {
constructor() {
super();
/** @type {!Array<!LibusbJsConfigurationDescriptor>} */
this.fakeConfigurations_ = [];
}

/**
* @param {!Array<!LibusbJsConfigurationDescriptor>} fakeConfigurations
*/
setFakeConfigurations(fakeConfigurations) {
this.fakeConfigurations_ = fakeConfigurations;
}

/** @override */
async listDevices() {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async getConfigurations(deviceId) {
return this.fakeConfigurations_;
}
/** @override */
async openDeviceHandle(deviceId) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async closeDeviceHandle(deviceId, deviceHandle) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async claimInterface(deviceId, deviceHandle, interfaceNumber) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async releaseInterface(deviceId, deviceHandle, interfaceNumber) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async resetDevice(deviceId, deviceHandle) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async controlTransfer(deviceId, deviceHandle, parameters) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async bulkTransfer(deviceId, deviceHandle, parameters) {
fail('Unexpected call');
goog.asserts.fail();
}
/** @override */
async interruptTransfer(deviceId, deviceHandle, parameters) {
fail('Unexpected call');
goog.asserts.fail();
}
};

/** @type {!FakeLibusbProxyHookDelegate|undefined} */
let fakeDelegate;
/** @type {!GSC.SmartCardFilterLibusbHook|undefined} */
let hook;

goog.exportSymbol('test_SmartCardFilterLibusbHook', {
'setUp': function() {
fakeDelegate = new FakeLibusbProxyHookDelegate();
hook = new GSC.SmartCardFilterLibusbHook();
hook.setDelegate(fakeDelegate);
},

'tearDown': function() {
hook = undefined;
fakeDelegate = undefined;
},

'testEmpty': async function() {
const got = await hook.getConfigurations(FAKE_DEVICE_ID);

assertObjectEquals([], got);
},

// A simple device with the Smart Card USB interface class is passed as-is.
'testSingleInterface_SmartCardClass': async function() {
const CONFIGS = [{
'active': true,
'configurationValue': 1,
'interfaces': [{
'interfaceNumber': 123,
'interfaceClass': 0xB,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
}],
}];
fakeDelegate.setFakeConfigurations(CONFIGS);

const got = await hook.getConfigurations(FAKE_DEVICE_ID);

assertObjectEquals(CONFIGS, got);
},

// A simple device with an unexpected USB interface class is filtered out.
'testSingleInterface_OtherClass': async function() {
const CONFIGS = [{
'active': true,
'configurationValue': 1,
'interfaces': [{
'interfaceNumber': 123,
'interfaceClass': 0x3,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
}],
}];
fakeDelegate.setFakeConfigurations(CONFIGS);

const got = await hook.getConfigurations(FAKE_DEVICE_ID);

const EXPECTED_RESULT = [{
'active': true,
'configurationValue': 1,
'interfaces': [],
}];
assertObjectEquals(EXPECTED_RESULT, got);
},

// A simple device with the Vendor Specific USB interface class and the
// specific "extra data" (54-byte long, conforming to the CCID specs) is
// passed as-is.
'testSingleInterface_VendorClassLikeSmartCard': async function() {
const CONFIGS = [{
'active': true,
'configurationValue': 1,
'interfaces': [{
'interfaceNumber': 123,
'interfaceClass': 0xFF,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'extraData': new ArrayBuffer(54),
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
}],
}];
fakeDelegate.setFakeConfigurations(CONFIGS);

const got = await hook.getConfigurations(FAKE_DEVICE_ID);

assertObjectEquals(CONFIGS, got);
},

// A simple device with the Vendor Specific USB interface class and without
// "extra data" is filtered out.
'testSingleInterface_VendorClassWithoutExtraData': async function() {
// Unlike the test above, the "extraData" key is missing.
const CONFIGS = [{
'active': true,
'configurationValue': 1,
'interfaces': [{
'interfaceNumber': 123,
'interfaceClass': 0xFF,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
}],
}];
fakeDelegate.setFakeConfigurations(CONFIGS);

const got = await hook.getConfigurations(FAKE_DEVICE_ID);

const EXPECTED_RESULT = [{
'active': true,
'configurationValue': 1,
'interfaces': [],
}];
assertObjectEquals(EXPECTED_RESULT, got);
},

// A simple device with the Vendor Specific USB interface class and with
// "extra data" of unexpected length is filtered out.
'testSingleInterface_VendorClassDifferentExtraDataLength': async function() {
// Unlike the successful test above, the "extraData" key is missing.
const CONFIGS = [{
'active': true,
'configurationValue': 1,
'interfaces': [{
'interfaceNumber': 123,
'interfaceClass': 0xFF,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'extraData': new ArrayBuffer(10),
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
}],
}];
fakeDelegate.setFakeConfigurations(CONFIGS);

const got = await hook.getConfigurations(FAKE_DEVICE_ID);

const EXPECTED_RESULT = [{
'active': true,
'configurationValue': 1,
'interfaces': [],
}];
assertObjectEquals(EXPECTED_RESULT, got);
},

// A composite device with both interfaces (one having the Smart Card class
// and another having a different class) is passed only in half.
'testSingleInterface_Composite_SmartCardAndOtherClass': async function() {
const SMART_CARD_INTERFACE = {
'interfaceNumber': 123,
'interfaceClass': 0xB,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
};
const HUMAN_INTERFACE_DEVICE_INTERFACE = {
'interfaceNumber': 234,
'interfaceClass': 0x3,
'interfaceSubclass': 0,
'interfaceProtocol': 0,
'endpoints': [
{
'endpointNumber': 1,
'direction': 'out',
'type': 'bulk',
'packetSize': 64,
},
],
};
const CONFIGS = [{
'active': true,
'configurationValue': 1,
'interfaces': [SMART_CARD_INTERFACE, HUMAN_INTERFACE_DEVICE_INTERFACE],
}];
fakeDelegate.setFakeConfigurations(CONFIGS);

const got = await hook.getConfigurations(FAKE_DEVICE_ID);

const EXPECTED_RESULT = [{
'active': true,
'configurationValue': 1,
'interfaces': [SMART_CARD_INTERFACE],
}];
assertObjectEquals(EXPECTED_RESULT, got);
},
});
});
Loading

0 comments on commit cfb267e

Please sign in to comment.