Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp31368929rwd; Thu, 6 Jul 2023 21:07:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlGosCrCqvhzX4DBele38hmqsdbC2l/3/+K2CgOHHUVHf04o5/bxMvu2m0raoy5ShBUSOkmp X-Received: by 2002:a17:902:724a:b0:1b8:903e:bcf3 with SMTP id c10-20020a170902724a00b001b8903ebcf3mr3019083pll.24.1688702858202; Thu, 06 Jul 2023 21:07:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688702858; cv=none; d=google.com; s=arc-20160816; b=VoiNVk5qoqFWgt0WOWHth2U74ui++yyP/ChQ9XbMUiV3FiK4ow6uzY+nD5iWbN45Pv gunEfGwQMS7ij/UQR7jE2APmM04kuKAAXzrcLW3P4mNYt2CoRAJ2I7PCTccEI+ZWQ/+J zy2oIyScAtNHgu8yzoIxTexNHfIskZPYTVlY0b53h00TlQ8unpBtbSdSWWvkcn5fHyoQ TQfpGXh4zugK7eYTWDuZpYTlnVRw4CUktlO9pYjfdWHRPLZQ+2ZYjBkD6/vqNJXW97GA k0dL7e6mZDLDH34nxhj+knKgdIIKofhqkGuTC9PQW7+IqeRWCPE7pw57Des3Q0WVUhVF WdNA== 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=dxVjzC4IHN0bLRcQJ+t462Mfpz4HJjmPkVnKpfKOqY0=; fh=yzFK41VSXRhpZC6SunRALAWQIdf3xU6ABFvpzQR/KFg=; b=gacWpUFFjBltqSD97l2Zo9lmmrRY5ZyPY48Icr9tdu4ThCWorvaRqSJVWN8dFXUN72 jygs+TnIBJRHKAzee53WwbUv605Hque6Cr1H6s1ML1wSJ/vALXVZV9fKety7w5FxUIod /DSvknvPmWDZXZ50VcRDK699iyUMX52MVV4061Byzu5Dv60msukiFBCJyZTMXQxuVmQP xgSERNG32Zn1cenDXag12IpZEUmuQ58Z0nDXQgQlTDxyLkjHg/YoUo0AHxHObJhbbfKt EMHXJTWmfuAqfN++WDApTq0hoSt+V6XobTed3dWQgJnbefhJT99yIJtomI/tT5K7NUyY h2UQ== 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:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kg16-20020a170903061000b001b880f88093si2693650plb.8.2023.07.06.21.07.23; Thu, 06 Jul 2023 21:07:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232077AbjGGDSb (ORCPT + 99 others); Thu, 6 Jul 2023 23:18:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231911AbjGGDST (ORCPT ); Thu, 6 Jul 2023 23:18:19 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 708351FCC; Thu, 6 Jul 2023 20:18:18 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Qxz9q4pV4z4f3nx7; Fri, 7 Jul 2023 11:18:07 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3mp7vg6dk0X+NNQ--.406S7; Fri, 07 Jul 2023 11:18:09 +0800 (CST) From: Zhong Jinghua To: josef@toxicpanda.com, axboe@kernel.dk Cc: linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org, zhongjinghua@huaweicloud.com, yi.zhang@huawei.com, yukuai3@huawei.com Subject: [PATCH -next 3/3] nbd: fix null-ptr-dereference while accessing 'nbd->config' Date: Fri, 7 Jul 2023 11:15:36 +0800 Message-Id: <20230707031536.666482-4-zhongjinghua@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230707031536.666482-1-zhongjinghua@huaweicloud.com> References: <20230707031536.666482-1-zhongjinghua@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgD3mp7vg6dk0X+NNQ--.406S7 X-Coremail-Antispam: 1UD129KBjvJXoW7ZF4kXF1fAr1fKry7Ar1UZFb_yoW8tryDpF 4UAFy5G3yUJF43GFWkA348Wr15J3Z7AryxGry7G3s5Zr9xCrySyrykK34xXF1UAr9xJF45 JFWrGa4IkFy8GFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9m14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwCF04k20xvY0x0EwIxGrwCFx2 IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v2 6r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67 AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IY s7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr 0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUd8n5UUUUU= X-CM-SenderInfo: x2kr0wpmlqwxtxd6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Zhong Jinghua nbd->config = config and refcount_set(&nbd->config_refs, 1) in nbd_genl_connect may be out of order, causing config_refs to be set to 1 first, and then nbd_open accessing nbd->config reports a null pointer reference. T1 T2 vfs_open do_dentry_open blkdev_open blkdev_get __blkdev_get nbd_open nbd_get_config_unlocked genl_rcv_msg genl_family_rcv_msg genl_family_rcv_msg_doit nbd_genl_connect nbd_alloc_and_init_config // out of order execution refcount_set(&nbd->config_refs, 1); // 2 nbd->config // null point nbd->config = config; // 1 Fix it by adding a cpu memory barrier to guarantee sequential execution. Signed-off-by: Zhong Jinghua --- drivers/block/nbd.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 7186a9a49445..c410cf29fb0c 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -395,8 +395,16 @@ static u32 req_to_nbd_cmd_type(struct request *req) static struct nbd_config *nbd_get_config_unlocked(struct nbd_device *nbd) { - if (refcount_inc_not_zero(&nbd->config_refs)) + if (refcount_inc_not_zero(&nbd->config_refs)) { + /* + * Add smp_mb__after_atomic to ensure that reading nbd->config_refs + * and reading nbd->config is ordered. The pair is the barrier in + * nbd_alloc_and_init_config(), avoid nbd->config_refs is set + * before nbd->config. + */ + smp_mb__after_atomic(); return nbd->config; + } return NULL; } @@ -1555,7 +1563,15 @@ static int nbd_alloc_and_init_config(struct nbd_device *nbd) init_waitqueue_head(&config->conn_wait); config->blksize_bits = NBD_DEF_BLKSIZE_BITS; atomic_set(&config->live_connections, 0); + nbd->config = config; + /* + * Order refcount_set(&nbd->config_refs, 1) and nbd->config assignment, + * its pair is the barrier in nbd_get_config_unlocked(). + * So nbd_get_config_unlocked() won't see nbd->config as null after + * refcount_inc_not_zero() succeed. + */ + smp_mb__before_atomic(); refcount_set(&nbd->config_refs, 1); return 0; -- 2.31.1