From: sashiko-bot@kernel.org
To: "Rafael Passos" <rafael@rcpassos.me>
Cc: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org
Subject: Re: [PATCH v2 1/4] HID: wiimote: turn on the LEDs indicating the controller id
Date: Wed, 15 Jul 2026 21:48:49 +0000 [thread overview]
Message-ID: <20260715214849.CE8821F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260715212501.3920033-2-rafael@rcpassos.me>
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Integer truncation of IDA allocation results in out-of-bounds read, IDA resource leak, and kernel WARN.
- [Medium] Race condition causing use-before-initialization of `player_id`, leading to an out-of-bounds read.
- [Medium] Mixing `goto`-based error handling and `scoped_guard()` in the same function violates kernel cleanup subsystem rules.
--
--- Patch [1]: [PATCH v2 1/4] HID: wiimote: turn on the LEDs indicating the controller id ---
commit 49992cb763819a5276c65ada6594868205443ccd
Author: Rafael Passos <rafael@rcpassos.me>
HID: wiimote: turn on the LEDs indicating the controller id
The behavior in a Wii/Wii U console is to have each controller turn on
a different LED indicating the controller id.
This commit implements the same behavior using the ida struct.
Unlike switch controllers, each ID only turns one LED (from 1 to 4).
> diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c
> index 63c4fa8fbb9b6..91c2cec35f6eb 100644
> --- a/drivers/hid/hid-wiimote-core.c
> +++ b/drivers/hid/hid-wiimote-core.c
[ ... ]
> @@ -671,6 +678,12 @@ static void wiimote_modules_load(struct wiimote_data *wdata,
> spin_lock_irq(&wdata->state.lock);
> wdata->state.devtype = devtype;
> spin_unlock_irq(&wdata->state.lock);
> +
> + /* after loading all modules, set the LED for the player ID cycling from 1 to 4*/
> + scoped_guard(spinlock_irqsave, &wdata->state.lock) {
> + wiiproto_req_leds(wdata, player_leds[(wdata->player_id - 1) % 4]);
> + }
> +
> return;
>
> error:
[Severity: Medium]
Does using scoped_guard() in this function violate the cleanup subsystem
guidelines?
The wiimote_modules_load() function appears to use a goto-based failure path
(goto error;), but introduces a scoped_guard() scope-based cleanup macro.
The cleanup subsystem documentation indicates that mixing goto-based error
handling and scoped_guard() in the same function can create confusing
ownership semantics and should be avoided.
[ ... ]
> @@ -1834,7 +1851,16 @@ static int wiimote_hid_probe(struct hid_device *hdev,
> if (ret)
> goto err_free;
>
> - hid_info(hdev, "New device registered\n");
> + player_id = ida_alloc_min(&wiimote_ida, 1, GFP_KERNEL);
> + if (player_id < 1) {
> + hid_err(hdev, "cannot allocate controller id\n");
> + ret = player_id;
> + goto err_free;
> + }
> +
> + wdata->player_id = player_id;
[Severity: High]
Can this assignment truncate the allocated ID?
The player_id variable returned by ida_alloc_min() is an int, but
wdata->player_id is defined as a __u8 in struct wiimote_data.
If enough devices are created (e.g., via /dev/uhid) to allocate ID 256,
the value would truncate to 0.
When this 0 is later evaluated in wiimote_modules_load():
player_leds[(wdata->player_id - 1) % 4]
The expression (0 - 1) % 4 evaluates to -1, which would lead to an
out-of-bounds array read on player_leds[-1].
Additionally, during device removal in wiimote_hid_remove(), calling
ida_free(&wiimote_ida, wdata->player_id) with the truncated 0 would
trigger a WARN_ON_ONCE in the IDA core (since ID 0 was never allocated)
and permanently leak the originally allocated ID.
[Severity: Medium]
Is there a race condition where wdata->player_id is used before it is
initialized?
The hardware I/O is started and opened via hid_hw_open() earlier in
wiimote_hid_probe(), before the player_id is allocated and assigned here.
If the device immediately sends an event, could a background worker
executing wiimote_modules_load() read wdata->player_id while it is still 0?
This would cause the same out-of-bounds read on player_leds[-1] described
above.
[ ... ]
> diff --git a/drivers/hid/hid-wiimote.h b/drivers/hid/hid-wiimote.h
> index 9c12f63f6dd2d..a53f72d5077ef 100644
> --- a/drivers/hid/hid-wiimote.h
> +++ b/drivers/hid/hid-wiimote.h
[ ... ]
> @@ -153,6 +153,7 @@ struct wiimote_data {
> struct input_dev *mp;
> struct timer_list timer;
> struct wiimote_debug *debug;
> + __u8 player_id;
>
> union {
> struct input_dev *input;
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260715212501.3920033-1-rafael@rcpassos.me?part=1
next prev parent reply other threads:[~2026-07-15 21:48 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-15 21:24 [PATCH v2 0/4] HID: wiimote: new LED behavior on connect, scoped guards, uaf Rafael Passos
2026-07-15 21:24 ` [PATCH v2 1/4] HID: wiimote: turn on the LEDs indicating the controller id Rafael Passos
2026-07-15 21:48 ` sashiko-bot [this message]
2026-07-15 21:24 ` [PATCH v2 2/4] HID: wiimote: replace spinlock pairs with scoped_guard Rafael Passos
2026-07-15 21:39 ` sashiko-bot
2026-07-15 21:24 ` [PATCH v2 3/4] HID: wiimote: use scoped cleanup in wiimote and led probes Rafael Passos
2026-07-15 21:35 ` [TEST] Patch used for testing the wiimote_probe_cleanup Rafael Passos
2026-07-15 21:42 ` [PATCH v2 3/4] HID: wiimote: use scoped cleanup in wiimote and led probes sashiko-bot
2026-07-15 21:24 ` [PATCH v2 4/4] HID: wiimote: fix uaf when hid events are handled during destroy Rafael Passos
2026-07-15 21:57 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260715214849.CE8821F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-input@vger.kernel.org \
--cc=rafael@rcpassos.me \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox