Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3518436ybv; Mon, 10 Feb 2020 01:15:48 -0800 (PST) X-Google-Smtp-Source: APXvYqwULUMLNzIrRC4J36sscvxLyZXJqXVXHtBKnM4hM5FwyaOFXUOd8Pk9bjPCbvnobvPwWpbo X-Received: by 2002:a05:6830:1e95:: with SMTP id n21mr364707otr.25.1581326148095; Mon, 10 Feb 2020 01:15:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581326148; cv=none; d=google.com; s=arc-20160816; b=rb5EDkzyYQcEytfy1hBeRW8kC6uzTKD2ff2LZT6iVh1OmK7kn8J+/jaGanqiISS/03 vHkNf2Q/6Yc6waWnz/8wIhcluIIQSDsZQknYNWvo4GwRcLf0IRiNxzDt2+IfuYZIYMyf zk2VCtPAud1fYaCnADdv06F0+WGi0ZKDx3LeTElC9ofwQ4J+t7NqHengFTv6XbAulh+n SI4ZKw4rSVycMk1JWk9BdwlWCIbGPJF4cEOGEm5nDL89xg1DAIJhmBRW2xFZomPyod94 07uaWSqKNcmC0d21+8b6GmOrTRYk9X3vYPxWvuGU1JBxUlF0oBdkBAMHqDqKMO9YiRXw +unQ== 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:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=Ol5AskhZm0hgk5GLMefvSUyQ/wdRek3xRqSjwZsU1Pw=; b=wSqkpEecZfk/8qqMQ54i4KiWYo+9hfPqxacBvwbV8ZJ/ETHvrtZBjoZYbiBSO31XNG 7ibyDbMz0AwkHFNM9KHpX/MT4IgSemhLH/ai61O4sHeTVFd7GyEWwW6/QXs77QxEPn1g HC5afBjyS/R/v0ikheMRKgoTzOdh1uQZg5B0UOUog1sxdVmp6Z0N8WvUtyipfXxKWU1Z tMwoxbwabQyF5FIw0ETT+Juao/r3PVQO0NgdaooEgXFwZnjFECZuhmMZg9YnhCeYE9sX dsWhIEF2LN6RAwfRy9bNXD7EavTqJlnrGgKzm/TdgmUh0n2XPV1v4YNygr6bC4KZnWC0 46kQ== ARC-Authentication-Results: i=1; mx.google.com; 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 c196si8210540oib.36.2020.02.10.01.15.36; Mon, 10 Feb 2020 01:15:48 -0800 (PST) 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; 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 S1727431AbgBJJPU (ORCPT + 99 others); Mon, 10 Feb 2020 04:15:20 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:42686 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726061AbgBJJPU (ORCPT ); Mon, 10 Feb 2020 04:15:20 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id AEEC67CCB37944FB8949; Mon, 10 Feb 2020 17:15:17 +0800 (CST) Received: from [127.0.0.1] (10.173.222.66) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.439.0; Mon, 10 Feb 2020 17:15:12 +0800 Subject: Re: [PATCH] nbd: fix potential NULL pointer fault in connect and disconnect process To: Mike Christie , , CC: , , , Xiubo Li References: <20200117115005.37006-1-sunke32@huawei.com> <5E21EF96.1010204@redhat.com> <5E40CB1F.7070301@redhat.com> From: "sunke (E)" Message-ID: Date: Mon, 10 Feb 2020 17:15:11 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: <5E40CB1F.7070301@redhat.com> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.173.222.66] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Mike Your idea looks good. Thanks, Sun Ke 在 2020/2/10 11:16, Mike Christie 写道: > On 01/19/2020 01:10 AM, sunke (E) wrote: >> >> Thanks for your detailed suggestions. >> >> 在 2020/1/18 1:32, Mike Christie 写道: >>> On 01/17/2020 05:50 AM, Sun Ke wrote: >>>> Connect and disconnect a nbd device repeatedly, will cause >>>> NULL pointer fault. >>>> >>>> It will appear by the steps: >>>> 1. Connect the nbd device and disconnect it, but now nbd device >>>> is not disconnected totally. >>>> 2. Connect the same nbd device again immediately, it will fail >>>> in nbd_start_device with a EBUSY return value. >>>> 3. Wait a second to make sure the last config_refs is reduced >>>> and run nbd_config_put to disconnect the nbd device totally. >>>> 4. Start another process to open the nbd_device, config_refs >>>> will increase and at the same time disconnect it. >>> >>> Just to make sure I understood this, for step 4 the process is doing: >>> >>> open(/dev/nbdX); >>> ioctl(NBD_DISCONNECT, /dev/nbdX) or nbd_genl_disconnect(for /dev/nbdX) >>> >>> ? >>> >> do nbd_genl_disconnect(for /dev/nbdX); >> I tested it. Connect /dev/nbdX >> through ioctl interface by nbd-client -L -N export localhost /dev/nbdX and >> through netlink interface by nbd-client localhost XXXX /dev/nbdX, >> disconnect /dev/nbdX by nbd-client -d /dev/nbdX. >> Both call nbd_genl_disconnect(for /dev/nbdX) and both contain the same >> null pointer dereference. >>> There is no successful NBD_DO_IT / nbd_genl_connect between the open and >>> disconnect calls at step #4, because it would normally be done at #2 and >>> that failed. nbd_disconnect_and_put could then reference a null >>> recv_workq. If we are also racing with a close() then that could free >>> the device/config from under nbd_disconnect_and_put. >>> >> Yes, nbd_disconnect_and_put could then reference a null recv_workq. > > Hey Sunke > > How about the attached patch. I am still testing it. The basic idea is > that we need to do a flush whenever we have done a sock_shutdown and are > in the disconnect/connect/clear sock path, so it just adds the flush in > that function. We then do not need to keep adding these flushes everywhere. >