Input: xbox_gip - Add new driver for Xbox GIP
Subsystems (3)
v4 cover Input: xbox_gip - Add new driver for Xbox GIP
This is a new version of the previously submitted xbox-gip series.
This introduces a new driver for the Xbox One/Series controller protocol, officially known as the Gaming Input Protocol, or GIP for short.
Microsoft released documentation on (some of) GIP in late 2024, upon which this driver is based. Though the documentation was incomplete, it still provided enough information to warrant a clean start over the previous, incomplete implementation.
This driver is already at feature parity with the GIP support in xpad, along with several more enhancements:
- Proper support for parsing message length and fragmented messages
- Metadata parsing, allowing for auto-detection on various parameters, including the presence and location in the message of the share button, as well as detection of specific device types
- Controllable LED support
- HID passthrough for the Chatpad
- Battery information support
- Arcade stick support
- Racing wheel support
- Support for the security flow, allowing use with wireless controllers that include their own dongles. (This does not include support for the first-party dongle yet.)
The framework set out in this driver also allows future expansion for specialized device types and additional features more cleanly than xpad.
Future plans include:
- Adding support for more device types, such as flight sticks.
- Exposing a raw character device to enable sending vendor-specific commands from userspace.
- Event logging to either sysfs or dmesg.
- Support for the headphone jack.
Since the last version this has included several minor bugfixes as adding support for sending ACME messages and porting the security flow from xone to allow for use with wireless controllers with their own dongles. It also adds support for PDP guitar controllers like the RiffMaster.
Vicki Pfau (12):
Input: xbox_gip - Add new driver for Xbox GIP
Input: xpad - Remove Xbox One support
Input: xbox_gip - Add controllable LED support
Input: xbox_gip - Add HID relaying
Input: xbox_gip - Add battery support
Input: xbox_gip - Allow sending fragmented and ACME messages
Input: xbox_gip - Add security implementation from xone
Input: xbox_gip - Add arcade stick support
Input: xbox_gip - Add support for PDP guitar controllers
Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER
HID: Map more automobile simulation inputs
Input: xbox_gip - Add wheel support
Documentation/input/devices/xpad.rst | 17 +- MAINTAINERS | 6 + drivers/hid/hid-debug.c | 16 +- drivers/hid/hid-input.c | 2 + drivers/input/joystick/Kconfig | 2 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/gip/Kconfig | 21 + drivers/input/joystick/gip/Makefile | 8 + drivers/input/joystick/gip/gip-arcade-stick.c | 173 + drivers/input/joystick/gip/gip-core.c | 3173 +++++++++++++++++ drivers/input/joystick/gip/gip-drivers.c | 222 ++ drivers/input/joystick/gip/gip-pdp-jaguar.c | 129 + drivers/input/joystick/gip/gip-security.c | 867 +++++ drivers/input/joystick/gip/gip-wheel.c | 349 ++ drivers/input/joystick/gip/gip.h | 423 +++ drivers/input/joystick/xpad.c | 636 +--- include/uapi/linux/input-event-codes.h | 3 + 17 files changed, 5399 insertions(+), 649 deletions(-) create mode 100644 drivers/input/joystick/gip/Kconfig create mode 100644 drivers/input/joystick/gip/Makefile create mode 100644 drivers/input/joystick/gip/gip-arcade-stick.c create mode 100644 drivers/input/joystick/gip/gip-core.c create mode 100644 drivers/input/joystick/gip/gip-drivers.c create mode 100644 drivers/input/joystick/gip/gip-pdp-jaguar.c create mode 100644 drivers/input/joystick/gip/gip-security.c create mode 100644 drivers/input/joystick/gip/gip-wheel.c create mode 100644 drivers/input/joystick/gip/gip.h
Signature · 1 line
2.54.0
PATCH v4 1/12 Input: xbox_gip - Add new driver for Xbox GIP
This introduces a new driver for the Xbox One/Series controller protocol, officially known as the Gaming Input Protocol, or GIP for short.
Microsoft released documentation on (some of) GIP in late 2024, upon which this driver is based. Though the documentation was incomplete, it still provided enough information to warrant a clean start over the previous, incomplete implementation.
This driver is already at feature parity with the GIP support in xpad, along with several more enhancements:
- Proper support for parsing message length and fragmented messages
- Metadata parsing, allowing for auto-detection on various parameters, including the presence and location in the message of the share button, as well as detection of specific device types
The framework set out in this driver also allows future expansion for specialized device types and additional features more cleanly than xpad.
Future plans include:
- Adding support for more device types, such as arcade sticks, racing wheels and flight sticks.
- Support for the security handshake, which is required for devices that use wireless dongles.
- Exposing a raw character device to enable sending vendor-specific commands from userspace.
- Event logging to either sysfs or dmesg.
- Support for the headphone jack.
--- MAINTAINERS | 6 + drivers/input/joystick/Kconfig | 2 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/gip/Kconfig | 21 + drivers/input/joystick/gip/Makefile | 4 + drivers/input/joystick/gip/gip-core.c | 2569 ++++++++++++++++++++++ drivers/input/joystick/gip/gip-drivers.c | 204 ++ drivers/input/joystick/gip/gip.h | 344 +++ 8 files changed, 3151 insertions(+) create mode 100644 drivers/input/joystick/gip/Kconfig create mode 100644 drivers/input/joystick/gip/Makefile create mode 100644 drivers/input/joystick/gip/gip-core.c create mode 100644 drivers/input/joystick/gip/gip-drivers.c create mode 100644 drivers/input/joystick/gip/gip.h
MAINTAINERS +6 -0
@@ -29347,6 +29347,12 @@ S: Maintained F: drivers/media/rc/keymaps/rc-xbox-dvd.c F: drivers/media/rc/xbox_remote.c +XBOX GIP +M: Vicki Pfau <[email protected]> +L: [email protected] +S: Maintained +F: drivers/input/joystick/gip/ + XC2028/3028 TUNER DRIVER M: Mauro Carvalho Chehab <[email protected]> L: [email protected]
drivers/input/joystick/Kconfig +2 -0
@@ -291,6 +291,8 @@ config JOYSTICK_JOYDUMP To compile this driver as a module, choose M here: the module will be called joydump. +source "drivers/input/joystick/gip/Kconfig" + config JOYSTICK_XPAD tristate "Xbox gamepad support" depends on USB_ARCH_HAS_HCD
drivers/input/joystick/Makefile +1 -0
@@ -39,5 +39,6 @@ obj-$(CONFIG_JOYSTICK_TURBOGRAFX) += turbografx.o obj-$(CONFIG_JOYSTICK_TWIDJOY) += twidjoy.o obj-$(CONFIG_JOYSTICK_WARRIOR) += warrior.o obj-$(CONFIG_JOYSTICK_WALKERA0701) += walkera0701.o +obj-$(CONFIG_JOYSTICK_XBOX_GIP) += gip/ obj-$(CONFIG_JOYSTICK_XPAD) += xpad.o obj-$(CONFIG_JOYSTICK_ZHENHUA) += zhenhua.o
drivers/input/joystick/gip/Kconfig new file +21 -0
@@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Gaming Input Protocol driver configuration +# +config JOYSTICK_XBOX_GIP + tristate "Xbox One/Series controller support" + depends on USB && USB_ARCH_HAS_HCD + help + Say Y here if you want to use Xbox One and Series controllers with your + computer. Make sure to say Y to "Joystick support" (CONFIG_INPUT_JOYDEV) + and/or "Event interface support" (CONFIG_INPUT_EVDEV) as well. + + To compile this driver as a module, choose M here: the + module will be called xbox_gip. + +config JOYSTICK_XBOX_GIP_FF + bool "Xbox One/Series controller rumble support" + depends on JOYSTICK_XBOX_GIP && INPUT + select INPUT_FF_MEMLESS + help + Say Y here if you want to take advantage of Xbox One/Series rumble.
drivers/input/joystick/gip/Makefile new file +4 -0
@@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +obj-$(CONFIG_JOYSTICK_XBOX_GIP) += xbox-gip.o +# Base driver +xbox-gip-y := gip-core.o gip-drivers.o
drivers/input/joystick/gip/gip-core.c new file +2569 -0
2,569 lines not on this page · raw message
drivers/input/joystick/gip/gip-drivers.c new file +204 -0
204 lines not on this page · raw message
drivers/input/joystick/gip/gip.h new file +344 -0
176 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.54.0
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 17 potential issue(s) to consider: - [High] Deadlock caused by calling `cancel_delayed_work_sync()` while holding a mutex required by the…
Thank you for your contribution! Sashiko AI review found 17 potential issue(s) to consider:
- [High] Deadlock caused by calling cancel_delayed_work_sync() while holding a mutex required by the work function.
- [High] Lifecycle mismatch causing devres memory leak due to allocating against the parent USB device instead of the USB interface.
- [High] Active OUT URBs are freed without being killed, causing use-after-free and DMA corruption.
- [High] Lock inversion during devres cleanup triggers an AB-BA deadlock between attachment->lock and cancel_work_sync.
- [High] Buffer over-read in gip_receive_fragment for non-initial fragments due to missing bounds check.
- [High] Probe error path leaves an active IN URB running, leading to a Use-After-Free.
- [High] Dangling pointer left in attachment->input upon input_register_device failure.
- [High] Blind dereference of potential ERR_PTR returned from gip_ensure_attachment.
- [High] Undefined behavior from left shifting by 64 or more bits in gip_decode_length.
- [High] NULL pointer dereference when in_fragment_data allocation fails.
- [High] Missing NULL check after devm_kmalloc in gip_send_raw_message.
- [High] Use-After-Free due to LED/Battery callbacks operating on explicitly freed attachment structure.
- [High] Sleeping in atomic context by calling usb_free_coherent while holding a spinlock.
- [High] Memory leak of URBs and DMA buffers due to return combined with dead goto error handling.
- [High] Heap buffer overflow in gip_receive_fragment due to bypassed bounds check on exact offset match.
- [Medium] Bitwise OR logic typo in gip_parse_preferred_types_metadata ignores the high byte of the string length.
- [Medium] Memory leak of attachment->uniq caused by repeated HELLO_DEVICE messages.
--
--- Patch [1]: [PATCH v4 01/12] Input: xbox_gip - Add new driver for Xbox GIP --- commit 8f3bdd937c628e28382af94b64282af26f6cbd22
Author: Vicki Pfau <[email protected]>
Subject: Input: xbox_gip - Add new driver for Xbox GIP
This commit introduces a new Gaming Input Protocol (GIP) driver for Xbox One and Series controllers. It supports parsing message lengths, metadata, and handles basic gamepad input functionality along with controller quirks.
drivers/input/joystick/gip/gip-core.c line 9
Quoted 13 lines
drivers/input/joystick/gip/gip-core.c new file +9
-0@@ -0,0 +1,2569 @@ +static int gip_decode_length (uint64_t *length,const uint8_t *bytes,int num_bytes) +{ + *length =0 ; +int offset; + +for (offset =0 ; offset < num_bytes; offset++) { +uint8_t byte = bytes[offset]; + + *length |= (byte &0x7full ) << (offset *7 );
[Severity: High]
Does this loop need a bounds check to prevent shifting by 64 or more bits?
If a device sends 10 or more consecutive bytes with the 0x80 bit set, offset will reach 10, causing the shift to become 70. This results in undefined behavior in C.
[ ... ]
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c new file +4
-0+static int gip_send_raw_message (struct gip_attachment *attachment, +uint8_t message_type,uint8_t flags,uint8_t seq,const uint8_t *bytes, +int num_bytes) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c new file +5
-0+ fragment->total_length = num_bytes; + fragment->data =devm_kmalloc (to_gip_device (attachment), + fragment->total_length,GFP_ATOMIC ); + fragment->fragment_offset =0 ; +memcpy (fragment->data, bytes, num_bytes);
[Severity: High]
Is there a missing NULL check for the devm_kmalloc() return value?
If the atomic memory allocation fails under memory pressure, the subsequent memcpy() will result in a NULL pointer dereference.
[ ... ]
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c new file +4
-0+static int gip_parse_preferred_types_metadata (struct device *dev, +struct gip_device_metadata *dev_metadata,const uint8_t *bytes, +int length,int buffer_offset) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 648
Quoted 7 lines
drivers/input/joystick/gip/gip-core.c new file +7
-0+ buffer_offset++; +for (i =0 ; i < dev_metadata->num_preferred_types; i++) { +if (buffer_offset +2 >= length) +return -EINVAL ; + + count = bytes[buffer_offset]; + count |= bytes[buffer_offset];
[Severity: Medium]
Is there a typo in the bitwise OR assignment for the count?
By OR-ing the exact same byte against itself, the high byte of the 16-bit length is ignored (it looks like it should read buffer_offset + 1). This could cause strings larger than 255 bytes to parse incorrectly as 0, desynchronizing the buffer offset.
[ ... ]
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c new file +3
-0+static bool gip_handle_command_hello_device (struct gip_attachment *attachment, +const struct gip_header *header,const uint8_t *bytes,int num_bytes) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 1400
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c new file +6
-0+ attachment->firmware_major_version =le16_to_cpu (message->firmware_major_version); + attachment->firmware_minor_version =le16_to_cpu (message->firmware_minor_version); + attachment->vendor_id =le16_to_cpu (message->vendor_id); + attachment->product_id =le16_to_cpu (message->product_id); + attachment->uniq =devm_kasprintf (to_gip_device (attachment), +GFP_KERNEL ,"%llx" ,le64_to_cpu (message->device_id));
[Severity: Medium]
Could this cause a memory leak if the device repeatedly sends HELLO_DEVICE messages?
The attachment->uniq pointer is assigned a newly allocated string each time this command is handled, but any previously allocated string is not freed prior to being overwritten.
[ ... ]
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c new file +2
-0+int gip_setup_input_device (struct gip_attachment *attachment) +{
[ ... ]
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c new file +6
-0+rcu_assign_pointer (attachment->input, input); + rc =input_register_device (input); +if (rc) + goto err_free_device; + + rc =gip_guide_led_probe (attachment, &input->dev);
[ ... ]
drivers/input/joystick/gip/gip-core.c line 1158
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c new file +4
-0+err_free_device: +input_free_device (input); +return rc; +}
[Severity: High]
Should attachment->input be cleared to NULL in the error path?
If input_register_device() fails, input_free_device() is called, but attachment->input remains set. If an interrupt arrives afterwards, gip_handle_ll_input_report() could dereference the freed dangling pointer.
[ ... ]
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c new file +6
-0+static void gip_reset_metadata (struct gip_attachment *attachment) +{ +struct input_dev *input; + +if (attachment->got_metadata !=GIP_METADATA_GOT ) +return ;
drivers/input/joystick/gip/gip-core.c line 1282
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c new file +10
-0+ +gip_metadata_free (to_gip_device (attachment), &attachment->metadata); +rcu_read_lock (); + input =rcu_dereference (attachment->input); +rcu_read_unlock (); +if (input) { +rcu_assign_pointer (attachment->input,NULL ); +synchronize_rcu (); +input_unregister_device (input); + }
[Severity: High]
Could this lead to an AB-BA deadlock between attachment->lock and the power supply's changed_work cancelation?
When a new message fragment is processed, gip_receive_message() holds attachment->lock. It might call into gip_reset_metadata(), which triggers input_unregister_device(). This synchronously releases devres resources, unregistering the battery power supply and flushing its work queue via cancel_work_sync().
If the power supply worker thread is simultaneously executing gip_battery_get_property(), it will attempt to acquire attachment->lock, creating a deadlock.
[ ... ]
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c new file +5
-0+static int gip_receive_fragment (struct gip_attachment *attachment, +const struct gip_header *header,int offset, +uint64_t *fragment_offset,uint16_t *bytes_remaining,uint8_t *bytes, +int num_bytes) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 1981
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c new file +4
-0+ attachment->in_fragment_data =devm_kmalloc (to_gip_device (attachment), + attachment->in_total_length,GFP_KERNEL ); +if (!attachment->in_fragment_data) +return -ENOMEM ;
[Severity: High]
Does this error path leave the attachment in an inconsistent state?
If the allocation fails, the function returns -ENOMEM but leaves attachment->in_fragment_message set to a valid message type. A subsequent non-initial fragment matching the type would pass validation and call memcpy() into the NULL in_fragment_data pointer.
[ ... ]
Quoted 1 line
drivers/input/joystick/gip/gip-core.c new file +1
-0+if (*fragment_offset != attachment->in_fragment_offset) {
[ ... ]
Quoted 15 lines
drivers/input/joystick/gip/gip-core.c new file +15
-0+if (header->length && *fragment_offset != attachment->in_total_length) { +gip_warn_ratelimited (attachment, +"Received out of sequence fragment: claimed %llu, expected %d\n" , + *fragment_offset, attachment->in_fragment_offset); +gip_acknowledge (attachment, header, + attachment->in_fragment_offset, + attachment->in_total_length - attachment->in_fragment_offset); +return -EPROTO ; + } +gip_dbg_ratelimited (attachment, +"Received erroneous final fragment offset, proceeding anyway\n" ); + *bytes_remaining =0 ; + }else if (*fragment_offset + header->length > attachment->in_total_length) { + gip_warn(attachment, + "Received too long fragment, %llu exceeds %d\n",
drivers/input/joystick/gip/gip-core.c line 2038
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c new file +10
-0+ *fragment_offset + header->length, attachment->in_total_length); +gip_fragment_failed (attachment, header); +return -EINVAL ; + }else { + *bytes_remaining = attachment->in_total_length - + (*fragment_offset + header->length); + } +if (header->length !=0 ) +memcpy (&attachment->in_fragment_data[*fragment_offset], + &bytes[offset], header->length);
[Severity: High]
Can this logic result in a heap buffer overflow?
If a device provides *fragment_offset == attachment->in_total_length and header->length > 0, the first if block is evaluated. Since it exits the block without returning, it skips the else if bounds check entirely and proceeds to memcpy(), writing header->length bytes past the end of the heap allocation.
[Severity: High]
Is there a missing bounds check for non-initial fragments?
For initial fragments (GIP_FLAG_INIT_FRAG), the driver checks if header->length > num_bytes - offset. However, for non-initial fragments, this validation appears to be missing before calling memcpy(). This allows the device to specify a header->length larger than the actual received packet, reading adjacent kernel heap memory into in_fragment_data.
[ ... ]
drivers/input/joystick/gip/gip-core.c line 2057
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c new file +5
-0+cancel_delayed_work_sync (&attachment->in_fragment_timeout); +schedule_delayed_work (&attachment->in_fragment_timeout,HZ ); + +return rc; +}
[Severity: High]
Can this lead to an AB-BA deadlock with the work function?
gip_receive_fragment() is called from gip_receive_message() while holding attachment->lock. It then calls cancel_delayed_work_sync(). If the delayed work gip_fragment_timeout() is currently executing, it will attempt to acquire attachment->lock, deadlocking the system.
[ ... ]
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c new file +3
-0+static int gip_receive_message (struct gip_device *device,uint8_t *bytes, +int num_bytes) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 2086
Quoted 8 lines
drivers/input/joystick/gip/gip-core.c new file +8
-0+ is_fragment = header.flags &GIP_FLAG_FRAGMENT ; + attachment_index = header.flags &GIP_FLAG_ATTACHMENT_MASK ; + attachment =gip_ensure_attachment (device, attachment_index); + +print_hex_dump_debug (KBUILD_MODNAME ": Received message: " , +DUMP_PREFIX_OFFSET ,16 ,1 , bytes, num_bytes, false); + +guard (mutex)(&attachment->lock);
[Severity: High]
Does gip_ensure_attachment() need to be checked for an ERR_PTR return?
If memory allocation within gip_ensure_attachment() fails, it returns ERR_PTR(-ENOMEM). Blindly calling guard(mutex)(&attachment->lock) with an ERR_PTR will trigger a kernel panic.
[ ... ]
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c new file +3
-0+static int gip_init_output (struct gip_interface *intf, +struct usb_endpoint_descriptor *ep_out) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 2279
Quoted 7 lines
drivers/input/joystick/gip/gip-core.c new file +7
-0+ intf->out_queue[i].data =usb_alloc_coherent (udev, intf->mtu,GFP_KERNEL , + &intf->out_queue[i].urb->transfer_dma); + +if (!intf->out_queue[i].data) { +return -ENOMEM ; + goto err_free_urbs; + }
[Severity: High]
Does the early return cause a memory leak on the error path?
The code executes return -ENOMEM; immediately, bypassing the goto statement. This bypasses the err_free_urbs cleanup logic, leaking all URBs and buffers allocated in previous loop iterations.
[ ... ]
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c new file +2
-0+static int gip_probe (struct usb_interface *intf,const struct usb_device_id *id) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 2383
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c new file +6
-0+ gip =devm_kzalloc (&udev->dev,sizeof (*gip),GFP_KERNEL ); +if (!gip) +return -ENOMEM ; + + gip->udev = udev; + gip->data.device = gip;
[Severity: High]
Are devres allocations incorrectly tied to the parent USB device?
The structure is allocated using devm_kzalloc(&udev->dev, ...). Since a usb_driver is bound to the usb_interface, its devres allocations must be tied to &intf->dev. Because they are tied to the parent device, unbinding the driver via sysfs does not trigger devres cleanup, permanently leaking memory until the physical device is unplugged.
[ ... ]
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c new file +6
-0+ rc =gip_interface_init (&gip->data); +if (rc) { +devm_kfree (to_gip_device (gip), gip); +return rc; + } +/* Don't init audio interface -- we aren't using it yet */
drivers/input/joystick/gip/gip-core.c line 2408
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c new file +10
-0+ +usb_set_intfdata (intf, gip); + +/* Pre-create the first attachment, as it should always exist */ + attachment =gip_ensure_attachment (gip,0 ); +if (IS_ERR (attachment)) +return PTR_ERR (attachment); + +return 0 ; +}
[Severity: High]
Are we leaking the running URB if gip_ensure_attachment() fails?
The call to gip_interface_init() submits urb_in. If ensuring the first attachment subsequently fails, the function returns without killing the active URB. When the hardware interrupt fires, the USB core will attempt to access the freed gip structure.
[ ... ]
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c new file +2
-0+static int gip_shutdown (struct gip_device *device) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 2425
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c new file +10
-0+for (i =0 ; i <MAX_ATTACHMENTS ; i++) { +struct gip_attachment *attachment = device->attachments[i]; +struct input_dev *input; + +if (!attachment) +continue ; + + scoped_guard (mutex, &attachment->lock) { +cancel_delayed_work_sync (&attachment->metadata_next); +cancel_delayed_work_sync (&attachment->in_fragment_timeout);
[Severity: High]
Does this scoped_guard introduce a deadlock?
The scoped_guard acquires attachment->lock, and then calls cancel_delayed_work_sync(). If the work functions (gip_retry_metadata or gip_fragment_timeout) are running concurrently, they attempt to acquire this same lock, causing an AB-BA deadlock during driver shutdown.
[ ... ]
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c new file +2
-0+static void gip_disconnect (struct usb_interface *intf) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c line 2459
Quoted 9 lines
drivers/input/joystick/gip/gip-core.c new file +9
-0+usb_kill_urb (gip->data.urb_in); +if (gip->audio.intf) +usb_kill_urb (gip->audio.urb_in); + +gip_shutdown (gip); + +spin_lock_irqsave (&gip->message_lock, flags); +gip_deinit_input (&gip->data); +gip_deinit_output (&gip->data);
[Severity: High]
Should the active OUT URBs be killed before de-initializing output?
Only the IN URBs are killed via usb_kill_urb() prior to cleanup. gip_deinit_output() frees the URBs and their DMA buffers via usb_free_coherent(). If the host controller is actively transmitting these buffers, it will cause DMA corruption and crash the USB core.
[Severity: High]
Is it safe to call usb_free_coherent() while holding a spinlock?
gip_deinit_input() and gip_deinit_output() invoke usb_free_coherent() to free DMA buffers. This function can involve TLB flushes and IOMMU operations that may sleep. Calling it while holding gip->message_lock and with interrupts disabled violates atomic context rules.
[ ... ]
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c new file +4
-0+for (i =0 ; i <MAX_ATTACHMENTS ; i++) { +struct gip_attachment *attachment = gip->attachments[i]; + +if (!attachment)
drivers/input/joystick/gip/gip-core.c line 2481
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c new file +10
-0+continue ; + +gip_reset_attachment (attachment); + +devm_kfree (to_gip_device (attachment), attachment->uniq); +devm_kfree (to_gip_device (attachment), attachment); + } + +devm_kfree (to_gip_device (gip), gip); +}
[Severity: High]
Can explicit freeing of the attachment cause a Use-After-Free?
The attachment memory is explicitly freed here via devm_kfree(). However, the LED and Battery devices were registered with &input->dev as their devres parent. If userspace holds the input device open, devres cleanup is delayed. A subsequent sysfs modification to the LED state will invoke gip_guide_led_set(), which dereferences the freed attachment structure.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 2/12 Input: xpad - Remove Xbox One support
In reply to Vicki PfauIt has been superseded by xbox_gip. As the new driver is already at feature parity, removing the fairly rough Xbox One support from xpad is safe and will prevent any potential conflicts.
--- Documentation/input/devices/xpad.rst | 17 +- drivers/input/joystick/xpad.c | 636 +-------------------------- 2 files changed, 11 insertions(+), 642 deletions(-)
Documentation/input/devices/xpad.rst +7 -10
@@ -2,19 +2,19 @@ xpad - Linux USB driver for Xbox compatible controllers ======================================================= -This driver exposes all first-party and third-party Xbox compatible -controllers. It has a long history and has enjoyed considerable usage -as Windows' xinput library caused most PC games to focus on Xbox -controller compatibility. +This driver exposes all first-party and third-party Xbox and Xbox 360 +compatible controllers. It has a long history and has enjoyed considerable +usage as Windows' xinput library caused most PC games to focus on Xbox +controller compatibility. Xbox One/Series controller support has been +superseded by the xbox_gip driver, which specializes in the Gaming Input +Protocl that is introduced on the Xbox One. Due to backwards compatibility all buttons are reported as digital. This only affects Original Xbox controllers. All later controller models have only digital face buttons. Rumble is supported on some models of Xbox 360 controllers but not of -Original Xbox controllers nor on Xbox One controllers. As of writing -the Xbox One's rumble protocol has not been reverse-engineered but in -the future could be supported. +Original Xbox controllers. Notes
@@ -98,9 +98,6 @@ All generations of Xbox controllers speak USB over the wire. - Wireless Xbox 360 controllers require a 'Xbox 360 Wireless Gaming Receiver for Windows' - Wired Xbox 360 controllers use standard USB connectors. -- Xbox One controllers can be wireless but speak Wi-Fi Direct and are not - yet supported. -- Xbox One controllers can be wired and use standard Micro-USB connectors.
drivers/input/joystick/xpad.c +4 -632
937 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 02/12] Input: xpad - Remove Xbox One support
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Low] Incomplete feature removal leaves behind dead code related to Xbox One support.
Pre-existing issues:
- [High] Use-after-free due to missing cancel_work_sync() in xpad_probe error path. --
--- Patch [2]: [PATCH v4 02/12] Input: xpad - Remove Xbox One support --- commit 5a5aa152af89fbca852a5ae95c5acba0ea842ea5
Author: Vicki Pfau <[email protected]>
Input: xpad - Remove Xbox One support
This commit removes Xbox One controller support from the xpad driver, as it has been superseded by the xbox_gip driver.
Quoted 3 lines
drivers/input/joystick/xpad.c +0
-0
[ ... ]
drivers/input/joystick/xpad.c struct usb_xpad { · line 481
Quoted 8 lines
drivers/input/joystick/xpad.c +0
-1@@ -754,7 +476,6 @@ struct usb_xpad {struct urb *irq_out;/* urb for interrupt out report */ struct usb_anchor irq_out_anchor;bool irq_out_active;/* we must not use an active URB */ -u8 odata_serial;/* serial number for xbox one protocol */ unsigned char *odata;/* output data */ dma_addr_t odata_dma;spinlock_t odata_lock;
[Severity: Low]
Since Xbox One support is being removed, is it intentional that fields like init_seq, packet_type, delay_init, and delayed_init_done were left in struct usb_xpad? They appear to be completely unused now and could cause confusion.
For example, xpad_probe() still contains a large block of logic detecting Elite firmware revisions purely to set xpad->packet_type:
xpad_probe() {
...
/* Packet type detection */
if (le16_to_cpu(udev->descriptor.idVendor) == 0x045e) {
if (le16_to_cpu(udev->descriptor.idProduct) == 0x02e3) {
/* The original elite controller always uses the oldest...
}
Could this dead code be cleaned up as part of the removal?
[ ... ]
drivers/input/joystick/xpad.c static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id · line 1489
Quoted 9 lines
drivers/input/joystick/xpad.c +0
-2@@ -2090,8 +1484,6 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *idif (intf->cur_altsetting->desc.bInterfaceClass ==USB_CLASS_VENDOR_SPEC ) {if (intf->cur_altsetting->desc.bInterfaceProtocol ==129 ) xpad->xtype =XTYPE_XBOX360W ; -else if (intf->cur_altsetting->desc.bInterfaceProtocol ==208 ) - xpad->xtype =XTYPE_XBOXONE ;else xpad->xtype =XTYPE_XBOX360 ; }else {
[Severity: High]
This is a pre-existing issue, but does the error path in xpad_probe() contain a use-after-free risk if xpad360w_start_input() fails?
If xpad360w_start_input() fails after submitting the input URB (for instance, if xpad_inquiry_pad_presence() fails), it returns an error. The error path in xpad_probe() then frees the xpad struct:
err_deinit_output:
xpad_deinit_output(xpad);
err_free_in_urb:
usb_free_urb(xpad->irq_in);
err_free_idata:
usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
err_free_mem:
kfree(xpad);
return error;However, if the URB completes before being fully killed and xpad360w_process_packet() handles a presence change, it calls schedule_work(&xpad->work).
Because the error path never calls cancel_work_sync(&xpad->work), the workqueue might dereference the freed xpad pointer when xpad_presence_work() executes. Could a malicious USB device trigger this race by intentionally failing the presence inquiry while sending a valid URB completion?
[ ... ]
Quoted 5 lines
drivers/input/joystick/xpad.c +0
-1@@ -2291,15 +1672,6 @@ static int xpad_resume(struct usb_interface *intf)if (input_device_enabled (input))return xpad_start_input (xpad); -if (xpad->xtype ==XTYPE_XBOXONE ) {
drivers/input/joystick/xpad.c static int xpad_resume(struct usb_interface *intf) · line 1676
Quoted 10 lines
drivers/input/joystick/xpad.c +0
-8-/* -* Even if there are no users, we'll send Xbox One pads -* the startup sequence so they don't sit there and -* blink until somebody opens the input device again. -*/ -return xpad_start_xbox_one (xpad); - } -return 0 ; }
[Severity: Low]
The delayed_init_done flag is still being cleared at the start of xpad_resume() despite being unused elsewhere. Should this assignment be removed as well?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v4 3/12 Input: xbox_gip - Add controllable LED support
In reply to Vicki PfauXbox One controllers have two different types of controllable LED support:
- Monochrome white, which most controllers have.
- RGBW addressible, which the Elite 2 controllers have.
This exposes both types as led cdevs.
--- drivers/input/joystick/gip/Kconfig | 2 +- drivers/input/joystick/gip/gip-core.c | 111 ++++++++++++++++++++++++++ drivers/input/joystick/gip/gip.h | 6 +- 3 files changed, 117 insertions(+), 2 deletions(-)
drivers/input/joystick/gip/Kconfig +1 -1
@@ -4,7 +4,7 @@ # config JOYSTICK_XBOX_GIP tristate "Xbox One/Series controller support" - depends on USB && USB_ARCH_HAS_HCD + depends on USB && USB_ARCH_HAS_HCD && LEDS_CLASS_MULTICOLOR help Say Y here if you want to use Xbox One and Series controllers with your computer. Make sure to say Y to "Joystick support" (CONFIG_INPUT_JOYDEV)
drivers/input/joystick/gip/gip-core.c +111 -0
126 lines not on this page · raw message
drivers/input/joystick/gip/gip.h +5 -1
@@ -12,6 +12,7 @@#ifndef _GIP_H#define _GIP_H +#include <linux/led-class-multicolor.h> #include <linux/rcupdate.h> #include <linux/usb/input.h>
@@ -234,6 +235,10 @@ struct gip_attachment {uint8_t seq_vendor;uint8_t device_state; +union { +struct led_classdev standard; +struct led_classdev_mc color; + } guide_led;struct gip_extended_status status;
@@ -245,7 +250,6 @@ struct gip_attachment {int extra_axes;bool dpad_as_buttons; -struct hid_device __rcu *hdev; };struct gip_urb {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 03/12] Input: xbox_gip - Add controllable LED support
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
New issues:
- [High] Lifecycle mismatch causing Use-After-Free and list corruption for the LED class device.
- [Medium] Missing initialization of intensity for multicolor LED subleds renders it functionally broken.
- [Medium] Data race on mc_cdev->subled_info during concurrent sysfs or trigger updates.
Pre-existing issues:
- [High] Missing bounds check on subsequent message fragments causes out-of-bounds read and kernel memory leak.
- [High] Input device properties phys and uniq point to freed memory, causing a Use-After-Free and information leak. --
--- Patch [3]: [PATCH v4 03/12] Input: xbox_gip - Add controllable LED support --- commit ac5f2a01657c115e9d51066385a4a06622ddd86f
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add controllable LED support
This commit adds support for monochrome white and RGBW addressable LEDs found on Xbox One and Elite 2 controllers. It exposes these controllable LEDs via the led class device and multicolor led class device subsystems.
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0
-0
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_send_guide_button_led(struct gip_attachment *attachment, · line 1047
Quoted 11 lines
drivers/input/joystick/gip/gip-core.c +10
-0@@ -1038,9 +1038,116 @@ static int gip_send_guide_button_led(struct gip_attachment *attachment, +static int gip_guide_color_led_set (struct led_classdev *led, +enum led_brightness value) +{ +struct led_classdev_mc *mc_cdev =container_of (led, + struct led_classdev_mc, led_cdev); +struct gip_attachment *attachment =container_of (mc_cdev, + struct gip_attachment, guide_led.color); + +led_mc_calc_color_components (mc_cdev, value); +guard (mutex)(&attachment->lock);
[Severity: Medium]
Can this cause a data race on the subled_info array? The function led_mc_calc_color_components() modifies mc_cdev->subled_info before the attachment->lock is acquired. Since the LED subsystem does not serialize concurrent calls to brightness_set_blocking (for instance, from concurrent sysfs writes and blink triggers), multiple threads could modify the shared subled_info array simultaneously before acquiring the mutex.
Quoted 18 lines
drivers/input/joystick/gip/gip-core.c +18
-0+return gip_send_guide_button_color_led (attachment, + mc_cdev->subled_info[0 ].brightness, + mc_cdev->subled_info[1 ].brightness, + mc_cdev->subled_info[2 ].brightness, + mc_cdev->subled_info[3 ].brightness); +} + +static int gip_guide_led_probe (struct gip_attachment *attachment,struct device *dev) +{ +int rc =0 ; + +if (!gip_supports_system_message (attachment,GIP_CMD_LED , false)) +return 0 ; + +if (attachment->features &GIP_FEATURE_GUIDE_COLOR ) { +struct mc_subled *mc_led_info; +struct led_classdev_mc *mc_cdev = &attachment->guide_led.color; +struct led_classdev *cdev = &mc_cdev->led_cdev;
drivers/input/joystick/gip/gip-core.c static int gip_send_guide_button_led(struct gip_attachment *attachment, · line 1110
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +10
-0+ + mc_led_info =devm_kcalloc (dev,4 , +sizeof (*mc_led_info),GFP_KERNEL ); +if (!mc_led_info) +return -ENOMEM ; + + mc_led_info[0 ].color_index =LED_COLOR_ID_RED ; + mc_led_info[1 ].color_index =LED_COLOR_ID_GREEN ; + mc_led_info[2 ].color_index =LED_COLOR_ID_BLUE ; + mc_led_info[3 ].color_index =LED_COLOR_ID_WHITE ;
[Severity: Medium]
Does this leave the intensity fields set to 0? The mc_led_info array is allocated with devm_kcalloc, which zero-initializes the memory. When the LED core later calls led_mc_calc_color_components(), it multiplies the requested brightness by each channel's intensity. If the intensity is 0, the computed brightness for all channels will be 0, and the LED will not turn on unless userspace manually writes to the multi_intensity sysfs file.
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c +6
-0+ + mc_cdev->subled_info = mc_led_info; + mc_cdev->num_colors =4 ; + + cdev->brightness =51 ; + cdev->max_brightness =255 ;
drivers/input/joystick/gip/gip-core.c static int gip_send_guide_button_led(struct gip_attachment *attachment, · line 1126
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +10
-0+ cdev->flags =LED_CORE_SUSPENDRESUME |LED_RETAIN_AT_SHUTDOWN ; + cdev->brightness_set_blocking = gip_guide_color_led_set; + cdev->name =devm_kasprintf (dev,GFP_KERNEL , +"%s:rgb:power" ,dev_name (dev)); +if (!cdev->name) + rc = -ENOMEM ; + +if (!rc) + rc =devm_led_classdev_multicolor_register (dev, + mc_cdev);
[Severity: High]
Can this lead to a use-after-free and list corruption on device disconnect? The LED is registered using devres tied to the input device (dev), meaning its unregistration is deferred until the input device is finally released. However, the mc_cdev is embedded in the attachment structure, which is explicitly freed during gip_disconnect():
drivers/input/joystick/gip/gip-core.c:gip_disconnect() {
...
devm_kfree(to_gip_device(attachment), attachment);
...
}If a user-space process holds an open file descriptor to the evdev node during a USB disconnect, the input_dev outlives the attachment. The led_classdev remains registered but its backing memory is freed. When the evdev node is finally closed, the input_dev devres cleanup will invoke led_classdev_unregister() on the freed memory.
While reviewing this patch, I noticed a pre-existing issue in gip_receive_fragment() that might need addressing:
drivers/input/joystick/gip/gip-core.c:gip_receive_fragment() {
...
} else {
if (header->length != 0)
memcpy(&attachment->in_fragment_data[*fragment_offset],
&bytes[offset], header->length);
...
}[Severity: High]
This is a pre-existing issue, but does this lack a bounds check for subsequent message fragments? The first fragment properly validates that header->length does not exceed the packet size, but this else branch omits that validation. A malicious or malfunctioning USB device could send a small packet with a large header->length, causing the memcpy to read out of bounds from the 64-byte gip_raw_message buffer into the heap-allocated in_fragment_data buffer, potentially leaking kernel memory.
I also noticed another pre-existing lifecycle issue in gip_setup_input_device() and gip_disconnect():
drivers/input/joystick/gip/gip-core.c:gip_setup_input_device() {
...
input->uniq = attachment->uniq;
...
input->phys = attachment->phys;
...
}drivers/input/joystick/gip/gip-core.c:gip_disconnect() {
...
devm_kfree(to_gip_device(attachment), attachment->uniq);
devm_kfree(to_gip_device(attachment), attachment);
...
}[Severity: High]
This isn't a bug introduced by this patch, but can this cause a use-after-free if the input device is held open? The input device properties phys and uniq are set to point to attachment->phys and attachment->uniq. When the device is disconnected, these are explicitly freed. If an evdev file descriptor is open, the input_dev outlives the attachment. Subsequent calls to EVIOCGPHYS or EVIOCGUNIQ ioctls would read from these freed memory pointers and copy the contents to user-space.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v4 4/12 Input: xbox_gip - Add HID relaying
In reply to Vicki PfauGIP allows tunneling of HID packets, with the HID descriptor embedded in the GIP metadata exchanged during the initial handshake. This patch creates a hid_device for this HID descriptor if found, as well as relaying the HID packets.
--- drivers/input/joystick/gip/gip-core.c | 94 ++++++++++++++++++++++++++- drivers/input/joystick/gip/gip.h | 2 + 2 files changed, 93 insertions(+), 3 deletions(-)
drivers/input/joystick/gip/gip-core.c +91 -3
@@ -560,6 +560,54 @@ int gip_send_vendor_message(struct gip_attachment *attachment, bytes, num_bytes); } +static int gip_hid_ll_parse (struct hid_device *hdev) +{ +struct gip_attachment *attachment = hdev->driver_data; + +return hid_parse_report (hdev, + attachment->metadata.device.hid_descriptor, + attachment->metadata.device.hid_descriptor_size); +} + +static int gip_hid_ll_start (struct hid_device *hdev) +{ +return 0 ; +} + +static void gip_hid_ll_stop (struct hid_device *hdev) +{ +} + +static int gip_hid_ll_open (struct hid_device *hdev) +{ +return 0 ; +} + +static void gip_hid_ll_close (struct hid_device *hdev) +{ +} + +static int gip_hid_ll_raw_request (struct hid_device *hdev, +unsigned char reportnum,uint8_t *buf,size_t count, +unsigned char report_type,int reqtype) +{ +/* +* TODO: Based on the metadata, output reports appear to be possible, +* but the chatpad doesn't have the LEDs it claims to support, so +* it's not clear how to test we're sending them properly. +*/ +return 0 ; +} + +static const struct hid_ll_driver gip_hid_ll_driver = { + .parse = gip_hid_ll_parse, + .start = gip_hid_ll_start, + .stop = gip_hid_ll_stop, + .open = gip_hid_ll_open, + .close = gip_hid_ll_close, + .raw_request = gip_hid_ll_raw_request, +}; +static void gip_metadata_free (struct device *dev,struct gip_metadata *metadata) {devm_kfree (dev, metadata->device.audio_formats);
@@ -1361,7 +1409,34 @@ static int gip_send_init_sequence(struct gip_attachment *attachment)if (rc)return rc; -return 0 ; +if (attachment->metadata.device.hid_descriptor) { +struct hid_device *hdev =hid_allocate_device (); + +if (IS_ERR (hdev)) +return PTR_ERR (hdev); + + hdev->ll_driver = &gip_hid_ll_driver; + hdev->bus =BUS_USB ; + hdev->vendor = attachment->vendor_id; + hdev->product = attachment->product_id; + hdev->dev.parent =to_gip_device (attachment); + hdev->driver_data = attachment; +if (attachment->name) +strscpy (hdev->name, attachment->name); +else +strscpy (hdev->name,"Xbox Chatpad" ); +strscpy (hdev->phys, attachment->phys); + rc =hid_add_device (hdev); +if (rc) { +gip_err (attachment,"HID device add failed: %d\n" , rc); +hid_destroy_device (hdev); + }else { +rcu_assign_pointer (attachment->hdev, hdev); +synchronize_rcu (); + } + } + +return rc; }static void gip_fragment_timeout (struct work_struct *work)
@@ -1769,9 +1844,16 @@ static int gip_handle_command_firmware(struct gip_attachment *attachment,static int gip_handle_command_hid_report (struct gip_attachment *attachment,const struct gip_header *header,uint8_t *bytes,int num_bytes) { -gip_warn (attachment,"Unimplemented HID report message\n" ); +struct hid_device *hdev; -return -EOPNOTSUPP ; +guard (rcu)(); + hdev =rcu_dereference (attachment->hdev); +if (hdev) +return hid_input_report (hdev,HID_INPUT_REPORT , bytes, num_bytes, true); + +gip_warn (attachment,"Got HID report with no HID descriptor\n" ); + +return -EPROTO ; } static int gip_handle_command_extended(struct gip_attachment *attachment,
@@ -2527,6 +2609,7 @@ static int gip_shutdown(struct gip_device *device)for (i =0 ; i <MAX_ATTACHMENTS ; i++) {struct gip_attachment *attachment = device->attachments[i];struct input_dev *input; +struct hid_device *hdev;if (!attachment)continue ;
@@ -2537,14 +2620,19 @@ static int gip_shutdown(struct gip_device *device)rcu_read_lock (); input =rcu_dereference (attachment->input); + hdev =rcu_dereference (attachment->hdev);rcu_read_unlock ();rcu_assign_pointer (attachment->input,NULL ); +rcu_assign_pointer (attachment->hdev,NULL );synchronize_rcu (); }if (input)input_unregister_device (input); + +if (hdev) +hid_destroy_device (hdev); }return 0 ;
drivers/input/joystick/gip/gip.h +2 -0
@@ -12,6 +12,7 @@#ifndef _GIP_H#define _GIP_H +#include <linux/hid.h> #include <linux/led-class-multicolor.h> #include <linux/rcupdate.h> #include <linux/usb/input.h>
@@ -250,6 +251,7 @@ struct gip_attachment {int extra_axes;bool dpad_as_buttons; +struct hid_device __rcu *hdev; };struct gip_urb {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 04/12] Input: xbox_gip - Add HID relaying
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
New issues:
- [High] The tunneled hid_device (attachment->hdev) is permanently leaked when the controller's metadata is reset, which can lead to a future Use-After-Free.
- [High] The HID device is configured with BUS_USB but its parent is set to a usb_device rather than a usb_interface, causing downstream HID drivers to crash.
- [Medium] The transport driver uses hid_input_report() instead of the required hid_safe_input_report(), circumventing HID core bounds checking.
- [Low] Unnecessary synchronize_rcu() after rcu_assign_pointer() when publishing hdev.
- [Low] Not all required hid_device fields are initialized before hid_add_device().
Pre-existing issues:
- [High] The error path in gip_setup_input_device() leaves a dangling pointer in attachment->input, which causes a Use-After-Free during shutdown.
- [High] Missing NULL check after allocating memory with devm_kmalloc in gip_send_raw_message(), leading to a NULL pointer dereference. --
--- Patch [4]: [PATCH v4 04/12] Input: xbox_gip - Add HID relaying --- commit a021e64e3205f2caaac1426796abb2e1c1a1a8e6
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add HID relaying
GIP allows tunneling of HID packets, with the HID descriptor embedded in the GIP metadata exchanged during the initial handshake. This patch creates a hid_device for this HID descriptor if found, as well as relaying the HID packets.
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0
-0
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_send_init_sequence(struct gip_attachment *attachment) · line 1416
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +5
-1@@ -1361,7 +1409,34 @@ static int gip_send_init_sequence(struct gip_attachment *attachment)if (rc)return rc; -return 0 ; +if (attachment->metadata.device.hid_descriptor) { +struct hid_device *hdev =hid_allocate_device (); + +if (IS_ERR (hdev)) +return PTR_ERR (hdev);
[Severity: High]
Does this leak the previous hdev if called multiple times, or does gip_reset_metadata() need to be updated to clean up this new device?
When the metadata is reset, gip_reset_metadata() tears down the input device:
if (input) {
rcu_assign_pointer(attachment->input, NULL);
synchronize_rcu();
input_unregister_device(input);
}But the newly added attachment->hdev is not destroyed there, which could leave a stray hid_device and lead to a use-after-free via hidraw.
drivers/input/joystick/gip/gip-core.c static int gip_send_init_sequence(struct gip_attachment *attachment) · line 1421
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +5
-0+ + hdev->ll_driver = &gip_hid_ll_driver; + hdev->bus =BUS_USB ; + hdev->vendor = attachment->vendor_id; + hdev->product = attachment->product_id;
[Severity: Low]
Are there missing hid_device initializations here?
The HID subsystem guidelines recommend fully initializing the device fields (such as uniq, version, country, etc.) before calling hid_add_device().
drivers/input/joystick/gip/gip-core.c static int gip_send_init_sequence(struct gip_attachment *attachment) · line 1422
Quoted 1 line
drivers/input/joystick/gip/gip-core.c +1
-0+ hdev->dev.parent =to_gip_device (attachment);
[Severity: High]
Could this parent assignment cause a crash in downstream HID drivers?
The device is configured as a USB bus device, but the parent is being set to a gip_device (which contains a usb_device) rather than a usb_interface. Many USB HID drivers assume the parent is a usb_interface and call to_usb_interface() on it. If they do, they will perform an invalid cast and crash.
drivers/input/joystick/gip/gip-core.c static int gip_send_init_sequence(struct gip_attachment *attachment) · line 1435
Quoted 13 lines
drivers/input/joystick/gip/gip-core.c +13
-0+ hdev->driver_data = attachment; +if (attachment->name) +strscpy (hdev->name, attachment->name); +else +strscpy (hdev->name,"Xbox Chatpad" ); +strscpy (hdev->phys, attachment->phys); + rc =hid_add_device (hdev); +if (rc) { +gip_err (attachment,"HID device add failed: %d\n" , rc); +hid_destroy_device (hdev); + }else { +rcu_assign_pointer (attachment->hdev, hdev); +synchronize_rcu ();
[Severity: Low]
Is this synchronize_rcu() call necessary?
Since rcu_assign_pointer() already provides the needed memory barriers to publish a new pointer, and we aren't waiting for readers of an old pointer to finish (as there isn't one being freed here), calling synchronize_rcu() might just unnecessarily block and degrade performance.
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +4
-0+ } + } + +return rc; }
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_handle_command_firmware(struct gip_attachment *attachment, · line 1852
Quoted 12 lines
drivers/input/joystick/gip/gip-core.c +5
-2@@ -1769,9 +1844,16 @@ static int gip_handle_command_firmware(struct gip_attachment *attachment,static int gip_handle_command_hid_report (struct gip_attachment *attachment,const struct gip_header *header,uint8_t *bytes,int num_bytes) { -gip_warn (attachment,"Unimplemented HID report message\n" ); +struct hid_device *hdev; -return -EOPNOTSUPP ; +guard (rcu)(); + hdev =rcu_dereference (attachment->hdev); +if (hdev) +return hid_input_report (hdev,HID_INPUT_REPORT , bytes, num_bytes, true);
[Severity: Medium]
Does this bypass the HID core's bounds checking?
Using hid_input_report() instead of hid_safe_input_report() for incoming data violates the HID subsystem transport guidelines, as it prevents the core from validating the size of the buffer.
drivers/input/joystick/gip/gip-core.c static int gip_handle_command_firmware(struct gip_attachment *attachment, · line 1857
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +4
-0+ +gip_warn (attachment,"Got HID report with no HID descriptor\n" ); + +return -EPROTO ; }
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the input device lifecycle, I noticed a potential use-after-free during shutdown.
In gip_setup_input_device(), if input_register_device() fails, the pointer is left dangling:
err_free_device: input_free_device(input); return rc;
When gip_shutdown() later runs, it sees the remaining attachment->input pointer and calls input_unregister_device() on an already freed device.
[Severity: High]
This is a pre-existing issue, but does this allocation in gip_send_raw_message() need a null check?
fragment->data = devm_kmalloc(to_gip_device(attachment), fragment->total_length, GFP_ATOMIC); fragment->fragment_offset = 0; memcpy(fragment->data, bytes, num_bytes);
If the atomic allocation fails and returns a null pointer, the immediate memcpy() will cause a kernel panic.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v4 5/12 Input: xbox_gip - Add battery support
In reply to Vicki PfauControllers are required to give information about the battery in the controller, if present. This patch exposes that information as a power_supply device.
--- drivers/input/joystick/gip/gip-core.c | 97 +++++++++++++++++++++++++++ drivers/input/joystick/gip/gip.h | 3 + 2 files changed, 100 insertions(+)
drivers/input/joystick/gip/gip-core.c +97 -0
@@ -255,6 +255,13 @@ static const struct gip_audio_format gip_audio_format_table[MAX_GIP_AUDIO_FORMAT }; +static enum power_supply_property gip_battery_props[] = { +POWER_SUPPLY_PROP_PRESENT , +POWER_SUPPLY_PROP_CAPACITY_LEVEL , +POWER_SUPPLY_PROP_SCOPE , +POWER_SUPPLY_PROP_STATUS , +}; +static const struct gip_quirks base_quirks[] = { {GIP_VID_PDP ,GIP_PID_PDP_ROCK_CANDY ,0 , .quirks =GIP_QUIRK_NO_HELLO },
@@ -1196,6 +1203,92 @@ static int gip_guide_led_probe(struct gip_attachment *attachment, struct devicereturn rc; } +static int gip_battery_get_property (struct power_supply *psy, +enum power_supply_property psp,union power_supply_propval *val) +{ +struct gip_attachment *attachment =power_supply_get_drvdata (psy); + +guard (mutex)(&attachment->lock); +switch (psp) { +case POWER_SUPPLY_PROP_PRESENT : + val->intval = attachment->status.base.battery_type !=GIP_BATTERY_ABSENT ; +break ; +case POWER_SUPPLY_PROP_SCOPE : + val->intval =POWER_SUPPLY_SCOPE_DEVICE ; +break ; +case POWER_SUPPLY_PROP_STATUS : +if (attachment->status.base.battery_type ==GIP_BATTERY_ABSENT ) { + val->intval =POWER_SUPPLY_STATUS_NOT_CHARGING ; + }else { +switch (attachment->status.base.charge) { +case GIP_CHARGING : +if (attachment->status.base.battery_level ==GIP_BATTERY_FULL ) + val->intval =POWER_SUPPLY_STATUS_FULL ; +else + val->intval =POWER_SUPPLY_STATUS_CHARGING ; +break ; +case GIP_NOT_CHARGING : + val->intval =POWER_SUPPLY_STATUS_DISCHARGING ; +break ; +case GIP_CHARGE_ERROR : +default : + val->intval =POWER_SUPPLY_STATUS_UNKNOWN ; +break ; + } + } +break ; +case POWER_SUPPLY_PROP_CAPACITY_LEVEL : +if (attachment->status.base.battery_type ==GIP_BATTERY_ABSENT ) { + val->intval =POWER_SUPPLY_CAPACITY_LEVEL_FULL ; + }else { +switch (attachment->status.base.battery_level) { +case GIP_BATTERY_CRITICAL : + val->intval =POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL ; +break ; +case GIP_BATTERY_LOW : + val->intval =POWER_SUPPLY_CAPACITY_LEVEL_LOW ; +break ; +case GIP_BATTERY_MEDIUM : + val->intval =POWER_SUPPLY_CAPACITY_LEVEL_NORMAL ; +break ; +case GIP_BATTERY_FULL : + val->intval =POWER_SUPPLY_CAPACITY_LEVEL_FULL ; +break ; +default : + val->intval =POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN ; +break ; + } +break ; + } +break ; +default : +return -EINVAL ; + } +return 0 ; +} + +static int gip_battery_create (struct gip_attachment *attachment,struct device *dev) +{ +struct power_supply_config supply_config = { .drv_data = attachment, }; + + attachment->battery_desc.properties = gip_battery_props; + attachment->battery_desc.num_properties =ARRAY_SIZE (gip_battery_props); + attachment->battery_desc.get_property = gip_battery_get_property; + attachment->battery_desc.type =POWER_SUPPLY_TYPE_BATTERY ; + attachment->battery_desc.name =devm_kasprintf (dev,GFP_KERNEL , +"gip-battery-%s" ,dev_name (dev)); + +if (!attachment->battery_desc.name) +return -ENOMEM ; + + attachment->battery =devm_power_supply_register (dev, + &attachment->battery_desc, &supply_config); +if (IS_ERR (attachment->battery)) +return PTR_ERR (attachment->battery); + +return power_supply_powers (attachment->battery, dev); +} +static bool gip_send_set_device_state (struct gip_attachment *attachment,uint8_t state) {uint8_t buffer[] = { state };
@@ -1308,6 +1401,9 @@ static int gip_setup_input_device(struct gip_attachment *attachment) rc =gip_guide_led_probe (attachment, &input->dev);if (rc)gip_err (attachment,"Failed to register LEDs: %d\n" , rc); + rc =gip_battery_create (attachment, &input->dev); +if (rc) +gip_err (attachment,"Failed to register battery: %d\n" , rc);return 0 ;
@@ -2623,6 +2719,7 @@ static int gip_shutdown(struct gip_device *device) hdev =rcu_dereference (attachment->hdev);rcu_read_unlock (); + attachment->battery =NULL ;rcu_assign_pointer (attachment->input,NULL );rcu_assign_pointer (attachment->hdev,NULL );synchronize_rcu ();
drivers/input/joystick/gip/gip.h +3 -0
@@ -14,6 +14,7 @@#include <linux/hid.h> #include <linux/led-class-multicolor.h> +#include <linux/power_supply.h> #include <linux/rcupdate.h> #include <linux/usb/input.h>
@@ -241,6 +242,8 @@ struct gip_attachment {struct led_classdev_mc color; } guide_led; +struct power_supply *battery; +struct power_supply_desc battery_desc;struct gip_extended_status status;enum gip_elite_button_format xbe_format;
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 05/12] Input: xbox_gip - Add battery support
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
New issues:
- [High] Use-After-Free of attachment via battery sysfs properties due to devres lifecycle mismatch.
- [Medium] Missing power_supply_changed() call prevents userspace from receiving battery state updates.
- [Low] Data Race on battery status fields causes torn reads in sysfs.
Pre-existing issues:
- [High] Use-After-Free of attachment via LED sysfs properties due to devres lifecycle mismatch.
- [High] Dangling attachment->input pointer on input_register_device failure causes Use-After-Free.
- [High] AB-BA Deadlock between gip_shutdown and gip_fragment_timeout.
- [High] Out-of-Bounds Read in gip_receive_fragment due to missing bounds check.
--
--- Patch [5]: [PATCH v4 05/12] Input: xbox_gip - Add battery support --- commit 467fca5f8d0f00e2e7affd3b50a79e158dfc1b7d
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add battery support
Controllers are required to give information about the battery in the controller, if present. This patch exposes that information as a power_supply device.
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0
-0
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_guide_led_probe(struct gip_attachment *attachment, struct device · line 1211
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +6
-0@@ -1196,6 +1203,92 @@ static int gip_guide_led_probe(struct gip_attachment *attachment, struct devicereturn rc; } +static int gip_battery_get_property (struct power_supply *psy, +enum power_supply_property psp,union power_supply_propval *val) +{ +struct gip_attachment *attachment =power_supply_get_drvdata (psy); + +guard (mutex)(&attachment->lock);
[Severity: Low]
Does taking this mutex fully protect the battery status fields?
Looking at gip_handle_command_status_device(), the fields are updated directly without acquiring attachment->lock:
gip_handle_command_status_device()
attachment->status.base.battery_level = bytes[0] & 3; attachment->status.base.battery_type = (bytes[0] >> 2) & 3; attachment->status.base.charge = (bytes[0] >> 4) & 3; attachment->status.base.power_level = (bytes[0] >> 6) & 3;
Could a concurrent sysfs read observe torn or partially updated state?
[Severity: Medium]
Also, when gip_handle_command_status_device() updates the battery state, is a call to power_supply_changed(attachment->battery) needed?
Without it, will userspace applications be notified of the updated battery state, or will they only see changes if they manually poll sysfs?
[ ... ]
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +5
-0+static int gip_battery_create (struct gip_attachment *attachment,struct device *dev) +{ +struct power_supply_config supply_config = { .drv_data = attachment, }; + + attachment->battery_desc.properties = gip_battery_props;
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_guide_led_probe(struct gip_attachment *attachment, struct device · line 1285
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +2
-0+ attachment->battery =devm_power_supply_register (dev, + &attachment->battery_desc, &supply_config);
[Severity: High]
Could this lead to a use-after-free when the device is disconnected?
The power supply is registered using devm with the input device as its parent, so its release is deferred until the input device's reference count drops to zero.
However, gip_disconnect() immediately frees the attachment structure:
gip_disconnect()
devm_kfree(to_gip_device(attachment), attachment);
If a userspace process holds the input evdev node open, the power supply sysfs node remains registered. Could a subsequent sysfs read trigger gip_battery_get_property() and dereference the freed attachment structure?
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +5
-0+if (IS_ERR (attachment->battery)) +return PTR_ERR (attachment->battery); + +return power_supply_powers (attachment->battery, dev); +}
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_setup_input_device(struct gip_attachment *attachment) · line 1401
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +0
-0@@ -1308,6 +1401,9 @@ static int gip_setup_input_device(struct gip_attachment *attachment) rc =gip_guide_led_probe (attachment, &input->dev);
[Severity: High]
This isn't a bug introduced by this patch, but does gip_guide_led_probe() have the same devm lifecycle mismatch as the battery creation?
The LEDs are registered using devm_led_classdev_multicolor_register() with the input device as their parent.
Since gip_disconnect() frees the attachment structure manually, could writing to the LED brightness sysfs node trigger callbacks that dereference the freed attachment if userspace still holds the input device open?
[Severity: High]
This is also a pre-existing issue, but if gip_setup_input_device() fails, is there a dangling pointer left behind?
The pointer is published before registration:
gip_setup_input_device()
rcu_assign_pointer(attachment->input, input); rc = input_register_device(input); if (rc) goto err_free_device;
But the error path does not clear it:
err_free_device:
input_free_device(input); return rc;
Could an incoming hardware event trigger gip_handle_ll_input_report() and read the freed attachment->input pointer?
Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +3
-0if (rc)gip_err (attachment,"Failed to register LEDs: %d\n" , rc); + rc =gip_battery_create (attachment, &input->dev); +if (rc) +gip_err (attachment,"Failed to register battery: %d\n" , rc);return 0 ;
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_shutdown(struct gip_device *device) · line 2722
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +1
-0@@ -2623,6 +2719,7 @@ static int gip_shutdown(struct gip_device *device) hdev =rcu_dereference (attachment->hdev);rcu_read_unlock (); + attachment->battery =NULL ;
[Severity: High]
This isn't a bug introduced by this patch, but can gip_shutdown() deadlock with gip_fragment_timeout()?
In gip_shutdown(), the lock is acquired before cancelling the work:
gip_shutdown()
scoped_guard (mutex, &attachment->lock) {
cancel_delayed_work_sync(&attachment->metadata_next);
cancel_delayed_work_sync(&attachment->in_fragment_timeout);If gip_fragment_timeout() runs concurrently, it attempts to acquire the same mutex:
gip_fragment_timeout()
guard(mutex)(&attachment->lock);
Could cancel_delayed_work_sync() block waiting for the work to finish, while the work blocks waiting for the mutex?
drivers/input/joystick/gip/gip-core.c static int gip_shutdown(struct gip_device *device) · line 2725
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0
-0rcu_assign_pointer (attachment->input,NULL );rcu_assign_pointer (attachment->hdev,NULL );synchronize_rcu ();
[Severity: High]
This is a pre-existing issue, but does gip_receive_fragment() have an out-of-bounds read when processing fragments?
When parsing a non-initial message fragment, it uses header->length directly in memcpy():
gip_receive_fragment()
if (header->length != 0) memcpy(&attachment->in_fragment_data[*fragment_offset], &bytes[offset], header->length);
Is there a check to ensure that offset + header->length does not exceed the actual size of the received USB buffer? Could a malicious USB device sending a fragmented message with an artificially large length cause an out-of-bounds read?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v4 6/12 Input: xbox_gip - Allow sending fragmented and ACME messages
In reply to Vicki PfauGIP supports sending messages that exceed the length of the MTU using a fragmented message flow. It sits on top of a reliable message transmission flow, which uses the ACME flag to signal that a message must be ACKed if it is received properly. We already supported receiving and coalescing these messages, but the ability to send them wasn't yet present due to not needing it for basic usage. This patch adds it for future use with the security flow.
--- drivers/input/joystick/gip/gip-core.c | 330 ++++++++++++++++++++++++-- drivers/input/joystick/gip/gip.h | 17 ++ 2 files changed, 331 insertions(+), 16 deletions(-)
drivers/input/joystick/gip/gip-core.c +314 -16
@@ -8,7 +8,6 @@* - Audio device support * - Security packet handshake * - Event logging -* - Sending fragmented messages * - Raw character device * - Wheel support * - Flight stick support
@@ -28,6 +27,7 @@#define GIP_WIRED_INTF_DATA 0#define GIP_WIRED_INTF_AUDIO 1 +#define GIP_DUAL_LENGTH 6#define MAX_MESSAGE_LENGTH 0x4000#define MAX_AUDIO_MESSAGES 9
@@ -437,6 +437,13 @@ static uint8_t gip_sequence_next(struct gip_attachment *attachment,return seq; } +static unsigned int gip_fragment_id (const struct gip_attachment *attachment, +const struct gip_out_fragment *fragment) +{ +return BIT (attachment->attachment_index *MAX_OUT_FRAGMENTS + + (unsigned ) (fragment - attachment->out_fragments)); +} +static void gip_handle_quirks_array (struct gip_attachment *attachment,const struct gip_quirks *quirks) {
@@ -479,6 +486,48 @@ static void gip_handle_quirks(struct gip_attachment *attachment)gip_handle_quirks_array (attachment, attachment->driver->quirks); } +static struct gip_out_fragment *gip_find_fragment (struct gip_attachment *attachment, +uint8_t message_type,uint8_t flags,uint8_t seq,bool strict) +{ +int i; + +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) { +if (!attachment->out_fragments[i].active) +continue ; +if (attachment->out_fragments[i].message != message_type) +continue ; +if ((attachment->out_fragments[i].flags ^ flags) &GIP_FLAG_SYSTEM ) +continue ; +if (attachment->out_fragments[i].seq != seq) { +if (strict) +continue ; + +/* +* Found one with the wrong sequence number. For some +* reason the official driver allows this. +*/ +gip_warn (attachment, +"Found fragment with different sequence number %02x, wanted %02x\n" , + attachment->out_fragments[i].seq, seq); + } + +return &attachment->out_fragments[i]; + } + +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) { +if (!attachment->out_fragments[i].active) +continue ; +if (attachment->out_fragments[i].message != message_type) +continue ; +if ((attachment->out_fragments[i].flags ^ flags) &GIP_FLAG_SYSTEM ) +continue ; + + +return &attachment->out_fragments[i]; + } +return NULL ; +} +static int gip_send_raw_message (struct gip_attachment *attachment,uint8_t message_type,uint8_t flags,uint8_t seq,const uint8_t *bytes,int num_bytes)
@@ -486,20 +535,25 @@ static int gip_send_raw_message(struct gip_attachment *attachment,struct gip_interface *intf;int offset =3 ;struct gip_urb *urb =NULL ; +struct gip_out_fragment *fragment =NULL ;int i;int rc =0 ; +int mtu = gip_data_class_mtu[message_type >>GIP_DATA_CLASS_SHIFT ]; +int reduced_mtu = mtu -GIP_DUAL_LENGTH ; +/* +* This buffer size shouldn't need to be more than 6 (there should never +* be more than 3 length bytes), but add enough extra to fit fully +* extended bignums for 32 bit (5 bytes) + 16 bit (3 bytes) values to be +* safe and then WARN_ON if we go over. If this ever happens it would be +* due to driver bugs. +*/ +uint8_t header[11 ];if (num_bytes <0 ) {gip_warn (attachment,"Invalid message length %d\n" , num_bytes);return -EINVAL ; } -if (num_bytes +6 > gip_data_class_mtu[message_type >>GIP_DATA_CLASS_SHIFT ]) { -gip_err (attachment, -"Attempted to send a message that requires fragmenting, which is not yet supported.\n" ); -return -EOPNOTSUPP ; - } -if ((message_type &GIP_DATA_CLASS_MASK ) ==GIP_DATA_CLASS_AUDIO ) intf = &attachment->device->audio;else
@@ -511,6 +565,89 @@ static int gip_send_raw_message(struct gip_attachment *attachment,return -EOPNOTSUPP ; } + header[0 ] = message_type; + header[2 ] = seq; + +if (num_bytes > reduced_mtu || + (flags & (GIP_FLAG_ACME |GIP_FLAG_FRAGMENT )) ==GIP_FLAG_ACME ) { +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) { +if (attachment->out_fragments[i].active) +continue ; + + fragment = &attachment->out_fragments[i]; +break ; + } + +if (!fragment) { +gip_err (attachment,"Fragmented message queue is full; dropping message\n" ); +return -EALREADY ; + } + + fragment->active = true; + fragment->message = message_type; + fragment->flags = flags &GIP_FLAG_SYSTEM ; + fragment->acked = false; + fragment->seq = seq; + fragment->total_length = num_bytes; + fragment->data =devm_kmalloc (to_gip_device (attachment), + fragment->total_length,GFP_ATOMIC ); + fragment->fragment_offset =0 ; +memcpy (fragment->data, bytes, num_bytes); + bytes = fragment->data; + +if (num_bytes > reduced_mtu) { +gip_dbg (attachment,"Starting new reliable message: %02x%02x%02x, total length %u\n" , + message_type, flags, seq, num_bytes); + flags |=GIP_FLAG_INIT_FRAG |GIP_FLAG_FRAGMENT |GIP_FLAG_ACME ; + fragment->flags |=GIP_FLAG_FRAGMENT ; +/* The spec says to extend the value to fit in 3 bytes */ + offset +=gip_encode_length (reduced_mtu, &header[offset],sizeof (header)); +if (offset ==4 && reduced_mtu <0x80 && num_bytes <0x80 ) { + header[3 ] |=0x80 ; + header[4 ] =0 ; + offset =5 ; + } + offset +=gip_encode_length (num_bytes, &header[offset], +sizeof (header) - offset); +WARN_ON (offset !=GIP_DUAL_LENGTH ); + num_bytes = mtu - offset; + }else { + offset +=gip_encode_length (num_bytes, &header[offset], +sizeof (header) - offset); + } + }else if (flags &GIP_FLAG_FRAGMENT ) { + fragment =gip_find_fragment (attachment, message_type, flags, seq, true); +if (!fragment) { +gip_err (attachment, +"Attempted to send message fragment with no associated message\n" ); +return -EINVAL ; + } + + num_bytes =min (reduced_mtu, fragment->total_length - fragment->fragment_offset); + offset +=gip_encode_length (num_bytes, &header[offset], +sizeof (header) - offset); +if (offset ==4 && num_bytes <0x80 && fragment->fragment_offset <0x80 ) { + header[3 ] |=0x80 ; + header[4 ] =0 ; + offset =5 ; + } + offset +=gip_encode_length (fragment->fragment_offset, &header[offset], +sizeof (header) - offset); +WARN_ON (offset !=GIP_DUAL_LENGTH ); + +if (num_bytes && fragment->fragment_offset + num_bytes == fragment->total_length) { +/* The final fragment in a fragmented message must be ACKed */ + flags |=GIP_FLAG_ACME ; + fragment->acked = false; + } + bytes = &fragment->data[fragment->fragment_offset]; + }else if (num_bytes >=0 ) { + offset +=gip_encode_length (num_bytes, &header[offset], +sizeof (header) - offset); + } + + header[1 ] = flags; +guard (spinlock_irqsave)(&attachment->device->message_lock);for (i =0 ; i <MAX_OUT_MESSAGES && !urb; i++) {if (!intf->out_queue[i].urb)
@@ -522,15 +659,10 @@ static int gip_send_raw_message(struct gip_attachment *attachment,gip_err (attachment,"Output queue is full; dropping message\n" );return -ENOSPC ; } - urb->data[0 ] = message_type; - urb->data[1 ] = flags; - urb->data[2 ] = seq; - offset +=gip_encode_length (num_bytes, &urb->data[offset], -sizeof (urb->data) - offset); +memcpy (urb->data, header, offset);if (num_bytes >0 )memcpy (&urb->data[offset], bytes, num_bytes); - num_bytes += offset; urb->urb->transfer_buffer_length = num_bytes;
@@ -547,9 +679,97 @@ static int gip_send_raw_message(struct gip_attachment *attachment, rc = -EIO ; } +if (fragment) { +/* +* Sending further fragments is handled in gip_urb_out if this fragment doesn't need +* ACKing or in gip_handle_command_protocol_control if it does. The flag should also +* be cleared if we're done sending this packet. +*/ +if (fragment->total_length > reduced_mtu && !(flags &GIP_FLAG_ACME )) + intf->has_pending_out |=gip_fragment_id (attachment, fragment); +else + intf->has_pending_out &= ~gip_fragment_id (attachment, fragment); + } +return rc; } +static void gip_free_fragment (struct device *dev,struct gip_out_fragment *fragment) +{ +devm_kfree (dev, fragment->data); +memset (fragment,0 ,sizeof (*fragment)); +} + +static int gip_send_next_fragment (struct gip_attachment *attachment, +struct gip_out_fragment *fragment) +{ +struct gip_interface *intf; +int next_bytes; +int flags =GIP_FLAG_FRAGMENT | fragment->flags | attachment->attachment_index; +int mtu; +int rc; + + mtu = gip_data_class_mtu[fragment->message >>GIP_DATA_CLASS_SHIFT ] -GIP_DUAL_LENGTH ; +if (fragment->fragment_offset == fragment->total_length) { +if ((fragment->message &GIP_DATA_CLASS_MASK ) ==GIP_DATA_CLASS_AUDIO ) + intf = &attachment->device->audio; +else + intf = &attachment->device->data; + + next_bytes =0 ; + intf->has_pending_out &= ~gip_fragment_id (attachment, fragment); + }else if (fragment->fragment_offset + mtu >= fragment->total_length) { + next_bytes = fragment->total_length - fragment->fragment_offset; + flags |=GIP_FLAG_ACME ; + }else { + next_bytes = mtu; + } + + rc =gip_send_raw_message (attachment, fragment->message, flags, + fragment->seq, &fragment->data[fragment->fragment_offset], + next_bytes); + +if (rc <0 ) +return rc; + +if (fragment->acked) + fragment->fragment_offset += next_bytes; +if (next_bytes ==0 && !(fragment->flags &GIP_FLAG_ACME )) +/* Finished sending the message */ +gip_free_fragment (to_gip_device (attachment), fragment); +return next_bytes; +} + +static void gip_send_fragment_work (struct work_struct *work) +{ + struct gip_interface *intf = container_of(work, struct gip_interface, send_fragment); +struct gip_out_fragment *fragment; +unsigned int has_pending_out; +unsigned long flags; +int i, j; + +spin_lock_irqsave (&intf->device->message_lock, flags); + has_pending_out = intf->has_pending_out; +spin_unlock_irqrestore (&intf->device->message_lock, flags); +for (i =0 ; i <MAX_ATTACHMENTS ; i++) { +struct gip_attachment *attachment = intf->device->attachments[i]; + +for (j =0 ; j <MAX_OUT_FRAGMENTS ; j++) { +/* Check for pending output fragments */ +if (!(has_pending_out &BIT (i *MAX_OUT_FRAGMENTS + j))) +continue ; + +guard (mutex)(&attachment->lock); + fragment = &attachment->out_fragments[j]; +if (!fragment->active) +continue ; +if (!(fragment->flags &GIP_FLAG_FRAGMENT )) +continue ; +gip_send_next_fragment (attachment, fragment); + } + } +} +int gip_send_system_message (struct gip_attachment *attachment,uint8_t message_type,uint8_t flags,const void *bytes,int num_bytes) {
@@ -1623,19 +1843,90 @@ static void gip_set_metadata_defaults(struct gip_attachment *attachment)static void gip_reset_attachment (struct gip_attachment *attachment) { +int i; +devm_kfree (to_gip_device (attachment), attachment->in_fragment_data); attachment->in_fragment_data =NULL ; attachment->in_fragment_message =-1 ; +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) { +if (attachment->out_fragments[i].active) +gip_free_fragment (to_gip_device (attachment), + &attachment->out_fragments[i]); + } +gip_reset_metadata (attachment); }static int gip_handle_command_protocol_control (struct gip_attachment *attachment,const struct gip_header *header,const uint8_t *bytes,int num_bytes) { -/* TODO */ -gip_warn (attachment,"Unimplemented Protocol Control message\n" ); -return -EOPNOTSUPP ; +const struct gip_protocol_control_ack *ack; +struct gip_out_fragment *fragment =NULL ; +uint32_t fragment_offset; +uint16_t remaining_buffer; +bool ok = true; +int mtu; +int rc; + +if (num_bytes <1 ) +return -EINVAL ; + +if (bytes[0 ] !=GIP_CONTROL_CODE_ACK ) { +gip_warn (attachment,"Unimplemented Protocol Control code %i message\n" , bytes[0 ]); +return -EOPNOTSUPP ; + } + +if (num_bytes <sizeof (*ack)) +return -EINVAL ; + + ack = (const struct gip_protocol_control_ack *)bytes; + + fragment =gip_find_fragment (attachment, ack->message_type, ack->flags, + header->sequence_id, false); +if (!fragment) { +gip_warn (attachment,"Received ACK for unknown message\n" ); + rc = -EINVAL ; + goto resend; + } + + + fragment_offset =le32_to_cpu (ack->fragment_offset); + remaining_buffer =le16_to_cpu (ack->remaining_buffer); + +if (fragment_offset > fragment->total_length) + ok = false; + mtu = gip_data_class_mtu[fragment->message >>GIP_DATA_CLASS_SHIFT ] -GIP_DUAL_LENGTH ; +if (!ok) { +gip_warn (attachment,"Received invalid buffer offset in ACK, " +"got offset=%u + remaining=%u, expected offset=%u + remaining=%u\n" , + fragment_offset, remaining_buffer, + fragment->fragment_offset, fragment->total_length - fragment->fragment_offset); + rc = -EINVAL ; + goto resend; + }else if ((fragment->flags &GIP_FLAG_FRAGMENT ) && + (fragment->fragment_offset > fragment_offset || + fragment->fragment_offset + mtu < fragment_offset)) { +gip_warn (attachment,"Received unexpected buffer offset in ACK, " +"got offset=%u, expected offset=%u\n" , + fragment_offset, fragment->fragment_offset + mtu); + } + + fragment->acked = true; +if (fragment->flags &GIP_FLAG_FRAGMENT ) { + fragment->fragment_offset = fragment_offset; + rc =gip_send_next_fragment (attachment, fragment); +if (rc <0 ) +return rc; + }else { +gip_free_fragment (to_gip_device (attachment), fragment); + } + +return 0 ; + +resend: +// TODO +return rc; } static bool gip_handle_command_hello_device(struct gip_attachment *attachment,
@@ -2175,6 +2466,7 @@ static struct gip_attachment *gip_ensure_attachment(struct gip_device *device, uint8_t attachment_index) {struct gip_attachment *attachment = device->attachments[attachment_index]; +int i;if (!attachment) { attachment =devm_kzalloc (to_gip_device (device),sizeof (*attachment),GFP_KERNEL );
@@ -2190,6 +2482,9 @@ static struct gip_attachment *gip_ensure_attachment(struct gip_device *device, attachment->product_id = device->udev->descriptor.idProduct; } +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) + attachment->out_fragments[i].message =-1 ; + device->attachments[attachment_index] = attachment;mutex_init (&attachment->lock);
@@ -2476,6 +2771,7 @@ static void gip_urb_out(struct urb *urb)switch (status) {case 0 :/* success */ +schedule_work (&intf->send_fragment);break ;case -ECONNRESET :
@@ -2672,9 +2968,11 @@ static int gip_probe(struct usb_interface *intf, const struct usb_device_id *id) gip->data.device = gip; gip->data.intf = intf; gip->data.mtu =BASE_GIP_MTU ; +INIT_WORK (&gip->data.send_fragment, gip_send_fragment_work); gip->audio.device = gip; gip->audio.mtu =MAX_GIP_MTU ; gip->audio.isoc_messages =MAX_AUDIO_MESSAGES ; +INIT_WORK (&gip->audio.send_fragment, gip_send_fragment_work);INIT_WORK (&gip->receive_message, gip_receive_work);spin_lock_init (&gip->message_lock);
drivers/input/joystick/gip/gip.h +17 -0
@@ -25,6 +25,7 @@#define MAX_IN_MESSAGES 8#define MAX_OUT_MESSAGES 8 +#define MAX_OUT_FRAGMENTS 2#define GIP_VID_MICROSOFT 0x045e#define GIP_VID_PDP 0x0e6f
@@ -194,6 +195,17 @@ struct gip_extended_status {struct gip_status_event events[5 ]; }; +struct gip_out_fragment { +bool active; +bool acked; +uint8_t message; +uint8_t flags; +uint8_t seq; +uint16_t total_length; +uint32_t fragment_offset; +uint8_t *data; +}; +struct gip_attachment ;typedef int (*gip_command_handler )(struct gip_attachment *a,const struct gip_header *header,const uint8_t *bytes,int num_bytes);
@@ -222,6 +234,8 @@ struct gip_attachment {struct delayed_work in_fragment_timeout;int in_fragment_retries; +struct gip_out_fragment out_fragments[MAX_OUT_FRAGMENTS ]; +uint16_t firmware_major_version;uint16_t firmware_minor_version;
@@ -274,6 +288,9 @@ struct gip_interface {struct usb_anchor out_anchor;struct gip_urb out_queue[MAX_OUT_MESSAGES ]; + +struct work_struct send_fragment; +unsigned int has_pending_out; };struct gip_device {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 06/12] Input: xbox_gip - Allow sending fragmented and ACME messages
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
New issues:
- [Critical] Missing cancellation of send_fragment work and OUT URBs causes Use-After-Free during driver teardown.
- [High] Missing NULL check for devm_kmalloc allocation in gip_send_raw_message.
- [High] Unsynchronized allocation of out_fragments slots in gip_send_raw_message leads to a TOCTOU race condition.
- [High] Fragment slots and memory are permanently leaked on output queue full or URB submission failures.
- [High] Unprotected read-modify-write of the intf->has_pending_out bitmask causes data races.
- [High] Broken search logic in gip_find_fragment completely ignores exact sequence numbers when strict is true.
- [High] Fragment slots for ACME messages permanently leak if the device drops the packet.
- [Medium] Improper use of managed resources (devm_kmalloc) for high-frequency, dynamic per-message buffers.
Pre-existing issues:
- [High] AB-BA deadlock in gip_shutdown when canceling delayed works.
- [High] gip_receive_fragment() blindly trusts header->length when processing subsequent fragments, leading to an out-of-bounds read from the kernel heap.
- [High] gip_receive_fragment() can dereference a NULL pointer if devm_kmalloc fails and the fragmented message sequence continues. --
--- Patch [6]: [PATCH v4 06/12] Input: xbox_gip - Allow sending fragmented and ACME messages --- commit 945e1fae94cb61e149c842cf10c67d06095eaf41
Author: Vicki Pfau <[email protected]>
Subject: Input: xbox_gip - Allow sending fragmented and ACME messages
This patch adds support for sending messages that exceed the length of the MTU using a fragmented message flow. It also supports sending messages requiring ACME acknowledgments, which will be necessary for future security flows.
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0
-0
[ ... ]
Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +6
-0@@ -479,6 +486,48 @@ static void gip_handle_quirks(struct gip_attachment *attachment) +static struct gip_out_fragment *gip_find_fragment (struct gip_attachment *attachment, +uint8_t message_type,uint8_t flags,uint8_t seq,bool strict) +{ +int i; + +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) {
[ ... ]
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c +6
-0+ } + +return &attachment->out_fragments[i]; + } + +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) {
drivers/input/joystick/gip/gip-core.c static void gip_handle_quirks(struct gip_attachment *attachment) · line 527
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +10
-0+if (!attachment->out_fragments[i].active) +continue ; +if (attachment->out_fragments[i].message != message_type) +continue ; +if ((attachment->out_fragments[i].flags ^ flags) &GIP_FLAG_SYSTEM ) +continue ; + + +return &attachment->out_fragments[i]; + }
[Severity: High]
Does this second loop bypass the sequence number check entirely when strict is true? If a strict search fails in the first loop, falling through to this one might unconditionally return a fragment that matches the type but has the wrong sequence number, potentially misrouting packets.
[ ... ]
Quoted 1 line
drivers/input/joystick/gip/gip-core.c +0
-0@@ -486,20 +535,25 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 573
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +3
-0+if (num_bytes > reduced_mtu || + (flags & (GIP_FLAG_ACME |GIP_FLAG_FRAGMENT )) ==GIP_FLAG_ACME ) { +for (i =0 ; i <MAX_OUT_FRAGMENTS ; i++) {
[Severity: High]
Are fragment slots permanently consumed if an ACME message is sent but the device drops the packet and never ACKs it? Since there doesn't appear to be a timeout or cleanup mechanism for pending fragments, a couple of dropped packets might permanently exhaust the queue.
drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 586
Quoted 13 lines
drivers/input/joystick/gip/gip-core.c +13
-0+if (attachment->out_fragments[i].active) +continue ; + + fragment = &attachment->out_fragments[i]; +break ; + } + +if (!fragment) { +gip_err (attachment,"Fragmented message queue is full; dropping message\n" ); +return -EALREADY ; + } + + fragment->active = true;
[Severity: High]
Can multiple threads race to claim the same fragment slot here? This loop and the subsequent assignment to fragment->active appear to occur without holding attachment->lock or device->message_lock.
Quoted 1 line
drivers/input/joystick/gip/gip-core.c +1
-0+ fragment->message = message_type;
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 595
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +4
-0+ fragment->data =devm_kmalloc (to_gip_device (attachment), + fragment->total_length,GFP_ATOMIC ); + fragment->fragment_offset =0 ; +memcpy (fragment->data, bytes, num_bytes);
[Severity: High]
Could this memcpy dereference a NULL pointer if the devm_kmalloc allocation fails? The GFP_ATOMIC flag means the allocation can fail under memory pressure, but there is no check before accessing the returned pointer.
[Severity: Medium]
Is devm_kmalloc the right choice for dynamic, per-message buffers? Since devm_kfree requires a linear scan of the device's devres linked list under a spinlock, frequent allocations and frees here might cause noticeable overhead and list fragmentation. Would standard kmalloc and kfree be better suited?
[ ... ]
Quoted 1 line
drivers/input/joystick/gip/gip-core.c +0
-0@@ -522,15 +659,10 @@ static int gip_send_raw_message(struct gip_attachment *attachment,
[ ... ]
Quoted 10 lines
drivers/input/joystick/gip/gip-core.c +0
-0for (i =0 ; i <MAX_OUT_MESSAGES && !urb; i++) {if (!intf->out_queue[i].urb)continue ;if (!intf->out_queue[i].urb->anchor) urb = &intf->out_queue[i]; }if (!urb) {gip_err (attachment,"Output queue is full; dropping message\n" );return -ENOSPC ; }
[ ... ]
drivers/input/joystick/gip/gip-core.c
Quoted 8 lines
drivers/input/joystick/gip/gip-core.c +8
-0+ rc =usb_submit_urb (urb->urb,GFP_ATOMIC ); +if (rc) { +dev_err (&intf->intf->dev, +"%s - usb_submit_urb failed with result %d\n" , + __func__, rc); +usb_unanchor_urb (urb->urb); + rc = -EIO ; + }
[Severity: High]
If the function returns early due to a full output queue (-ENOSPC) or a failure in usb_submit_urb() (-EIO), does the fragment slot remain permanently active? It looks like the allocated memory and the active slot aren't freed on these error paths.
[ ... ]
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +3
-0+static int gip_send_next_fragment (struct gip_attachment *attachment, +struct gip_out_fragment *fragment) +{
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_send_raw_message(struct gip_attachment *attachment, · line 720
Quoted 8 lines
drivers/input/joystick/gip/gip-core.c +8
-0+if (fragment->fragment_offset == fragment->total_length) { +if ((fragment->message &GIP_DATA_CLASS_MASK ) ==GIP_DATA_CLASS_AUDIO ) + intf = &attachment->device->audio; +else + intf = &attachment->device->data; + + next_bytes =0 ; + intf->has_pending_out &= ~gip_fragment_id (attachment, fragment);
[Severity: High]
Is it safe to perform an unprotected read-modify-write on intf->has_pending_out here? In gip_send_raw_message(), this same bitmask is modified while holding the message_lock spinlock. Concurrent modifications could lead to a data race and cause the driver to lose track of pending output.
[ ... ]
drivers/input/joystick/gip/gip-core.c static void gip_urb_out(struct urb *urb) · line 2775
Quoted 6 lines
drivers/input/joystick/gip/gip-core.c +1
-0@@ -2476,6 +2771,7 @@ static void gip_urb_out(struct urb *urb)switch (status) {case 0 :/* success */ +schedule_work (&intf->send_fragment);break ;
[Severity: High]
Could this result in a use-after-free during driver teardown?
If gip_disconnect() frees the out_queue URBs using usb_free_urb() instead of usb_kill_urb(), in-flight URBs might complete after the device structures are freed. The completion handler here would then schedule the send_fragment work using a freed intf context.
[ ... ]
A few additional observations in the surrounding codebase:
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +0
-0static void gip_shutdown (struct gip_device *gip) {
[ ... ]
Quoted 3 lines
scoped_guard (mutex, &attachment->lock) {
cancel_delayed_work_sync(&attachment->metadata_next);
cancel_delayed_work_sync(&attachment->in_fragment_timeout);[Severity: High]
This isn't a bug introduced by this patch, but could this cause an AB-BA deadlock? gip_shutdown() acquires attachment->lock before waiting synchronously on these works, but the callbacks for both works (gip_retry_metadata() and gip_fragment_timeout()) begin by acquiring the same attachment->lock.
Quoted 2 lines
static int
gip_receive_fragment(...)
{
[ ... ]
Quoted 3 lines
if (header->length != 0)
memcpy(&attachment->in_fragment_data[*fragment_offset],
&bytes[offset], header->length);[Severity: High]
This isn't a bug introduced by this patch, but is it safe to blindly trust header->length here? There doesn't appear to be a check ensuring that offset + header->length <= num_bytes (the actual size of the received USB packet). A malicious device could send a short packet with a large length field, potentially causing an out-of-bounds read from the kernel heap.
Quoted 2 lines
static int
gip_receive_fragment(...)
{
[ ... ]
Quoted 6 lines
attachment->in_fragment_message = header->message_type;
...
attachment->in_fragment_data = devm_kmalloc(to_gip_device(attachment),
attachment->in_total_length, GFP_KERNEL);
if (!attachment->in_fragment_data)
return -ENOMEM;[Severity: High]
This isn't a bug introduced by this patch, but does an allocation failure leave the state inconsistent? By updating in_fragment_message before a failing devm_kmalloc(), a subsequent fragment from the device might pass the message type check and blindly memcpy into a NULL in_fragment_data pointer.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v4 7/12 Input: xbox_gip - Add security implementation from xone
In reply to Vicki PfauThis patch imports and adapts the security message implementation from the out-of-tree xone driver, which is licensed under the GPL 2.0. This message is necessary for most controllers that use wireless dongles. The implementation is largely unchanged, with minor changes being made to adapt it to the xbox_gip driver and renaming components as appropriate for the official terminology from the released spec.
--- drivers/input/joystick/gip/Kconfig | 2 +- drivers/input/joystick/gip/Makefile | 2 +- drivers/input/joystick/gip/gip-core.c | 49 +- drivers/input/joystick/gip/gip-drivers.c | 8 + drivers/input/joystick/gip/gip-security.c | 867 ++++++++++++++++++++++ drivers/input/joystick/gip/gip.h | 47 +- 6 files changed, 950 insertions(+), 25 deletions(-) create mode 100644 drivers/input/joystick/gip/gip-security.c
drivers/input/joystick/gip/Kconfig +1 -1
@@ -4,7 +4,7 @@ # config JOYSTICK_XBOX_GIP tristate "Xbox One/Series controller support" - depends on USB && USB_ARCH_HAS_HCD && LEDS_CLASS_MULTICOLOR + depends on USB && USB_ARCH_HAS_HCD && LEDS_CLASS_MULTICOLOR && CRYPTO_ECDH help Say Y here if you want to use Xbox One and Series controllers with your computer. Make sure to say Y to "Joystick support" (CONFIG_INPUT_JOYDEV)
drivers/input/joystick/gip/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-or-later obj-$(CONFIG_JOYSTICK_XBOX_GIP) += xbox-gip.o # Base driver -xbox-gip-y := gip-core.o gip-drivers.o +xbox-gip-y := gip-core.o gip-drivers.o gip-security.o
drivers/input/joystick/gip/gip-core.c +27 -22
91 lines not on this page · raw message
drivers/input/joystick/gip/gip-drivers.c +8 -0
@@ -81,6 +81,14 @@ const struct gip_driver gip_driver_gamepad = { 0xa3, 0x12, 0x7a, 0xf1, 0x97, 0xb5), .quirks = (const struct gip_quirks []) { +/* PowerA Xbox One Classic Controller */ + {GIP_VID_BDA ,GIP_PID_BDA_XB1_CLASSIC ,0 , + .quirks =GIP_QUIRK_SKIP_SECURITY }, + +/* PowerA Xbox One Fusion Pro */ + {GIP_VID_BDA ,GIP_PID_BDA_XB1_FUSION_PRO ,0 , + .quirks =GIP_QUIRK_SKIP_SECURITY }, +/* Xbox One Controller (model 1573) */ {GIP_VID_MICROSOFT ,GIP_PID_XBOX_ONE_1573 ,0 , .override_name ="Xbox One Controller" },
drivers/input/joystick/gip/gip-security.c new file +867 -0
867 lines not on this page · raw message
drivers/input/joystick/gip/gip.h +46 -1
98 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 07/12] Input: xbox_gip - Add security implementation from xone
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider: - [Critical] Receiving a GIP_CMD_SECURITY message before the handshake is explicitly started causes a NULL…
Thank you for your contribution! Sashiko AI review found 8 potential issue(s) to consider:
- [Critical] Receiving a GIP_CMD_SECURITY message before the handshake is explicitly started causes a NULL pointer dereference.
- [High] Concurrent unprotected calls to crypto_shash_update corrupt the cryptographic transcript.
- [High] Missing NULL check for the state buffer allocation in gip_security_get_transcript leads to a NULL pointer dereference.
- [High] Allocating the transcript state buffer using crypto_shash_descsize instead of crypto_shash_statesize causes a heap buffer overflow.
- [High] Unchecked return values in gip_security_compute_prf lead to an information leak of kernel stack memory.
- [Medium] Missing cancellation of pending workers during protocol upgrade.
- [Medium] Unchecked return value of crypto_shash_tfm_digest() in gip_security_compute_ecdh.
- [Medium] The ECDH shared secret is freed without being securely zeroized.
--
--- Patch [7]: [PATCH v4 07/12] Input: xbox_gip - Add security implementation from xone --- commit 5605e9d7634da03b416418354e348fe0ff787ead
Author: Vicki Pfau <[email protected]>
Subject: Input: xbox_gip - Add security implementation from xone
This patch imports and adapts the security message implementation from the out-of-tree xone driver. It adds a security message driver for Xbox One and Series controllers, establishing handshakes needed for wireless dongles.
Quoted 5 lines
drivers/input/joystick/gip/gip-security.c new file +0
-0
[ ... ]
drivers/input/joystick/gip/gip-security.c line 204
Quoted 3 lines
drivers/input/joystick/gip/gip-security.c new file +3
-0+static int gip_security_get_transcript (struct shash_desc *desc,void *transcript) +{ +void *state =kzalloc (crypto_shash_descsize (desc->tfm),GFP_KERNEL );
[Severity: High]
Does this allocate the wrong size? The crypto_shash_export function expects a buffer sized by crypto_shash_statesize, not crypto_shash_descsize. Since the state size can be strictly larger than the descriptor size for certain algorithms, this could cause a heap buffer overflow.
[Severity: High]
Is it possible for kzalloc to fail here? The return value is passed directly to crypto_shash_export without a null check, which would cause a null pointer dereference under memory pressure.
Quoted 3 lines
drivers/input/joystick/gip/gip-security.c new file +3
-0+int err; + + err =crypto_shash_export (desc, state);
[ ... ]
Quoted 13 lines
drivers/input/joystick/gip/gip-security.c new file +13
-0+static int gip_security_compute_prf (struct shash_desc *desc,const char *label, +u8 *key,int key_len,u8 *seed,int seed_len,u8 *out,int out_len) +{ +u8 hash[SHA256_DIGEST_SIZE ], hash_out[SHA256_DIGEST_SIZE ]; +int err; + + err =crypto_shash_setkey (desc->tfm, key, key_len); +if (err) +return err; + +crypto_shash_init (desc); +crypto_shash_update (desc, label,strlen (label)); +crypto_shash_update (desc, seed, seed_len);
drivers/input/joystick/gip/gip-security.c line 244
Quoted 10 lines
drivers/input/joystick/gip/gip-security.c new file +10
-0+crypto_shash_final (desc, hash); + +while (out_len >0 ) { +crypto_shash_init (desc); +crypto_shash_update (desc, hash,sizeof (hash)); +crypto_shash_update (desc, label,strlen (label)); +crypto_shash_update (desc, seed, seed_len); +crypto_shash_final (desc, hash_out); + +memcpy (out, hash_out,min_t (int, out_len,sizeof (hash)));
[Severity: High]
Should we check the return values of the crypto operations inside this loop? If crypto_shash_init, crypto_shash_update, or crypto_shash_final fail, the hash_out buffer will retain its uninitialized kernel stack data. This data is then copied into the output buffer and eventually sent over USB, leading to an information leak.
Quoted 8 lines
drivers/input/joystick/gip/gip-security.c new file +8
-0+ out +=sizeof (hash); + out_len -=sizeof (hash); + +crypto_shash_digest (desc, hash,sizeof (hash), hash); + } + +return 0 ; +}
[ ... ]
Quoted 6 lines
drivers/input/joystick/gip/gip-security.c new file +6
-0+static int gip_security_compute_ecdh (u8 *pubkey_in,u8 *pubkey_out,int pubkey_len,u8 *secret_hash) +{ +struct crypto_kpp *tfm_ecdh; +struct crypto_shash *tfm_sha; +u8 *secret; +int err;
[ ... ]
drivers/input/joystick/gip/gip-security.c line 389
Quoted 10 lines
drivers/input/joystick/gip/gip-security.c new file +10
-0+ err =gip_security_ecdh_get_pubkey (tfm_ecdh, pubkey_out, pubkey_len); +if (err) + goto err_free_sha; + + err =gip_security_ecdh_get_secret (tfm_ecdh, pubkey_in, pubkey_len, + secret,GIP_SECURITY_ECDH_SECRET_LEN ); +if (err) + goto err_free_sha; + +crypto_shash_tfm_digest (tfm_sha, secret,GIP_SECURITY_ECDH_SECRET_LEN , secret_hash);
[Severity: Medium]
Could this leave secret_hash uninitialized if the digest fails? The return value is ignored, which means the caller might silently proceed to compute the master secret using uninitialized stack memory.
drivers/input/joystick/gip/gip-security.c line 396
Quoted 7 lines
drivers/input/joystick/gip/gip-security.c new file +7
-0+ +err_free_sha: +crypto_free_shash (tfm_sha); +err_free_ecdh: +crypto_free_kpp (tfm_ecdh); +err_free_secret: +kfree (secret);
[Severity: Medium]
Since the secret buffer holds raw cryptographic key material, should this use kfree_sensitive instead of kfree to prevent sensitive data from lingering in the slab allocator?
Quoted 12 lines
drivers/input/joystick/gip/gip-security.c new file +12
-0+ +return err; +} + +static int gip_security_send_message (struct gip_security *security, +enum gip_security_command_handshake cmd,void *message,u16 len) +{ + struct gip_attachment *attachment = container_of(security, struct gip_attachment, security); +struct gip_security_header_full *hdr = message; +u16 data_len = len -sizeof (hdr->handshake) -GIP_SECURITY_TRAILER_LEN ; + + hdr->handshake.context =GIP_SECURITY_CTX_HANDSHAKE ;
drivers/input/joystick/gip/gip-security.c line 418
Quoted 10 lines
drivers/input/joystick/gip/gip-security.c new file +10
-0+ hdr->handshake.options =GIP_SECURITY_OPT_ACKNOWLEDGE |GIP_SECURITY_OPT_FROM_HOST ; + hdr->handshake.command = cmd; + hdr->handshake.length =cpu_to_be16 (data_len); + + hdr->data.command = cmd; + hdr->data.version = cmd >=GIP_SECURITY2_CMD_HOST_HELLO ?0x02 :0x01 ; + hdr->data.length =cpu_to_be16 (data_len -sizeof (hdr->data)); + + security->last_sent_command = cmd; +crypto_shash_update (security->shash_transcript, message +sizeof (hdr->handshake), data_len);
[Severity: High]
Can this code execute concurrently with the message handler? The gip_security_send_message function is called from asynchronous workers, while gip_security_handle_message can also update the same shash_transcript descriptor. Since shash_desc is not thread-safe, concurrent modifications could corrupt the cryptographic transcript.
Quoted 3 lines
drivers/input/joystick/gip/gip-security.c new file +3
-0+ +return gip_send_system_message (attachment,GIP_CMD_SECURITY ,GIP_FLAG_ACME , message, len); +}
[ ... ]
Quoted 20 lines
drivers/input/joystick/gip/gip-security.c new file +20
-0+int gip_security_handle_message (struct gip_security *security,const void *bytes,int num_bytes) +{ +const struct gip_security_header_handshake *handshake = bytes; +const struct gip_security_header_full *hdr; +int err; + +if (num_bytes <sizeof (*handshake)) +return -EINVAL ; + +if (handshake->error) +return -EPROTO ; + +if (handshake->options &GIP_SECURITY_OPT_ACKNOWLEDGE ) { +if (handshake->command ==0x01 ) +return gip_security_handle_acknowledge (security); + +gip_err (security,"%s: handshake failed: 0x%02x\n" , + __func__, handshake->command); +return -EPROTO ; + }
drivers/input/joystick/gip/gip-security.c line 795
Quoted 10 lines
drivers/input/joystick/gip/gip-security.c new file +10
-0+ +if (num_bytes <sizeof (*hdr)) +return -EINVAL ; + + hdr = bytes; +/* client uses v2 */ +if (handshake->command != hdr->data.command) { +/* reset transcript hash and restart handshake */ +gip_dbg (security,"%s: protocol upgrade\n" , __func__); +crypto_shash_init (security->shash_transcript);
[Severity: Critical]
If an attacker-controlled USB device sends a security packet before the handshake is explicitly started, could security->shash_transcript be a null pointer here and cause a dereference? The cryptographic descriptors are initialized lazily in gip_security_start_handshake, so early packets routed to this handler might trigger a crash.
[Severity: Medium]
If we restart the handshake here, do we need to cancel any previously scheduled v1 workers? If a v1 worker like work_exchange_rsa is pending, it could execute concurrently, modify the state, and corrupt the new v2 handshake.
drivers/input/joystick/gip/gip-security.c line 806
Quoted 11 lines
drivers/input/joystick/gip/gip-security.c new file +11
-0+return gip_security2_send_hello (security); + } + + err =gip_security_dispatch_message (security, hdr->data.command, + bytes +sizeof (*hdr), num_bytes -sizeof (*hdr)); +if (err) +return err; + +return crypto_shash_update (security->shash_transcript, + bytes +sizeof (hdr->handshake), + num_bytes -sizeof (hdr->handshake));
[Severity: Critical]
Similar to the issue above, could security->shash_transcript be a null pointer here if a packet is received before the handshake was explicitly started, resulting in a null pointer dereference in crypto_shash_update?
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v4 07/12] Input: xbox_gip - Add security implementation from xone
In reply to Vicki PfauQuoted 13 lines · Vicki Pfau
drivers/input/joystick/gip/gip-security.c new file +13
-0+int gip_security_start_handshake (struct gip_security *security) +{ +struct shash_desc *shash_transcript, *shash_prf; + +if (!security->shash_transcript) { + shash_transcript =gip_security_alloc_shash ("sha256" ); +if (IS_ERR (shash_transcript)) +return PTR_ERR (shash_transcript); + + security->shash_transcript = shash_transcript; + } + +if (!security->shash_prf) {
drivers/input/joystick/gip/gip-security.c line 849
Quoted 10 lines
drivers/input/joystick/gip/gip-security.c new file +10
-0+ shash_prf =gip_security_alloc_shash ("hmac(sha256)" ); +if (IS_ERR (shash_prf)) { +crypto_free_shash (shash_transcript->tfm); +kfree (shash_transcript); + security->shash_transcript =NULL ; +return PTR_ERR (shash_prf); + } + + security->shash_prf = shash_prf; + }
There's no need to use crypto_shash. Just call the SHA-256 and HMAC-SHA256 functions (<crypto/sha2.h>) directly.
- Eric
Re: [PATCH v4 07/12] Input: xbox_gip - Add security implementation from xone
Hi Eric,
Quoted 19 lines · Eric Biggers
Quoted 18 lines · Vicki Pfau
drivers/input/joystick/gip/gip-security.c new file +18
-0+int gip_security_start_handshake (struct gip_security *security) +{ +struct shash_desc *shash_transcript, *shash_prf; + +if (!security->shash_transcript) { + shash_transcript =gip_security_alloc_shash ("sha256" ); +if (IS_ERR (shash_transcript)) +return PTR_ERR (shash_transcript); + + security->shash_transcript = shash_transcript; + } + +if (!security->shash_prf) { + shash_prf =gip_security_alloc_shash ("hmac(sha256)" ); +if (IS_ERR (shash_prf)) { +crypto_free_shash (shash_transcript->tfm); +kfree (shash_transcript); + security->shash_transcript =NULL ;
Quoted 8 lines
drivers/input/joystick/gip/gip-security.c line 849
Quoted 5 lines
drivers/input/joystick/gip/gip-security.c new file +5
-0+return PTR_ERR (shash_prf); + } + + security->shash_prf = shash_prf; + }There's no need to use
crypto_shash. Just call the SHA-256 and HMAC-SHA256 functions (<crypto/sha2.h>) directly.
This code was originally written by someone else more familiar with these functions than me, but it looks like shash_transcript gets finalized repeatedly without being reset. See gip_security_get_transcript, which exports the state, finalizes it, then rolls back the state before finalizing it. I don't think this can be done with the basic sha2 functions. I might be able to do it with the HMAC though. I'd need to familiarize myself with the API first.>
Quoted 1 line
- Eric
Vicki
Re: [PATCH v4 07/12] Input: xbox_gip - Add security implementation from xone
Quoted 10 lines · Vicki Pfau
Quoted 3 lines
There's no need to use
crypto_shash. Just call the SHA-256 and HMAC-SHA256 functions (<crypto/sha2.h>) directly.This code was originally written by someone else more familiar with these functions than me, but it looks like
shash_transcriptgets finalized repeatedly without being reset. Seegip_security_get_transcript, which exports the state, finalizes it, then rolls back the state before finalizing it. I don't think this can be done with the basic sha2 functions.
What do you mean? In the library the context is just a plain struct, so you can just make a copy of it if needed. Like this:
static void gip_security_get_transcript(const struct sha256_ctx *ctx,
u8 transcript[GIP_SECURITY_TRANSCRIPT_LEN])
{
struct sha256_ctx tmp = *ctx;
sha256_final(&tmp, transcript);
}- Eric
Re: [PATCH v4 07/12] Input: xbox_gip - Add security implementation from xone
Hi Eric,
Quoted 15 lines · Eric Biggers
Quoted 10 lines · Vicki Pfau
Quoted 3 lines
There's no need to use
crypto_shash. Just call the SHA-256 and HMAC-SHA256 functions (<crypto/sha2.h>) directly.This code was originally written by someone else more familiar with these functions than me, but it looks like
shash_transcriptgets finalized repeatedly without being reset. Seegip_security_get_transcript, which exports the state, finalizes it, then rolls back the state before finalizing it. I don't think this can be done with the basic sha2 functions.What do you mean? In the library the context is just a plain struct, so you can just make a copy of it if needed. Like this:
Quoted 8 lines
static void gip_security_get_transcript(const struct
sha256_ctx*ctx,
u8 transcript[GIP_SECURITY_TRANSCRIPT_LEN]){ struct sha256_ctx tmp = *ctx; sha256_final(&tmp, transcript); }
I probably should have thought of that. That does make sense. I'm still trying to make sense of this code to fix things sashiko reported.
Quoted 1 line
- Eric
Vicki
PATCH v4 8/12 Input: xbox_gip - Add arcade stick support
In reply to Vicki PfauThis adds support for the arcade stick device type. Currently I'm only aware of one Xbox controller that uses this device type, the Razer Atrox, so testing coverage is limited.
--- drivers/input/joystick/gip/Makefile | 2 + drivers/input/joystick/gip/gip-arcade-stick.c | 173 ++++++++++++++++++ drivers/input/joystick/gip/gip-core.c | 6 +- drivers/input/joystick/gip/gip.h | 4 +- 4 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 drivers/input/joystick/gip/gip-arcade-stick.c
drivers/input/joystick/gip/Makefile +2 -0
@@ -2,3 +2,5 @@ obj-$(CONFIG_JOYSTICK_XBOX_GIP) += xbox-gip.o # Base driver xbox-gip-y := gip-core.o gip-drivers.o gip-security.o +# Additional device support +xbox-gip-y += gip-arcade-stick.o
drivers/input/joystick/gip/gip-arcade-stick.c new file +173 -0
173 lines not on this page · raw message
drivers/input/joystick/gip/gip-core.c +4 -2
@@ -10,7 +10,8 @@* - Raw character device * - Wheel support * - Flight stick support -* - Arcade stick support +* - More arcade stick testing +* - Arcade stick extra buttons * - Split into driver-per-attachment GIP-as-a-bus approach drivers * * This driver is based on the Microsoft GIP spec at:
@@ -328,6 +329,7 @@ struct gip_direct_motor {static const struct gip_driver *base_drivers[] = { &gip_driver_navigation, &gip_driver_gamepad, + &gip_driver_arcade_stick,NULL /* Sentinel */ };
@@ -1554,7 +1556,7 @@ static int gip_handle_command_raw_report(struct gip_attachment *attachment,return 0 ; } -static int gip_setup_input_device (struct gip_attachment *attachment) +int gip_setup_input_device (struct gip_attachment *attachment) {struct input_dev *input;int rc;
drivers/input/joystick/gip/gip.h +2 -2
@@ -395,6 +395,8 @@ static inline struct device *gip_security_dev(struct gip_security *security) return gip_attachment_dev(container_of(security, struct gip_attachment, security)); } +int gip_setup_input_device (struct gip_attachment *attachment); +bool gip_supports_vendor_message (struct gip_attachment *attachment,uint8_t command,bool upstream); int gip_send_system_message(struct gip_attachment *attachment,
@@ -410,6 +412,4 @@ void gip_security_release(struct gip_security *security);extern const struct gip_driver gip_driver_navigation;extern const struct gip_driver gip_driver_gamepad;extern const struct gip_driver gip_driver_arcade_stick; -extern const struct gip_driver gip_driver_wheel; -extern const struct gip_driver gip_driver_flight_stick;#endif
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 08/12] Input: xbox_gip - Add arcade stick support
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [Medium] Memory leak of gip_arcade_stick_info on device disconnect because the core framework never invokes the .remove() callback.
- [Medium] Missing event synchronization when an input report is truncated (2 to 5 bytes), leaving spurious events stuck in the input subsystem buffer.
Pre-existing issues:
- [High] Use-After-Free of attachment->input if input_register_device fails.
- [High] Use-After-Free and DMA corruption in the teardown path due to failing to stop outbound URBs and the send_fragment work. --
--- Patch [8]: [PATCH v4 08/12] Input: xbox_gip - Add arcade stick support --- commit 0a0bb97c59622a30366ca74123a1a5db969d555d
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add arcade stick support
This adds support for the arcade stick device type. Currently I'm only aware of one Xbox controller that uses this device type, the Razer Atrox, so testing coverage is limited.
Quoted 6 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +0
-0@@ -0,0 +1,173 @@
[ ... ]
Quoted 8 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +8
-0+static int gip_arcade_stick_probe (struct gip_attachment *attachment) +{ +struct gip_arcade_stick_info *info =kzalloc (sizeof (*info),GFP_KERNEL ); + +if (!info) +return -ENOMEM ; + + attachment->quirks |=GIP_QUIRK_SWAP_LB_RB ;
drivers/input/joystick/gip/gip-arcade-stick.c line 50
Quoted 10 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +10
-0+ attachment->driver_data = info; + +return 0 ; +} + +static void gip_arcade_stick_remove (struct gip_attachment *attachment) +{ +kfree (attachment->driver_data); + attachment->driver_data =NULL ; +}
[Severity: Medium]
Will this leak gip_arcade_stick_info on device disconnect? Looking at the core framework in gip-core.c, it doesn't appear to invoke the .remove() callback of any attachment driver during device teardown (gip_disconnect), so the memory allocated here might never get freed.
[ ... ]
Quoted 7 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +7
-0+static int gip_handle_arcade_stick_report (struct gip_attachment *attachment, +struct input_dev *input,const uint8_t *bytes,int num_bytes) +{ +struct gip_arcade_stick_info *info = attachment->driver_data; +int rc; + +if (!info)
drivers/input/joystick/gip/gip-arcade-stick.c line 111
Quoted 10 lines
drivers/input/joystick/gip/gip-arcade-stick.c new file +10
-0+return -ENODEV ; + + rc = gip_driver_navigation.handle_input_report (attachment, input, bytes, num_bytes); +if (rc <0 ) +return rc; + +if (num_bytes <6 ) { +gip_dbg (attachment,"Discarding too-short input report\n" ); +return -EINVAL ; + }
[Severity: Medium]
Can this leave unsynchronized events in the input buffer if an input report is truncated (between 2 and 5 bytes)?
The base handler gip_driver_navigation.handle_input_report() may successfully process the first 2 bytes, report multiple EV_KEY events, and return 0. If num_bytes is then evaluated as < 6, returning -EINVAL here causes the core caller to skip input_sync().
[ ... ]
Quoted 4 lines
drivers/input/joystick/gip/gip-core.c +0
-0
[ ... ]
drivers/input/joystick/gip/gip-core.c static int gip_handle_command_raw_report(struct gip_attachment *attachment, · line 1560
Quoted 7 lines
drivers/input/joystick/gip/gip-core.c +1
-1@@ -1554,7 +1556,7 @@ static int gip_handle_command_raw_report(struct gip_attachment *attachment,return 0 ; } -static int gip_setup_input_device (struct gip_attachment *attachment) +int gip_setup_input_device (struct gip_attachment *attachment) {
[Severity: High]
This isn't a bug introduced by this patch, but can attachment->input become a dangling pointer if input_register_device() fails?
In gip_setup_input_device(), attachment->input is assigned the newly allocated input device:
rcu_assign_pointer(attachment->input, input);
rc = input_register_device(input);
if (rc)
goto err_free_device;
...
err_free_device:
input_free_device(input);
return rc;If registration fails, the error path frees the input device but doesn't set attachment->input back to NULL. Later disconnects or input packets could dereference the freed pointer.
drivers/input/joystick/gip/gip-core.c static int gip_handle_command_raw_report(struct gip_attachment *attachment, · line 1562
Quoted 2 lines
drivers/input/joystick/gip/gip-core.c +0
-0struct input_dev *input;int rc;
[Severity: High]
This is a pre-existing issue, but does the teardown path in gip_disconnect() cause a use-after-free or DMA corruption?
Looking at gip_disconnect() in drivers/input/joystick/gip/gip-core.c:
static void gip_disconnect(struct usb_interface *intf)
{
...
usb_kill_urb(gip->data.urb_in);
if (gip->audio.intf)
usb_kill_urb(gip->audio.urb_in);
gip_shutdown(gip);
spin_lock_irqsave(&gip->message_lock, flags);
gip_deinit_input(&gip->data);
gip_deinit_output(&gip->data);It calls usb_kill_urb() for inbound URBs, but misses outbound URBs (intf->out_queue[i].urb). It then calls gip_deinit_output(), which unconditionally frees the coherent DMA buffers and the URB structures. If an outbound URB is in flight, the USB host controller might perform DMA reads from the freed memory.
Additionally, gip_disconnect() doesn't seem to cancel the &intf->send_fragment work, meaning if an outbound URB completes, it could schedule the work to access the freed gip device structure.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v4 9/12 Input: xbox_gip - Add support for PDP guitar controllers
In reply to Vicki PfauThis patch adds support for the PDP guitar controllers, namely the Fender Jaguar and RiffMaster models. These use mostly the same protocol, but the RiffMaster appears t use a slightly newer variant.
--- drivers/input/joystick/gip/Makefile | 1 + drivers/input/joystick/gip/gip-core.c | 1 + drivers/input/joystick/gip/gip-pdp-jaguar.c | 129 ++++++++++++++++++++ drivers/input/joystick/gip/gip.h | 2 + 4 files changed, 133 insertions(+) create mode 100644 drivers/input/joystick/gip/gip-pdp-jaguar.c
drivers/input/joystick/gip/Makefile +1 -0
@@ -4,3 +4,4 @@ obj-$(CONFIG_JOYSTICK_XBOX_GIP) += xbox-gip.o xbox-gip-y := gip-core.o gip-drivers.o gip-security.o # Additional device support xbox-gip-y += gip-arcade-stick.o +xbox-gip-y += gip-pdp-jaguar.o
drivers/input/joystick/gip/gip-core.c +1 -0
@@ -330,6 +330,7 @@ static const struct gip_driver *base_drivers[] = { &gip_driver_navigation, &gip_driver_gamepad, &gip_driver_arcade_stick, + &gip_driver_pdp_jaguar, NULL /* Sentinel */ };
drivers/input/joystick/gip/gip-pdp-jaguar.c new file +129 -0
129 lines not on this page · raw message
drivers/input/joystick/gip/gip.h +2 -0
@@ -40,6 +40,7 @@#define GIP_PID_XBOX_WIRELESS 0x0b12#define GIP_PID_PDP_ROCK_CANDY 0x0246 +#define GIP_PID_PDP_RIFFMASTER 0x0248#define GIP_PID_BDA_XB1_CLASSIC 0x581a#define GIP_PID_BDA_XB1_FUSION_PRO 0x591a
@@ -412,4 +413,5 @@ void gip_security_release(struct gip_security *security);extern const struct gip_driver gip_driver_navigation;extern const struct gip_driver gip_driver_gamepad;extern const struct gip_driver gip_driver_arcade_stick; +extern const struct gip_driver gip_driver_pdp_jaguar;#endif
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 09/12] Input: xbox_gip - Add support for PDP guitar controllers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Dynamic protocol detection relies on payload contents, causing face buttons and the thumbstick click on the RiffMaster to trigger ghost fret inputs when no frets are held.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but there is a dangling RCU pointer and synchronous free of input_dev on registration failure. --
--- Patch [9]: [PATCH v4 09/12] Input: xbox_gip - Add support for PDP guitar controllers --- commit 6b8e97ecc5fa40007c8c5bf1a4aa892abddb06cf
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add support for PDP guitar controllers
This patch adds support for the PDP guitar controllers, namely the Fender Jaguar and RiffMaster models. These use mostly the same protocol, but the RiffMaster appears t use a slightly newer variant.
Quoted 4 lines
drivers/input/joystick/gip/gip-pdp-jaguar.c new file +0
-0
[ ... ]
Quoted 4 lines
drivers/input/joystick/gip/gip-pdp-jaguar.c new file +4
-0+static int gip_handle_pdp_jaguar_report (struct gip_attachment *attachment, +struct input_dev *input,const uint8_t *bytes,int num_bytes) +{ +bool lower;
drivers/input/joystick/gip/gip-pdp-jaguar.c line 67
Quoted 10 lines
drivers/input/joystick/gip/gip-pdp-jaguar.c new file +10
-0+ +if (num_bytes <4 ) { +gip_dbg (attachment,"Discarding too-short input report\n" ); +return -EINVAL ; + } + +input_report_key (input,BTN_START , bytes[0 ] &BIT (2 )); +input_report_key (input,BTN_SELECT , bytes[0 ] &BIT (3 )); + +if (num_bytes >=7 && (bytes[5 ] || bytes[6 ])) {
[Severity: High]
Does this condition cause ghost fret inputs on the RiffMaster when no frets are held?
If face buttons or the thumbstick click are pressed on the RiffMaster without pressing any frets, bytes[5] and bytes[6] would be zero. This causes the check to fall through to the legacy report logic, which seems like it would misinterpret the standard face button bits as legacy fret bits.
drivers/input/joystick/gip/gip-pdp-jaguar.c line 69
Quoted 2 lines
drivers/input/joystick/gip/gip-pdp-jaguar.c new file +2
-0+/* Newer report version on the RiffMaster */ +input_report_key (input,BTN_TRIGGER_HAPPY1 , bytes[5 ] &BIT (0 ));
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but is there a dangling RCU pointer and synchronous free if input_register_device() fails?
Looking at gip_setup_input_device() in drivers/input/joystick/gip/gip-core.c:
rcu_assign_pointer(attachment->input, input); rc = input_register_device(input); if (rc) goto err_free_device; ... err_free_device: input_free_device(input);
The input device pointer is published to RCU readers and subsequently freed synchronously on the error path without clearing the pointer or waiting for an RCU grace period. Can concurrent incoming USB packets process and dereference the dangling attachment->input pointer?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v4 10/12 Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER
In reply to Vicki PfauAdd new absolute axes for racing game controllers
--- drivers/hid/hid-debug.c | 16 +++++++++------- include/uapi/linux/input-event-codes.h | 3 +++ 2 files changed, 12 insertions(+), 7 deletions(-)
drivers/hid/hid-debug.c +9 -7
@@ -3511,13 +3511,15 @@ static const char *absolutes[ABS_CNT] = { [ABS_RY] = "Ry", [ABS_RZ] = "Rz", [ABS_THROTTLE] = "Throttle", [ABS_RUDDER] = "Rudder", [ABS_WHEEL] = "Wheel", [ABS_GAS] = "Gas", - [ABS_BRAKE] = "Brake", [ABS_HAT0X] = "Hat0X", - [ABS_HAT0Y] = "Hat0Y", [ABS_HAT1X] = "Hat1X", - [ABS_HAT1Y] = "Hat1Y", [ABS_HAT2X] = "Hat2X", - [ABS_HAT2Y] = "Hat2Y", [ABS_HAT3X] = "Hat3X", - [ABS_HAT3Y] = "Hat 3Y", [ABS_PRESSURE] = "Pressure", - [ABS_DISTANCE] = "Distance", [ABS_TILT_X] = "XTilt", - [ABS_TILT_Y] = "YTilt", [ABS_TOOL_WIDTH] = "ToolWidth", + [ABS_BRAKE] = "Brake", [ABS_CLUTCH] = "Clutch", + [ABS_HANDBRAKE] = "Handbrake", [ABS_SHIFTER] = "Shifter", + [ABS_HAT0X] = "Hat0X", [ABS_HAT0Y] = "Hat0Y", + [ABS_HAT1X] = "Hat1X", [ABS_HAT1Y] = "Hat1Y", + [ABS_HAT2X] = "Hat2X", [ABS_HAT2Y] = "Hat2Y", + [ABS_HAT3X] = "Hat3X", [ABS_HAT3Y] = "Hat3Y", + [ABS_PRESSURE] = "Pressure", [ABS_DISTANCE] = "Distance", + [ABS_TILT_X] = "XTilt", [ABS_TILT_Y] = "YTilt", + [ABS_TOOL_WIDTH] = "ToolWidth", [ABS_VOLUME] = "Volume", [ABS_PROFILE] = "Profile", [ABS_SND_PROFILE] = "SoundProfile", [ABS_MISC] = "Misc",
include/uapi/linux/input-event-codes.h +3 -0
@@ -879,6 +879,9 @@#define ABS_WHEEL 0x08#define ABS_GAS 0x09#define ABS_BRAKE 0x0a +#define ABS_CLUTCH 0x0b +#define ABS_HANDBRAKE 0x0c +#define ABS_SHIFTER 0x0d#define ABS_HAT0X 0x10#define ABS_HAT0Y 0x11#define ABS_HAT1X 0x12
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 10/12] Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `hid-universal-pidff.c` driver contains a hardcoded loop bound (`ABS_BRAKE`) for racing axis…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The hid-universal-pidff.c driver contains a hardcoded loop bound (ABS_BRAKE) for racing axis initialization, leaving the newly added racing axes without correct fuzz and deadzone configuration. --
--- Patch [10]: [PATCH v4 10/12] Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER --- commit 518c533c53885e9a0bf15581d9e84560c1f27740
Author: Vicki Pfau <[email protected]>
Input: Add ABS_CLUTCH, HANDBRAKE, and SHIFTER
This commit introduces new absolute axes (ABS_CLUTCH, ABS_HANDBRAKE, and ABS_SHIFTER) for racing game controllers. It updates the relevant definitions in input-event-codes.h and the debug string representations in the HID subsystem.
[ ... ]
include/uapi/linux/input-event-codes.h line 884
Quoted 11 lines
include/uapi/linux/input-event-codes.h +3
-0@@ -879,6 +879,9 @@#define ABS_WHEEL 0x08#define ABS_GAS 0x09#define ABS_BRAKE 0x0a +#define ABS_CLUTCH 0x0b +#define ABS_HANDBRAKE 0x0c +#define ABS_SHIFTER 0x0d
[Severity: Medium]
Will these newly added axes be missing fuzz and deadzone configuration in some drivers?
Looking at universal_pidff_input_configured() in drivers/hid/hid-universal-pidff.c, there is a hardcoded loop bound that explicitly terminates at ABS_BRAKE:
/* Decrease fuzz and deadzone on available axes */
for (axis = ABS_X; axis <= ABS_BRAKE; axis++) {Does this loop need to be updated to include ABS_CLUTCH, ABS_HANDBRAKE, and ABS_SHIFTER, since they logically belong in this racing axes group and might otherwise incorrectly retain default settings?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH v4 11/12 HID: Map more automobile simulation inputs
In reply to Vicki PfauThe HID usage tables section 5.3 specify clutch and shifter values that had previously been ignored. As the ABS_CLUTCH and ABS_SHIFTER bits now exist, we should use them appropriately.
--- drivers/hid/hid-input.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/hid/hid-input.c +2 -0
@@ -828,6 +828,8 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fielcase 0xbb :map_abs (ABS_THROTTLE );break ;case 0xc4 :map_abs (ABS_GAS );break ;case 0xc5 :map_abs (ABS_BRAKE );break ; +case 0xc6 :map_abs (ABS_CLUTCH );break ; +case 0xc7 :map_abs (ABS_SHIFTER );break ;case 0xc8 :map_abs (ABS_WHEEL );break ;default : goto ignore; }
Signature · 1 line
2.54.0
PATCH v4 12/12 Input: xbox_gip - Add wheel support
In reply to Vicki PfauThis adds preliminary support for racing wheel support in xbox_gip, exposing them mapped to the newly added axes.
--- drivers/input/joystick/gip/Makefile | 1 + drivers/input/joystick/gip/gip-core.c | 4 +- drivers/input/joystick/gip/gip-drivers.c | 10 + drivers/input/joystick/gip/gip-wheel.c | 349 +++++++++++++++++++++++ drivers/input/joystick/gip/gip.h | 6 + 5 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 drivers/input/joystick/gip/gip-wheel.c
drivers/input/joystick/gip/Makefile +1 -0
@@ -5,3 +5,4 @@ xbox-gip-y := gip-core.o gip-drivers.o gip-security.o # Additional device support xbox-gip-y += gip-arcade-stick.o xbox-gip-y += gip-pdp-jaguar.o +xbox-gip-y += gip-wheel.o
drivers/input/joystick/gip/gip-core.c +3 -1
@@ -8,7 +8,7 @@* - Audio device support * - Event logging * - Raw character device -* - Wheel support +* - Wheel force feedback * - Flight stick support * - More arcade stick testing * - Arcade stick extra buttons
@@ -331,6 +331,8 @@ static const struct gip_driver *base_drivers[] = { &gip_driver_gamepad, &gip_driver_arcade_stick, &gip_driver_pdp_jaguar, + &gip_driver_trueforce_wheel, + &gip_driver_wheel, NULL /* Sentinel */ };
drivers/input/joystick/gip/gip-drivers.c +10 -0
@@ -142,6 +142,11 @@ static int gip_setup_navigation_input(struct gip_attachment *attachment, structinput_set_capability (input,EV_KEY ,BTN_TR );input_set_capability (input,EV_KEY ,BTN_TL ); +if (attachment->quirks &GIP_QUIRK_FORCE_GAMEPAD_SB ) { +input_set_capability (input,EV_KEY ,BTN_THUMBR ); +input_set_capability (input,EV_KEY ,BTN_THUMBL ); + } + attachment->dpad_as_buttons = dpad_as_buttons;if (attachment->dpad_as_buttons) {input_set_capability (input,EV_KEY ,BTN_DPAD_UP );
@@ -193,6 +198,11 @@ static int gip_handle_navigation_report(struct gip_attachment *attachment,input_report_key (input,BTN_TR , bytes[1 ] &BIT (5 )); } +if (attachment->quirks &GIP_QUIRK_FORCE_GAMEPAD_SB ) { +input_report_key (input,BTN_THUMBL , bytes[1 ] &BIT (6 )); +input_report_key (input,BTN_THUMBR , bytes[1 ] &BIT (7 )); + } +return 0 ; }
drivers/input/joystick/gip/gip-wheel.c new file +349 -0
349 lines not on this page · raw message
drivers/input/joystick/gip/gip.h +6 -0
@@ -27,10 +27,13 @@#define MAX_OUT_MESSAGES 8#define MAX_OUT_FRAGMENTS 2 +#define GIP_VID_THRUSTMASTER 0x044f#define GIP_VID_MICROSOFT 0x045e#define GIP_VID_PDP 0x0e6f#define GIP_VID_BDA 0x24c6 +#define GIP_PID_THRUSTMASTER_T128X 0xb69c +#define GIP_PID_XBOX_ONE_1573 0x02d1#define GIP_PID_XBOX_ONE_1697 0x02dd#define GIP_PID_XBOX_ELITE 0x02e3
@@ -49,6 +52,7 @@#define GIP_QUIRK_NO_IMPULSE_VIBRATION BIT(1)#define GIP_QUIRK_SWAP_LB_RB BIT(2)#define GIP_QUIRK_SKIP_SECURITY BIT(3) +#define GIP_QUIRK_FORCE_GAMEPAD_SB BIT(4)/* * Driver-specific quirks should start from 31 and go downwards to avoid * conflicts with newly-added core quirks
@@ -414,4 +418,6 @@ extern const struct gip_driver gip_driver_navigation;extern const struct gip_driver gip_driver_gamepad;extern const struct gip_driver gip_driver_arcade_stick;extern const struct gip_driver gip_driver_pdp_jaguar; +extern const struct gip_driver gip_driver_wheel; +extern const struct gip_driver gip_driver_trueforce_wheel;#endif
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 12/12] Input: xbox_gip - Add wheel support
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
New issues:
- [High] Mismatched raw value offset and declared axis bounds for the ABS_WHEEL axis.
- [High] Brittle wraparound logic for the TrueForce wheel hardware rotary dial causes erratic backward spikes.
- [Medium] Packet length validation occurs after partially processing events, causing dropped input_sync() calls and stuck events.
Pre-existing issues:
- [High] Dangling pointer left in attachment->input when input_register_device() fails, leading to Use-After-Free on subsequent input reports.
- [High] AB-BA Deadlock between gip_receive_fragment and gip_fragment_timeout workqueue handler.
--
--- Patch [12]: [PATCH v4 12/12] Input: xbox_gip - Add wheel support --- commit fd5878269a3ac89add6a8a8b7336791b58d87703
Author: Vicki Pfau <[email protected]>
Input: xbox_gip - Add wheel support
This adds preliminary support for racing wheel support in xbox_gip, exposing them mapped to the newly added axes.
Quoted 5 lines
drivers/input/joystick/gip/gip-wheel.c new file +0
-0
[ ... ]
Quoted 5 lines
drivers/input/joystick/gip/gip-wheel.c new file +5
-0+static int gip_handle_wheel_report (struct gip_attachment *attachment, +struct input_dev *input,const uint8_t *bytes,int num_bytes) +{ +uint8_t connections; +struct gip_wheel_info *info = attachment->driver_data;
drivers/input/joystick/gip/gip-wheel.c line 168
Quoted 10 lines
drivers/input/joystick/gip/gip-wheel.c new file +10
-0+int rc = gip_driver_navigation.handle_input_report (attachment, input, bytes, num_bytes); + +if (rc <0 ) +return rc; + +if (!info) +return -ENODEV ; + +if (num_bytes <17 ) +return -EINVAL ;
[Severity: Medium]
If the packet length is less than 17, gip_driver_navigation.handle_input_report() has already processed the first 2 bytes and pushed button states to the input core buffer.
Returning -EINVAL here causes the caller to exit without calling input_sync(). Will this leave the parsed button events stuck in the input buffer, leading to desynchronized state in userspace?
Could we validate the total packet length before calling the base handler to modify device state?
drivers/input/joystick/gip/gip-wheel.c
Quoted 2 lines
drivers/input/joystick/gip/gip-wheel.c new file +2
-0+ +input_report_abs (input,ABS_WHEEL ,get_unaligned_le16 (&bytes[2 ]) -0x8000 );
[Severity: High]
In gip_handle_wheel_ll_static_configuration(), max_angle is calculated as 65535 for 16-bit precision, setting the ABS_WHEEL bounds to [-65536, 65535].
Here, the raw 16-bit wheel value (0-65535) is shifted by a hardcoded 0x8000 (32768), resulting in physical values ranging strictly from -32768 to 32767. Does this restrict the reported values to only cover 50% of the declared logical bounding box?
Additionally, if the device reports a precision other than 16 bits, will this hardcoded 0x8000 shift push the values completely outside the bounding box?
[ ... ]
Quoted 3 lines
drivers/input/joystick/gip/gip-wheel.c new file +3
-0+static int gip_handle_trueforce_wheel_report (struct gip_attachment *attachment, +struct input_dev *input,const uint8_t *bytes,int num_bytes) +{
[ ... ]
drivers/input/joystick/gip/gip-wheel.c line 328
Quoted 7 lines
drivers/input/joystick/gip/gip-wheel.c new file +7
-0+if (dial ==0 && state->dial ==7 ) +input_report_rel (input,REL_DIAL ,-1 ); +else if (dial ==7 && state->dial ==0 ) +input_report_rel (input,REL_DIAL ,1 ); +else +input_report_rel (input,REL_DIAL , + state->dial - dial);
[Severity: High]
If the user spins the hardware dial rapidly, the 3-bit value might jump by more than one step per report (e.g., from 7 to 1).
Will this bypass the single-step wrap checks and fall back to state->dial - dial (7 - 1 = 6), erroneously reporting a massive 6-step backward jump instead of the actual 2-step forward movement?
Can this be updated to use modular arithmetic (like sign-extending the 3-bit difference) instead of checking for exact neighbor boundaries?
[ ... ]
Quoted 3 lines
drivers/input/joystick/gip/gip-core.c +0
-0
[ ... ]
Quoted 3 lines
int
gip_setup_input_device(struct gip_attachment *attachment){
structinput_dev*input;
[ ... ]
Quoted 5 lines
drivers/input/joystick/gip/gip-core.c +0
-0input_set_drvdata (input, attachment);rcu_assign_pointer (attachment->input, input); rc =input_register_device (input);if (rc) goto err_free_device;
[ ... ]
drivers/input/joystick/gip/gip-core.c
Quoted 4 lines
err_free_device:drivers/input/joystick/gip/gip-core.c +0
-0input_free_device (input);return rc; }
[Severity: High]
This is a pre-existing issue, but in gip_setup_input_device(), the allocated input_dev is assigned to attachment->input via rcu_assign_pointer().
If input_register_device() fails, we jump to err_free_device and call input_free_device(input). Does this leave a dangling pointer in attachment->input that could cause a use-after-free when a subsequent input report is processed?
[ ... ]
Quoted 3 lines
static void gip_receive_fragment(struct
gip_attachment*attachment,
constuint8_t*bytes, intnum_bytes)
{
[ ... ]
Quoted 1 line
cancel_delayed_work_sync(&attachment->in_fragment_timeout);
[Severity: High]
This is a pre-existing issue, but I noticed a potential deadlock between gip_receive_fragment() and gip_fragment_timeout().
gip_receive_message() acquires attachment->lock before calling gip_receive_fragment(), which then calls cancel_delayed_work_sync(&attachment->in_fragment_timeout).
However, the delayed work handler, gip_fragment_timeout(), immediately attempts to acquire attachment->lock:
static void gip_fragment_timeout(struct work_struct *work)
{
...
guard(mutex)(&attachment->lock);
...
}If the delayed work has already started executing on another CPU, will cancel_delayed_work_sync() block forever waiting for the work to finish, while the work handler blocks forever waiting for attachment->lock to be released?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12