This patch fix error path for cgw_module_init
to avoid possible crash if some error occurs.
Fixes: c1aabdf379bc ("can-gw: add netlink based CAN routing")
Signed-off-by: YueHaibing <[email protected]>
---
net/can/gw.c | 46 +++++++++++++++++++++++++++++++---------------
1 file changed, 31 insertions(+), 15 deletions(-)
diff --git a/net/can/gw.c b/net/can/gw.c
index 53859346..8b53ec7 100644
--- a/net/can/gw.c
+++ b/net/can/gw.c
@@ -1046,32 +1046,48 @@ static __init int cgw_module_init(void)
pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
max_hops);
- register_pernet_subsys(&cangw_pernet_ops);
+ ret = register_pernet_subsys(&cangw_pernet_ops);
+ if (ret)
+ return ret;
+
+ ret = -ENOMEM;
cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
0, 0, NULL);
-
if (!cgw_cache)
- return -ENOMEM;
+ goto out_cache_create;
/* set notifier */
notifier.notifier_call = cgw_notifier;
- register_netdevice_notifier(¬ifier);
+ ret = register_netdevice_notifier(¬ifier);
+ if (ret)
+ goto out_register_notifier;
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
NULL, cgw_dump_jobs, 0);
- if (ret) {
- unregister_netdevice_notifier(¬ifier);
- kmem_cache_destroy(cgw_cache);
- return -ENOBUFS;
- }
-
- /* Only the first call to rtnl_register_module can fail */
- rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
- cgw_create_job, NULL, 0);
- rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
- cgw_remove_job, NULL, 0);
+ if (ret)
+ goto out_rtnl_register1;
+
+ ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
+ cgw_create_job, NULL, 0);
+ if (ret)
+ goto out_rtnl_register2;
+ ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
+ cgw_remove_job, NULL, 0);
+ if (ret)
+ goto out_rtnl_register2;
return 0;
+
+out_rtnl_register2:
+ rtnl_unregister_all(PF_CAN);
+out_rtnl_register1:
+ unregister_netdevice_notifier(¬ifier);
+out_register_notifier:
+ kmem_cache_destroy(cgw_cache);
+out_cache_create:
+ unregister_pernet_subsys(&cangw_pernet_ops);
+
+ return ret;
}
static __exit void cgw_module_exit(void)
--
1.8.3.1
On 16.05.19 17:54, YueHaibing wrote:
> This patch fix error path for cgw_module_init
> to avoid possible crash if some error occurs.
>
> Fixes: c1aabdf379bc ("can-gw: add netlink based CAN routing")
> Signed-off-by: YueHaibing <[email protected]>
Acked-by: Oliver Hartkopp <[email protected]>
Thanks!
> ---
> net/can/gw.c | 46 +++++++++++++++++++++++++++++++---------------
> 1 file changed, 31 insertions(+), 15 deletions(-)
>
> diff --git a/net/can/gw.c b/net/can/gw.c
> index 53859346..8b53ec7 100644
> --- a/net/can/gw.c
> +++ b/net/can/gw.c
> @@ -1046,32 +1046,48 @@ static __init int cgw_module_init(void)
> pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
> max_hops);
>
> - register_pernet_subsys(&cangw_pernet_ops);
> + ret = register_pernet_subsys(&cangw_pernet_ops);
> + if (ret)
> + return ret;
> +
> + ret = -ENOMEM;
> cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
> 0, 0, NULL);
> -
> if (!cgw_cache)
> - return -ENOMEM;
> + goto out_cache_create;
>
> /* set notifier */
> notifier.notifier_call = cgw_notifier;
> - register_netdevice_notifier(¬ifier);
> + ret = register_netdevice_notifier(¬ifier);
> + if (ret)
> + goto out_register_notifier;
>
> ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
> NULL, cgw_dump_jobs, 0);
> - if (ret) {
> - unregister_netdevice_notifier(¬ifier);
> - kmem_cache_destroy(cgw_cache);
> - return -ENOBUFS;
> - }
> -
> - /* Only the first call to rtnl_register_module can fail */
> - rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
> - cgw_create_job, NULL, 0);
> - rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
> - cgw_remove_job, NULL, 0);
> + if (ret)
> + goto out_rtnl_register1;
> +
> + ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
> + cgw_create_job, NULL, 0);
> + if (ret)
> + goto out_rtnl_register2;
> + ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
> + cgw_remove_job, NULL, 0);
> + if (ret)
> + goto out_rtnl_register2;
>
> return 0;
> +
> +out_rtnl_register2:
> + rtnl_unregister_all(PF_CAN);
> +out_rtnl_register1:
> + unregister_netdevice_notifier(¬ifier);
> +out_register_notifier:
> + kmem_cache_destroy(cgw_cache);
> +out_cache_create:
> + unregister_pernet_subsys(&cangw_pernet_ops);
> +
> + return ret;
> }
>
> static __exit void cgw_module_exit(void)
>
On 5/16/19 5:54 PM, YueHaibing wrote:
> This patch fix error path for cgw_module_init
> to avoid possible crash if some error occurs.
>
> Fixes: c1aabdf379bc ("can-gw: add netlink based CAN routing")
> Signed-off-by: YueHaibing <[email protected]>
> ---
> net/can/gw.c | 46 +++++++++++++++++++++++++++++++---------------
> 1 file changed, 31 insertions(+), 15 deletions(-)
>
> diff --git a/net/can/gw.c b/net/can/gw.c
> index 53859346..8b53ec7 100644
> --- a/net/can/gw.c
> +++ b/net/can/gw.c
> @@ -1046,32 +1046,48 @@ static __init int cgw_module_init(void)
> pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
> max_hops);
>
> - register_pernet_subsys(&cangw_pernet_ops);
> + ret = register_pernet_subsys(&cangw_pernet_ops);
> + if (ret)
> + return ret;
> +
> + ret = -ENOMEM;
> cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
> 0, 0, NULL);
> -
> if (!cgw_cache)
> - return -ENOMEM;
> + goto out_cache_create;
>
> /* set notifier */
> notifier.notifier_call = cgw_notifier;
> - register_netdevice_notifier(¬ifier);
> + ret = register_netdevice_notifier(¬ifier);
> + if (ret)
> + goto out_register_notifier;
>
> ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
> NULL, cgw_dump_jobs, 0);
> - if (ret) {
> - unregister_netdevice_notifier(¬ifier);
> - kmem_cache_destroy(cgw_cache);
> - return -ENOBUFS;
> - }
> -
> - /* Only the first call to rtnl_register_module can fail */
> - rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
> - cgw_create_job, NULL, 0);
> - rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
> - cgw_remove_job, NULL, 0);
> + if (ret)
> + goto out_rtnl_register1;
> +
> + ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
> + cgw_create_job, NULL, 0);
> + if (ret)
> + goto out_rtnl_register2;
> + ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
> + cgw_remove_job, NULL, 0);
> + if (ret)
> + goto out_rtnl_register2;
>
> return 0;
> +
> +out_rtnl_register2:
> + rtnl_unregister_all(PF_CAN);
Currently gw.c is the only user of rtnl_register_module(PF_CAN), but
PF_CAN is not specific to gw. Better change this to individual
rtnl_unregister(int protocol, int msgtype).
> +out_rtnl_register1:
> + unregister_netdevice_notifier(¬ifier);
> +out_register_notifier:
> + kmem_cache_destroy(cgw_cache);
> +out_cache_create:
> + unregister_pernet_subsys(&cangw_pernet_ops);
> +
> + return ret;
> }
>
> static __exit void cgw_module_exit(void)
>
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
This patch add error path for cgw_module_init
to avoid possible crash if some error occurs.
Fixes: c1aabdf379bc ("can-gw: add netlink based CAN routing")
Signed-off-by: YueHaibing <[email protected]>
---
net/can/gw.c | 48 +++++++++++++++++++++++++++++++++---------------
1 file changed, 33 insertions(+), 15 deletions(-)
diff --git a/net/can/gw.c b/net/can/gw.c
index 53859346..bd21614 100644
--- a/net/can/gw.c
+++ b/net/can/gw.c
@@ -1046,32 +1046,50 @@ static __init int cgw_module_init(void)
pr_info("can: netlink gateway (rev " CAN_GW_VERSION ") max_hops=%d\n",
max_hops);
- register_pernet_subsys(&cangw_pernet_ops);
+ ret = register_pernet_subsys(&cangw_pernet_ops);
+ if (ret)
+ return ret;
+
+ ret = -ENOMEM;
cgw_cache = kmem_cache_create("can_gw", sizeof(struct cgw_job),
0, 0, NULL);
-
if (!cgw_cache)
- return -ENOMEM;
+ goto out_cache_create;
/* set notifier */
notifier.notifier_call = cgw_notifier;
- register_netdevice_notifier(¬ifier);
+ ret = register_netdevice_notifier(¬ifier);
+ if (ret)
+ goto out_register_notifier;
ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_GETROUTE,
NULL, cgw_dump_jobs, 0);
- if (ret) {
- unregister_netdevice_notifier(¬ifier);
- kmem_cache_destroy(cgw_cache);
- return -ENOBUFS;
- }
-
- /* Only the first call to rtnl_register_module can fail */
- rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
- cgw_create_job, NULL, 0);
- rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
- cgw_remove_job, NULL, 0);
+ if (ret)
+ goto out_rtnl_register1;
+
+ ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_NEWROUTE,
+ cgw_create_job, NULL, 0);
+ if (ret)
+ goto out_rtnl_register2;
+ ret = rtnl_register_module(THIS_MODULE, PF_CAN, RTM_DELROUTE,
+ cgw_remove_job, NULL, 0);
+ if (ret)
+ goto out_rtnl_register3;
return 0;
+
+out_rtnl_register3:
+ rtnl_unregister(PF_CAN, RTM_NEWROUTE);
+out_rtnl_register2:
+ rtnl_unregister(PF_CAN, RTM_GETROUTE);
+out_rtnl_register1:
+ unregister_netdevice_notifier(¬ifier);
+out_register_notifier:
+ kmem_cache_destroy(cgw_cache);
+out_cache_create:
+ unregister_pernet_subsys(&cangw_pernet_ops);
+
+ return ret;
}
static __exit void cgw_module_exit(void)
--
1.8.3.1