Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp789705yba; Thu, 16 May 2019 08:58:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqweiUOtqSgMxDj2Oo8ok47QYEFgnA6kiiPXAJUVjpfgrf+4Lh3WIpBM8dY2zrgSyqiBpt4D X-Received: by 2002:a63:f843:: with SMTP id v3mr50948847pgj.69.1558022298228; Thu, 16 May 2019 08:58:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558022298; cv=none; d=google.com; s=arc-20160816; b=f2scmKHa7yyvzcJGhTTfDJI2im6pQzuqgYLveS6DyIdoJiGrXVAR9aatoZ1g8tQUeS 8sYwj7zdk3Toqhe5Ods8hKuCo2qbrmrTnWRpL7rkE/32i/zXN5PAQ09WfkUVycDhr6iT ETAuoxCLxPzliR3N2WIa0MwDFTW7+gJh0YqqGYKTqLh+qu23vxCIa945EBfcytGdBIeq N+U2Y2YJn+4CjC20svNxrccLQfkmj2zeh4DCU6ruQ+Dbt9qXZUBx9w+3XWnF0xZt85Tu knAlihoPLXTzPp5OOWzGhRradXkNiJzoPJTgiY60LX08RE9PbxKcOwg0PC6PWgUD7+Ln IMRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=h1BUAeiSL75LXw+klZLFyrSFdJuRCgoNH3hH8Gi45PM=; b=swzpLOCZKAENw2QaFQpsGR8FyylCb8DAcO6g9uIn8F/Z77kn2la1ynx4KXNTY85+JH BjaO5BR6VtEk6vt6aO2CdRuJUaoO6yDE+xkjew5ih3H02adjq3eaNYKrlfxU2NoTSK7p yL4lHcJdisq7SFJOw/poilQCvlsKqkyGZ32/KhKCuMWGG8d5nTtTHALLJtv/TtnCAas9 eVbBsufnHv6idqVNoF5oAMHDj0oI5GkvDqeClzpZ6nVgv4Ow0000/h+n5Rj3oEZKjIqA BWkAvLIgsmtkxKjBaKoMF3A9ZyEmKaJnVDigNQ2EnDLjla3XYemAJwn2CwTRiQ+pEqVd gZSw== 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 g3si3214158pgm.200.2019.05.16.08.58.02; Thu, 16 May 2019 08:58:18 -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 S1726574AbfEPPza (ORCPT + 99 others); Thu, 16 May 2019 11:55:30 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:57778 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726314AbfEPPza (ORCPT ); Thu, 16 May 2019 11:55:30 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 877F9E94BECBF6786BB9; Thu, 16 May 2019 23:55:24 +0800 (CST) Received: from localhost (10.177.31.96) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.439.0; Thu, 16 May 2019 23:55:18 +0800 From: YueHaibing To: , , CC: , , , YueHaibing Subject: [PATCH] can: gw: Fix error path of cgw_module_init Date: Thu, 16 May 2019 23:54:35 +0800 Message-ID: <20190516155435.42376-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 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 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 --- 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