Skip to contentSkip to conversation

atm: skip driver close callback when device is already removed

PATCH In review Zhenghang Xiao 2 participants 2 messages May 26 – May 28

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 atm: skip driver close callback when device is already removed

After atm_dev_deregister() returns, the ATM device driver frees its private resources. Any socket that was connected to the device at the time of deregistration still holds a valid vcc->dev pointer (struct atm_dev is kept alive by the VCC's refcount). When that socket is subsequently closed, vcc_destroy_socket() calls dev->ops->close(vcc) which dereferences the already-freed driver private data — a deterministic use-after-free.

Skip the driver close callback when ATM_DF_REMOVED is set on the device, matching the existing guard in __vcc_connect() that rejects new connections to removed devices.

Fixes 64bf69ddff76 ("[ATM]: deregistration removes device from atm_devs list immediately")
---
 net/atm/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/atm/common.c +2 -1
index fe77f51f6ce1..cbb87b9c10d2 100644
@@ -177,7 +177,8 @@ static void vcc_destroy_socket(struct sock *sk)
 
 	set_bit(ATM_VF_CLOSE, &vcc->flags);
 	clear_bit(ATM_VF_READY, &vcc->flags);
-	if (vcc->dev && vcc->dev->ops->close)
+	if (vcc->dev && vcc->dev->ops->close &&
+	    !test_bit(ATM_DF_REMOVED, &vcc->dev->flags))
 		vcc->dev->ops->close(vcc);
 	if (vcc->push)
 		vcc->push(vcc, NULL); /* atmarpd has no push */
Signature · 1 line
2.50.1 (Apple Git-155)
reviewer
#1 lore raw

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev

Simon says: Although the first two issues are appear to be being pre-existing
the do seem to effect the functionality of this patch. So I think all three issues flagged by AI should
be considered in the context of this patch.
---
atm: skip driver close callback when device is already removed

This commit skips the ATM device driver close callback when ATM_DF_REMOVED is set on the device. It attempts to prevent a use-after-free when the driver has already freed its private resources during deregistration.

net/atm/common.c static void vcc_destroy_socket(struct sock *sk) · line 182

Quoted 12 lines
net/atm/common.c +2 -1
index fe77f51f6ce18..cbb87b9c10d2f 100644
@@ -177,7 +177,8 @@ static void vcc_destroy_socket(struct sock *sk)
 
 	set_bit(ATM_VF_CLOSE, &vcc->flags);
 	clear_bit(ATM_VF_READY, &vcc->flags);
-	if (vcc->dev && vcc->dev->ops->close)
+	if (vcc->dev && vcc->dev->ops->close &&
+	    !test_bit(ATM_DF_REMOVED, &vcc->dev->flags))
 		vcc->dev->ops->close(vcc);

[Severity: Critical]
Does this lockless check introduce a Time-Of-Check to Time-Of-Use (TOCTOU) regression that could still trigger the use-after-free?

If a thread executing vcc_destroy_socket() checks the ATM_DF_REMOVED flag and finds it false, it might be preempted. Another thread could then handle device removal, call atm_dev_deregister() (which sets ATM_DF_REMOVED and locklessly removes VCCs via atm_dev_release_vccs()), and return.

The ATM driver, like solos-pci, could then complete its unbind process and free its private device data in vcc->dev->dev_data.

When the first thread resumes, it would execute vcc->dev->ops->close(vcc) without any locks synchronizing against deregistration, dereferencing the newly freed driver data.

Since vcc_destroy_socket() only holds lock_sock() and lacks the vcc_sklist_lock protection used in __vcc_connect(), can this memory corruption race still occur?

[Severity: Medium]
Will skipping the ops->close callback cause a memory leak regression for compliant ATM drivers on device hot-unplug?

Drivers like usbatm allocate per-VCC private state in their ops->open callback and rely entirely on ops->close to free it.

By skipping the close callback for removed devices, won't the core ATM code permanently leak these per-VCC software resources if a device is deregistered while sockets are still open?

Quoted 2 lines
net/atm/common.c +0 -0
 	if (vcc->push)
 		vcc->push(vcc, NULL); /* atmarpd has no push */