Skip to content

Commit f36b019

Browse files
Phil Sutterummakynes
authored andcommitted
netfilter: IDLETIMER: Fix for possible ABBA deadlock
Deletion of the last rule referencing a given idletimer may happen at the same time as a read of its file in sysfs: | ====================================================== | WARNING: possible circular locking dependency detected | 6.12.0-rc7-01692-g5e9a28f41134-dirty #594 Not tainted | ------------------------------------------------------ | iptables/3303 is trying to acquire lock: | ffff8881057e04b8 (kn->active#48){++++}-{0:0}, at: __kernfs_remove+0x20 | | but task is already holding lock: | ffffffffa0249068 (list_mutex){+.+.}-{3:3}, at: idletimer_tg_destroy_v] | | which lock already depends on the new lock. A simple reproducer is: | #!/bin/bash | | while true; do | iptables -A INPUT -i foo -j IDLETIMER --timeout 10 --label "testme" | iptables -D INPUT -i foo -j IDLETIMER --timeout 10 --label "testme" | done & | while true; do | cat /sys/class/xt_idletimer/timers/testme >/dev/null | done Avoid this by freeing list_mutex right after deleting the element from the list, then continuing with the teardown. Fixes: 0902b46 ("netfilter: xtables: idletimer target implementation") Signed-off-by: Phil Sutter <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent d92906f commit f36b019

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

net/netfilter/xt_IDLETIMER.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -407,21 +407,23 @@ static void idletimer_tg_destroy(const struct xt_tgdtor_param *par)
407407

408408
mutex_lock(&list_mutex);
409409

410-
if (--info->timer->refcnt == 0) {
411-
pr_debug("deleting timer %s\n", info->label);
412-
413-
list_del(&info->timer->entry);
414-
timer_shutdown_sync(&info->timer->timer);
415-
cancel_work_sync(&info->timer->work);
416-
sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
417-
kfree(info->timer->attr.attr.name);
418-
kfree(info->timer);
419-
} else {
410+
if (--info->timer->refcnt > 0) {
420411
pr_debug("decreased refcnt of timer %s to %u\n",
421412
info->label, info->timer->refcnt);
413+
mutex_unlock(&list_mutex);
414+
return;
422415
}
423416

417+
pr_debug("deleting timer %s\n", info->label);
418+
419+
list_del(&info->timer->entry);
424420
mutex_unlock(&list_mutex);
421+
422+
timer_shutdown_sync(&info->timer->timer);
423+
cancel_work_sync(&info->timer->work);
424+
sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
425+
kfree(info->timer->attr.attr.name);
426+
kfree(info->timer);
425427
}
426428

427429
static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param *par)
@@ -432,25 +434,27 @@ static void idletimer_tg_destroy_v1(const struct xt_tgdtor_param *par)
432434

433435
mutex_lock(&list_mutex);
434436

435-
if (--info->timer->refcnt == 0) {
436-
pr_debug("deleting timer %s\n", info->label);
437-
438-
list_del(&info->timer->entry);
439-
if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
440-
alarm_cancel(&info->timer->alarm);
441-
} else {
442-
timer_shutdown_sync(&info->timer->timer);
443-
}
444-
cancel_work_sync(&info->timer->work);
445-
sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
446-
kfree(info->timer->attr.attr.name);
447-
kfree(info->timer);
448-
} else {
437+
if (--info->timer->refcnt > 0) {
449438
pr_debug("decreased refcnt of timer %s to %u\n",
450439
info->label, info->timer->refcnt);
440+
mutex_unlock(&list_mutex);
441+
return;
451442
}
452443

444+
pr_debug("deleting timer %s\n", info->label);
445+
446+
list_del(&info->timer->entry);
453447
mutex_unlock(&list_mutex);
448+
449+
if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
450+
alarm_cancel(&info->timer->alarm);
451+
} else {
452+
timer_shutdown_sync(&info->timer->timer);
453+
}
454+
cancel_work_sync(&info->timer->work);
455+
sysfs_remove_file(idletimer_tg_kobj, &info->timer->attr.attr);
456+
kfree(info->timer->attr.attr.name);
457+
kfree(info->timer);
454458
}
455459

456460

0 commit comments

Comments
 (0)