Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp14347259pxu; Mon, 4 Jan 2021 21:54:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJzTR/4dsSF8hfpLQOc2898lLv0pW9T4QxUx8k3SxUUJReGsutiPPJKTnIDsZR/ZJpcUC5z5 X-Received: by 2002:a17:906:3146:: with SMTP id e6mr54750926eje.363.1609826041108; Mon, 04 Jan 2021 21:54:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609826041; cv=none; d=google.com; s=arc-20160816; b=T9fTywqm0jSI41RaxwRqBYMoY/WWVXlUPEJJlFmlsnrKE3VOggDJr6wMW8xs0KQQ8A PmUb/05jl5rGHmodolRvAf+LdRVMoY0+S/YWGLwmiklb4V5hJnZZdk+SWyu2KFhN2B+6 yhCPbiJneZpXTUtutStJgad2saRLH4qOaFV/velk9l1lqZFGo74yayXshNTdlK70oNFO f5iwlwBTKqlpS8mFqn2xI7xS3aoUPLpUcb21WK3P/cWjlD+hPoZLa0ygH1BjLG06yOh0 iNkx3j/eJ1+3py2l0KZYJiXEg2bp/YKsKobtMJQ+3HUO9wZziDcpTmgh6CxQQKgxGIXs ge+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=QtpqLnhCLZJxT0OmMAdkn6GMGyWR8T270bnpwN5C9pI=; b=tUEsuporixRj1o8VjFhdYb9lg5Q47FtdLeLzhKO2WwroPy723eZKvyYRgK7EM2aezm +djuqojXeP6Ip+ffuv/DQW5L5Ff1gJp6afC7oq9MbpHuuWMlHFcQffhV5XcXg8ImOtDQ xBDiqu8OzFSgLBZIZQ/SGD3IhIl4EYlnQbDQjnlFTtahFSG0Q61ZrvhJ7XNhl4vbUW+y Frv5iGqqgscCari69ZwahZ1GxjAIo5wLlYgMDZmOgU0XPFzi4SVhIPnyWYiu97cxXjdK yR/EozheDa6KpHdCv2sU1+4wUEqFB6DCzQxzLtmwoz9oNlb4fPFovim6gXnvsbInING8 vUWg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pw16si29005753ejb.430.2021.01.04.21.53.38; Mon, 04 Jan 2021 21:54:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726096AbhAEFwj (ORCPT + 99 others); Tue, 5 Jan 2021 00:52:39 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:10383 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725290AbhAEFwj (ORCPT ); Tue, 5 Jan 2021 00:52:39 -0500 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4D91nf36prz7Q31; Tue, 5 Jan 2021 13:51:02 +0800 (CST) Received: from localhost.localdomain.localdomain (10.175.113.25) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.498.0; Tue, 5 Jan 2021 13:51:49 +0800 From: Qinglang Miao To: "David S. Miller" , Jakub Kicinski CC: , , Qinglang Miao Subject: [PATCH v2] net: qrtr: fix null-ptr-deref in qrtr_ns_remove Date: Tue, 5 Jan 2021 13:57:54 +0800 Message-ID: <20210105055754.16486-1-miaoqinglang@huawei.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.25] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A null-ptr-deref bug is reported by Hulk Robot like this: -------------- KASAN: null-ptr-deref in range [0x0000000000000128-0x000000000000012f] Call Trace: qrtr_ns_remove+0x22/0x40 [ns] qrtr_proto_fini+0xa/0x31 [qrtr] __x64_sys_delete_module+0x337/0x4e0 do_syscall_64+0x34/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x468ded -------------- When qrtr_ns_init fails in qrtr_proto_init, qrtr_ns_remove which would be called later on would raise a null-ptr-deref because qrtr_ns.workqueue has been destroyed. Fix it by making qrtr_ns_init have a return value and adding a check in qrtr_proto_init. Reported-by: Hulk Robot Signed-off-by: Qinglang Miao --- v1->v2: remove redundant braces for single statement blocks. net/qrtr/ns.c | 7 ++++--- net/qrtr/qrtr.c | 16 +++++++++++----- net/qrtr/qrtr.h | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/net/qrtr/ns.c b/net/qrtr/ns.c index 56aaf8cb6..8d00dfe81 100644 --- a/net/qrtr/ns.c +++ b/net/qrtr/ns.c @@ -755,7 +755,7 @@ static void qrtr_ns_data_ready(struct sock *sk) queue_work(qrtr_ns.workqueue, &qrtr_ns.work); } -void qrtr_ns_init(void) +int qrtr_ns_init(void) { struct sockaddr_qrtr sq; int ret; @@ -766,7 +766,7 @@ void qrtr_ns_init(void) ret = sock_create_kern(&init_net, AF_QIPCRTR, SOCK_DGRAM, PF_QIPCRTR, &qrtr_ns.sock); if (ret < 0) - return; + return ret; ret = kernel_getsockname(qrtr_ns.sock, (struct sockaddr *)&sq); if (ret < 0) { @@ -797,12 +797,13 @@ void qrtr_ns_init(void) if (ret < 0) goto err_wq; - return; + return 0; err_wq: destroy_workqueue(qrtr_ns.workqueue); err_sock: sock_release(qrtr_ns.sock); + return ret; } EXPORT_SYMBOL_GPL(qrtr_ns_init); diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c index f4ab3ca6d..b34358282 100644 --- a/net/qrtr/qrtr.c +++ b/net/qrtr/qrtr.c @@ -1287,13 +1287,19 @@ static int __init qrtr_proto_init(void) return rc; rc = sock_register(&qrtr_family); - if (rc) { - proto_unregister(&qrtr_proto); - return rc; - } + if (rc) + goto err_proto; - qrtr_ns_init(); + rc = qrtr_ns_init(); + if (rc) + goto err_sock; + return 0; + +err_sock: + sock_unregister(qrtr_family.family); +err_proto: + proto_unregister(&qrtr_proto); return rc; } postcore_initcall(qrtr_proto_init); diff --git a/net/qrtr/qrtr.h b/net/qrtr/qrtr.h index dc2b67f17..3f2d28696 100644 --- a/net/qrtr/qrtr.h +++ b/net/qrtr/qrtr.h @@ -29,7 +29,7 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep); int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len); -void qrtr_ns_init(void); +int qrtr_ns_init(void); void qrtr_ns_remove(void); -- 2.23.0