IPVS: Fix use-after-free issue in ip_vs_unbind_app()
Subsystems (4)
PATCH 1/1 IPVS: Fix use-after-free issue in ip_vs_unbind_app()
When exiting a network namespace, in cleanup_net()->ops_undo_list(), ip_vs_ftp_ops->exit() is called before ip_vs_core_ops->exit_batch().
The ip_vs_app ip_vs_ftp and its incarnations will be freed by unregister_ip_vs_app(). However, there could still be connections bound to ip_vs_ftp's incarnation. cp->app points to the free'd incarnation, which will be accessed later by __ip_vs_cleanup_batch()->ip_vs_conn_net_cleanup()->ip_vs_conn_flush()->ip_vs_conn_del()-> ip_vs_conn_expire()->ip_vs_unbind_app(), causing a uaf. This vulnarability can lead to a local privilege escalation.
Reproduction steps:
1. create a ipvs service on (127.0.0.1:21)
2. create a ipvs destination on the service, to (127.0.0.1:<any>)
3. send a tcp packet to (127.0.0.1:21)
4. exit the network namespace
I think the fix should flush all connection to ftp before unregistration. The simpler fix is to delete ip_vs_ftp_ops->exit, and defer the unregistration of ip_vs_ftp to ip_vs_app_net_cleanup(), which will unregister all ip_vs_app. It's after ip_vs_conn_net_cleanup() so there is no uaf issue. This patch seems to solve the issue but has't been fully tested yet, and is also not graceful.
--- net/netfilter/ipvs/ip_vs_ftp.c | 13 ------------- 1 file changed, 13 deletions(-)
net/netfilter/ipvs/ip_vs_ftp.c +0 -13
@@ -598,22 +598,9 @@ static int __net_init __ip_vs_ftp_init(struct net *net)unregister_ip_vs_app (ipvs, &ip_vs_ftp);return ret; } -/* -* netns exit -*/ -static void __ip_vs_ftp_exit (struct net *net) -{ -struct netns_ipvs *ipvs =net_ipvs (net); - -if (!ipvs) -return ; - -unregister_ip_vs_app (ipvs, &ip_vs_ftp); -}static struct pernet_operations ip_vs_ftp_ops = { .init = __ip_vs_ftp_init, - .exit = __ip_vs_ftp_exit, };static int __init ip_vs_ftp_init (void )
Signature · 1 line
2.34.1
Julian Anastasov
maintainerHello,
Quoted 32 lines · Slavin Liu
When exiting a network namespace, in cleanup_net()->ops_undo_list(),
ip_vs_ftp_ops->exit()is called beforeip_vs_core_ops->exit_batch().
Theip_vs_appip_vs_ftpand its incarnations will be freed byunregister_ip_vs_app(). However, there could still be connections bound to ip_vs_ftp's incarnation.cp->apppoints to the free'd incarnation, which will be accessed later by __ip_vs_cleanup_batch()->ip_vs_conn_net_cleanup()->ip_vs_conn_flush()->ip_vs_conn_del()-> ip_vs_conn_expire()->ip_vs_unbind_app(), causing a uaf. This vulnarability can lead to a local privilege escalation.Reproduction steps:
1. create a ipvs service on (127.0.0.1:21)
2. create a ipvs destination on the service, to (127.0.0.1:<any>)
3. send a tcp packet to (127.0.0.1:21)
4. exit the network namespaceI think the fix should flush all connection to ftp before unregistration. The simpler fix is to delete
ip_vs_ftp_ops->exit, and defer the unregistration ofip_vs_ftptoip_vs_app_net_cleanup(), which will unregister allip_vs_app. It's afterip_vs_conn_net_cleanup()so there is no uaf issue. This patch seems to solve the issue but has't been fully tested yet, and is also not graceful.Signed-off-by Slavin Liu <[email protected]>--- net/netfilter/ipvs/ip_vs_ftp.c | 13 ------------- 1 file changed, 13 deletions(-)net/netfilter/ipvs/ip_vs_ftp.c +0
-0@@ -598,22 +598,9 @@ static int __net_init __ip_vs_ftp_init(struct net *net)unregister_ip_vs_app (ipvs, &ip_vs_ftp);
net/netfilter/ipvs/ip_vs_ftp.c static int __net_init __ip_vs_ftp_init(struct net *net) · line 608
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ftp.c +0
-8return ret; } -/* -* netns exit -*/ -static void __ip_vs_ftp_exit (struct net *net) -{ -struct netns_ipvs *ipvs =net_ipvs (net); - -if (!ipvs)
What if we change this 'if' check to:
if (!ipvs || !ipvs->enable)
If netns exits, the cleanup order is:
1. exit handlers for pernet device (&ipvs_core_dev_ops) where
ipvs->enable is set to 0
2. exit handlers for ip_vs_ftp_ops (as last pernet subsys)
By checking for ipvs->enable, we should not call unregister_ip_vs_app() in __ip_vs_ftp_exit() because there can be existing conns with valid cp->app
3. exit handlers for pernet subsys (&ipvs_core_ops) where ip_vs_app_net_cleanup() unregisters all apps for netns. Here the apps will be freed after all conns are gone
Why we should keep the ftp pernet subsys: because when
there are no conns using the app, the module can be removed and it must unregister its app from all netns where ipvs->enable will be 1.
But note that we have a pending patch that changes
the access to ipvs->enable to use READ_ONCE/WRITE_ONCE:
https://archive.linuxvirtualserver.org/html/lvs-devel/2025-09/msg00000.html
Quoted 13 lines
net/netfilter/ipvs/ip_vs_ftp.c +0
-5-return ; - -unregister_ip_vs_app (ipvs, &ip_vs_ftp); -}static struct pernet_operations ip_vs_ftp_ops = { .init = __ip_vs_ftp_init, - .exit = __ip_vs_ftp_exit, };static int __init ip_vs_ftp_init (void )Signature · 1 line
2.34.1
Regards
--
Julian Anastasov <[email protected]>
PATCH v2 1/1 ipvs: Check ipvs->enable before unregistration in __ip_vs_ftp_exit()
In reply to Slavin LiuOn netns cleanup path, before unregistration in __ip_vs_ftp_exit(), there could still be existing conns with valid cp->app.
Suggested by Julian, this patch fixes this issue by checking ipvs->enable to ensure the right order of cleanup:
1. Set ipvs->enable to 0 in ipvs_core_dev_ops->exit_batch()
2. Skip app unregistration in ip_vs_ftp_ops->exit() by
checking ipvs->enable
3. Flush all conns in ipvs_core_ops->exit_batch()
4. Unregister all apps in ipvs_core_ops->exit_batch()
Access ipvs->enable by READ_ONCE to avoid concurrency issue.
--- net/netfilter/ipvs/ip_vs_ftp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_ftp.c +1 -1
@@ -605,7 +605,7 @@ static void __ip_vs_ftp_exit(struct net *net) {struct netns_ipvs *ipvs =net_ipvs (net); -if (!ipvs) +if (!ipvs || !READ_ONCE (ipvs->enable))return ;unregister_ip_vs_app (ipvs, &ip_vs_ftp);
Signature · 1 line
2.34.1
Julian Anastasov
maintainerRe: [PATCH v2] ipvs: Check ipvs->enable before unregistration in __ip_vs_ftp_exit()
Hello,
Quoted 24 lines · Slavin Liu
On netns cleanup path, before unregistration in
__ip_vs_ftp_exit(), there could still be existing conns with validcp->app.Suggested by Julian, this patch fixes this issue by checking
ipvs->enableto ensure the right order of cleanup:
1. Setipvs->enableto 0 inipvs_core_dev_ops->exit_batch()
2. Skip app unregistration inip_vs_ftp_ops->exit()bychecking ipvs->enable3. Flush all conns in
ipvs_core_ops->exit_batch()
4. Unregister all apps inipvs_core_ops->exit_batch()Access
ipvs->enablebyREAD_ONCEto avoid concurrency issue.Suggested-by Julian Anastasov <[email protected]>Signed-off-by Slavin Liu <[email protected]>--- net/netfilter/ipvs/ip_vs_ftp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/netfilter/ipvs/ip_vs_ftp.c +0
-0
net/netfilter/ipvs/ip_vs_ftp.c static void __ip_vs_ftp_exit(struct net *net) · line 608
Quoted 5 lines
net/netfilter/ipvs/ip_vs_ftp.c +1
-1@@ -605,7 +605,7 @@ static void __ip_vs_ftp_exit(struct net *net) {struct netns_ipvs *ipvs =net_ipvs (net); -if (!ipvs) +if (!ipvs || !READ_ONCE (ipvs->enable))
Ops, I forgot that ipvs->enable is set later when
service is added. May be we have to add some new global flag for this in ip_vs_ftp.c:
static bool removing;
We will set it in ip_vs_ftp_exit() before callingunregister_pernet_subsys() and the above check will become:
if (!ipvs || !removing) Also, we have to add Fixes tag, this looks the
one that starts to remove apps from netns exit handler:
Regards
--
Julian Anastasov <[email protected]>
RFC PATCH v3 1/1 ipvs: Defer ip_vs_ftp unregister during netns cleanup
In reply to Slavin LiuOn the netns cleanup path, __ip_vs_ftp_exit() may unregister ip_vs_ftp before connections with valid cp->app pointers are flushed, leading to a use-after-free.
Fix this by introducing a global module_removing flag, set to true in ip_vs_ftp_exit() before unregistering the pernet subsystem. In __ip_vs_ftp_exit(), skip ip_vs_ftp unregister if called during netns cleanup (when module_removing is false) and defer it to __ip_vs_cleanup_batch(), which unregisters all apps after all connections are flushed. If called during module exit, unregister ip_vs_ftp immediately.
--- net/netfilter/ipvs/ip_vs_ftp.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_ftp.c +4 -1
@@ -53,6 +53,7 @@ enum { IP_VS_FTP_EPSV, }; +static bool module_exiting;/* * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper * First port is set to the default port.
@@ -605,7 +606,7 @@ static void __ip_vs_ftp_exit(struct net *net) {struct netns_ipvs *ipvs =net_ipvs (net); -if (!ipvs) +if (!ipvs || !module_exiting)return ;unregister_ip_vs_app (ipvs, &ip_vs_ftp);
@@ -627,7 +628,9 @@ static int __init ip_vs_ftp_init(void)*/ static void __exit ip_vs_ftp_exit(void) { + module_exiting = true;unregister_pernet_subsys (&ip_vs_ftp_ops); + module_exiting = false;/* rcu_barrier() is called by netns */ }
Signature · 1 line
2.34.1
PATCH v4 1/1 ipvs: Defer ip_vs_ftp unregister during netns cleanup
On the netns cleanup path, __ip_vs_ftp_exit() may unregister ip_vs_ftp before connections with valid cp->app pointers are flushed, leading to a use-after-free.
Fix this by introducing a global exiting_module flag, set to true in ip_vs_ftp_exit() before unregistering the pernet subsystem. In __ip_vs_ftp_exit(), skip ip_vs_ftp unregister if called during netns cleanup (when module_removing is false) and defer it to __ip_vs_cleanup_batch(), which unregisters all apps after all connections are flushed. If called during module exit, unregister ip_vs_ftp immediately.
--- net/netfilter/ipvs/ip_vs_ftp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/ipvs/ip_vs_ftp.c +3 -1
@@ -53,6 +53,7 @@ enum { IP_VS_FTP_EPSV, }; +static bool exiting_module;/* * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper * First port is set to the default port.
@@ -605,7 +606,7 @@ static void __ip_vs_ftp_exit(struct net *net) {struct netns_ipvs *ipvs =net_ipvs (net); -if (!ipvs) +if (!ipvs || !exiting_module)return ;unregister_ip_vs_app (ipvs, &ip_vs_ftp);
@@ -627,6 +628,7 @@ static int __init ip_vs_ftp_init(void)*/ static void __exit ip_vs_ftp_exit(void) { + exiting_module = true;unregister_pernet_subsys (&ip_vs_ftp_ops);/* rcu_barrier() is called by netns */ }
Signature · 1 line
2.34.1
Julian Anastasov
maintainerRe: [PATCH v4] ipvs: Defer ip_vs_ftp unregister during netns cleanup
Hello,
Quoted 8 lines · Slavin Liu
On the netns cleanup path,
__ip_vs_ftp_exit()may unregisterip_vs_ftpbefore connections with validcp->apppointers are flushed, leading to a use-after-free.Fix this by introducing a global
exiting_moduleflag, set to true inip_vs_ftp_exit()before unregistering the pernet subsystem. In__ip_vs_ftp_exit(), skipip_vs_ftpunregister if called during netns cleanup (whenmodule_removingis false) and defer it to
Pablo, can you change here module_removing to exiting_module
before applying?
Quoted 7 lines
__ip_vs_cleanup_batch(), which unregisters all apps after all connections are flushed. If called during module exit, unregisterip_vs_ftpimmediately.Fixes 61b1ab4583e2 ("IPVS: netns, add basic init per netns.")Suggested-by Julian Anastasov <[email protected]>Signed-off-by Slavin Liu <[email protected]>
Looks good to me for the nf tree after above text is
changed, thanks!
Quoted 35 lines
--- net/netfilter/ipvs/ip_vs_ftp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)net/netfilter/ipvs/ip_vs_ftp.c +3
-1@@ -53,6 +53,7 @@ enum { IP_VS_FTP_EPSV, }; +static bool exiting_module;/* * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper * First port is set to the default port. @@ -605,7 +606,7 @@ static void __ip_vs_ftp_exit(struct net *net) {struct netns_ipvs *ipvs =net_ipvs (net); -if (!ipvs) +if (!ipvs || !exiting_module)return ;unregister_ip_vs_app (ipvs, &ip_vs_ftp);@@ -627,6 +628,7 @@ static int __init ip_vs_ftp_init(void)*/ static void __exit ip_vs_ftp_exit(void) { + exiting_module = true;unregister_pernet_subsys (&ip_vs_ftp_ops);/* rcu_barrier() is called by netns */ }Signature · 1 line
2.34.1
Regards
--
Julian Anastasov <[email protected]>
Florian Westphal
maintainerRe: [PATCH v4] ipvs: Defer ip_vs_ftp unregister during netns cleanup
Quoted 6 lines · Julian Anastasov
Quoted 3 lines
Fixes 61b1ab4583e2 ("IPVS: netns, add basic init per netns.")Suggested-by Julian Anastasov <[email protected]>Signed-off-by Slavin Liu <[email protected]>Looks good to me for the nf tree after above text is
changed, thanks!
Thaks Julian for reviewing, i pushed it to nf:testing.