Skip to contentSkip to conversation

thunderbolt: Fix use-after-free in remove_unplugged_switch()

PATCH Superseded Mika Westerberg 2 participants 3 messages Nov 18, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 thunderbolt: Fix use-after-free in remove_unplugged_switch()

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

Fixes 4f7c2e0d8765 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
---
 drivers/thunderbolt/icm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/thunderbolt/icm.c +5 -3
index b51fc3f62b1f..05323c442b56 100644
@@ -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 = sw->dev.parent;
+
+	pm_runtime_get_sync(parent);
 
 	/*
 	 * Signal this and switches below for rpm_complete because
@@ -1987,8 +1989,8 @@ 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);
 }
 
 static void icm_free_unplugged_children(struct tb_switch *sw)
Signature · 1 line
2.29.2
maintainer
#1 lore raw
Quoted 37 lines · 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().

Fixes 4f7c2e0d8765 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
---
 drivers/thunderbolt/icm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/thunderbolt/icm.c +0 -0
index b51fc3f62b1f..05323c442b56 100644

drivers/thunderbolt/icm.c static int complete_rpm(struct device *dev, void *data) · line 1981

Quoted 7 lines
drivers/thunderbolt/icm.c +3 -1
@@ -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 = sw->dev.parent;
+
+	pm_runtime_get_sync(parent);

If you are saving a pointer to a structure, shouldn't you increment the reference count?

	struct device *parent = get_device(sw->dev.parent);

Then you know it is valid when you use it later on.

Just remember to call put_device() when you are done.

thanks,

greg k-h

#2 lore raw
Quoted 49 lines · Greg KH
Quoted 37 lines · 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().

Fixes 4f7c2e0d8765 ("thunderbolt: Make sure device runtime resume completes before taking domain lock")
---
 drivers/thunderbolt/icm.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/thunderbolt/icm.c +0 -0
index b51fc3f62b1f..05323c442b56 100644

drivers/thunderbolt/icm.c static int complete_rpm(struct device *dev, void *data) · line 1981

Quoted 7 lines
drivers/thunderbolt/icm.c +3 -1
@@ -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 = sw->dev.parent;
+
+	pm_runtime_get_sync(parent);

If you are saving a pointer to a structure, shouldn't you increment the reference count?

Quoted 3 lines
	struct device *parent = get_device(sw->dev.parent);

Then you know it is valid when you use it later on.

Indeed, I missed that.

Quoted 1 line

Just remember to call put_device() when you are done.

Sure I'll do this in v2. Thanks!