104 lines · python
1#!/bin/env python32# SPDX-License-Identifier: GPL-2.03# -*- coding: utf-8 -*-4#5# Copyright (c) 2021 Benjamin Tissoires <benjamin.tissoires@gmail.com>6# Copyright (c) 2021 Red Hat, Inc.7#8 9# This is to ensure we don't crash when emulating USB devices10 11from . import base12import pytest13import logging14 15logger = logging.getLogger("hidtools.test.usb")16 17 18class USBDev(base.UHIDTestDevice):19 # fmt: off20 report_descriptor = [21 0x05, 0x01, # .Usage Page (Generic Desktop) 022 0x09, 0x02, # .Usage (Mouse) 223 0xa1, 0x01, # .Collection (Application) 424 0x09, 0x02, # ..Usage (Mouse) 625 0xa1, 0x02, # ..Collection (Logical) 826 0x09, 0x01, # ...Usage (Pointer) 1027 0xa1, 0x00, # ...Collection (Physical) 1228 0x05, 0x09, # ....Usage Page (Button) 1429 0x19, 0x01, # ....Usage Minimum (1) 1630 0x29, 0x03, # ....Usage Maximum (3) 1831 0x15, 0x00, # ....Logical Minimum (0) 2032 0x25, 0x01, # ....Logical Maximum (1) 2233 0x75, 0x01, # ....Report Size (1) 2434 0x95, 0x03, # ....Report Count (3) 2635 0x81, 0x02, # ....Input (Data,Var,Abs) 2836 0x75, 0x05, # ....Report Size (5) 3037 0x95, 0x01, # ....Report Count (1) 3238 0x81, 0x03, # ....Input (Cnst,Var,Abs) 3439 0x05, 0x01, # ....Usage Page (Generic Desktop) 3640 0x09, 0x30, # ....Usage (X) 3841 0x09, 0x31, # ....Usage (Y) 4042 0x15, 0x81, # ....Logical Minimum (-127) 4243 0x25, 0x7f, # ....Logical Maximum (127) 4444 0x75, 0x08, # ....Report Size (8) 4645 0x95, 0x02, # ....Report Count (2) 4846 0x81, 0x06, # ....Input (Data,Var,Rel) 5047 0xc0, # ...End Collection 5248 0xc0, # ..End Collection 5349 0xc0, # .End Collection 5450 ]51 # fmt: on52 53 def __init__(self, name=None, input_info=None):54 super().__init__(55 name, "Mouse", input_info=input_info, rdesc=USBDev.report_descriptor56 )57 58 # skip witing for udev events, it's likely that the report59 # descriptor is wrong60 def is_ready(self):61 return True62 63 # we don't have an evdev node here, so paper over64 # the checks65 def get_evdev(self, application=None):66 return "OK"67 68 69class TestUSBDevice(base.BaseTestCase.TestUhid):70 """71 Test class to test if an emulated USB device crashes72 the kernel.73 """74 75 # conftest.py is generating the following fixture:76 #77 # @pytest.fixture(params=[('modulename', 1, 2)])78 # def usbVidPid(self, request):79 # return request.param80 81 @pytest.fixture()82 def new_uhdev(self, usbVidPid, request):83 self.module, self.vid, self.pid = usbVidPid84 self._load_kernel_module(None, self.module)85 return USBDev(input_info=(3, self.vid, self.pid))86 87 def test_creation(self):88 """89 inject the USB dev through uhid and immediately see if there is a crash:90 91 uhid can create a USB device with the BUS_USB bus, and some92 drivers assume that they can then access USB related structures93 when they are actually provided a uhid device. This leads to94 a crash because those access result in a segmentation fault.95 96 The kernel should not crash on any (random) user space correct97 use of its API. So run through all available modules and declared98 devices to see if we can generate a uhid device without a crash.99 100 The test is empty as the fixture `check_taint` is doing the job (and101 honestly, when the kernel crashes, the whole machine freezes).102 """103 assert True104