dibs: fix double free of dmb_clientid_arr
Subsystems (5)
PATCH 1/1 dibs: fix double free of dmb_clientid_arr
dibs_dev_add() frees dibs->dmb_clientid_arr on both of its failure paths, but does not clear the pointer. dibs_lo_dev_probe() then frees the same pointer again in its err_reg path, so a failing device_add() or sysfs_create_group() results in a double free.
Drop the kfree() from dibs_lo_dev_probe().
Note: commit cc21191b584c ("dibs: Move data path to dibs layer") moved the code to its current location; the race was introduced earlier by commit c3a910f2380f ("net/smc: implement DMB-merged operations of loopback-ism").
--- drivers/dibs/dibs_loopback.c | 1 - 1 file changed, 1 deletion(-)
drivers/dibs/dibs_loopback.c +0 -1
@@ -335,7 +335,6 @@ static int dibs_lo_dev_probe(void)return 0 ; err_reg: -kfree (dibs->dmb_clientid_arr);/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);
base-commit: 98379cf6109c0adb5879f86ba3efd4afe389a64a
prerequisite-patch-id: da145e40ff1ce22e9701dd49676606f612c03872
Signature · 1 line
2.52.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] The hardware interrupt handler ism_handle_irq() can execute out-of-order with respect to initialization and teardown of critical resources, leading to crashes (UAF, NULL deref, and uninitialized spinlock accesses).
- [High] dibs_unregister_client() leaves the subsystem in an inconsistent partial state if it aborts midway through its device iteration.
- [High] dibs_lo_attach_dmb drops the hash table read lock before incrementing the object's reference count, leading to a Use-After-Free.
--
--- Patch [1]: [PATCH net] dibs: fix double free of dmb_clientid_arr --- commit 45565b8a1cec1239be540b3b54fad60822ae8b57
Author: Hidayath Khan <[email protected]>
dibs: fix double free of dmb_clientid_arr
dibs_dev_add() frees dibs->dmb_clientid_arr on both of its failure paths, but does not clear the pointer. dibs_lo_dev_probe() then frees the same pointer again in its err_reg path, so a failing device_add() or sysfs_create_group() results in a double free.
Drop the kfree() from dibs_lo_dev_probe().
drivers/dibs/dibs_loopback.c static int dibs_lo_dev_probe(void) · line 337
Quoted 9 lines
drivers/dibs/dibs_loopback.c +0
-1@@ -334,7 +334,6 @@ static int dibs_lo_dev_probe(void)return 0 ; err_reg: -kfree (dibs->dmb_clientid_arr);
[Severity: High]
This isn't a bug introduced by this patch, but does dibs_lo_attach_dmb() have a use-after-free if the reference count is incremented after dropping the hash table lock?
drivers/dibs/dibs_loopback.c:dibs_lo_attach_dmb() {
...
read_unlock_bh(&ldev->dmb_ht_lock); if (!refcount_inc_not_zero(&dmb_node->refcnt))
...
}
If a concurrent thread executing dibs_lo_unregister_dmb() acquires the lock and drops the refcount to zero, could it free the node before the first thread resumes, causing refcount_inc_not_zero() to operate on freed memory?
[Severity: High]
This is a pre-existing issue, but is there an initialization race with the hardware interrupt handler in ism_drv.c?
drivers/s390/net/ism_drv.c:ism_probe() {
...
ret = dibs_dev_add(dibs);
...
}
If the hardware interrupt is registered before dibs_dev_add() initializes resources, and a spurious interrupt fires, could ism_handle_irq() access an uninitialized spinlock or dereference a NULL pointer?
drivers/s390/net/ism_drv.c:ism_handle_irq() {
...
client_id = dibs->dmb_clientid_arr[bit];
...
}
Similarly, during teardown:
drivers/s390/net/ism_drv.c:ism_remove() {
...
dibs_dev_del(dibs); ism_dev_exit(ism);
...
}
Could freeing resources like dibs->dmb_clientid_arr via dibs_dev_del() before the active IRQ is unregistered create a use-after-free window?
[Severity: High]
This is also a pre-existing issue, but does dibs_unregister_client() leave the subsystem in an inconsistent state if it aborts early?
drivers/dibs/dibs_main.c:dibs_unregister_client() {
...
rc = -EBUSY; goto err_reg_dmb;
...
}
If the loop successfully processes some devices but then encounters a device with active DMBs, it returns -EBUSY without rolling back the teardown on the previously processed devices. If the caller ignores this return value and proceeds to unload the module, could the detached devices later trigger an event and dereference freed ops function pointers?
Quoted 3 lines
drivers/dibs/dibs_loopback.c +0
-0/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Alexandra Winter
maintainerQuoted 38 lines · On 31.07.26 09:56, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] The hardware interrupt handlerism_handle_irq()can execute out-of-order with respect to initialization and teardown of critical resources, leading to crashes (UAF, NULL deref, and uninitialized spinlock accesses).
- [High]dibs_unregister_client()leaves the subsystem in an inconsistent partial state if it aborts midway through its device iteration.
- [High]dibs_lo_attach_dmbdrops the hash table read lock before incrementing the object's reference count, leading to a Use-After-Free.
----- Patch [1]: [PATCH net] dibs: fix double free of
dmb_clientid_arr--- commit45565b8a1cec1239be540b3b54fad60822ae8b57
Author: Hidayath Khan <[email protected]>dibs: fix double free of
dmb_clientid_arr
dibs_dev_add()freesdibs->dmb_clientid_arron both of its failure paths, but does not clear the pointer.dibs_lo_dev_probe()then frees the same pointer again in itserr_regpath, so a failingdevice_add()orsysfs_create_group()results in a double free.Drop the
kfree()fromdibs_lo_dev_probe().drivers/dibs/dibs_loopback.c static int dibs_lo_dev_probe(void) · line 337
Quoted 9 lines
drivers/dibs/dibs_loopback.c +0
-1@@ -334,7 +334,6 @@ static int dibs_lo_dev_probe(void)return 0 ; err_reg: -kfree (dibs->dmb_clientid_arr);[Severity: High]
This isn't a bug introduced by this patch, but doesdibs_lo_attach_dmb()have a use-after-free if the reference count is incremented after dropping the hash table lock?
Quoted 7 lines
drivers/dibs/dibs_loopback.c:dibs_lo_attach_dmb() {
...read_unlock_bh(&ldev->dmb_ht_lock); if (!refcount_inc_not_zero(&dmb_node->refcnt))...
}
Fixed bya10ea943356b ("dibs: fix use-after-free of dmb_node in loopback attach/detach/unregister") that got accepted to net yesterday.
Quoted 11 lines
If a concurrent thread executing
dibs_lo_unregister_dmb()acquires the lock and drops the refcount to zero, could it free the node before the first thread resumes, causingrefcount_inc_not_zero()to operate on freed memory?[Severity: High]
This is a pre-existing issue, but is there an initialization race with the hardware interrupt handler inism_drv.c?drivers/s390/net/ism_drv.c:ism_probe() {
...
Quoted 8 lines
ret = dibs_dev_add(dibs);...
}If the hardware interrupt is registered before
dibs_dev_add()initializes resources, and a spurious interrupt fires, couldism_handle_irq()access an uninitialized spinlock or dereference a NULL pointer?
Hidayath has already sent a fix for the lock initialization:
https://lore.kernel.org/netdev/[email protected]/
Quoted 5 lines
drivers/s390/net/ism_drv.c:ism_handle_irq() {
...client_id = dibs->dmb_clientid_arr[bit];...
}
No client can have registered a dmb at that point, so
no dmb interrupt can occur, so this NULL pointer dereference cannot happen irl. I plan to restructure with a future cleanup patch.
Quoted 5 lines
Similarly, during teardown:
drivers/s390/net/ism_drv.c:ism_remove() {
...
Quoted 8 lines
dibs_dev_del(dibs); ism_dev_exit(ism);...
}Could freeing resources like
dibs->dmb_clientid_arrviadibs_dev_del()before the active IRQ is unregistered create a use-after-free window?
Yes, we will send a fix.
Quoted 8 lines
[Severity: High]
This is also a pre-existing issue, but doesdibs_unregister_client()leave the subsystem in an inconsistent state if it aborts early?drivers/dibs/dibs_main.c:dibs_unregister_client() {
...rc = -EBUSY; goto err_reg_dmb;
Quoted 13 lines
...
}If the loop successfully processes some devices but then encounters a device with active DMBs, it returns
-EBUSYwithout rolling back the teardown on the previously processed devices. If the caller ignores this return value and proceeds to unload the module, could the detached devices later trigger an event and dereference freed ops function pointers?Quoted 3 lines
drivers/dibs/dibs_loopback.c +0
-0/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);
Does not happen with smc today.
But the guard is not clean, it should do a better and safer cleanup, should such a situation arise with a future client.
I will work on an improvement.
Simon Horman
reviewerThis 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://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]?part=1
---
--- Patch [1]: [PATCH net] dibs: fix double free of dmb_clientid_arr ---
commit d9da866ee3cff93c660daf037f85b099a2326866
Author: Hidayath Khan <[email protected]>
dibs: fix double free of dmb_clientid_arr
This drops the kfree(dibs->dmb_clientid_arr) from the err_reg path of dibs_lo_dev_probe(), since dibs_dev_add() already frees that buffer on both of its own failure paths without clearing the pointer.
Two questions below, one about the commit message tags and one about the lifetime of the same buffer on the removal path.
Quoted 9 lines
dibs: fix double free of
dmb_clientid_arr
dibs_dev_add()freesdibs->dmb_clientid_arron both of its failure paths, but does not clear the pointer.dibs_lo_dev_probe()then frees the same pointer again in itserr_regpath, so a failingdevice_add()orsysfs_create_group()results in a double free.Drop the
kfree()fromdibs_lo_dev_probe().
Quoted 7 lines
Note: commit
cc21191b584c("dibs: Move data path to dibs layer") moved the code to its current location; the race was introduced earlier by commitc3a910f2380f("net/smc: implement DMB-merged operations of loopback-ism").Fixes c3a910f2380f ("net/smc: implement DMB-merged operations of loopback-ism")
[Severity: Medium]
[Finding: main-1-0]
[Sources: opus-5]
Is c3a910f2380f the right commit to blame here?
That commit only touches net/smc/smc_loopback.c and net/smc/smc_loopback.h, adding DMB refcounting plus attach/detach/nocopy support. It creates no client-id array, and it adds no kfree() on any probe error path. At c3a910f2380f^ the only matching array in the tree is ISM's private sba_client_arr in drivers/s390/net/ism_drv.c and include/linux/ism.h.
Blame instead points both halves of the double free at cc21191b584c ("dibs: Move data path to dibs layer"): the allocation and the free in dibs_dev_add(),
dibs->dmb_clientid_arr = kzalloc(max_dmbs, GFP_KERNEL); ... free_client_arr: kfree(dibs->dmb_clientid_arr);
and the duplicate kfree() in dibs_lo_dev_probe()'s err_reg path that this patch removes. Would it be more accurate to say cc21191b584c introduced the problem, rather than that it "moved the code to its current location; the race was introduced earlier"?
With Cc: [email protected] present, a Fixes tag naming a v6.10-era net/smc commit points backports at trees where neither drivers/dibs/ nor dmb_clientid_arr exists, so the patch cannot apply there.
Also, the note calls this a "race", but the double free happens unconditionally on a deterministic error path when device_add() or sysfs_create_group() fails. Would "double free" be the better wording?
drivers/dibs/dibs_loopback.c static int dibs_lo_dev_probe(void) · line 340
Quoted 12 lines
drivers/dibs/dibs_loopback.c +0
-1@@ -335,7 +335,6 @@ static int dibs_lo_dev_probe(void)return 0 ; err_reg: -kfree (dibs->dmb_clientid_arr);/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);
[Severity: High]
[Finding: main-5-4]
[Sources: opus-5]
This isn't a bug introduced by this patch, but while looking at the lifetime of dmb_clientid_arr: does the removal path have the same missing NULL assignment, and can the ISM interrupt handler read the buffer after it is freed?
dibs_dev_del() in drivers/dibs/dibs_main.c ends with the free outside dibs->lock and leaves the pointer set:
device_del(&dibs->dev); kfree(dibs->dmb_clientid_arr);
The spin_lock_irqsave(&dibs->lock) section earlier in that function only clears dibs->subs[], and is released well before the kfree().
ism_remove() in drivers/s390/net/ism_drv.c then runs:
dibs_dev_del(dibs); ism_dev_exit(ism);
and ism_dev_exit() is the only place that calls unregister_sba() and free_irq(pci_irq_vector(pdev, 0), ism). So the SBA stays registered and the MSI handler stays armed across the kfree(). ism_handle_irq() does, for every set DMB bit while holding dibs->lock:
client_id = dibs->dmb_clientid_arr[bit]; if (unlikely(client_id == NO_DIBS_CLIENT || !dibs->subs[client_id])) continue; dibs->subs[client_id]->ops->handle_irq(dibs, ...);
Since the free does not take dibs->lock, can the handler be mid-loop while the removal path frees the array underneath it?
There is a second effect if the byte read back is garbage: only NO_DIBS_CLIENT is rejected, while subs[] has MAX_DIBS_CLIENTS == 8 entries and is the last member of struct dibs_dev in include/linux/dibs.h. Can a client_id in 8..254 read past the end of the kmalloc'ed struct dibs_dev and, if that slot is non-NULL, produce an indirect call through a bogus struct dibs_client pointer?
For the window to be reachable a DMB bit must still be settable by the peer. ism_unregister_dmb() writes NO_DIBS_CLIENT and then issues the hardware unregister command, but:
ret = ism_cmd(ism, &cmd); if (ret && ret != ISM_ERROR) goto out;
leaves the DMB registered in the adapter when the command fails, so a remote peer can keep signalling it.
Would it make sense to free and NULL dmb_clientid_arr only after the owning driver has quiesced its interrupt and data path, do it under dibs->lock, and bound-check client_id against MAX_DIBS_CLIENTS in ism_handle_irq()? The missing NULL assignment in dibs_dev_add() is also what turned the removed kfree() into a double free.
Alexandra Winter
maintainerQuoted 54 lines · Simon Horman
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://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]?part=1
---
--- Patch [1]: [PATCH net] dibs: fix double free ofdmb_clientid_arr---
commitd9da866ee3cff93c660daf037f85b099a2326866
Author: Hidayath Khan <[email protected]>dibs: fix double free of
dmb_clientid_arrThis drops the
kfree(dibs->dmb_clientid_arr)from theerr_regpath ofdibs_lo_dev_probe(), sincedibs_dev_add()already frees that buffer on both of its own failure paths without clearing the pointer.Two questions below, one about the commit message tags and one about the lifetime of the same buffer on the removal path.
Quoted 9 lines
dibs: fix double free of
dmb_clientid_arr
dibs_dev_add()freesdibs->dmb_clientid_arron both of its failure paths, but does not clear the pointer.dibs_lo_dev_probe()then frees the same pointer again in itserr_regpath, so a failingdevice_add()orsysfs_create_group()results in a double free.Drop the
kfree()fromdibs_lo_dev_probe().Quoted 7 lines
Note: commit
cc21191b584c("dibs: Move data path to dibs layer") moved the code to its current location; the race was introduced earlier by commitc3a910f2380f("net/smc: implement DMB-merged operations of loopback-ism").Fixes c3a910f2380f ("net/smc: implement DMB-merged operations of loopback-ism")[Severity: Medium]
[Finding: main-1-0]
[Sources: opus-5]
Isc3a910f2380fthe right commit to blame here?That commit only touches
net/smc/smc_loopback.candnet/smc/smc_loopback.h, adding DMB refcounting plus attach/detach/nocopy support. It creates no client-id array, and it adds nokfree()on any probe error path. At c3a910f2380f^ the only matching array in the tree is ISM's privatesba_client_arrindrivers/s390/net/ism_drv.candinclude/linux/ism.h.Blame instead points both halves of the double free at
cc21191b584c("dibs: Move data path to dibs layer"): the allocation and the free indibs_dev_add(),dibs->dmb_clientid_arr = kzalloc(max_dmbs, GFP_KERNEL); ... free_client_arr: kfree(dibs->dmb_clientid_arr);
Quoted 5 lines
and the duplicate
kfree()in dibs_lo_dev_probe()'serr_regpath that this patch removes. Would it be more accurate to saycc21191b584cintroduced the problem, rather than that it "moved the code to its current location; the race was introduced earlier"?
Hidayath,
I think, AI is right here, the original smc_loopback.c did not have such a construct. Only cc21191b584c is to blame.
(Seems you errounously copied this from your other patcha10ea943356b ("dibs: fix use-after-free of dmb_node in loopback attach/detach/unregister") Please send a v2
Quoted 8 lines
With Cc: [email protected] present, a Fixes tag naming a v6.10-era net/smc commit points backports at trees where neither
drivers/dibs/nordmb_clientid_arrexists, so the patch cannot apply there.Also, the note calls this a "race", but the double free happens unconditionally on a deterministic error path when
device_add()orsysfs_create_group()fails. Would "double free" be the better wording?
Correct (but moot, if this paragraph is removed)
Quoted 7 lines
Quoted 7 lines
drivers/dibs/dibs_loopback.c +0
-0@@ -335,7 +335,6 @@ static int dibs_lo_dev_probe(void)return 0 ;
Quoted 14 lines
drivers/dibs/dibs_loopback.c static int dibs_lo_dev_probe(void) · line 340
Quoted 5 lines
drivers/dibs/dibs_loopback.c +0
-1err_reg: -kfree (dibs->dmb_clientid_arr);/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);[Severity: High]
[Finding: main-5-4]
[Sources: opus-5]
This isn't a bug introduced by this patch, but while looking at the lifetime ofdmb_clientid_arr: does the removal path have the same missing NULL assignment, and can the ISM interrupt handler read the buffer after it is freed?
[PATCH net] net/dibs: Fix UAF of dmb_clientid_arr after dibs_dev_del() is currently discussed on the netdev mailing list to fix this.
PATCH v2 1/1 dibs: fix double free of dmb_clientid_arr
In reply to Hidayath Khandibs_dev_add() frees dibs->dmb_clientid_arr on both of its failure paths, but does not clear the pointer. dibs_lo_dev_probe() then frees the same pointer again in its err_reg path, so a failing device_add() or sysfs_create_group() results in a double free.
Drop the kfree() from dibs_lo_dev_probe().
---
v2:
- Fix Fixes: tag (was c3a910f2380f, should be cc21191b584c)
- Remove incorrect Note paragraph copied from a different patch
drivers/dibs/dibs_loopback.c | 1 - 1 file changed, 1 deletion(-)
drivers/dibs/dibs_loopback.c +0 -1
@@ -335,7 +335,6 @@ static int dibs_lo_dev_probe(void)return 0 ; err_reg: -kfree (dibs->dmb_clientid_arr);/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);
base-commit: 92a09f2af870e91efb9471b109db15c2030f90f2
Signature · 1 line
2.52.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Hardware interrupt is registered before required data structures and locks are initialized.
- [Critical] Asymmetric teardown order creates a Use-After-Free window in ism_remove() and probe error paths.
- [High] Missing class_unregister(&dibs_class) on the error path in dibs_init().
- [High] Incomplete rollback in dibs_unregister_client() leaves client in a partially unregistered state. --
--- Patch [1]: [PATCH net v2] dibs: fix double free of dmb_clientid_arr --- commit 1001308ced15383d91624271c00a3bbcf60208a4
Author: Hidayath Khan <[email protected]>
dibs: fix double free of dmb_clientid_arr
This commit fixes a double free of dibs->dmb_clientid_arr by dropping the kfree() in the err_reg path of dibs_lo_dev_probe(). The pointer is already freed by dibs_dev_add() on failure paths.
drivers/dibs/dibs_loopback.c static int dibs_lo_dev_probe(void) · line 340
Quoted 12 lines
drivers/dibs/dibs_loopback.c +0
-1@@ -335,7 +335,6 @@ static int dibs_lo_dev_probe(void)return 0 ; err_reg: -kfree (dibs->dmb_clientid_arr);/* pairs with dibs_dev_alloc() */ put_device (&dibs->dev);kfree (ldev);
[Severity: High]
This isn't a bug introduced by this patch, but I noticed a missing class_unregister() on the error path in dibs_init().
If dibs_loopback_init() fails, it returns an error without cleaning up the registered class:
drivers/dibs/dibs_main.c:dibs_init() {
...
rc = dibs_loopback_init();
if (rc)
pr_err("%s fails with %d\n", __func__, rc);
return rc;
}Can this lead to a use-after-free? Any subsequent interaction with /sys/class/dibs or reloading the module might dereference the freed class struct.
[Severity: High]
This is a pre-existing issue, but incomplete rollback in dibs_unregister_client() seems to leave the client in a partially unregistered state:
drivers/dibs/dibs_main.c:dibs_unregister_client() {
...
rc = -EBUSY;
goto err_reg_dmb;
}
}
...
}If it encounters a device with active DMBs, it returns -EBUSY immediately without restoring the state of previously unregistered devices. Does this permanently corrupt the client state where it misses events for those earlier devices while believing it is still fully registered?
[Severity: Critical]
This isn't a bug introduced by this patch, but the hardware interrupt appears to be registered before the required data structures and locks are initialized:
drivers/s390/net/ism_drv.c:ism_probe() {
...
ret = ism_dev_init(ism);
if (ret)
goto err_dibs;
...
}ism_dev_init() registers the hardware IRQ before dibs_dev_add() initializes dibs->lock and dibs->dmb_clientid_arr. If a spurious or pending interrupt fires during this small window:
drivers/s390/net/ism_drv.c:ism_handle_irq() {
...
spin_lock(&dibs->lock);
...
client_id = dibs->dmb_clientid_arr[bit];
...
}Will this cause a panic due to attempting to acquire an uninitialized spinlock and dereferencing the NULL dmb_clientid_arr pointer?
[Severity: Critical]
This is a pre-existing issue, but there seems to be an asymmetric teardown order creating a use-after-free window in ism_remove():
drivers/s390/net/ism_drv.c:ism_remove() {
...
dibs_dev_del(dibs);
ism_dev_exit(ism);
...
}dibs_dev_del() frees dibs->dmb_clientid_arr:
drivers/dibs/dibs_main.c:dibs_dev_del() {
...
kfree(dibs->dmb_clientid_arr);
...
}However, the interrupt is only disabled later in ism_dev_exit().
If an interrupt fires in this window, ism_handle_irq() will read from the freed array. Could garbage data interpreted as client_id cause an out-of-bounds access when indexing dibs->subs[client_id]?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1