Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2479877yba; Mon, 15 Apr 2019 12:31:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqzOjAAI+oC9ADr6kOS8JrjWOGHZB0MQL596stOslZpUgAn0IsmCsVDNGdcyWi3oE/YUR9hR X-Received: by 2002:a63:e845:: with SMTP id a5mr73727848pgk.246.1555356678859; Mon, 15 Apr 2019 12:31:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555356678; cv=none; d=google.com; s=arc-20160816; b=RUqS7CT1Av5v8s2h1VNHuuSfnmi2a0wPzhlWF1o0GFgLiWsi0jNoCIhEPJPGhcmWKW jGRSdlp+Ys4nFZ9bQwRDKZH1HxTZBdNPNqFOYjS+Bkem/pBS0sp45epRH8ZN1/ailO3Z XNO5exDohvzRCSJsARn2jQG/J+aVVlez8ipc4bUrX9QorX3s2n5WVGP4nzIwvEnoRUcD OGNXPS7428Szgcjy/XFHLl58Wj+/1GSir2EKC99l9Ai45Mbtbq3STFuye/mKrZIC6uWN F50XLYcaW/yG8T3TTGQDRx0YglfRm8XlJCk7N/Ax1br1i2nnnA/yFg42ib/VtiJd2CeF jDOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=M3P/2gxO+g+2hkjyhrjpOvqq+DEjLDow4XGYdDqmVz4=; b=c/hWbHL872IjpXUDUMCULO4XUSXpa49YGjJQumEoQriJkMciAH/eWpw1OkIKJaiQ92 CkYbdcBlaSEeZA0VkErcJLAmTSICXpLNbPzOaPHWpnrkZtVvslaEAoKcFYyxyaTSLAeL u+eqgOpKhTUcsrtwNwjLC5JLd9Cz76NvGobm9wuoP4rrsoDqx1ax8dBlpa8vICxauqkW cxJvdIW93H8r7o4XBT94/T6Wck1T+uxTEudwt2bOfh3MCg+gny89Ws9jJxLMwcg4L/+8 uxR7fPZDXfd2bgjXsfn4Q4gOuHPDFv1BWVZbS9MpG583uxtw/Hi7SXtKGQodn9WQhy3a ZgNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="WAu5jr/5"; 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 k3si34661258pgh.113.2019.04.15.12.31.02; Mon, 15 Apr 2019 12:31: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; dkim=pass header.i=@kernel.org header.s=default header.b="WAu5jr/5"; 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 S1728605AbfDOTCO (ORCPT + 99 others); Mon, 15 Apr 2019 15:02:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:32786 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728555AbfDOTCK (ORCPT ); Mon, 15 Apr 2019 15:02:10 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 44535218FE; Mon, 15 Apr 2019 19:02:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555354929; bh=AFyaG/1pjIJlyyYguMkxYcc/vNPGObXpMMKNCpxZUVM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WAu5jr/5E3uLYOH7iEqPbUvTXR8hil+jOXaatctYN24UBbZne8IutSHWxgc/vCWkc GwRqXjbzF2rHuL2CBHXWgKFc4g/AZmlq3fjMOe+EqxreJsywlEd6WwxDLn5MQy5/RZ F1+ppixp4dd1EkauBg9E9Udz5V6KzARbTswHlhBI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michal Kubecek , Jiri Slaby , "David S. Miller" Subject: [PATCH 4.14 18/69] kcm: switch order of device registration to fix a crash Date: Mon, 15 Apr 2019 20:58:36 +0200 Message-Id: <20190415183730.023327571@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415183726.036654568@linuxfoundation.org> References: <20190415183726.036654568@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Slaby [ Upstream commit 3c446e6f96997f2a95bf0037ef463802162d2323 ] When kcm is loaded while many processes try to create a KCM socket, a crash occurs: BUG: unable to handle kernel NULL pointer dereference at 000000000000000e IP: mutex_lock+0x27/0x40 kernel/locking/mutex.c:240 PGD 8000000016ef2067 P4D 8000000016ef2067 PUD 3d6e9067 PMD 0 Oops: 0002 [#1] SMP KASAN PTI CPU: 0 PID: 7005 Comm: syz-executor.5 Not tainted 4.12.14-396-default #1 SLE15-SP1 (unreleased) RIP: 0010:mutex_lock+0x27/0x40 kernel/locking/mutex.c:240 RSP: 0018:ffff88000d487a00 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 000000000000000e RCX: 1ffff100082b0719 ... CR2: 000000000000000e CR3: 000000004b1bc003 CR4: 0000000000060ef0 Call Trace: kcm_create+0x600/0xbf0 [kcm] __sock_create+0x324/0x750 net/socket.c:1272 ... This is due to race between sock_create and unfinished register_pernet_device. kcm_create tries to do "net_generic(net, kcm_net_id)". but kcm_net_id is not initialized yet. So switch the order of the two to close the race. This can be reproduced with mutiple processes doing socket(PF_KCM, ...) and one process doing module removal. Fixes: ab7ac4eb9832 ("kcm: Kernel Connection Multiplexor module") Reviewed-by: Michal Kubecek Signed-off-by: Jiri Slaby Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/kcm/kcmsock.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -2059,14 +2059,14 @@ static int __init kcm_init(void) if (err) goto fail; - err = sock_register(&kcm_family_ops); - if (err) - goto sock_register_fail; - err = register_pernet_device(&kcm_net_ops); if (err) goto net_ops_fail; + err = sock_register(&kcm_family_ops); + if (err) + goto sock_register_fail; + err = kcm_proc_init(); if (err) goto proc_init_fail; @@ -2074,12 +2074,12 @@ static int __init kcm_init(void) return 0; proc_init_fail: - unregister_pernet_device(&kcm_net_ops); - -net_ops_fail: sock_unregister(PF_KCM); sock_register_fail: + unregister_pernet_device(&kcm_net_ops); + +net_ops_fail: proto_unregister(&kcm_proto); fail: @@ -2095,8 +2095,8 @@ fail: static void __exit kcm_exit(void) { kcm_proc_exit(); - unregister_pernet_device(&kcm_net_ops); sock_unregister(PF_KCM); + unregister_pernet_device(&kcm_net_ops); proto_unregister(&kcm_proto); destroy_workqueue(kcm_wq);