linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function
@ 2023-02-27  2:49 Reka Norman
  2023-02-27  5:18 ` srinivas pandruvada
  2023-03-03 14:06 ` Jiri Kosina
  0 siblings, 2 replies; 3+ messages in thread
From: Reka Norman @ 2023-02-27  2:49 UTC (permalink / raw)
  To: Srinivas Pandruvada
  Cc: Reka Norman, Benjamin Tissoires, Jiri Kosina, Li Qiong,
	linux-input, linux-kernel

When a reset notify IPC message is received, the ISR schedules a work
function and passes the ISHTP device to it via a global pointer
ishtp_dev. If ish_probe() fails, the devm-managed device resources
including ishtp_dev are freed, but the work is not cancelled, causing a
use-after-free when the work function tries to access ishtp_dev. Use
devm_work_autocancel() instead, so that the work is automatically
cancelled if probe fails.

Signed-off-by: Reka Norman <rekanorman@chromium.org>
---

 drivers/hid/intel-ish-hid/ipc/ipc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/intel-ish-hid/ipc/ipc.c b/drivers/hid/intel-ish-hid/ipc/ipc.c
index 15e14239af829..a49c6affd7c4c 100644
--- a/drivers/hid/intel-ish-hid/ipc/ipc.c
+++ b/drivers/hid/intel-ish-hid/ipc/ipc.c
@@ -5,6 +5,7 @@
  * Copyright (c) 2014-2016, Intel Corporation.
  */
 
+#include <linux/devm-helpers.h>
 #include <linux/sched.h>
 #include <linux/spinlock.h>
 #include <linux/delay.h>
@@ -621,7 +622,6 @@ static void	recv_ipc(struct ishtp_device *dev, uint32_t doorbell_val)
 	case MNG_RESET_NOTIFY:
 		if (!ishtp_dev) {
 			ishtp_dev = dev;
-			INIT_WORK(&fw_reset_work, fw_reset_work_fn);
 		}
 		schedule_work(&fw_reset_work);
 		break;
@@ -940,6 +940,7 @@ struct ishtp_device *ish_dev_init(struct pci_dev *pdev)
 {
 	struct ishtp_device *dev;
 	int	i;
+	int	ret;
 
 	dev = devm_kzalloc(&pdev->dev,
 			   sizeof(struct ishtp_device) + sizeof(struct ish_hw),
@@ -975,6 +976,12 @@ struct ishtp_device *ish_dev_init(struct pci_dev *pdev)
 		list_add_tail(&tx_buf->link, &dev->wr_free_list);
 	}
 
+	ret = devm_work_autocancel(&pdev->dev, &fw_reset_work, fw_reset_work_fn);
+	if (ret) {
+		dev_err(dev->devc, "Failed to initialise FW reset work\n");
+		return NULL;
+	}
+
 	dev->ops = &ish_hw_ops;
 	dev->devc = &pdev->dev;
 	dev->mtu = IPC_PAYLOAD_SIZE - sizeof(struct ishtp_msg_hdr);
-- 
2.39.2.637.g21b0678d19-goog


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

* Re: [PATCH] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function
  2023-02-27  2:49 [PATCH] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function Reka Norman
@ 2023-02-27  5:18 ` srinivas pandruvada
  2023-03-03 14:06 ` Jiri Kosina
  1 sibling, 0 replies; 3+ messages in thread
From: srinivas pandruvada @ 2023-02-27  5:18 UTC (permalink / raw)
  To: Reka Norman
  Cc: Benjamin Tissoires, Jiri Kosina, Li Qiong, linux-input, linux-kernel

On Mon, 2023-02-27 at 13:49 +1100, Reka Norman wrote:
> When a reset notify IPC message is received, the ISR schedules a work
> function and passes the ISHTP device to it via a global pointer
> ishtp_dev. If ish_probe() fails, the devm-managed device resources
> including ishtp_dev are freed, but the work is not cancelled, causing
> a
> use-after-free when the work function tries to access ishtp_dev. Use
> devm_work_autocancel() instead, so that the work is automatically
> cancelled if probe fails.
> 
> Signed-off-by: Reka Norman <rekanorman@chromium.org>
Acked-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>

> ---
> 
>  drivers/hid/intel-ish-hid/ipc/ipc.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hid/intel-ish-hid/ipc/ipc.c b/drivers/hid/intel-
> ish-hid/ipc/ipc.c
> index 15e14239af829..a49c6affd7c4c 100644
> --- a/drivers/hid/intel-ish-hid/ipc/ipc.c
> +++ b/drivers/hid/intel-ish-hid/ipc/ipc.c
> @@ -5,6 +5,7 @@
>   * Copyright (c) 2014-2016, Intel Corporation.
>   */
>  
> +#include <linux/devm-helpers.h>
>  #include <linux/sched.h>
>  #include <linux/spinlock.h>
>  #include <linux/delay.h>
> @@ -621,7 +622,6 @@ static void recv_ipc(struct ishtp_device *dev,
> uint32_t doorbell_val)
>         case MNG_RESET_NOTIFY:
>                 if (!ishtp_dev) {
>                         ishtp_dev = dev;
> -                       INIT_WORK(&fw_reset_work, fw_reset_work_fn);
>                 }
>                 schedule_work(&fw_reset_work);
>                 break;
> @@ -940,6 +940,7 @@ struct ishtp_device *ish_dev_init(struct pci_dev
> *pdev)
>  {
>         struct ishtp_device *dev;
>         int     i;
> +       int     ret;
>  
>         dev = devm_kzalloc(&pdev->dev,
>                            sizeof(struct ishtp_device) +
> sizeof(struct ish_hw),
> @@ -975,6 +976,12 @@ struct ishtp_device *ish_dev_init(struct pci_dev
> *pdev)
>                 list_add_tail(&tx_buf->link, &dev->wr_free_list);
>         }
>  
> +       ret = devm_work_autocancel(&pdev->dev, &fw_reset_work,
> fw_reset_work_fn);
> +       if (ret) {
> +               dev_err(dev->devc, "Failed to initialise FW reset
> work\n");
> +               return NULL;
> +       }
> +
>         dev->ops = &ish_hw_ops;
>         dev->devc = &pdev->dev;
>         dev->mtu = IPC_PAYLOAD_SIZE - sizeof(struct ishtp_msg_hdr);


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

* Re: [PATCH] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function
  2023-02-27  2:49 [PATCH] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function Reka Norman
  2023-02-27  5:18 ` srinivas pandruvada
@ 2023-03-03 14:06 ` Jiri Kosina
  1 sibling, 0 replies; 3+ messages in thread
From: Jiri Kosina @ 2023-03-03 14:06 UTC (permalink / raw)
  To: Reka Norman
  Cc: Srinivas Pandruvada, Benjamin Tissoires, Li Qiong, linux-input,
	linux-kernel

On Mon, 27 Feb 2023, Reka Norman wrote:

> When a reset notify IPC message is received, the ISR schedules a work
> function and passes the ISHTP device to it via a global pointer
> ishtp_dev. If ish_probe() fails, the devm-managed device resources
> including ishtp_dev are freed, but the work is not cancelled, causing a
> use-after-free when the work function tries to access ishtp_dev. Use
> devm_work_autocancel() instead, so that the work is automatically
> cancelled if probe fails.
> 
> Signed-off-by: Reka Norman <rekanorman@chromium.org>

Applied, thank you.

-- 
Jiri Kosina
SUSE Labs


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

end of thread, other threads:[~2023-03-03 14:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-27  2:49 [PATCH] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function Reka Norman
2023-02-27  5:18 ` srinivas pandruvada
2023-03-03 14:06 ` Jiri Kosina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).