Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore PCI devices on non-0000 domain instead of crashing #560

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions qubes/ext/pci.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
pci_classes = None


#: emit warning on unspported device only once
unsupported_devices_warned = set()


class UnsupportedDevice(Exception):
pass


def load_pci_classes():
''' List of known device classes, subclasses and programming interfaces. '''
# Syntax:
Expand Down Expand Up @@ -138,7 +146,8 @@ class PCIDevice(qubes.devices.DeviceInfo):
def __init__(self, backend_domain, ident, libvirt_name=None):
if libvirt_name:
dev_match = self._libvirt_regex.match(libvirt_name)
assert dev_match
if not dev_match:
raise UnsupportedDevice(libvirt_name)
ident = '{bus}_{device}.{function}'.format(**dev_match.groupdict())

super().__init__(backend_domain, ident, None)
Expand Down Expand Up @@ -170,6 +179,7 @@ def frontend_domain(self):
return all_attached.get(self.ident, None)



class PCIDeviceExtension(qubes.ext.Extension):
def __init__(self):
super().__init__()
Expand All @@ -189,7 +199,12 @@ def on_device_list_pci(self, vm, event):

xml_desc = lxml.etree.fromstring(dev.XMLDesc())
libvirt_name = xml_desc.findtext('name')
yield PCIDevice(vm, None, libvirt_name=libvirt_name)
try:
yield PCIDevice(vm, None, libvirt_name=libvirt_name)
except UnsupportedDevice:
if libvirt_name not in unsupported_devices_warned:
vm.log.warning("Unsupported device: %s", libvirt_name)
unsupported_devices_warned.add(libvirt_name)

@qubes.ext.handler('device-get:pci')
def on_device_get_pci(self, vm, event, ident):
Expand Down
1 change: 1 addition & 0 deletions qubes/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,7 @@ def load_tests(loader, tests, pattern): # pylint: disable=unused-argument
"qubes.tests.events",
"qubes.tests.devices",
"qubes.tests.devices_block",
"qubes.tests.devices_pci",
"qubes.tests.firewall",
"qubes.tests.init",
"qubes.tests.vm.init",
Expand Down
94 changes: 94 additions & 0 deletions qubes/tests/devices_pci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- encoding: utf8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2023 Marek Marczykowski-Górecki
# <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <https://www.gnu.org/licenses/>.

from unittest import mock

import jinja2

import qubes.tests
import qubes.ext.pci


class TestVM(object):
def __init__(self, running=True, name='dom0', qid=0):
self.name = name
self.qid = qid
self.is_running = lambda: running
self.log = mock.Mock()
self.app = mock.Mock()

def __eq__(self, other):
if isinstance(other, TestVM):
return self.name == other.name

class TC_00_Block(qubes.tests.QubesTestCase):
def setUp(self):
super().setUp()
self.ext = qubes.ext.pci.PCIDeviceExtension()

def test_000_unsupported_device(self):
vm = TestVM()
vm.app.configure_mock(**{
'vmm.libvirt_conn.listAllDevices.return_value':
[mock.Mock(**{"XMLDesc.return_value": """<device>
<name>pci_0000_00_14_0</name>
<path>/sys/devices/pci0000:00/0000:00:14.0</path>
<parent>computer</parent>
<driver>
<name>pciback</name>
</driver>
<capability type='pci'>
<class>0x0c0330</class>
<domain>0</domain>
<bus>0</bus>
<slot>20</slot>
<function>0</function>
<product id='0x8cb1'>9 Series Chipset Family USB xHCI Controller</product>
<vendor id='0x8086'>Intel Corporation</vendor>
</capability>
</device>
""",
"listCaps.return_value": ["pci"]
}),
mock.Mock(**{"XMLDesc.return_value": """<device>
<name>pci_1000_00_14_0</name>
<path>/sys/devices/pci1000:00/1000:00:14.0</path>
<parent>computer</parent>
<driver>
<name>pciback</name>
</driver>
<capability type='pci'>
<class>0x0c0330</class>
<domain>0</domain>
<bus>0</bus>
<slot>20</slot>
<function>0</function>
<product id='0x8cb1'>9 Series Chipset Family USB xHCI Controller</product>
<vendor id='0x8086'>Intel Corporation</vendor>
</capability>
</device>
""",
"listCaps.return_value": ["pci"]
}),
]
})
devices = list(self.ext.on_device_list_pci(vm, 'device-list:pci'))
self.assertEqual(len(devices), 1)
self.assertEqual(devices[0].ident, "00_14.0")
1 change: 1 addition & 0 deletions rpm_spec/core-dom0.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ done
%{python3_sitelib}/qubes/tests/app.py
%{python3_sitelib}/qubes/tests/devices.py
%{python3_sitelib}/qubes/tests/devices_block.py
%{python3_sitelib}/qubes/tests/devices_pci.py
%{python3_sitelib}/qubes/tests/events.py
%{python3_sitelib}/qubes/tests/ext.py
%{python3_sitelib}/qubes/tests/firewall.py
Expand Down