Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2786202yba; Sat, 18 May 2019 02:59:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqzalU23z9FAicrc93kHPfb4+saPdNXb0TNOHy/xnqgtBN+FeRgBI/AYi1nMfcBmDSZ94ibO X-Received: by 2002:a65:6543:: with SMTP id a3mr37542015pgw.300.1558173554357; Sat, 18 May 2019 02:59:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558173554; cv=none; d=google.com; s=arc-20160816; b=tUMwtqZCM/a0EJaEw02qFW0cF5toNcjVJvNrJSwbsPiVJBrvw9YwPQq9zPN06rJn0+ cPnmsQLCCeO51bJahrA5F+CRPCkt6BGEmyo197CVLUU44JvuPlCOekCGoZ7mbYjwroOE oAmpeOrKfaS+0YnsK77aL3wdUTH+YcKh2qYBJGrKlzuNeEbr6hKjr2/1iKYGopURoZWy WWsmYI3IEIRw6+M/0YUwY6CT2l1mo7lYeYJiO20eh1oJ/71iFssL6twyzMGFfkPShc5I ohEtngb3RBb1hegThRGV8UlsgVuQn3rOqUUQ00vVnCZvCUa5aqVGu83USEWNCkrmu+ak rW+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=zdZnLxix4lpF6dwj6KiaXT1kRdotyXcqYEMnFM/J0pc=; b=CQfErHO7C0cO/rwgnerbcIqhLZNFImb0+nPFhnFdN3zUOrudM+zu5zwbrpFUZtw+aS BDOUJcykBgHIpU2of1KdjLIWLC8735quts11XJV2RXD/zEGqnXFuEHdG+Egv7LL97vEy CxnTGBEqWaXbtiN22jxLNjG7S2hTSV8fDGvGpCyT2fB6tX7X+ATl9TsF27LXwt6e1ov8 JRHqQFgAZw8AKy8RAdQuU8WiEgvwZRXYnjJwPNQQKjq4JI+Aemno9BF4tRROeW/naggO BlLZztW6Wqp2gUFGs8hRFCliSet04S3uaZnFUHY4C07fw3phbbcxNph+xqJwdyK8VStn F0jA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h1si10478538pgv.67.2019.05.18.02.58.59; Sat, 18 May 2019 02:59:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729876AbfERJ5g (ORCPT + 99 others); Sat, 18 May 2019 05:57:36 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:8206 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728654AbfERJ5g (ORCPT ); Sat, 18 May 2019 05:57:36 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id C449A285E9918B8D2269; Sat, 18 May 2019 17:57:33 +0800 (CST) Received: from localhost (10.177.31.96) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.439.0; Sat, 18 May 2019 17:57:24 +0800 From: YueHaibing To: , , CC: , , , YueHaibing Subject: [PATCH v2] can: gw: Fix error path of cgw_module_init Date: Sat, 18 May 2019 17:35:43 +0800 Message-ID: <20190518093543.11016-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 In-Reply-To: <80ca1430-da86-42c3-75c7-eadf91b20220@pengutronix.de> References: <80ca1430-da86-42c3-75c7-eadf91b20220@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.177.31.96] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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