test_usb_crash.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. # -*- coding: utf-8 -*-
  4. #
  5. # Copyright (c) 2021 Benjamin Tissoires <benjamin.tissoires@gmail.com>
  6. # Copyright (c) 2021 Red Hat, Inc.
  7. #
  8. # This is to ensure we don't crash when emulating USB devices
  9. from . import base
  10. import pytest
  11. import logging
  12. logger = logging.getLogger("hidtools.test.usb")
  13. class USBDev(base.UHIDTestDevice):
  14. # fmt: off
  15. report_descriptor = [
  16. 0x05, 0x01, # .Usage Page (Generic Desktop) 0
  17. 0x09, 0x02, # .Usage (Mouse) 2
  18. 0xa1, 0x01, # .Collection (Application) 4
  19. 0x09, 0x02, # ..Usage (Mouse) 6
  20. 0xa1, 0x02, # ..Collection (Logical) 8
  21. 0x09, 0x01, # ...Usage (Pointer) 10
  22. 0xa1, 0x00, # ...Collection (Physical) 12
  23. 0x05, 0x09, # ....Usage Page (Button) 14
  24. 0x19, 0x01, # ....Usage Minimum (1) 16
  25. 0x29, 0x03, # ....Usage Maximum (3) 18
  26. 0x15, 0x00, # ....Logical Minimum (0) 20
  27. 0x25, 0x01, # ....Logical Maximum (1) 22
  28. 0x75, 0x01, # ....Report Size (1) 24
  29. 0x95, 0x03, # ....Report Count (3) 26
  30. 0x81, 0x02, # ....Input (Data,Var,Abs) 28
  31. 0x75, 0x05, # ....Report Size (5) 30
  32. 0x95, 0x01, # ....Report Count (1) 32
  33. 0x81, 0x03, # ....Input (Cnst,Var,Abs) 34
  34. 0x05, 0x01, # ....Usage Page (Generic Desktop) 36
  35. 0x09, 0x30, # ....Usage (X) 38
  36. 0x09, 0x31, # ....Usage (Y) 40
  37. 0x15, 0x81, # ....Logical Minimum (-127) 42
  38. 0x25, 0x7f, # ....Logical Maximum (127) 44
  39. 0x75, 0x08, # ....Report Size (8) 46
  40. 0x95, 0x02, # ....Report Count (2) 48
  41. 0x81, 0x06, # ....Input (Data,Var,Rel) 50
  42. 0xc0, # ...End Collection 52
  43. 0xc0, # ..End Collection 53
  44. 0xc0, # .End Collection 54
  45. ]
  46. # fmt: on
  47. def __init__(self, name=None, input_info=None):
  48. super().__init__(
  49. name, "Mouse", input_info=input_info, rdesc=USBDev.report_descriptor
  50. )
  51. # skip witing for udev events, it's likely that the report
  52. # descriptor is wrong
  53. def is_ready(self):
  54. return True
  55. # we don't have an evdev node here, so paper over
  56. # the checks
  57. def get_evdev(self, application=None):
  58. return "OK"
  59. class TestUSBDevice(base.BaseTestCase.TestUhid):
  60. """
  61. Test class to test if an emulated USB device crashes
  62. the kernel.
  63. """
  64. # conftest.py is generating the following fixture:
  65. #
  66. # @pytest.fixture(params=[('modulename', 1, 2)])
  67. # def usbVidPid(self, request):
  68. # return request.param
  69. @pytest.fixture()
  70. def new_uhdev(self, usbVidPid, request):
  71. self.module, self.vid, self.pid = usbVidPid
  72. self._load_kernel_module(None, self.module)
  73. return USBDev(input_info=(3, self.vid, self.pid))
  74. def test_creation(self):
  75. """
  76. inject the USB dev through uhid and immediately see if there is a crash:
  77. uhid can create a USB device with the BUS_USB bus, and some
  78. drivers assume that they can then access USB related structures
  79. when they are actually provided a uhid device. This leads to
  80. a crash because those access result in a segmentation fault.
  81. The kernel should not crash on any (random) user space correct
  82. use of its API. So run through all available modules and declared
  83. devices to see if we can generate a uhid device without a crash.
  84. The test is empty as the fixture `check_taint` is doing the job (and
  85. honestly, when the kernel crashes, the whole machine freezes).
  86. """
  87. assert True