Sashiko warns that we have two problems during app module init which can lead to possible use-after-free. 1. We may successfully register app for particular port and netns allowing the connections to take module reference but later if init fails we still hold these references after the init phase, long after the module is freed. 2. We may successfully register the ftp app for some netns but later if init fails we still leak the registered apps and incarnations after the init phase, long after the module is freed. To fix the first problem we will disallow try_module_get() to be called in MODULE_STATE_COMING state, i.e. while module init is still running. For the second problem, we recently added exiting_module variable to catch the case when the ftp module was exiting but this does not cover the init phase. Fix it by using defer_unreg variable which is set while not in module init/exit phase to defer the app pernet unregistration during cleanup_net to ip_vs_app_net_cleanup() after all module references are dropped. Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261103140308.591690-3-pablo%40netfilter.org Fixes: 134121bfd99a ("ipvs: Defer ip_vs_ftp unregister during netns cleanup") Fixes: be97fdb5fbcc ("ipvs: generalize app registration in netns") Signed-off-by: Julian Anastasov --- net/netfilter/ipvs/ip_vs_app.c | 9 ++++++++- net/netfilter/ipvs/ip_vs_ftp.c | 24 +++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/net/netfilter/ipvs/ip_vs_app.c b/net/netfilter/ipvs/ip_vs_app.c index 2c1b47702da2..fdec626124be 100644 --- a/net/netfilter/ipvs/ip_vs_app.c +++ b/net/netfilter/ipvs/ip_vs_app.c @@ -43,7 +43,14 @@ static DEFINE_MUTEX(__ip_vs_app_mutex); */ static inline int ip_vs_app_get(struct ip_vs_app *app) { - return try_module_get(app->module); + struct module *mod = app->module; + + /* Failed module init should return without any module references, + * so allow access to the module only after the init phase. + */ + if (mod && module_is_coming(mod)) + return false; + return try_module_get(mod); } diff --git a/net/netfilter/ipvs/ip_vs_ftp.c b/net/netfilter/ipvs/ip_vs_ftp.c index 44de99f4d187..0a6440c2132c 100644 --- a/net/netfilter/ipvs/ip_vs_ftp.c +++ b/net/netfilter/ipvs/ip_vs_ftp.c @@ -52,7 +52,8 @@ enum { IP_VS_FTP_EPSV, }; -static bool exiting_module; +static bool defer_unreg; /* Defer unreg on cleanup_net */ + /* * List of ports (up to IP_VS_APP_MAX_PORTS) to be handled by helper * First port is set to the default port. @@ -629,10 +630,12 @@ static void __ip_vs_ftp_exit(struct net *net) { struct netns_ipvs *ipvs = net_ipvs(net); - if (!ipvs || !exiting_module) - return; - - unregister_ip_vs_app(ipvs, &ip_vs_ftp); + /* Unregister app only on module init/exit, on netns cleanup + * it is done later in ip_vs_app_net_cleanup(). + * It is equally correct to check for !net->is_dying. + */ + if (ipvs && !defer_unreg) + unregister_ip_vs_app(ipvs, &ip_vs_ftp); } static struct pernet_operations ip_vs_ftp_ops = { @@ -643,7 +646,14 @@ static struct pernet_operations ip_vs_ftp_ops = { static int __init ip_vs_ftp_init(void) { /* rcu_barrier() is called by netns on error */ - return register_pernet_subsys(&ip_vs_ftp_ops); + int ret = register_pernet_subsys(&ip_vs_ftp_ops); + + if (ret >= 0) { + defer_unreg = true; + /* commit before switching to MODULE_STATE_LIVE */ + smp_wmb(); + } + return ret; } /* @@ -651,7 +661,7 @@ static int __init ip_vs_ftp_init(void) */ static void __exit ip_vs_ftp_exit(void) { - exiting_module = true; + defer_unreg = false; unregister_pernet_subsys(&ip_vs_ftp_ops); /* rcu_barrier() is called by netns */ } -- 2.56.1