* [PATCH net v2] l2tp: bound the reorder queue
@ 2026-11-23 16:20 Ren Wei
0 siblings, 0 replies; 2+ messages in thread
From: Ren Wei @ 2026-11-23 16:20 UTC (permalink / raw)
To: netdev, edumazet
Cc: davem, kuba, pabeni, willemb, horms, gnault, kuniyu,
michael.bommarito, alice.kernel, nicoyip.dev, hengyul, jchapman,
vega, caoruide123, weir
From: Ruide Cao <caoruide123@gmail.com>
The receive-window check accepts every sequence number in the large
window. If the peer omits the expected packet, distinct future packets
can therefore fill the ordered reorder queue. Since insertion walks the
queue and expiration was checked only while receiving another packet,
this allowed unbounded memory and CPU use.
Reject duplicate sequence numbers and cap the reorder queue at 64
packets. Packets beyond the cap are discarded, except that the expected
sequence number is admitted so an in-order packet can drain the queue.
The no-reorder tail-enqueue path uses the same exception, and also
admits a sequenced recovery packet once reorder_skip is set. That
exception is limited to a single extra slot and is refused when the
queue head cannot be dequeued, so a stalled head cannot grow the
queue without bound. Advance session->nr under the reorder queue lock
when unlinking a sequenced skb, so the cap checks observe a consistent
expected sequence number. Declare the dequeued flag first in
l2tp_recv_dequeue() to follow reverse xmas tree.
The existing timeout recovery then skips a missing sequence number when
the queue limit is reached.
Use a session timer to run the existing dequeue path at the head
expiration and shut it down before purging a session. This bounds the
queue and services expiration even when the peer stops sending.
Insert into the reorder queue by walking from the tail so an in-order
packet after a gap does not scan the queue from the head. Compare
sequence numbers only for skbs with has_seq set, and use modular
comparison in the 16-bit or 24-bit space so wrap-around is handled.
Refuse enqueue once session->dead is set under the queue lock, and
purge the queue again in l2tp_session_free() so an in-flight receive
path cannot leak an skb after the deletion purge.
Fixes: 3557baabf280 ("[L2TP]: PPP over L2TP driver core")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Signed-off-by: Ruide Cao <caoruide123@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
Changes in v2:
- Insert in-order-after-gap packets by walking the reorder queue from
the tail, as suggested by Eric Dumazet, so insertion does not always
scan from the head.
- Compare Ns only when has_seq is set, and use modular comparison in
the L2TPv2 16-bit / L2TPv3 24-bit sequence space instead of a raw
'>' check.
- Check session->dead under reorder_q.lock at the enqueue sites, and
purge the queue again in l2tp_session_free(), so an in-flight receive
path cannot leak an skb after timer_shutdown_sync() and the deletion
purge.
- Advance session->nr under reorder_q.lock when unlinking a sequenced
skb, so the full-queue expected-Ns exception cannot race with dequeue.
- On the no-reorder tail path, admit one sequenced expected or recovery
packet when the queue is at the cap, but only if the head can be
dequeued. Further packets are rejected until dequeue makes progress,
so a stalled head cannot bypass the cap.
- Declare the dequeued flag first in l2tp_recv_dequeue() to follow
reverse xmas tree.
- Keep dropping non-expected packets when the queue is full; that is
the cap. After the head times out, reorder_skip lets the queue drain.
- Leave the dequeue-timer dst lifetime unchanged: teardown already
uses timer_shutdown_sync() and a second purge, and this path does
not dereference dst on its own.
- Leave concurrent timer and receive dequeue otherwise unchanged;
the Nr race is fixed by updating session->nr under the queue lock.
- Leave the l2tp_recv_data_seq() return-value comment unchanged; a
rejected skb stays with the caller and is freed on the discard path.
- Leave timer rearming unchanged if reorder_timeout is shortened at
runtime; a tighter deadline is left for a follow-up.
- v1 link:
https://lore.kernel.org/netdev/cover.1790136447.git.caoruide123@gmail.com/
---
net/l2tp/l2tp_core.c | 136 +++++++++++++++++++++++++++++++++++++------
net/l2tp/l2tp_core.h | 2 +
2 files changed, 121 insertions(+), 17 deletions(-)
diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
index 0cd3e38bf662..42900c3cff63 100644
--- a/net/l2tp/l2tp_core.c
+++ b/net/l2tp/l2tp_core.c
@@ -82,6 +82,7 @@
#define L2TP_SL_SEQ_MASK 0x00ffffff
#define L2TP_HDR_SIZE_MAX 14
+#define L2TP_REORDER_MAX_QUEUE 64
/* Default trace flags */
#define L2TP_DEFAULT_DEBUG_FLAGS 0
@@ -166,9 +167,13 @@ static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
kfree_rcu(tunnel, rcu);
}
+static void l2tp_session_queue_purge(struct l2tp_session *session);
+
static void l2tp_session_free(struct l2tp_session *session)
{
trace_free_session(session);
+ /* Catch a post-purge enqueue from an in-flight receive path. */
+ l2tp_session_queue_purge(session);
if (session->tunnel)
l2tp_tunnel_put(session->tunnel);
kfree_rcu(session, rcu);
@@ -636,30 +641,104 @@ EXPORT_SYMBOL_GPL(l2tp_session_register);
* Receive data handling
*****************************************************************************/
+/* True if sequence number a precedes b in the 16-bit (L2TPv2) or
+ * 24-bit (L2TPv3) sequence space.
+ */
+static bool l2tp_seq_before(const struct l2tp_session *session, u32 a, u32 b)
+{
+ u32 dist = (b - a) & session->nr_max;
+
+ return dist && dist <= session->nr_max / 2;
+}
+
/* Queue a skb in order. We come here only if the skb has an L2TP sequence
- * number.
+ * number. Walk from the tail so an in-order packet after a gap is not
+ * inserted by scanning the whole queue from the head.
*/
static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
{
struct sk_buff *skbp;
- struct sk_buff *tmp;
u32 ns = L2TP_SKB_CB(skb)->ns;
+ bool saw_seq = false;
spin_lock_bh(&session->reorder_q.lock);
- skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
- if (L2TP_SKB_CB(skbp)->ns > ns) {
- __skb_queue_before(&session->reorder_q, skbp, skb);
- atomic_long_inc(&session->stats.rx_oos_packets);
+ if (test_bit(0, &session->dead)) {
+ kfree_skb(skb);
+ goto out;
+ }
+ if (skb_queue_len(&session->reorder_q) >= L2TP_REORDER_MAX_QUEUE &&
+ ns != session->nr) {
+ atomic_long_inc(&session->stats.rx_seq_discards);
+ atomic_long_inc(&session->stats.rx_errors);
+ kfree_skb(skb);
+ goto out;
+ }
+
+ skb_queue_reverse_walk(&session->reorder_q, skbp) {
+ if (!L2TP_SKB_CB(skbp)->has_seq)
+ continue;
+ if (unlikely(L2TP_SKB_CB(skbp)->ns == ns)) {
+ atomic_long_inc(&session->stats.rx_seq_discards);
+ atomic_long_inc(&session->stats.rx_errors);
+ kfree_skb(skb);
goto out;
}
+ if (l2tp_seq_before(session, L2TP_SKB_CB(skbp)->ns, ns)) {
+ __skb_queue_after(&session->reorder_q, skbp, skb);
+ if (saw_seq)
+ atomic_long_inc(&session->stats.rx_oos_packets);
+ goto out;
+ }
+ saw_seq = true;
}
- __skb_queue_tail(&session->reorder_q, skb);
+ if (saw_seq)
+ atomic_long_inc(&session->stats.rx_oos_packets);
+ __skb_queue_head(&session->reorder_q, skb);
out:
spin_unlock_bh(&session->reorder_q.lock);
}
+static bool l2tp_recv_queue_tail_skb(struct l2tp_session *session,
+ struct sk_buff *skb)
+{
+ struct l2tp_skb_cb *cb = L2TP_SKB_CB(skb);
+ struct l2tp_skb_cb *hcb;
+ struct sk_buff *head;
+ unsigned int qlen;
+
+ spin_lock_bh(&session->reorder_q.lock);
+ if (test_bit(0, &session->dead))
+ goto reject;
+ /* Admit one expected or recovery packet when the queue is at
+ * the cap, but only if the head can be dequeued. Further
+ * packets are rejected until dequeue makes progress.
+ */
+ qlen = skb_queue_len(&session->reorder_q);
+ if (qlen >= L2TP_REORDER_MAX_QUEUE) {
+ if (qlen > L2TP_REORDER_MAX_QUEUE)
+ goto reject;
+ if (!(cb->has_seq &&
+ (cb->ns == session->nr || session->reorder_skip)))
+ goto reject;
+
+ head = skb_peek(&session->reorder_q);
+ hcb = L2TP_SKB_CB(head);
+ if (!time_after(jiffies, hcb->expires) &&
+ hcb->has_seq && !session->reorder_skip &&
+ hcb->ns != session->nr)
+ goto reject;
+ }
+ __skb_queue_tail(&session->reorder_q, skb);
+ spin_unlock_bh(&session->reorder_q.lock);
+ return true;
+
+reject:
+ spin_unlock_bh(&session->reorder_q.lock);
+ return false;
+}
+
/* Dequeue a single skb.
*/
static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *skb)
@@ -677,13 +756,6 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
atomic_long_inc(&session->stats.rx_packets);
atomic_long_add(length, &session->stats.rx_bytes);
- if (L2TP_SKB_CB(skb)->has_seq) {
- /* Bump our Nr */
- session->nr++;
- session->nr &= session->nr_max;
- trace_session_seqnum_update(session);
- }
-
/* call private receive handler */
if (session->recv_skb)
(*session->recv_skb)(session, skb, L2TP_SKB_CB(skb)->length);
@@ -696,6 +768,7 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
*/
static void l2tp_recv_dequeue(struct l2tp_session *session)
{
+ bool dequeued = false;
struct sk_buff *skb;
struct sk_buff *tmp;
@@ -715,6 +788,7 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
trace_session_pkt_expired(session, cb->ns);
session->reorder_skip = 1;
__skb_unlink(skb, &session->reorder_q);
+ dequeued = true;
kfree_skb(skb);
continue;
}
@@ -729,6 +803,16 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
goto out;
}
__skb_unlink(skb, &session->reorder_q);
+ dequeued = true;
+
+ /* Advance Nr under the queue lock so the enqueue cap
+ * checks observe a consistent expected sequence number.
+ */
+ if (cb->has_seq) {
+ session->nr++;
+ session->nr &= session->nr_max;
+ trace_session_seqnum_update(session);
+ }
/* Process the skb. We release the queue lock while we
* do so to let other contexts process the queue.
@@ -739,9 +823,22 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
}
out:
+ if (skb_queue_empty(&session->reorder_q))
+ timer_delete(&session->reorder_timer);
+ else if (dequeued || !timer_pending(&session->reorder_timer))
+ timer_reduce(&session->reorder_timer,
+ L2TP_SKB_CB(skb_peek(&session->reorder_q))->expires);
spin_unlock_bh(&session->reorder_q.lock);
}
+static void l2tp_recv_dequeue_timer(struct timer_list *timer)
+{
+ struct l2tp_session *session = timer_container_of(session, timer,
+ reorder_timer);
+
+ l2tp_recv_dequeue(session);
+}
+
static int l2tp_seq_check_rx_window(struct l2tp_session *session, u32 nr)
{
u32 nws;
@@ -783,7 +880,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
* sequence number to re-enable packet reception.
*/
if (cb->ns == session->nr) {
- skb_queue_tail(&session->reorder_q, skb);
+ if (!l2tp_recv_queue_tail_skb(session, skb))
+ goto discard;
} else {
u32 nr_oos = cb->ns;
u32 nr_next = (session->nr_oos + 1) & session->nr_max;
@@ -802,7 +900,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
trace_session_pkt_oos(session, cb->ns);
goto discard;
}
- skb_queue_tail(&session->reorder_q, skb);
+ if (!l2tp_recv_queue_tail_skb(session, skb))
+ goto discard;
}
out:
@@ -991,7 +1090,8 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
* reorder queue. This ensures that it will be
* delivered after all previous sequenced skbs.
*/
- skb_queue_tail(&session->reorder_q, skb);
+ if (!l2tp_recv_queue_tail_skb(session, skb))
+ goto discard;
}
/* Try to dequeue as many skbs from reorder_q as we can. */
@@ -1760,6 +1860,7 @@ static void l2tp_session_del_work(struct work_struct *work)
del_work);
l2tp_session_unhash(session);
+ timer_shutdown_sync(&session->reorder_timer);
l2tp_session_queue_purge(session);
if (session->session_close)
(*session->session_close)(session);
@@ -1810,6 +1911,7 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn
tunnel->tunnel_id, session->session_id);
skb_queue_head_init(&session->reorder_q);
+ timer_setup(&session->reorder_timer, l2tp_recv_dequeue_timer, 0);
session->hlist_key = l2tp_v3_session_hashkey(tunnel->sock, session->session_id);
INIT_HLIST_NODE(&session->hlist);
diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h
index 25ae4c25d3ef..bf403598fff8 100644
--- a/net/l2tp/l2tp_core.h
+++ b/net/l2tp/l2tp_core.h
@@ -4,6 +4,7 @@
* Copyright (c) 2008,2009 Katalix Systems Ltd
*/
#include <linux/refcount.h>
+#include <linux/timer.h>
#ifndef _L2TP_CORE_H_
#define _L2TP_CORE_H_
@@ -80,6 +81,7 @@ struct l2tp_session {
u32 nr; /* session NR state (receive) */
u32 ns; /* session NR state (send) */
struct sk_buff_head reorder_q; /* receive reorder queue */
+ struct timer_list reorder_timer;
u32 nr_max; /* max NR. Depends on tunnel */
u32 nr_window_size; /* NR window size */
u32 nr_oos; /* NR of last OOS packet */
base-commit: ad2c7240a3981fd5f0142b61ca913b02ab476ede
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net v2] l2tp: bound the reorder queue
@ 2026-11-23 16:22 netdev-bot+sinfo
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sinfo @ 2026-11-23 16:22 UTC (permalink / raw)
To: Ren Wei
Cc: netdev, edumazet, davem, kuba, pabeni, willemb, horms, gnault,
kuniyu, michael.bommarito, alice.kernel, nicoyip.dev, hengyul,
jchapman, vega, caoruide123
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during
development, syzbot report, manual code inspection, LLM or static
analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical
(e.g. found by LLM and not proven at runtime). If it was triggered
please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 16:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 16:22 [PATCH net v2] l2tp: bound the reorder queue netdev-bot+sinfo
-- strict thread matches above, loose matches on Subject: below --
2026-11-23 16:20 Ren Wei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox