Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp2595204rdh; Mon, 30 Oct 2023 01:50:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrEbexL5gNeG8PI+WMrwGXcoai37c95meH6grTtMw5CyFFkkMi00fgirfLRrsgqjn9iAeo X-Received: by 2002:a17:902:f354:b0:1c5:efd1:82b6 with SMTP id q20-20020a170902f35400b001c5efd182b6mr5681244ple.30.1698655816205; Mon, 30 Oct 2023 01:50:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698655816; cv=none; d=google.com; s=arc-20160816; b=N6woJr8qGABwnZnp1xmiTEPqbcfpj7pK6x1R9Atm0d4HgX7pmF9Jvbtr2ax0AOLijL aZawhAs8nqVfp1/sVl0y2MyoYi1cPtvqHXPbwyVoZzM7uNhJ4Lc2XvWsNLs5oehh44bX Np6+WmudoIUFjCYKTeZMJdp+i2EqOBWYQFYhYaOCrPuzHrhrD319G/V1O8xKvkyMvzf7 fh9xGJv1aY0CW9pMEixOn8ELbu3vQ2q5UOWkwHR17Rx7faqyaJTdHPek7EcxCYUoMMfX DjQTkNWhm3NT45iz4JwEk7dZCPaAtGFXbiWWjcTXwP9Z6fMYAxFgmMUTPZTqZJJxTFDo W6FQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=rsBt1YsRFkZwz58y9/Z0DGYDBEYKK7yn3js4k+R4D0s=; fh=bW+KZy1lEB1QuXAqiZ+w4+pLfpsVE8Q/yiVStPM61q8=; b=J8UKK8sRgcOJsTfzr/IL/FPW3V+gqF+JiT4oDA55PfdCZDw8JTfHTztzF0nvTs/OsH JMUUAs3ON8S7PORFiuk9u78EmcZOWkwUjUEOtMOQDpySBh2bSm97byrE27XIVbtT3ORU 3HIJv4kTZli9SiabOUOx80v2Rr2sI0JXJm9UNlBdwg3mwE4fl4DUFalLsUYrQz1Lx4/Y L21fO2rDF7IStxTPOgHaxXE/N+lRcq4wzJ2KNE/+vznCFbdMhyjZ1LDs/xi13MAkYTvP k/242JkbSnS9y67Z1ILVZ3lsMVJe6X8xg1sE+WUDNSoCkNu8RfyzHup7bSk5BXEZW50x KtyQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id m2-20020a170902d18200b001cc30cb3fbesi1153660plb.147.2023.10.30.01.50.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 01:50:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id C07B2809C1A1; Mon, 30 Oct 2023 01:50:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232261AbjJ3IuC (ORCPT + 99 others); Mon, 30 Oct 2023 04:50:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232297AbjJ3It7 (ORCPT ); Mon, 30 Oct 2023 04:49:59 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47999C1; Mon, 30 Oct 2023 01:49:56 -0700 (PDT) Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4SJn5Q75f3z4f3jJ6; Mon, 30 Oct 2023 16:49:46 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 14B2E1A016F; Mon, 30 Oct 2023 16:49:53 +0800 (CST) Received: from [10.174.178.159] (unknown [10.174.178.159]) by APP4 (Coremail) with SMTP id gCh0CgB3BdUvbj9lkx9mEQ--.43659S3; Mon, 30 Oct 2023 16:49:52 +0800 (CST) Message-ID: Date: Mon, 30 Oct 2023 16:49:50 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH -next] nbd: get config_lock before sock_shutdown To: Yu Kuai , Jens Axboe , josef@toxicpanda.com Cc: linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, "yukuai (C)" References: <20230707062256.1271948-1-zhongjinghua@huaweicloud.com> <1b67a9dd-c28a-661a-3a46-dab509d4c34e@kernel.dk> From: zhongjinghua In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgB3BdUvbj9lkx9mEQ--.43659S3 X-Coremail-Antispam: 1UD129KBjvJXoWxAw1UWF17Gr4kAw1rJryDAwb_yoW5ur4kpr 1kAF1UGrW5Gw1Iqr1UJw1UXryUJw1Ut3WUJr1UJa4UArsrCry2gr1UWr1q9r1UJr48Jr1U Jr15GF13Zry7Jr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk0b4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4CEbIxvr21l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIE c7CjxVAFwI0_Gr1j6F4UJbIYCTnIWIevJa73UjIFyTuYvjxUrR6zUUUUU X-CM-SenderInfo: x2kr0wpmlqwxtxd6x35dzhxuhorxvhhfrp/ X-Spam-Status: No, score=-5.2 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 30 Oct 2023 01:50:13 -0700 (PDT) 在 2023/9/28 14:04, Yu Kuai 写道: > Hi, > > 在 2023/08/01 8:27, Jens Axboe 写道: >> On 7/7/23 12:22?AM, Zhong Jinghua wrote: >>> Config->socks in sock_shutdown may trigger a UAF problem. >>> The reason is that sock_shutdown does not hold the config_lock, >>> so that nbd_ioctl can release config->socks at this time. >>> >>> T0: NBD_SET_SOCK >>> T1: NBD_DO_IT >>> >>> T0                        T1 >>> >>> nbd_ioctl >>>    mutex_lock(&nbd->config_lock) >>>    // get lock >>>    __nbd_ioctl >>>      nbd_start_device_ioctl >>>        nbd_start_device >>>         mutex_unlock(&nbd->config_lock) >>>           // relase lock >>>           wait_event_interruptible >>>           (kill, enter sock_shutdown) >>>           sock_shutdown >>>                     nbd_ioctl >>>                       mutex_lock(&nbd->config_lock) >>>                       // get lock >>>                       __nbd_ioctl >>>                         nbd_add_socket >>>                           krealloc >>>                         kfree(p) >>>                             //config->socks is NULL >>>             nbd_sock *nsock = config->socks // error >>> >>> Fix it by moving config_lock up before sock_shutdown. >>> >>> Signed-off-by: Zhong Jinghua >>> --- >>>   drivers/block/nbd.c | 7 ++++++- >>>   1 file changed, 6 insertions(+), 1 deletion(-) >>> >>> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c >>> index c410cf29fb0c..accbe99ebb7e 100644 >>> --- a/drivers/block/nbd.c >>> +++ b/drivers/block/nbd.c >>> @@ -1428,13 +1428,18 @@ static int nbd_start_device_ioctl(struct >>> nbd_device *nbd) >>>       mutex_unlock(&nbd->config_lock); >>>       ret = wait_event_interruptible(config->recv_wq, >>> atomic_read(&config->recv_threads) == 0); >>> + >>> +    /* >>> +     * recv_work in flush_workqueue will not get this lock, because >>> nbd_open >>> +     * will hold nbd->config_refs >>> +     */ >>> +    mutex_lock(&nbd->config_lock); >>>       if (ret) { >>>           sock_shutdown(nbd); >>>           nbd_clear_que(nbd); >>>       } >>>         flush_workqueue(nbd->recv_workq); >>> -    mutex_lock(&nbd->config_lock); >> >> Feels pretty iffy to hold config_lock over the flush. If anything off >> recv_work() ever grabs it, we'd be stuck. Your comment assumes that the >> only case this will currently happen is if we drop the last ref, or at >> least that's the case that'd do it even if you don't mention it >> explicitly. >> >> Maybe this is all fine, but recv_work() should have a comment matching >> this one, and this comment should be more descriptive as well. > > Jinghua, > > Please add comment as Jens suggested, and resend this patch. > > Thanks, > Kuai > >> OK. Later I'll send out, Thanks to Jens for the advice.