linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] thunderbolt: Fix use-after-free in remove_unplugged_switch()
@ 2020-11-19  8:34 Mika Westerberg
  2020-11-19  8:41 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Mika Westerberg @ 2020-11-19  8:34 UTC (permalink / raw)
  To: linux-usb
  Cc: Yehezkel Bernat, Michael Jamet, Paulian Bogdan Marinca,
	Andreas Noever, Lukas Wunner, Greg Kroah-Hartman,
	Mika Westerberg

Paulian reported a crash that happens when a dock is unplugged during
hibernation:

[78436.228217] thunderbolt 0-1: device disconnected
[78436.228365] BUG: kernel NULL pointer dereference, address: 00000000000001e0
...
[78436.228397] RIP: 0010:icm_free_unplugged_children+0x109/0x1a0
...
[78436.228432] Call Trace:
[78436.228439]  icm_rescan_work+0x24/0x30
[78436.228444]  process_one_work+0x1a3/0x3a0
[78436.228449]  worker_thread+0x30/0x370
[78436.228454]  ? process_one_work+0x3a0/0x3a0
[78436.228457]  kthread+0x13d/0x160
[78436.228461]  ? kthread_park+0x90/0x90
[78436.228465]  ret_from_fork+0x1f/0x30

This happens because remove_unplugged_switch() calls tb_switch_remove()
that releases the memory pointed by sw so the following lines reference
to a memory that might be released already.

Fix this by saving pointer to the parent device before calling
tb_switch_remove().

Reported-by: Paulian Bogdan Marinca <paulian@marinca.net>
Fixes: 4f7c2e0d8765 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
Cc: stable@vger.kernel.org
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
---
Previous version (v1) can be found:

  https://lore.kernel.org/linux-usb/20201118133745.65628-1-mika.westerberg@linux.intel.com/

v2: Use get_device()/put_device() for the parent device.

 drivers/thunderbolt/icm.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
index b51fc3f62b1f..cd985f334b9b 100644
--- a/drivers/thunderbolt/icm.c
+++ b/drivers/thunderbolt/icm.c
@@ -1976,7 +1976,9 @@ static int complete_rpm(struct device *dev, void *data)
 
 static void remove_unplugged_switch(struct tb_switch *sw)
 {
-	pm_runtime_get_sync(sw->dev.parent);
+	struct device *parent = get_device(sw->dev.parent);
+
+	pm_runtime_get_sync(parent);
 
 	/*
 	 * Signal this and switches below for rpm_complete because
@@ -1987,8 +1989,10 @@ static void remove_unplugged_switch(struct tb_switch *sw)
 	bus_for_each_dev(&tb_bus_type, &sw->dev, NULL, complete_rpm);
 	tb_switch_remove(sw);
 
-	pm_runtime_mark_last_busy(sw->dev.parent);
-	pm_runtime_put_autosuspend(sw->dev.parent);
+	pm_runtime_mark_last_busy(parent);
+	pm_runtime_put_autosuspend(parent);
+
+	put_device(parent);
 }
 
 static void icm_free_unplugged_children(struct tb_switch *sw)
-- 
2.29.2


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

* Re: [PATCH v2] thunderbolt: Fix use-after-free in remove_unplugged_switch()
  2020-11-19  8:34 [PATCH v2] thunderbolt: Fix use-after-free in remove_unplugged_switch() Mika Westerberg
@ 2020-11-19  8:41 ` Greg Kroah-Hartman
  2020-11-19 14:48   ` Mika Westerberg
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2020-11-19  8:41 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: linux-usb, Yehezkel Bernat, Michael Jamet,
	Paulian Bogdan Marinca, Andreas Noever, Lukas Wunner

On Thu, Nov 19, 2020 at 11:34:29AM +0300, Mika Westerberg wrote:
> Paulian reported a crash that happens when a dock is unplugged during
> hibernation:
> 
> [78436.228217] thunderbolt 0-1: device disconnected
> [78436.228365] BUG: kernel NULL pointer dereference, address: 00000000000001e0
> ...
> [78436.228397] RIP: 0010:icm_free_unplugged_children+0x109/0x1a0
> ...
> [78436.228432] Call Trace:
> [78436.228439]  icm_rescan_work+0x24/0x30
> [78436.228444]  process_one_work+0x1a3/0x3a0
> [78436.228449]  worker_thread+0x30/0x370
> [78436.228454]  ? process_one_work+0x3a0/0x3a0
> [78436.228457]  kthread+0x13d/0x160
> [78436.228461]  ? kthread_park+0x90/0x90
> [78436.228465]  ret_from_fork+0x1f/0x30
> 
> This happens because remove_unplugged_switch() calls tb_switch_remove()
> that releases the memory pointed by sw so the following lines reference
> to a memory that might be released already.
> 
> Fix this by saving pointer to the parent device before calling
> tb_switch_remove().
> 
> Reported-by: Paulian Bogdan Marinca <paulian@marinca.net>
> Fixes: 4f7c2e0d8765 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
> Cc: stable@vger.kernel.org
> Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> ---

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

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

* Re: [PATCH v2] thunderbolt: Fix use-after-free in remove_unplugged_switch()
  2020-11-19  8:41 ` Greg Kroah-Hartman
@ 2020-11-19 14:48   ` Mika Westerberg
  0 siblings, 0 replies; 3+ messages in thread
From: Mika Westerberg @ 2020-11-19 14:48 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-usb, Yehezkel Bernat, Michael Jamet,
	Paulian Bogdan Marinca, Andreas Noever, Lukas Wunner

On Thu, Nov 19, 2020 at 09:41:17AM +0100, Greg Kroah-Hartman wrote:
> On Thu, Nov 19, 2020 at 11:34:29AM +0300, Mika Westerberg wrote:
> > Paulian reported a crash that happens when a dock is unplugged during
> > hibernation:
> > 
> > [78436.228217] thunderbolt 0-1: device disconnected
> > [78436.228365] BUG: kernel NULL pointer dereference, address: 00000000000001e0
> > ...
> > [78436.228397] RIP: 0010:icm_free_unplugged_children+0x109/0x1a0
> > ...
> > [78436.228432] Call Trace:
> > [78436.228439]  icm_rescan_work+0x24/0x30
> > [78436.228444]  process_one_work+0x1a3/0x3a0
> > [78436.228449]  worker_thread+0x30/0x370
> > [78436.228454]  ? process_one_work+0x3a0/0x3a0
> > [78436.228457]  kthread+0x13d/0x160
> > [78436.228461]  ? kthread_park+0x90/0x90
> > [78436.228465]  ret_from_fork+0x1f/0x30
> > 
> > This happens because remove_unplugged_switch() calls tb_switch_remove()
> > that releases the memory pointed by sw so the following lines reference
> > to a memory that might be released already.
> > 
> > Fix this by saving pointer to the parent device before calling
> > tb_switch_remove().
> > 
> > Reported-by: Paulian Bogdan Marinca <paulian@marinca.net>
> > Fixes: 4f7c2e0d8765 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
> > ---
> 
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Thanks!

Applied to thunderbolt.git/fixes.

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

end of thread, other threads:[~2020-11-19 14:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-19  8:34 [PATCH v2] thunderbolt: Fix use-after-free in remove_unplugged_switch() Mika Westerberg
2020-11-19  8:41 ` Greg Kroah-Hartman
2020-11-19 14:48   ` Mika Westerberg

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).