Skip to contentSkip to conversation

cxl/port: Fix use after free of parent_port in cxl_detach_ep()

PATCH Superseded Alison Schofield 2 participants 3 messages Feb 24 – Feb 25 CXL

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alison Schofield

author maintainer
#0 lore raw

PATCH v2 1/1 cxl/port: Fix use after free of parent_port in cxl_detach_ep()

cxl_detach_ep() is called during bottom-up removal when all CXL memory devices beneath a switch port have been removed. For each port in the hierarchy it locks both the port and its parent, removes the endpoint, and if the port is now empty, marks it dead and unregisters the port by calling delete_switch_port(). There are two places during this work where the parent_port may be used after freeing:

First, a concurrent detach may have already processed a port by the time a second worker finds it via bus_find_device(). Without pinning parent_port, it may already be freed when we discover port->dead and attempt to unlock the parent_port. In a production kernel that's a silent memory corruption, with lock debug, it looks like this:

[]DEBUG_LOCKS_WARN_ON(__owner_task(owner) != get_current()) []WARNING: kernel/locking/mutex.c:949 at __mutex_unlock_slowpath+0x1ee/0x310 []Call Trace:
[]mutex_unlock+0xd/0x20
[]cxl_detach_ep+0x180/0x400 [cxl_core] []devm_action_release+0x10/0x20
[]devres_release_all+0xa8/0xe0
[]device_unbind_cleanup+0xd/0xa0
[]really_probe+0x1a6/0x3e0

Fix this first case by adding a check for port->dead after acquiring both locks. Unlock and release the parent reference before continuing.

Second, delete_switch_port() releases three devm actions registered against parent_port. The last of those is unregister_port() and it calls device_unregister() on the child port, which can cascade. If parent_port is now also empty the device core may unregister and free it too. So by the time delete_switch_port() returns, parent_port may be free, and the subsequent device_unlock(&parent_port->dev) operates on freed memory. The kernel log looks same as above, with a different offset in cxl_detach_ep().

Fix this second issue by taking an extra reference on parent_port before locking it, preventing the memory from being freed across delete_switch_port(). Release it after device_unlock().

These easily reproduce with a reload of cxl_acpi in QEMU environment with CXL devices present.

Fixes 2345df54249c ("cxl/memdev: Fix endpoint port removal")

---

Changes in v2:
Remove stray blank line
Rebase on 7.0-rc1
Add Fixes Tag

 drivers/cxl/core/port.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/cxl/core/port.c +17 -0
index b69c2529744c..5a58336c2a2f 100644
@@ -1533,8 +1533,18 @@ static void cxl_detach_ep(void *data)
 		port = to_cxl_port(dev);
 
 		parent_port = to_cxl_port(port->dev.parent);
+		get_device(&parent_port->dev);
 		device_lock(&parent_port->dev);
 		device_lock(&port->dev);
+
+		/* A concurrent detach may have already removed this port */
+		if (port->dead) {
+			device_unlock(&port->dev);
+			device_unlock(&parent_port->dev);
+			put_device(&parent_port->dev);
+			continue;
+		}
+
 		ep = cxl_ep_load(port, cxlmd);
 		dev_dbg(&cxlmd->dev, "disconnect %s from %s\n",
 			ep ? dev_name(ep->ep) : "", dev_name(&port->dev));
@@ -1553,11 +1563,18 @@ static void cxl_detach_ep(void *data)
 		device_unlock(&port->dev);
 
 		if (died) {
+			/*
+			 * Hold an extra reference to parent_port across
+			 * delete_switch_port() since unregister_port(port)
+			 * may cascade and unregister parent_port, freeing
+			 * it before the call to device_unlock().
+			 */
 			dev_dbg(&cxlmd->dev, "delete %s\n",
 				dev_name(&port->dev));
 			delete_switch_port(port);
 		}
 		device_unlock(&parent_port->dev);
+		put_device(&parent_port->dev);
 	}
 }
 

base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f

Signature · 1 line
2.37.3
#1 lore raw
Quoted 35 lines · Alison Schofield

cxl_detach_ep() is called during bottom-up removal when all CXL memory devices beneath a switch port have been removed. For each port in the hierarchy it locks both the port and its parent, removes the endpoint, and if the port is now empty, marks it dead and unregisters the port by calling delete_switch_port(). There are two places during this work where the parent_port may be used after freeing:

First, a concurrent detach may have already processed a port by the time a second worker finds it via bus_find_device(). Without pinning parent_port, it may already be freed when we discover port->dead and attempt to unlock the parent_port. In a production kernel that's a silent memory corruption, with lock debug, it looks like this:

[]DEBUG_LOCKS_WARN_ON(__owner_task(owner) != get_current()) []WARNING: kernel/locking/mutex.c:949 at __mutex_unlock_slowpath+0x1ee/0x310 []Call Trace:
[]mutex_unlock+0xd/0x20
[]cxl_detach_ep+0x180/0x400 [cxl_core] []devm_action_release+0x10/0x20
[]devres_release_all+0xa8/0xe0
[]device_unbind_cleanup+0xd/0xa0
[]really_probe+0x1a6/0x3e0

Fix this first case by adding a check for port->dead after acquiring both locks. Unlock and release the parent reference before continuing.

Second, delete_switch_port() releases three devm actions registered against parent_port. The last of those is unregister_port() and it calls device_unregister() on the child port, which can cascade. If parent_port is now also empty the device core may unregister and free it too. So by the time delete_switch_port() returns, parent_port may be free, and the subsequent device_unlock(&parent_port->dev) operates on freed memory. The kernel log looks same as above, with a different offset in cxl_detach_ep().

Quoted 6 lines

Fix this second issue by taking an extra reference on parent_port before locking it, preventing the memory from being freed across delete_switch_port(). Release it after device_unlock().

These easily reproduce with a reload of cxl_acpi in QEMU environment with CXL devices present.

The analysis looks correct, but I worry this fix is too tightly scoped to this one problematic path. Just make the rule be that parent ports have an elevated reference until child release rather than child unregister.

This would be a similar semantic to what happens for regions where cxl_region_alloc() takes a reference on the root decoder, and cxl_region_release() drops it.

author maintainer
#2 lore raw
Quoted 43 lines · Dan Williams
Quoted 35 lines · Alison Schofield

cxl_detach_ep() is called during bottom-up removal when all CXL memory devices beneath a switch port have been removed. For each port in the hierarchy it locks both the port and its parent, removes the endpoint, and if the port is now empty, marks it dead and unregisters the port by calling delete_switch_port(). There are two places during this work where the parent_port may be used after freeing:

First, a concurrent detach may have already processed a port by the time a second worker finds it via bus_find_device(). Without pinning parent_port, it may already be freed when we discover port->dead and attempt to unlock the parent_port. In a production kernel that's a silent memory corruption, with lock debug, it looks like this:

[]DEBUG_LOCKS_WARN_ON(__owner_task(owner) != get_current()) []WARNING: kernel/locking/mutex.c:949 at __mutex_unlock_slowpath+0x1ee/0x310 []Call Trace:
[]mutex_unlock+0xd/0x20
[]cxl_detach_ep+0x180/0x400 [cxl_core] []devm_action_release+0x10/0x20
[]devres_release_all+0xa8/0xe0
[]device_unbind_cleanup+0xd/0xa0
[]really_probe+0x1a6/0x3e0

Fix this first case by adding a check for port->dead after acquiring both locks. Unlock and release the parent reference before continuing.

Second, delete_switch_port() releases three devm actions registered against parent_port. The last of those is unregister_port() and it calls device_unregister() on the child port, which can cascade. If parent_port is now also empty the device core may unregister and free it too. So by the time delete_switch_port() returns, parent_port may be free, and the subsequent device_unlock(&parent_port->dev) operates on freed memory. The kernel log looks same as above, with a different offset in cxl_detach_ep().

Quoted 6 lines

Fix this second issue by taking an extra reference on parent_port before locking it, preventing the memory from being freed across delete_switch_port(). Release it after device_unlock().

These easily reproduce with a reload of cxl_acpi in QEMU environment with CXL devices present.

Quoted 8 lines

The analysis looks correct, but I worry this fix is too tightly scoped to this one problematic path. Just make the rule be that parent ports have an elevated reference until child release rather than child unregister.

This would be a similar semantic to what happens for regions where cxl_region_alloc() takes a reference on the root decoder, and cxl_region_release() drops it.

Thanks for the review! I've removed the work in the detach path and now refs are taken/released at port alloc/release. I wasn't sure that would make dead ports in cxl_detach_ep go away entirely, but it seems to have cleaned up the unregister cascade and already dead ports are no longer an issue. New revision WIP.