Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Derek J. Clark" <derekjohn.clark@gmail.com>
Cc: linux-input@vger.kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v7 3/4] HID: hid-msi: Add RGB control interface
Date: Wed, 20 May 2026 03:23:57 +0000	[thread overview]
Message-ID: <20260520032357.F0FB21F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260520013158.3633277-4-derekjohn.clark@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] A race condition in `claw_remove()` allows the `rgb_queue` delayed work to be re-armed after it is cancelled, leading to a Use-After-Free.
- [High] Failing to unregister the LED class device if `device_add_group()` fails leaves it registered but sets `rgb_registered = false`, causing list corruption upon retry.
--

commit 35b79b95f2f2fd70b7faaa568ae55b37643edb83
Author: Derek J. Clark <derekjohn.clark@gmail.com>

HID: hid-msi: Add RGB control interface
    
Adds RGB control interface for MSI Claw devices. The MSI Claw uses a
fairly unique RGB interface. It has 9 total zones (4 per joystick ring
and 1 for the ABXY buttons), and supports up to 8 sequential frames of
RGB zone data.

> diff --git a/drivers/hid/hid-msi.c b/drivers/hid/hid-msi.c
> index 193435ec9386c..61917902e38d3 100644
> --- a/drivers/hid/hid-msi.c
> +++ b/drivers/hid/hid-msi.c

[ ... ]

> @@ -871,31 +960,521 @@ static const struct attribute_group claw_gamepad_attr_group = {

[ ... ]

> +try_rgb:
> +	/* Add and enable RGB interface once we have the device state */
> +	/* Pairs with smp_store_release from below */
> +	if (!smp_load_acquire(&drvdata->rgb_registered) && rgb_ready) {
> +		ret = devm_led_classdev_multicolor_register(&drvdata->hdev->dev, &drvdata->led_mc);
> +		if (ret) {
> +			dev_err(&drvdata->hdev->dev,
> +				"Failed to create led device: %d\n", ret);
> +			goto update_kobjects;
> +		}
> +
> +		ret = device_add_group(drvdata->led_mc.led_cdev.dev, &claw_rgb_attr_group);
> +		if (ret) {
> +			dev_err(&drvdata->hdev->dev,
> +				"Failed to create RGB attrs: %d\n", ret);
> +			goto update_kobjects;
> +		}

[Severity: High]
If device_add_group() fails here, we jump to update_kobjects without rolling
back the devm_led_classdev_multicolor_register() call, and rgb_registered
remains false.

On a subsequent system resume, cfg_resume_fn() will see rgb_registered is
false and schedule cfg_setup_fn() again:

    if (!smp_load_acquire(&drvdata->rgb_registered))
        schedule_delayed_work(&drvdata->cfg_setup, msecs_to_jiffies(500));

Will the second call to devm_led_classdev_multicolor_register() with the exact
same drvdata->led_mc structure corrupt the leds_list, since led_cdev->node
is already in the list from the first call?

> +
> +		/* Pairs with smp_load_acquire in attribute show/store functions */
> +		smp_store_release(&drvdata->rgb_registered, true);
> +	}

[ ... ]

> @@ -1021,11 +1622,15 @@ static void claw_remove(struct hid_device *hdev)
>  		return;
>  	}
>  
> +	/* Block writes to brightness/multi_intensity during teardown */
>  	cancel_delayed_work_sync(&drvdata->cfg_resume);
>  	cancel_delayed_work_sync(&drvdata->cfg_setup);
> +	cancel_delayed_work_sync(&drvdata->rgb_queue);
>  
>  	/* Pairs with smp_load_acquire in attribute show/store functions */
>  	smp_store_release(&drvdata->gamepad_registered, false);
> +	/* Pairs with smp_load_acquire in attribute show/store functions */
> +	smp_store_release(&drvdata->rgb_registered, false);

[Severity: High]
Could setting rgb_registered to false after cancelling the delayed work
cause a use-after-free here?

Because sysfs attributes are managed by devres, they remain active until
claw_remove() returns. A concurrent sysfs write could check rgb_registered
and then get preempted:

    /* in effect_store() */
    if (!smp_load_acquire(&drvdata->rgb_registered))
        return -ENODEV;
    /* Preempted here */

Meanwhile, claw_remove() runs:

    cancel_delayed_work_sync(&drvdata->rgb_queue);
    smp_store_release(&drvdata->rgb_registered, false);

Then the sysfs writer resumes and queues the work again:

    mod_delayed_work(system_wq, &drvdata->rgb_queue, msecs_to_jiffies(50));

After claw_remove() returns, devres will free drvdata. When the queued work
eventually executes claw_rgb_queue_fn(), will it access the freed drvdata?

Would clearing rgb_registered before calling cancel_delayed_work_sync()
prevent the work from being re-queued?

>  
>  	hid_hw_close(hdev);
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260520013158.3633277-1-derekjohn.clark@gmail.com?part=3

  reply	other threads:[~2026-05-20  3:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20  1:31 [PATCH v7 0/4] Add MSI Claw HID Configuration Driver Derek J. Clark
2026-05-20  1:31 ` [PATCH v7 1/4] HID: hid-msi: Add MSI Claw configuration driver Derek J. Clark
2026-05-20  2:08   ` sashiko-bot
2026-05-20  1:31 ` [PATCH v7 2/4] HID: hid-msi: Add M-key mapping attributes Derek J. Clark
2026-05-20  2:51   ` sashiko-bot
2026-05-20  1:31 ` [PATCH v7 3/4] HID: hid-msi: Add RGB control interface Derek J. Clark
2026-05-20  3:23   ` sashiko-bot [this message]
2026-05-20  1:31 ` [PATCH v7 4/4] HID: hid-msi: Add Rumble Intensity Attributes Derek J. Clark
2026-05-20  3:58   ` 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=20260520032357.F0FB21F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=derekjohn.clark@gmail.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --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