]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
inet: frags: avoid theoretical race in ip_frag_reinit()
authorJakub Kicinski <kuba@kernel.org>
Sun, 7 Dec 2025 01:09:39 +0000 (17:09 -0800)
committerJakub Kicinski <kuba@kernel.org>
Wed, 10 Dec 2025 09:15:27 +0000 (01:15 -0800)
In ip_frag_reinit() we want to move the frag timeout timer into
the future. If the timer fires in the meantime we inadvertently
scheduled it again, and since the timer assumes a ref on frag_queue
we need to acquire one to balance things out.

This is technically racy, we should have acquired the reference
_before_ we touch the timer, it may fire again before we take the ref.
Avoid this entire dance by using mod_timer_pending() which only modifies
the timer if its pending (and which exists since Linux v2.6.30)

Note that this was the only place we ever took a ref on frag_queue
since Eric's conversion to RCU. So we could potentially replace
the whole refcnt field with an atomic flag and a bit more RCU.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reviewed-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20251207010942.1672972-2-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/ipv4/inet_fragment.c
net/ipv4/ip_fragment.c

index 025895eb6ec5978b9a4cade35b3893dfda6b603d..30f4fa50ee2d732a753a9a01a62e6146214afbc5 100644 (file)
@@ -327,7 +327,9 @@ static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir,
 
        timer_setup(&q->timer, f->frag_expire, 0);
        spin_lock_init(&q->lock);
-       /* One reference for the timer, one for the hash table. */
+       /* One reference for the timer, one for the hash table.
+        * We never take any extra references, only decrement this field.
+        */
        refcount_set(&q->refcnt, 2);
 
        return q;
index f7012479713ba68db7c1c3fcee07a86141de31d3..d7bccdc9dc693862ce87ae9d966b8454354c6c8a 100644 (file)
@@ -242,10 +242,8 @@ static int ip_frag_reinit(struct ipq *qp)
 {
        unsigned int sum_truesize = 0;
 
-       if (!mod_timer(&qp->q.timer, jiffies + qp->q.fqdir->timeout)) {
-               refcount_inc(&qp->q.refcnt);
+       if (!mod_timer_pending(&qp->q.timer, jiffies + qp->q.fqdir->timeout))
                return -ETIMEDOUT;
-       }
 
        sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments,
                                              SKB_DROP_REASON_FRAG_TOO_FAR);