All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fan Wu" <fanwu01@zju.edu.cn>
Cc: dmitry.torokhov@gmail.com, linux-input@vger.kernel.org
Subject: Re: [PATCH] HID: playstation: Fix DualShock4 dongle hotplug work use-after-free
Date: Wed, 23 Sep 2026 03:41:00 +0000	[thread overview]
Message-ID: <20260923034100.92D691F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260923032634.417433-1-fanwu01@zju.edu.cn>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hid: redundant synchronization due to false-positive race
--

commit 13619e48822a1be542eef83182d65e856c6ee40b
Author: Fan Wu <fanwu01@zju.edu.cn>

HID: playstation: Fix DualShock4 dongle hotplug work use-after-free
    
This commit attempts to fix a use-after-free issue by adding synchronization
state (dongle_hotplug_worker_initialized) and clearing it during error and
teardown paths. It claims this prevents the raw_event handler from scheduling
work concurrently during probe() and remove().

> HID: playstation: Fix DualShock4 dongle hotplug work use-after-free
>
> dualshock4_remove() cancels ds4->dongle_hotplug_worker while input
> reports can still arrive: hid_hw_close() and hid_hw_stop() only run
> later in ps_remove(), so a dongle connect report in that window makes
> dualshock4_dongle_parse_report() schedule the just-cancelled work
> again. ds4 is devm-managed and freed once ps_remove() returns, so
> dualshock4_dongle_calibration_work() then runs on freed memory.
>
> The dualshock4_create() error path has the same exposure. Reports are
> already flowing when it runs, because ps_probe() starts and opens the
> device before creating it, so the connect branch of the dongle report
> handler can also queue ds4->output_worker through its lightbar update.

Is this race actually possible?

Looking at the HID core, __hid_input_report() uses down_trylock() on
hdev->driver_input_lock and drops incoming reports when the lock is held.

During initialization, hid_device_probe() acquires this exact lock before
calling the driver's probe function (ps_probe()). The driver never calls
hid_device_io_start(), meaning this lock is held for the entire duration
of ps_probe(), including all of dualshock4_create().

Similarly, during teardown, hid_device_remove() acquires this lock before
calling the driver's remove function (ps_remove()), meaning it is held
for the entire duration of dualshock4_remove().

Since dualshock4_dongle_parse_report() is called from the raw_event
handler, which is fed by __hid_input_report(), it cannot execute concurrently
with either dualshock4_create() or dualshock4_remove().

> diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c
> index f9dc9378cf777..1b7a18fbeeb4b 100644
> --- a/drivers/hid/hid-playstation.c
> +++ b/drivers/hid/hid-playstation.c
[ ... ]
> @@ -2618,10 +2619,12 @@ static int dualshock4_dongle_parse_report(struct ps_device *ps_dev, struct hid_r
>  
>  			dualshock4_set_default_lightbar_colors(ds4);
>  
> -			scoped_guard(spinlock_irqsave, &ps_dev->lock)
> +			scoped_guard(spinlock_irqsave, &ps_dev->lock) {
>  				ds4->dongle_state = DONGLE_CALIBRATING;
>  
> -			schedule_work(&ds4->dongle_hotplug_worker);
> +				if (ds4->dongle_hotplug_worker_initialized)
> +					schedule_work(&ds4->dongle_hotplug_worker);
> +			}

[Severity: Medium]
Does this flag check actually do anything in dualshock4_dongle_parse_report(),
given that this function cannot run concurrently with the teardown sequence?

>  			/* Don't process the report since we don't have
>  			 * calibration data, but let hidraw have it anyway.
[ ... ]
> @@ -2677,8 +2680,12 @@ static void dualshock4_remove(struct ps_device *ps_dev)
>  
>  	cancel_work_sync(&ds4->output_worker);
>  
> -	if (ps_dev->hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE)
> +	if (ps_dev->hdev->product == USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE) {
> +		scoped_guard(spinlock_irqsave, &ds4->base.lock)
> +			ds4->dongle_hotplug_worker_initialized = false;
> +
>  		cancel_work_sync(&ds4->dongle_hotplug_worker);
> +	}
>  }

[Severity: Medium]
Does clearing this flag in dualshock4_remove() prevent a race, or is the
incoming report path already physically blocked by hdev->driver_input_lock?

[ ... ]
> @@ -2858,6 +2868,18 @@ static struct ps_device *dualshock4_create(struct hid_device *hdev)
>  
>  err:
>  	ps_devices_list_remove(ps_dev);
> +err_cancel:
> +	scoped_guard(spinlock_irqsave, &ps_dev->lock)
> +		ds4->output_worker_initialized = false;
> +
> +	cancel_work_sync(&ds4->output_worker);
> +
> +	if (ds4->dongle_hotplug_worker_initialized) {
> +		scoped_guard(spinlock_irqsave, &ps_dev->lock)
> +			ds4->dongle_hotplug_worker_initialized = false;
> +
> +		cancel_work_sync(&ds4->dongle_hotplug_worker);
> +	}
>  	return ERR_PTR(ret);
>  }

[Severity: Medium]
Since hdev->driver_input_lock is held throughout dualshock4_create(), are
these flag checks and locks on the error path protecting against a race that
is impossible to hit?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260923032634.417433-1-fanwu01@zju.edu.cn?part=1

  reply	other threads:[~2026-09-23  3:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-23  3:26 [PATCH] HID: playstation: Fix DualShock4 dongle hotplug work use-after-free Fan Wu
2026-09-23  3:41 ` sashiko-bot [this message]
2026-09-23  6:58 ` Fan Wu

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=20260923034100.92D691F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=fanwu01@zju.edu.cn \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.