dibs: fix use-after-free in dmb lookup functions
Subsystems (3)
PATCH 1/1 dibs: fix use-after-free in dmb lookup functions
From 68f633e58deb6edffa435a3b2bdc4a12a5e36daf Mon Sep 17 00:00:00 2001
From: Rahul Chandelkar <[email protected]>
Date: Fri, 29 May 2026 12:32:34 +0530
Subject: [PATCH net] dibs: fix use-after-free in dmb lookup functions
dibs_lo_attach_dmb(), dibs_lo_detach_dmb(), and dibs_lo_unregister_dmb() all release read_lock_bh before operating on the dmb_node pointer found during hash table lookup. A concurrent unregister on the same token can kfree() the node in this window, causing a use-after-free when the original caller subsequently performs refcount_inc_not_zero() or refcount_dec_and_test() on the freed object.
Fix by performing the refcount operation while still holding read_lock_bh, so the node cannot be freed between lookup and use. Release the lock before any code path that may call __dibs_lo_unregister_dmb(), since that function acquires write_lock_bh internally via hash_del().
--- drivers/dibs/dibs_loopback.c | 42 +++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-)
drivers/dibs/dibs_loopback.c +20 -22
@@ -150,17 +150,21 @@ static int dibs_lo_unregister_dmb(struct dibs_dev *dibs, struct dibs_dmb *dmb)break ; } } -read_unlock_bh (&ldev->dmb_ht_lock); -if (!dmb_node) +if (!dmb_node) { +read_unlock_bh (&ldev->dmb_ht_lock);return -EINVAL ; + } +if (!refcount_dec_and_test (&dmb_node->refcnt)) { +read_unlock_bh (&ldev->dmb_ht_lock); +return 0 ; + } +read_unlock_bh (&ldev->dmb_ht_lock); -if (refcount_dec_and_test (&dmb_node->refcnt)) { -spin_lock_irqsave (&dibs->lock, flags); - dibs->dmb_clientid_arr[dmb_node->sba_idx] =NO_DIBS_CLIENT ; -spin_unlock_irqrestore (&dibs->lock, flags); +spin_lock_irqsave (&dibs->lock, flags); + dibs->dmb_clientid_arr[dmb_node->sba_idx] =NO_DIBS_CLIENT ; +spin_unlock_irqrestore (&dibs->lock, flags); -__dibs_lo_unregister_dmb (ldev, dmb_node); - } +__dibs_lo_unregister_dmb (ldev, dmb_node);return 0 ; }
@@ -184,16 +188,10 @@ static int dibs_lo_attach_dmb(struct dibs_dev *dibs, struct dibs_dmb *dmb)break ; } } -if (!dmb_node) { -read_unlock_bh (&ldev->dmb_ht_lock); -return -EINVAL ; - } +if (dmb_node && !refcount_inc_not_zero (&dmb_node->refcnt)) + dmb_node =NULL ;read_unlock_bh (&ldev->dmb_ht_lock); - -if (!refcount_inc_not_zero (&dmb_node->refcnt)) -/* the dmb is being unregistered, but has -* not been removed from the hash table. -*/ +if (!dmb_node)return -EINVAL ;/* provide dmb information */
@@ -220,14 +218,14 @@ static int dibs_lo_detach_dmb(struct dibs_dev *dibs, u64 token)break ; } } -if (!dmb_node) { +if (dmb_node &&refcount_dec_and_test (&dmb_node->refcnt)) {read_unlock_bh (&ldev->dmb_ht_lock); -return -EINVAL ; +__dibs_lo_unregister_dmb (ldev, dmb_node); +return 0 ; }read_unlock_bh (&ldev->dmb_ht_lock); - -if (refcount_dec_and_test (&dmb_node->refcnt)) -__dibs_lo_unregister_dmb (ldev, dmb_node); +if (!dmb_node) +return -EINVAL ;return 0 ; }
Signature · 1 line
2.54.0
Alexandra Winter
maintainerQuoted 11 lines · Rahul Chandelkar
From
68f633e58deb6edffa435a3b2bdc4a12a5e36dafMon Sep 17 00:00:00 2001
From: Rahul Chandelkar <[email protected]>
Date: Fri, 29 May 2026 12:32:34 +0530
Subject: [PATCH net] dibs: fix use-after-free in dmb lookup functions
dibs_lo_attach_dmb(),dibs_lo_detach_dmb(), anddibs_lo_unregister_dmb()all releaseread_lock_bhbefore operating on thedmb_nodepointer found during hash table lookup. A concurrent unregister on the same token cankfree()the node in this window, causing a use-after-free when the original caller subsequently performsrefcount_inc_not_zero()orrefcount_dec_and_test()on the freed object.
As discussed off-list, there is no such problem in the current kernel, as there is no such caller.
I fully agree that this should be improved, but this should go to [net-next] as an improvement without a Fixes tag. (This matters, because it removes the burden of backporting, porting to Stable etc..)
Quoted 9 lines
Fix by performing the refcount operation while still holding
read_lock_bh, so the node cannot be freed between lookup and use. Release the lock before any code path that may call__dibs_lo_unregister_dmb(), since that function acquireswrite_lock_bhinternally viahash_del().Fixes cb990a45d7f6 ("dibs: Define dibs loopback")Signed-off-by Rahul Chandelkar <[email protected]>---
Thank you very much for this patch, please allow for some more days for me to review and test it.
Quoted 15 lines · Alexandra Winter
Quoted 11 lines · Rahul Chandelkar
From
68f633e58deb6edffa435a3b2bdc4a12a5e36dafMon Sep 17 00:00:00 2001
From: Rahul Chandelkar <[email protected]>
Date: Fri, 29 May 2026 12:32:34 +0530
Subject: [PATCH net] dibs: fix use-after-free in dmb lookup functions
dibs_lo_attach_dmb(),dibs_lo_detach_dmb(), anddibs_lo_unregister_dmb()all releaseread_lock_bhbefore operating on thedmb_nodepointer found during hash table lookup. A concurrent unregister on the same token cankfree()the node in this window, causing a use-after-free when the original caller subsequently performsrefcount_inc_not_zero()orrefcount_dec_and_test()on the freed object.
Quoted 6 lines
As discussed off-list, there is no such problem in the current kernel, as there is no such caller.
I fully agree that this should be improved, but this should go to [net-next] as an improvement without a Fixes tag. (This matters, because it removes the burden of backporting, porting to Stable etc..)
Giving it some more thought, I think there could be such a race, when server and client shutdown the smcd connection at the same time.
Quoted 7 lines
Quoted 7 lines
Fix by performing the refcount operation while still holding
read_lock_bh, so the node cannot be freed between lookup and use. Release the lock before any code path that may call__dibs_lo_unregister_dmb(), since that function acquireswrite_lock_bhinternally viahash_del().Fixes cb990a45d7f6 ("dibs: Define dibs loopback")
Actually this mechanism was introduced withc3a910f2380f ("net/smc: implement DMB-merged operations of loopback-ism")
cb990a45d7f6 only moved it into the dibs layer.
With that:
Quoted 8 lines
Quoted 2 lines
Signed-off-by Rahul Chandelkar <[email protected]>---
Thank you very much for this patch, please allow for some more days for me to review and test it.
Future work:
I think attach/detach_dmb() in dibs_lo have more scenarios where robustness could be improved. e.g. when a does unregister_dmb() but not detach_dmb(). I don't like it, that in such a case, after successful unregister_dmb(), the dmb is still in the hash table, can still receive data, trigger receive interrupts (move_data()), be target of attach_dmb(). Wen Gu, Tony Lu, what do you think about that? Maybe that can even happen in SMC-D today?
Maybe a get/put mechanism with release function would be appropriate, where dibs_lo_unregister_dmb() does everything but freeing the dmb_node.
On a similar note, I think Shashiko is right with her second comment:
"Looking at dibs_lo_move_data(), it reads rmb_node->sba_idx while holding read_lock_bh(&ldev->dmb_ht_lock), then drops the read lock before acquiring spin_lock(&dibs->lock) to look up the client ID:
drivers/dibs/dibs_loopback.c:dibs_lo_move_data() {
...
sba_idx = rmb_node->sba_idx;
read_unlock_bh(&ldev->dmb_ht_lock);
if (!sf)
return 0;
spin_lock(&dibs->lock);
client_id = dibs->dmb_clientid_arr[sba_idx];
...
}Between dropping the read lock and acquiring the spinlock, could the dmb be completely unregistered by a concurrent thread, and its sba_idx reallocated for a new dmb belonging to a completely different client?
If so, would the function read the new client's ID and erroneously call its handle_irq handler?"
Alexandra Winter
maintainerQuoted 50 lines · Alexandra Winter
Quoted 15 lines · Alexandra Winter
Quoted 11 lines · Rahul Chandelkar
From
68f633e58deb6edffa435a3b2bdc4a12a5e36dafMon Sep 17 00:00:00 2001
From: Rahul Chandelkar <[email protected]>
Date: Fri, 29 May 2026 12:32:34 +0530
Subject: [PATCH net] dibs: fix use-after-free in dmb lookup functions
dibs_lo_attach_dmb(),dibs_lo_detach_dmb(), anddibs_lo_unregister_dmb()all releaseread_lock_bhbefore operating on thedmb_nodepointer found during hash table lookup. A concurrent unregister on the same token cankfree()the node in this window, causing a use-after-free when the original caller subsequently performsrefcount_inc_not_zero()orrefcount_dec_and_test()on the freed object.Quoted 6 lines
As discussed off-list, there is no such problem in the current kernel, as there is no such caller.
I fully agree that this should be improved, but this should go to [net-next] as an improvement without a Fixes tag. (This matters, because it removes the burden of backporting, porting to Stable etc..)Giving it some more thought, I think there could be such a race, when server and client shutdown the smcd connection at the same time.
Quoted 7 lines
Quoted 7 lines
Fix by performing the refcount operation while still holding
read_lock_bh, so the node cannot be freed between lookup and use. Release the lock before any code path that may call__dibs_lo_unregister_dmb(), since that function acquireswrite_lock_bhinternally viahash_del().Fixes cb990a45d7f6 ("dibs: Define dibs loopback")Actually this mechanism was introduced with
c3a910f2380f("net/smc: implement DMB-merged operations of loopback-ism")
cb990a45d7f6only moved it into the dibs layer.With that:
Reviewed-by Alexandra Winter <[email protected]>Quoted 3 lines
Quoted 2 lines
Signed-off-by Rahul Chandelkar <[email protected]>---
[...]
Quoted 29 lines
Future work:
I think attach/detach_dmb() indibs_lohave more scenarios where robustness could be improved. e.g. when a doesunregister_dmb()but notdetach_dmb(). I don't like it, that in such a case, after successfulunregister_dmb(), the dmb is still in the hash table, can still receive data, trigger receive interrupts (move_data()), be target ofattach_dmb(). Wen Gu, Tony Lu, what do you think about that? Maybe that can even happen in SMC-D today?Maybe a get/put mechanism with release function would be appropriate, where
dibs_lo_unregister_dmb()does everything but freeing thedmb_node.On a similar note, I think Shashiko is right with her second comment:
"Looking at
dibs_lo_move_data(), it readsrmb_node->sba_idxwhile holdingread_lock_bh(&ldev->dmb_ht_lock), then drops the read lock before acquiringspin_lock(&dibs->lock)to look up the client ID:drivers/dibs/dibs_loopback.c:dibs_lo_move_data() { ... sba_idx = rmb_node->sba_idx; read_unlock_bh(&ldev->dmb_ht_lock); if (!sf) return 0; spin_lock(&dibs->lock); client_id = dibs->dmb_clientid_arr[sba_idx]; ... }
Quoted 8 lines
Between dropping the read lock and acquiring the spinlock, could the dmb be completely unregistered by a concurrent thread, and its
sba_idxreallocated for a new dmb belonging to a completely different client?
If so, would the function read the new client's ID and erroneously call itshandle_irqhandler?"
Rahul,
what are your plans with this patch? Will you send a v2?
See
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
the current version does not apply on net.