Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Pablo Neira Ayuso
Fri, 13 Nov 2026 01:14
[PATCH net 04/15] ipvs: avoid stack overflow from recursive connection expiration
Author: Pablo Neira Ayuso
Date: Fri, 13 Nov 2026 01:14
Date: Fri, 13 Nov 2026 01:14
134 lines
12674 bytes
12674 bytes
From: Zihan Xi <zihanx@nebusec.ai>
When a controlled IPVS connection expires, its controller may be expired
synchronously if it has no remaining controlled connections. A chain of
controlled connections can then recurse through ip_vs_conn_expire() and
exhaust the kernel stack during namespace cleanup.
Continue expiration with the controller after the current connection has
been fully cleaned up instead of calling ip_vs_conn_del() recursively. Keep
the expiration walk under RCU, preserve the immediate-drop timeout for a
controller with its own controller, and switch to deletion mode before the
next iteration.
This keeps controlled-connection cleanup synchronous while using one stack
frame for the whole chain. The timer callback race during connection
deletion is handled by the preceding connection-deletion fix.
This patch depends on "ipvs: fix problems during connection deletion" to
protect the iterative controller cleanup from the timer callback race.
Fixes: f9200a52eedf ("ipvs: avoid expiring many connections from timer")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Co-developed-by: Luxing Yin <root@tr0jan.top>
Signed-off-by: Luxing Yin <root@tr0jan.top>
Signed-off-by: Zihan Xi <zihanx@nebusec.ai>
Signed-off-by: Julian Anastasov <ja@ssi.bg>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
net/netfilter/ipvs/ip_vs_conn.c | 40 ++++++++++++++++++++++++---------
1 file changed, 29 insertions(+), 11 deletions(-)
diff --git a/net/netfilter/ipvs/ip_vs_conn.c b/net/netfilter/ipvs/ip_vs_conn.c
index 86c5e6c7b5af..27db8d558a03 100644
--- a/net/netfilter/ipvs/ip_vs_conn.c
+++ b/net/netfilter/ipvs/ip_vs_conn.c
@@ -1336,9 +1336,23 @@ static void ip_vs_conn_rcu_free(struct rcu_head *head)
kmem_cache_free(ip_vs_conn_cachep, cp);
}
-/* Try to delete connection while not holding reference.
+/* Try to steal the reference from the pending timer.
* It can be called concurrently and always under RCU lock
* to protect from connection deletion from others.
+ */
+static bool ip_vs_conn_del_reference(struct ip_vs_conn *cp)
+{
+ /* Steal reference from the pending timer */
+ if (!timer_delete(&cp->timer))
+ return false;
+
+ /* Drop cp->control chain too */
+ if (cp->control)
+ cp->timeout = 0;
+ return true;
+}
+
+/* Try to delete connection while not holding reference.
* Used when we need to remove the connection immediately
* and without overloading the timer processing.
* OTOH, we use ip_vs_conn_expire_now() when we prefer to
@@ -1347,13 +1361,8 @@ static void ip_vs_conn_rcu_free(struct rcu_head *head)
*/
static void ip_vs_conn_del(struct ip_vs_conn *cp)
{
- /* Steal reference from the pending timer */
- if (timer_delete(&cp->timer)) {
- /* Drop cp->control chain too */
- if (cp->control)
- cp->timeout = 0;
+ if (ip_vs_conn_del_reference(cp))
ip_vs_conn_expire(&cp->timer);
- }
}
/* Connection is removed in the following steps:
@@ -1366,6 +1375,9 @@ static void ip_vs_conn_expire(struct timer_list *t)
struct ip_vs_conn *cp = timer_container_of(cp, t, timer);
struct netns_ipvs *ipvs = cp->ipvs;
+ rcu_read_lock();
+
+repeat:
/*
* do I control anybody?
*/
@@ -1375,19 +1387,18 @@ static void ip_vs_conn_expire(struct timer_list *t)
/* Unlink conn if not referenced anymore */
if (likely(ip_vs_conn_unlink(cp))) {
struct ip_vs_conn *ct = cp->control;
+ bool next = false;
/* does anybody control me? */
if (ct) {
- rcu_read_lock();
ip_vs_control_del(cp);
/* Drop CTL or non-assured TPL if not used anymore */
if (!cp->timeout && !atomic_read(&ct->n_control) &&
(!(ct->flags & IP_VS_CONN_F_TEMPLATE) ||
!(ct->state & IP_VS_CTPL_S_ASSURED))) {
IP_VS_DBG(4, "drop controlling connection\n");
- ip_vs_conn_del(ct);
+ next = ip_vs_conn_del_reference(ct);
}
- rcu_read_unlock();
}
if ((cp->flags & IP_VS_CONN_F_NFCT) &&
@@ -1414,7 +1425,11 @@ static void ip_vs_conn_expire(struct timer_list *t)
else
call_rcu(&cp->rcu_head, ip_vs_conn_rcu_free);
atomic_dec(&ipvs->conn_count);
- return;
+ if (next) {
+ cp = ct;
+ goto repeat;
+ }
+ goto out;
}
expire_later:
@@ -1429,6 +1444,9 @@ static void ip_vs_conn_expire(struct timer_list *t)
ip_vs_sync_conn(ipvs, cp, sysctl_sync_threshold(ipvs));
__ip_vs_conn_put_timer(cp);
+
+out:
+ rcu_read_unlock();
}
/* Modify timer, so that it expires as soon as possible.
--
2.47.3
Re: [PATCH net 04/15] ipvs: avoid stack overflow from recursive connection expiration
Author: netdev-bot+sashiko
Date: Sat, 14 Nov 2026 02:15
Date: Sat, 14 Nov 2026 02:15
90 lines
10883 bytes
10883 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message says the recursive chain walk exhausts the
kernel stack "during namespace cleanup".
--
ipvs: avoid stack overflow from recursive connection expiration
This commit replaces the recursive controller expiration in
ip_vs_conn_expire() with an iterative walk. It no longer calls
ip_vs_conn_del() on the controller. Instead, the new
ip_vs_conn_del_reference() helper steals the controller's timer
reference, and the function loops back with cp = ct inside a single
rcu_read_lock() section.
> When a controlled IPVS connection expires, its controller may be expired
> synchronously if it has no remaining controlled connections. A chain of
> controlled connections can then recurse through ip_vs_conn_expire() and
> exhaust the kernel stack during namespace cleanup.
[Severity: Low]
This isn't a bug, but is "during namespace cleanup" the only place the
recursion can happen?
ip_vs_conn_del_reference() is the only code that sets cp->timeout = 0,
which arms the drop-controller chain. It is reached from runtime callers
as well as from the netns flush.
ip_vs_random_dropentry() runs from defense_work when drop_entry defense
is active:
net/netfilter/ipvs/ip_vs_conn.c:ip_vs_random_dropentry() {
...
drop:
IP_VS_DBG(4, "drop connection\n");
ip_vs_conn_del(cp);
...
}
ip_vs_expire_nodest_conn_flush() runs when a real server becomes
unavailable and expire_nodest_conn is set:
net/netfilter/ipvs/ip_vs_conn.c:ip_vs_expire_nodest_conn_flush() {
...
ip_vs_conn_del(cp);
if (cp_c && !atomic_read(&cp_c->n_control)) {
IP_VS_DBG(4, "del controlling connection\n");
ip_vs_conn_del(cp_c);
}
...
}
The chain can also run from the timer softirq. If ip_vs_conn_unlink()
fails because refcnt > 1, the expire_later path keeps timeout == 0 when
n_control == 0:
net/netfilter/ipvs/ip_vs_conn.c:ip_vs_conn_expire() {
...
expire_later:
...
if (cp->timeout || atomic_read(&cp->n_control))
cp->timeout = 60 * HZ;
...
__ip_vs_conn_put_timer(cp);
}
The timer is then re-armed at jiffies + 0. The next callback walks the
controller chain, and before this patch that walk recursed on the
softirq stack.
The loop in ip_vs_conn_expire() covers all of these entry points, so the
code change itself looks fine. Could the commit message also mention the
runtime paths, since stable and CVE triage will rely on the stated
impact?
The commit message also doesn't explain how a deep chain is built. The
ip_vs_conn_expire_now() comment still says chains are at most
DATA->CTL->TPL. Both points are handled by the follow-up patch in the
series, "ipvs: reject FTP control ports as data ports". It rejects
control ports as FTP data ports in ip_vs_ftp_out() and ip_vs_ftp_in(),
which bounds the chain again.
--
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113011447.2357709-2-pablo%40netfilter.org
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads