Skip to contentSkip to conversation

cxl/port: Fix missing port lock in cxl_dport_remove()

PATCH Superseded Terry Bowman 3 participants 3 messages Jun 5

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/port: Fix missing port lock in cxl_dport_remove()

xa_erase() in cxl_dport_remove() runs without the port device lock, creating a race with any caller that does xa_load() on port->dports and then dereferences the returned dport pointer. A concurrent cxl_dport_remove() can erase and free the dport between the xa_load() and the caller acquiring the port lock, causing a use-after-free.

For non-root ports the port lock is already held by the caller on two paths:

1. Driver unbind: devres_release_all() is called from __device_release_driver() which holds port->dev.mutex.

2. Dynamic endpoint removal: cxl_detach_ep() takes the port lock before calling del_dports() -> del_dport() -> devres_release_group(), which synchronously runs cxl_dport_remove().

Use cond_cxl_root_lock/unlock(), which only acquires the port lock when the port is a root port and the lock is therefore not already held. This matches the pattern used in __devm_cxl_add_dport() for the same reason.

Reported-by Sashiko
Fixes 391785859e7e ("cxl/port: Move dport tracking to an xarray")
---
 drivers/cxl/core/port.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/cxl/core/port.c +9 -0
index 0c5957d1d329..80ce7c4d357c 100644
@@ -1088,8 +1088,17 @@ static void cxl_dport_remove(void *data)
 	struct cxl_dport *dport = data;
 	struct cxl_port *port = dport->port;
 
+	/*
+	 * For non-root ports the port lock is already held by the caller
+	 * (driver unbind via devres_release_all(), or cxl_detach_ep() via
+	 * devres_release_group()).  Acquiring it again unconditionally would
+	 * deadlock.  Use cond_cxl_root_lock() which only acquires when the
+	 * port is a root port and the lock is therefore not yet held.
+	 */
+	cond_cxl_root_lock(port);
 	port->nr_dports--;
 	xa_erase(&port->dports, (unsigned long) dport->dport_dev);
+	cond_cxl_root_unlock(port);
 	put_device(dport->dport_dev);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch attempts to fix a Use-After-Free by adding cond_cxl_root_lock(port) around xa_erase() in cxl_dport_remove(). However, this is an incomplete fix that leaves the vulnerability unmitigated because the read paths perform lockless lookups of the dport pointer and dereference it without holding the port lock. --

commit ca3b77804e4fe414bebb82b07916a450f1a53a28
Author: Terry Bowman <[email protected]>

cxl/port: Fix missing port lock in cxl_dport_remove()

This patch attempts to fix a race condition where cxl_dport_remove() runs without the port device lock during xa_erase(). It adds conditional lock acquisition around the xa_erase() call to prevent a use-after-free by concurrent callers.

Quoted 8 lines
drivers/cxl/core/port.c +0 -0
index c5aacd7054f1d..b9bbe6c5767c4 100644
@@ -1092,8 +1092,17 @@ static void cxl_dport_remove(void *data)
 	struct cxl_dport *dport = data;
 	struct cxl_port *port = dport->port;
 

drivers/cxl/core/port.c static void cxl_dport_remove(void *data) · line 1104

Quoted 10 lines
drivers/cxl/core/port.c +8 -0
+	/*
+	 * For non-root ports the port lock is already held by the caller
+	 * (driver unbind via devres_release_all(), or cxl_detach_ep() via
+	 * devres_release_group()).  Acquiring it again unconditionally would
+	 * deadlock.  Use cond_cxl_root_lock() which only acquires when the
+	 * port is a root port and the lock is therefore not yet held.
+	 */
+	cond_cxl_root_lock(port);
 	port->nr_dports--;
 	xa_erase(&port->dports, (unsigned long) dport->dport_dev);

[Severity: High]
This is a pre-existing issue, but does adding the lock here actually prevent the use-after-free for the readers?

The read paths perform lockless lookups of the dport pointer and dereference it without holding the port lock. For example, in drivers/cxl/mem.c:cxl_mem_probe(), it calls cxl_mem_find_port() which locklessly finds dport via xa_load(), and then dereferences dport->rch before acquiring the endpoint parent lock.

Similarly, in drivers/cxl/pci.c:rcd_pcie_cap_emit(), it calls cxl_mem_find_port(), locklessly dereferences dport->regs.rcd_pcie_cap, and only later takes the root_dev lock.

Since these readers don't acquire the port lock before the lookup or re-validate the pointer after acquiring any locks, isn't the UAF still possible when the writer concurrently frees the dport?

Quoted 4 lines
drivers/cxl/core/port.c +1 -0
+	cond_cxl_root_unlock(port);
 	put_device(dport->dport_dev);
 }
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Terry Bowman
Quoted 16 lines · Terry Bowman

xa_erase() in cxl_dport_remove() runs without the port device lock, creating a race with any caller that does xa_load() on port->dports and then dereferences the returned dport pointer. A concurrent cxl_dport_remove() can erase and free the dport between the xa_load() and the caller acquiring the port lock, causing a use-after-free.

For non-root ports the port lock is already held by the caller on two paths:

1. Driver unbind: devres_release_all() is called from __device_release_driver() which holds port->dev.mutex.

2. Dynamic endpoint removal: cxl_detach_ep() takes the port lock before calling del_dports() -> del_dport() -> devres_release_group(), which synchronously runs cxl_dport_remove().

Quoted 8 lines

Use cond_cxl_root_lock/unlock(), which only acquires the port lock when the port is a root port and the lock is therefore not already held. This matches the pattern used in __devm_cxl_add_dport() for the same reason.

Reported-by Sashiko
Fixes 391785859e7e ("cxl/port: Move dport tracking to an xarray")

So I think this is a real bug, but I had to think about it pretty long and hard to convince myself. The scenario I'm envisioning would be an error occurs and the driver is force unbound while the error routine is running. I guess it could also happen if someone does something weird with force unbinding and rebinding the driver, but I'm not sure that's possible.

What I guess I'm getting at is this could benefit from an example of how this can happen in the commit log. With that:

				> ---
Quoted 25 lines
 drivers/cxl/core/port.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/cxl/core/port.c +9 -0
index 0c5957d1d329..80ce7c4d357c 100644
@@ -1088,8 +1088,17 @@ static void cxl_dport_remove(void *data)
 	struct cxl_dport *dport = data;
 	struct cxl_port *port = dport->port;
 
+	/*
+	 * For non-root ports the port lock is already held by the caller
+	 * (driver unbind via devres_release_all(), or cxl_detach_ep() via
+	 * devres_release_group()).  Acquiring it again unconditionally would
+	 * deadlock.  Use cond_cxl_root_lock() which only acquires when the
+	 * port is a root port and the lock is therefore not yet held.
+	 */
+	cond_cxl_root_lock(port);
 	port->nr_dports--;
 	xa_erase(&port->dports, (unsigned long) dport->dport_dev);
+	cond_cxl_root_unlock(port);
 	put_device(dport->dport_dev);
 }