From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796355796; b=tKxf8Iz7/alCsmuloKuPIIRZW2hN3/VOETD9Zuw+7+OjGOn4YMFqqZQYQZsVLq4cwsLX 4h1z5XrX97TR5xyofet1gwvYUTGKF7h18aqMHQCLCahlvttauavfNCYtuchk9Qfqat+Hj ASuimvMEgBlqU69cSRIPMdv/rHTdF0suGMmZA1PVuwTmXD7tQsKrOIK/uvLkHWsUWtXgJ p+Ht8lyjzezikV60FOYgQhWzc7Atnk4ahR79kUYrcgqS3kdl6ZF9Wz44oyJMCXe/FEYh6 JHYTcwfxgEJGu9LTSs17+efJbZHT1xIq3fXoHLW2kBQtwGR2HVhype6mM3z9QsLsGxw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796355796; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=kIaYPYXptR95GfwThe6YnwrMpIeCJqtOBb5/YqxG0Ww=; b=mddhCay0ZZdQ4CGC+H0/1VzeinsEV9b++84DulTlmjJq9eZdy3l8h6JTTBC2ZZrJd05b PTW7b+54QmgJBuWZ0i0vfCKKOmYQGO69mMFHWcyo9C7hydXK7fNLc0VDhxGARPMnr8OpZ TJlI3De92lr5RXxPxCo7/fJTaFF8N7rlzYcxY0ElJXOm5xwWjRHegE+b4eWbqVAz1en+C sqxcXQ7fltDtSrldb7uK4XpWjBi40eX2wGfaHxvJbMcvjLQVgEMc8un6bS6tQPo8EJyuw iYBDlvl7trAfpG3aeRsuUU3tUlc0oPqbbYitAAdNprCqqbsWikmC3Sg/X8uQCDK4U4g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CrHIQQWL; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D183606A322 for ; Fri, 4 Dec 2026 03:43:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CrHIQQWL Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14E021F0092C; Fri, 4 Dec 2026 03:43:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796355795; bh=kIaYPYXptR95GfwThe6YnwrMpIeCJqtOBb5/YqxG0Ww=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CrHIQQWLDn8gZBgp2DHReuyIGMOuIHKiAFGJkpOGgz8XyCF6jsESJdQba+XaxGfy1 CKD89Um74ogazwgl4wAKo0f5++8PNtAX99ZePHH0ntEBKAEfTg/61/MvCfZiDBJrfw YPQe/HaW3UKnGrY4GFqs8478pswhZa0cgSA11wXZVtidE7SXidA1kJP96Pb0DL0Xyt DypR6g2+4ysyIjhDqbx5vVzEStCEcJqHh2szOEH5Vf9ylVvSecBgN0fQRmglCRCzKb fj44YC4BH7B1VCkq21iCUUo9mqC/Rtc0MsJZeJ1BFFCbPEmqsCo8faEqsjdWM9l+uG Kq8+gZYJ31odA== From: Sasha Levin To: stable@vger.kernel.org Cc: Zihan Xi , Vega , Luxing Yin , Julian Anastasov , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 7.3.y 2/2] ipvs: avoid stack overflow from recursive connection expiration Date: Thu, 3 Dec 2026 22:43:13 -0500 Message-ID: <20261204034307.2471506-2-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204034307.2471506-1-sashal@kernel.org> References: <2026120212-slug-verbally-6162@gregkh> <20261204034307.2471506-1-sashal@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Zihan Xi [ Upstream commit d4e4cd04de2c47ed05f837a26edd20c56be3f097 ] 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 Co-developed-by: Luxing Yin Signed-off-by: Luxing Yin Signed-off-by: Zihan Xi Signed-off-by: Julian Anastasov Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- 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 bb4b9b195b58c..83d8a9153a342 100644 --- a/net/netfilter/ipvs/ip_vs_conn.c +++ b/net/netfilter/ipvs/ip_vs_conn.c @@ -1338,9 +1338,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 @@ -1349,13 +1363,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: @@ -1368,6 +1377,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? */ @@ -1377,19 +1389,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) && @@ -1416,7 +1427,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: @@ -1431,6 +1446,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.53.0