Skip to content

Commit 940f39e

Browse files
f6bvpgregkh
authored andcommitted
rose: fix race between loopback timer and module removal
commit 47dd6ec1a77d77895afb00aa2e68373a48289108 upstream. rose_loopback_clear() called timer_delete() which returns immediately without waiting for any running callback to complete. If the timer fired concurrently with module removal, rose_loopback_timer() could re-arm the timer after timer_delete() returned and then access rose_loopback_neigh after it was freed. Two complementary changes close the race: 1. Add a loopback_stopping atomic flag. rose_loopback_timer() checks it at entry (before acquiring a reference) and again inside the loop; when set it drains the queue and exits without re-arming the timer. 2. Switch rose_loopback_clear() to timer_delete_sync() so it blocks until any in-flight callback has returned before freeing resources. The smp_mb() between setting the flag and calling timer_delete_sync() ensures the flag is visible to any callback that is about to run. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Bernard Pidoux <bernard.f6bvp@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent fe8cbcc commit 940f39e

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

net/rose/rose_loopback.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
#include <net/rose.h>
1313
#include <linux/init.h>
1414

15-
static struct sk_buff_head loopback_queue;
1615
#define ROSE_LOOPBACK_LIMIT 1000
17-
static struct timer_list loopback_timer;
1816

17+
static struct timer_list loopback_timer;
18+
static struct sk_buff_head loopback_queue;
1919
static void rose_set_loopback_timer(void);
2020
static void rose_loopback_timer(struct timer_list *unused);
2121

22+
static atomic_t loopback_stopping = ATOMIC_INIT(0);
23+
2224
void rose_loopback_init(void)
2325
{
2426
skb_queue_head_init(&loopback_queue);
@@ -66,6 +68,9 @@ static void rose_loopback_timer(struct timer_list *unused)
6668
unsigned int lci_i, lci_o;
6769
int count;
6870

71+
if (atomic_read(&loopback_stopping))
72+
return;
73+
6974
if (rose_loopback_neigh)
7075
rose_neigh_hold(rose_loopback_neigh);
7176
else
@@ -75,6 +80,13 @@ static void rose_loopback_timer(struct timer_list *unused)
7580
skb = skb_dequeue(&loopback_queue);
7681
if (!skb)
7782
goto out;
83+
84+
if (atomic_read(&loopback_stopping)) {
85+
kfree_skb(skb);
86+
skb_queue_purge(&loopback_queue);
87+
goto out;
88+
}
89+
7890
if (skb->len < ROSE_MIN_LEN) {
7991
kfree_skb(skb);
8092
continue;
@@ -118,18 +130,23 @@ static void rose_loopback_timer(struct timer_list *unused)
118130
out:
119131
rose_neigh_put(rose_loopback_neigh);
120132

121-
if (!skb_queue_empty(&loopback_queue))
133+
if (!atomic_read(&loopback_stopping) && !skb_queue_empty(&loopback_queue))
122134
mod_timer(&loopback_timer, jiffies + 1);
123135
}
124136

125137
void __exit rose_loopback_clear(void)
126138
{
127139
struct sk_buff *skb;
128140

129-
timer_delete(&loopback_timer);
141+
atomic_set(&loopback_stopping, 1);
142+
/* Pairs with atomic_read() in rose_loopback_timer(): ensure the
143+
* stopping flag is visible before we cancel, so a concurrent
144+
* callback aborts its loop early rather than re-arming the timer.
145+
*/
146+
smp_mb();
147+
148+
timer_delete_sync(&loopback_timer);
130149

131-
while ((skb = skb_dequeue(&loopback_queue)) != NULL) {
132-
skb->sk = NULL;
150+
while ((skb = skb_dequeue(&loopback_queue)) != NULL)
133151
kfree_skb(skb);
134-
}
135152
}

0 commit comments

Comments
 (0)