From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
andrew+netdev@lunn.ch, horms@kernel.org, pablo@netfilter.org,
fw@strlen.de, netfilter-devel@vger.kernel.org,
willemdebruijn.kernel@gmail.com, kuniyu@google.com,
Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net 1/4] inet: frags: avoid theoretical race in ip_frag_reinit()
Date: Sat, 6 Dec 2025 17:09:39 -0800 [thread overview]
Message-ID: <20251207010942.1672972-2-kuba@kernel.org> (raw)
In-Reply-To: <20251207010942.1672972-1-kuba@kernel.org>
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")
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
net/ipv4/inet_fragment.c | 4 +++-
net/ipv4/ip_fragment.c | 4 +---
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/net/ipv4/inet_fragment.c b/net/ipv4/inet_fragment.c
index 025895eb6ec5..30f4fa50ee2d 100644
--- a/net/ipv4/inet_fragment.c
+++ b/net/ipv4/inet_fragment.c
@@ -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;
diff --git a/net/ipv4/ip_fragment.c b/net/ipv4/ip_fragment.c
index f7012479713b..d7bccdc9dc69 100644
--- a/net/ipv4/ip_fragment.c
+++ b/net/ipv4/ip_fragment.c
@@ -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);
--
2.52.0
next prev parent reply other threads:[~2025-12-07 1:10 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-07 1:09 [PATCH net 0/4] inet: frags: flush pending skbs in fqdir_pre_exit() Jakub Kicinski
2025-12-07 1:09 ` Jakub Kicinski [this message]
2025-12-08 15:18 ` [PATCH net 1/4] inet: frags: avoid theoretical race in ip_frag_reinit() Eric Dumazet
2025-12-07 1:09 ` [PATCH net 2/4] inet: frags: add inet_frag_queue_flush() Jakub Kicinski
2025-12-08 15:19 ` Eric Dumazet
2025-12-07 1:09 ` [PATCH net 3/4] inet: frags: flush pending skbs in fqdir_pre_exit() Jakub Kicinski
2025-12-08 15:17 ` Eric Dumazet
2025-12-07 1:09 ` [PATCH net 4/4] netfilter: conntrack: warn when cleanup is stuck Jakub Kicinski
2025-12-07 10:21 ` Florian Westphal
2025-12-08 15:20 ` Eric Dumazet
2025-12-10 9:50 ` [PATCH net 0/4] inet: frags: flush pending skbs in fqdir_pre_exit() patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251207010942.1672972-2-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=fw@strlen.de \
--cc=horms@kernel.org \
--cc=kuniyu@google.com \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).