IB/cm: Fix possible use-after-free in ib_cm_cleanup()
Subsystems (2)
PATCH 1/1 IB/cm: Fix possible use-after-free in ib_cm_cleanup()
This module's remove path calls cancel_delayed_work(). However, that function does not wait until the work function finishes. This means that the callback function may still be running after the driver's remove function has finished, which would result in a use-after-free.
Fix by calling cancel_delayed_work_sync(), which ensures that the work is properly cancelled, no longer running, and unable to re-schedule itself.
--- drivers/infiniband/core/cm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/core/cm.c +1 -1
@@ -4508,7 +4508,7 @@ static void __exit ib_cm_cleanup(void)spin_lock_irq (&cm.lock);list_for_each_entry (timewait_info, &cm.timewait_list, list) -cancel_delayed_work (&timewait_info->work.work); +cancel_delayed_work_sync (&timewait_info->work.work);spin_unlock_irq (&cm.lock);ib_unregister_client (&cm_client);
Signature · 1 line
2.25.1
Jason Gunthorpe
maintainerQuoted 19 lines · Wang Hai
This module's remove path calls
cancel_delayed_work(). However, that function does not wait until the work function finishes. This means that the callback function may still be running after the driver's remove function has finished, which would result in a use-after-free.Fix by calling
cancel_delayed_work_sync(), which ensures that the work is properly cancelled, no longer running, and unable to re-schedule itself.Fixes 8575329d4f85 ("IB/cm: Fix timewait crash after module unload")Reported-by Hulk Robot <[email protected]>Signed-off-by Wang Hai <[email protected]>drivers/infiniband/core/cm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/infiniband/core/cm.c +1
-0+++ b/drivers/infiniband/core/cm.c
drivers/infiniband/core/cm.c static void __exit ib_cm_cleanup(void) · line 4512
Quoted 6 lines
drivers/infiniband/core/cm.c +1
-1@@ -4508,7 +4508,7 @@ static void __exit ib_cm_cleanup(void)spin_lock_irq (&cm.lock);list_for_each_entry (timewait_info, &cm.timewait_list, list) -cancel_delayed_work (&timewait_info->work.work); +cancel_delayed_work_sync (&timewait_info->work.work);spin_unlock_irq (&cm.lock);
No, this will deadlock:
static int cm_timewait_handler(struct cm_work *work)
{
struct cm_timewait_info *timewait_info;
struct cm_id_private *cm_id_priv;
timewait_info = container_of(work, struct cm_timewait_info, work);
spin_lock_irq(&cm.lock);
^^^^^^^^^^^^Holds the same lock
What is your bug? The destroy_wq() a few lines below will flush out all the work so it is already not possible that work can still exist after the driver's remove function has finished.
Jason
Quoted 37 lines · Jason Gunthorpe
Quoted 19 lines · Wang Hai
This module's remove path calls
cancel_delayed_work(). However, that function does not wait until the work function finishes. This means that the callback function may still be running after the driver's remove function has finished, which would result in a use-after-free.Fix by calling
cancel_delayed_work_sync(), which ensures that the work is properly cancelled, no longer running, and unable to re-schedule itself.Fixes 8575329d4f85 ("IB/cm: Fix timewait crash after module unload")Reported-by Hulk Robot <[email protected]>Signed-off-by Wang Hai <[email protected]>drivers/infiniband/core/cm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/infiniband/core/cm.c +1
-0+++ b/drivers/infiniband/core/cm.cdrivers/infiniband/core/cm.c static void __exit ib_cm_cleanup(void) · line 4512
Quoted 6 lines
drivers/infiniband/core/cm.c +1
-1@@ -4508,7 +4508,7 @@ static void __exit ib_cm_cleanup(void)spin_lock_irq (&cm.lock);list_for_each_entry (timewait_info, &cm.timewait_list, list) -cancel_delayed_work (&timewait_info->work.work); +cancel_delayed_work_sync (&timewait_info->work.work);spin_unlock_irq (&cm.lock);No, this will deadlock:
static int
cm_timewait_handler(struct cm_work *work){ struct cm_timewait_info *timewait_info; struct cm_id_private *cm_id_priv; timewait_info = container_of(work, struct cm_timewait_info, work); spin_lock_irq(&cm.lock); ^^^^^^^^^^^^
Quoted 8 lines
Holds the same lock
What is your bug? The
destroy_wq()a few lines below will flush out all the work so it is already not possible that work can still exist after the driver's remove function has finished.Jason
.
Sorry, this is a wrong bugfix, thank you for pointing it out.
I was studying the code here and thought there might be a null pointer reference problem.
You are right, I didn't take into account destroy_workqueue(). There are no bugs here. sorry for making this problematic patch.
Please ignore this patch.
Signature · 1 line
Wang Hai