Path: blob/master/tools/testing/selftests/hid/tests/test_usb_crash.py
26308 views
#!/bin/env python31# SPDX-License-Identifier: GPL-2.02# -*- coding: utf-8 -*-3#4# Copyright (c) 2021 Benjamin Tissoires <[email protected]>5# Copyright (c) 2021 Red Hat, Inc.6#78# This is to ensure we don't crash when emulating USB devices910from . import base11import pytest12import logging1314logger = logging.getLogger("hidtools.test.usb")151617class USBDev(base.UHIDTestDevice):18# fmt: off19report_descriptor = [200x05, 0x01, # .Usage Page (Generic Desktop) 0210x09, 0x02, # .Usage (Mouse) 2220xa1, 0x01, # .Collection (Application) 4230x09, 0x02, # ..Usage (Mouse) 6240xa1, 0x02, # ..Collection (Logical) 8250x09, 0x01, # ...Usage (Pointer) 10260xa1, 0x00, # ...Collection (Physical) 12270x05, 0x09, # ....Usage Page (Button) 14280x19, 0x01, # ....Usage Minimum (1) 16290x29, 0x03, # ....Usage Maximum (3) 18300x15, 0x00, # ....Logical Minimum (0) 20310x25, 0x01, # ....Logical Maximum (1) 22320x75, 0x01, # ....Report Size (1) 24330x95, 0x03, # ....Report Count (3) 26340x81, 0x02, # ....Input (Data,Var,Abs) 28350x75, 0x05, # ....Report Size (5) 30360x95, 0x01, # ....Report Count (1) 32370x81, 0x03, # ....Input (Cnst,Var,Abs) 34380x05, 0x01, # ....Usage Page (Generic Desktop) 36390x09, 0x30, # ....Usage (X) 38400x09, 0x31, # ....Usage (Y) 40410x15, 0x81, # ....Logical Minimum (-127) 42420x25, 0x7f, # ....Logical Maximum (127) 44430x75, 0x08, # ....Report Size (8) 46440x95, 0x02, # ....Report Count (2) 48450x81, 0x06, # ....Input (Data,Var,Rel) 50460xc0, # ...End Collection 52470xc0, # ..End Collection 53480xc0, # .End Collection 5449]50# fmt: on5152def __init__(self, name=None, input_info=None):53super().__init__(54name, "Mouse", input_info=input_info, rdesc=USBDev.report_descriptor55)5657# skip witing for udev events, it's likely that the report58# descriptor is wrong59def is_ready(self):60return True6162# we don't have an evdev node here, so paper over63# the checks64def get_evdev(self, application=None):65return "OK"666768class TestUSBDevice(base.BaseTestCase.TestUhid):69"""70Test class to test if an emulated USB device crashes71the kernel.72"""7374# conftest.py is generating the following fixture:75#76# @pytest.fixture(params=[('modulename', 1, 2)])77# def usbVidPid(self, request):78# return request.param7980@pytest.fixture()81def new_uhdev(self, usbVidPid, request):82self.module, self.vid, self.pid = usbVidPid83self._load_kernel_module(None, self.module)84return USBDev(input_info=(3, self.vid, self.pid))8586def test_creation(self):87"""88inject the USB dev through uhid and immediately see if there is a crash:8990uhid can create a USB device with the BUS_USB bus, and some91drivers assume that they can then access USB related structures92when they are actually provided a uhid device. This leads to93a crash because those access result in a segmentation fault.9495The kernel should not crash on any (random) user space correct96use of its API. So run through all available modules and declared97devices to see if we can generate a uhid device without a crash.9899The test is empty as the fixture `check_taint` is doing the job (and100honestly, when the kernel crashes, the whole machine freezes).101"""102assert True103104105