Received: by 2002:ac2:464d:0:0:0:0:0 with SMTP id s13csp3304036lfo; Mon, 23 May 2022 01:10:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZthCq2teHHHBMWc+2HlpicUULFuvATxSyuDpDmzNYnRcLKpmsiSUV+d8enc6kzQYP1B2f X-Received: by 2002:a17:902:e94d:b0:158:2f50:d04c with SMTP id b13-20020a170902e94d00b001582f50d04cmr21797807pll.11.1653293446277; Mon, 23 May 2022 01:10:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653293446; cv=none; d=google.com; s=arc-20160816; b=S2UdZvVh738xqVOiaod4gMc6Odr8A/m0TRGGsR/U3m5ufnK3ccWVhqmATQFp6LT1p8 Y0NiI+g5qb7G5ERAJlg+MDshK9s4rVDdHft7ctn0Kzvst4M8zuXsjdrCUfj1GNPlSXK2 SfzXLGCigsvIZPOUF83ssRszL3hJqManXzv9XUZi5xHD+AATeWMB5Z2WMpqN7cWZhV9K zsdlfdLjdRVtmLuEepxJrmKfOrpS2DFvTP72ajL4/dwVQ3qljuXGNcNyEfG5GQGhZB7u H5Hii454un9Yb/Q+CtCGW02kdh4k4VeIfiXttGxtnFPBp3UKm6b4RtRvbYc/XJrA7me0 8TTg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Cb7AUOzylxYY7GeCeSRd4QHxg3GB1QLimYm44KJvr0w=; b=sy/x1ht8i6lFH9ByLIngbOPZuWFCXejRqzy6ImaXh//VYun2dYiVIWhC7pbGNuPVaT UwFaI8tRUou768zisBTgQIVMvXO7FD5Iy13cSuoxArB455yf18zEDcv9khEzBotaDlJX Lp0nI81Rfd6BKpKgvbR1/2+E21+hufNsWt8ZxX+8fMq+P+fnBnw5D3cRQHWq/mjr+8tp InlDXuug2VYfEGs02ww7+2/qE/MUioTkR2TBCcVMBiD5Xcddz77FFVi2j+QcSd+6rbMf YsL35yoIRvmclxTVrRPvsnaq3aKYY49m5oTZl7BFXXghTNuqj1MuJstYpebf3jzWquwb 9r2w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u7-20020a170902a60700b0016184b32d35si7846436plq.174.2022.05.23.01.10.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 May 2022 01:10:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B749969297; Mon, 23 May 2022 00:05:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354754AbiEUHYW (ORCPT + 99 others); Sat, 21 May 2022 03:24:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354724AbiEUHYP (ORCPT ); Sat, 21 May 2022 03:24:15 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B98E2166688; Sat, 21 May 2022 00:24:13 -0700 (PDT) Received: from kwepemi100014.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4L4w2962p0zCsjW; Sat, 21 May 2022 15:19:13 +0800 (CST) Received: from kwepemm600009.china.huawei.com (7.193.23.164) by kwepemi100014.china.huawei.com (7.221.188.106) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 21 May 2022 15:24:11 +0800 Received: from huawei.com (10.175.127.227) by kwepemm600009.china.huawei.com (7.193.23.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Sat, 21 May 2022 15:24:10 +0800 From: Yu Kuai To: , , , CC: , , , Subject: [PATCH -next v3 2/6] nbd: fix race between nbd_alloc_config() and module removal Date: Sat, 21 May 2022 15:37:45 +0800 Message-ID: <20220521073749.3146892-3-yukuai3@huawei.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220521073749.3146892-1-yukuai3@huawei.com> References: <20220521073749.3146892-1-yukuai3@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm600009.china.huawei.com (7.193.23.164) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When nbd module is being removing, nbd_alloc_config() may be called concurrently by nbd_genl_connect(), although try_module_get() will return false, but nbd_alloc_config() doesn't handle it. The race may lead to the leak of nbd_config and its related resources (e.g, recv_workq) and oops in nbd_read_stat() due to the unload of nbd module as shown below: BUG: kernel NULL pointer dereference, address: 0000000000000040 Oops: 0000 [#1] SMP PTI CPU: 5 PID: 13840 Comm: kworker/u17:33 Not tainted 5.14.0+ #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996) Workqueue: knbd16-recv recv_work [nbd] RIP: 0010:nbd_read_stat.cold+0x130/0x1a4 [nbd] Call Trace: recv_work+0x3b/0xb0 [nbd] process_one_work+0x1ed/0x390 worker_thread+0x4a/0x3d0 kthread+0x12a/0x150 ret_from_fork+0x22/0x30 Fixing it by checking the return value of try_module_get() in nbd_alloc_config(). As nbd_alloc_config() may return ERR_PTR(-ENODEV), assign nbd->config only when nbd_alloc_config() succeeds to ensure the value of nbd->config is binary (valid or NULL). Also adding a debug message to check the reference counter of nbd_config during module removal. Signed-off-by: Hou Tao Signed-off-by: Yu Kuai --- drivers/block/nbd.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index a73e853f5833..2ee1e376d5c4 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -1518,15 +1518,20 @@ static struct nbd_config *nbd_alloc_config(void) { struct nbd_config *config; + if (!try_module_get(THIS_MODULE)) + return ERR_PTR(-ENODEV); + config = kzalloc(sizeof(struct nbd_config), GFP_NOFS); - if (!config) - return NULL; + if (!config) { + module_put(THIS_MODULE); + return ERR_PTR(-ENOMEM); + } + atomic_set(&config->recv_threads, 0); init_waitqueue_head(&config->recv_wq); init_waitqueue_head(&config->conn_wait); config->blksize_bits = NBD_DEF_BLKSIZE_BITS; atomic_set(&config->live_connections, 0); - try_module_get(THIS_MODULE); return config; } @@ -1553,12 +1558,13 @@ static int nbd_open(struct block_device *bdev, fmode_t mode) mutex_unlock(&nbd->config_lock); goto out; } - config = nbd->config = nbd_alloc_config(); - if (!config) { - ret = -ENOMEM; + config = nbd_alloc_config(); + if (IS_ERR(config)) { + ret = PTR_ERR(config); mutex_unlock(&nbd->config_lock); goto out; } + nbd->config = config; refcount_set(&nbd->config_refs, 1); refcount_inc(&nbd->refs); mutex_unlock(&nbd->config_lock); @@ -1964,13 +1970,14 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) nbd_put(nbd); return -EINVAL; } - config = nbd->config = nbd_alloc_config(); - if (!nbd->config) { + config = nbd_alloc_config(); + if (IS_ERR(config)) { mutex_unlock(&nbd->config_lock); nbd_put(nbd); printk(KERN_ERR "nbd: couldn't allocate config\n"); - return -ENOMEM; + return PTR_ERR(config); } + nbd->config = config; refcount_set(&nbd->config_refs, 1); set_bit(NBD_RT_BOUND, &config->runtime_flags); @@ -2543,6 +2550,9 @@ static void __exit nbd_cleanup(void) while (!list_empty(&del_list)) { nbd = list_first_entry(&del_list, struct nbd_device, list); list_del_init(&nbd->list); + if (refcount_read(&nbd->config_refs)) + printk(KERN_ERR "nbd: possibly leaking nbd_config (ref %d)\n", + refcount_read(&nbd->config_refs)); if (refcount_read(&nbd->refs) != 1) printk(KERN_ERR "nbd: possibly leaking a device\n"); nbd_put(nbd); -- 2.31.1