net/smc: Fix possible leaked pernet namespace in smc_init()
authorChen Zhongjin <chenzhongjin@huawei.com>
Tue, 1 Nov 2022 09:37:22 +0000 (17:37 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 10 Nov 2022 17:15:31 +0000 (18:15 +0100)
[ Upstream commit 62ff373da2534534c55debe6c724c7fe14adb97f ]

In smc_init(), register_pernet_subsys(&smc_net_stat_ops) is called
without any error handling.
If it fails, registering of &smc_net_ops won't be reverted.
And if smc_nl_init() fails, &smc_net_stat_ops itself won't be reverted.

This leaves wild ops in subsystem linkedlist and when another module
tries to call register_pernet_operations() it triggers page fault:

BUG: unable to handle page fault for address: fffffbfff81b964c
RIP: 0010:register_pernet_operations+0x1b9/0x5f0
Call Trace:
  <TASK>
  register_pernet_subsys+0x29/0x40
  ebtables_init+0x58/0x1000 [ebtables]
  ...

Fixes: 194730a9beb5 ("net/smc: Make SMC statistics network namespace aware")
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
Reviewed-by: Tony Lu <tonylu@linux.alibaba.com>
Reviewed-by: Wenjia Zhang <wenjia@linux.ibm.com>
Link: https://lore.kernel.org/r/20221101093722.127223-1-chenzhongjin@huawei.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
net/smc/af_smc.c

index 26f81e2..d5ddf28 100644 (file)
@@ -2744,14 +2744,14 @@ static int __init smc_init(void)
 
        rc = register_pernet_subsys(&smc_net_stat_ops);
        if (rc)
-               return rc;
+               goto out_pernet_subsys;
 
        smc_ism_init();
        smc_clc_init();
 
        rc = smc_nl_init();
        if (rc)
-               goto out_pernet_subsys;
+               goto out_pernet_subsys_stat;
 
        rc = smc_pnet_init();
        if (rc)
@@ -2829,6 +2829,8 @@ out_pnet:
        smc_pnet_exit();
 out_nl:
        smc_nl_exit();
+out_pernet_subsys_stat:
+       unregister_pernet_subsys(&smc_net_stat_ops);
 out_pernet_subsys:
        unregister_pernet_subsys(&smc_net_ops);