mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] HID: amd_sfh: clear hid_sensor_hubs entry on probe failure
@ 2026-07-15  9:46 Chen Changcheng
  2026-07-17 10:37 ` Basavaraj Natikar
  0 siblings, 1 reply; 2+ messages in thread
From: Chen Changcheng @ 2026-07-15  9:46 UTC (permalink / raw)
  To: basavaraj.natikar, jikos, bentiss
  Cc: linux-input, linux-kernel, Chen Changcheng

In amdtp_hid_probe(), the newly allocated HID device is stored in
cli_data->hid_sensor_hubs[cur_hid_dev] before calling hid_add_device().
If hid_add_device() fails, the error path frees the HID device and its
driver_data but does not clear the array entry, leaving a dangling
pointer.

When the caller (amd_sfh_hid_client_init or
amd_sfh1_1_hid_client_init) detects the probe failure, it jumps to its
cleanup label, which unconditionally calls amd_sfh_hid_client_deinit()
and subsequently amdtp_hid_remove(). The latter iterates over all
hid_sensor_hubs[] entries and, upon encountering the non-NULL but freed
pointer, performs a use-after-free read followed by double-free of both
the HID device and its driver_data.

Clear the array entry in the error path of amdtp_hid_probe() so that
amdtp_hid_remove() skips the failed entry.

Signed-off-by: Chen Changcheng <chenchangcheng@kylinos.cn>
---
 drivers/hid/amd-sfh-hid/amd_sfh_hid.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_hid.c b/drivers/hid/amd-sfh-hid/amd_sfh_hid.c
index b04f675d49b0..9eaa2785a9ac 100644
--- a/drivers/hid/amd-sfh-hid/amd_sfh_hid.c
+++ b/drivers/hid/amd-sfh-hid/amd_sfh_hid.c
@@ -162,6 +162,7 @@ int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
 	return 0;
 
 err_hid_device:
+	cli_data->hid_sensor_hubs[cur_hid_dev] = NULL;
 	kfree(hid_data);
 err_hid_data:
 	hid_destroy_device(hid);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] HID: amd_sfh: clear hid_sensor_hubs entry on probe failure
  2026-07-15  9:46 [PATCH] HID: amd_sfh: clear hid_sensor_hubs entry on probe failure Chen Changcheng
@ 2026-07-17 10:37 ` Basavaraj Natikar
  0 siblings, 0 replies; 2+ messages in thread
From: Basavaraj Natikar @ 2026-07-17 10:37 UTC (permalink / raw)
  To: Chen Changcheng, basavaraj.natikar, jikos, bentiss
  Cc: linux-input, linux-kernel


On 7/15/2026 3:16 PM, Chen Changcheng wrote:
> [You don't often get email from chenchangcheng@kylinos.cn. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> In amdtp_hid_probe(), the newly allocated HID device is stored in
> cli_data->hid_sensor_hubs[cur_hid_dev] before calling hid_add_device().
> If hid_add_device() fails, the error path frees the HID device and its
> driver_data but does not clear the array entry, leaving a dangling
> pointer.
>
> When the caller (amd_sfh_hid_client_init or
> amd_sfh1_1_hid_client_init) detects the probe failure, it jumps to its
> cleanup label, which unconditionally calls amd_sfh_hid_client_deinit()
> and subsequently amdtp_hid_remove(). The latter iterates over all
> hid_sensor_hubs[] entries and, upon encountering the non-NULL but freed
> pointer, performs a use-after-free read followed by double-free of both
> the HID device and its driver_data.
>
> Clear the array entry in the error path of amdtp_hid_probe() so that
> amdtp_hid_remove() skips the failed entry.
>
> Signed-off-by: Chen Changcheng <chenchangcheng@kylinos.cn>
> ---
>   drivers/hid/amd-sfh-hid/amd_sfh_hid.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/drivers/hid/amd-sfh-hid/amd_sfh_hid.c b/drivers/hid/amd-sfh-hid/amd_sfh_hid.c
> index b04f675d49b0..9eaa2785a9ac 100644
> --- a/drivers/hid/amd-sfh-hid/amd_sfh_hid.c
> +++ b/drivers/hid/amd-sfh-hid/amd_sfh_hid.c
> @@ -162,6 +162,7 @@ int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
>          return 0;
>
>   err_hid_device:
> +       cli_data->hid_sensor_hubs[cur_hid_dev] = NULL;

Acked-by: Basavaraj Natikar <Basavaraj.Natikar@amd.com>

Thanks,
--
Basavaraj

>          kfree(hid_data);
>   err_hid_data:
>          hid_destroy_device(hid);
> --
> 2.25.1
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-07-17 10:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-15  9:46 [PATCH] HID: amd_sfh: clear hid_sensor_hubs entry on probe failure Chen Changcheng
2026-07-17 10:37 ` Basavaraj Natikar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox