Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp146994ybi; Wed, 29 May 2019 18:29:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxNYeYTWDRiwnVYVikKot+q61GOxmR0STb1oZlSfPCgIx2zBh1l60pNFIlN2HHCKdUFICzh X-Received: by 2002:a62:2ec4:: with SMTP id u187mr836539pfu.84.1559179773627; Wed, 29 May 2019 18:29:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559179773; cv=none; d=google.com; s=arc-20160816; b=dTt1LdB1HTlRZnM8Tl292BE5Zc6y0rLFULY5miQGEIQUjF0NrvIKCW8dkmSFqz5zyV 3fPusETYsGZhCrFYx1O5q8MjaQoeRWqCHqdQdlMZRVWDmy1RTj0wg12n6MVfDjrRFvQF je7p2bdFUikai59Emua6UTw0uFeeQn/h7GbsAcQ3XB65HfCzvYddWwtmC8HQ3tFW8PiW hplZSebVNZOrnOJ/RkipyCdPWHryUuiVj6cgyuh0KQe7SC8KVx6+0mlOgwP5FhSkYpe9 sd9FYIF1XXcFdGEmJR5O9V9fgeNsmRLd+WwR5be8vTZO8tist6YGU/ACPVp2QQOx0+u2 iOjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=RU0hQiCKiZKnhVtxJaZ61AU6Mv6y68L6kxn2Mw86/AA=; b=VoJarU/XPvr3bFhtCqNx8rBOiy2VAnZcF0ZBY7ou+qFdQvyQcXoD5bx1GP/CEyZE8s e/yz6rrGsXItdd7HNbsBPMcBWL2wo5dX/uHS2Q8AZ79ahF9vXAU5c42C/UBnXJ+FK7lU exfOyeH/kuXZ/RY7twAXWHLkFcmdAgbp/qh5yl4fjsuqfWRs3rYAh0qReL9rAiQl0/jT dNcnBjTZmeNhMcTBy4q/VfRQIicdvXBa8EmbzOKfSLrL4bn29573T27vvlOPuANaCPKx sh6qeTFl8Ow/lzVbMrc5IAUJpoo6NFbNvCJNMxQFZ9h/kI1v05ugzLae71BvTfumLnRi iu4g== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z10si1419964plk.235.2019.05.29.18.29.17; Wed, 29 May 2019 18:29:33 -0700 (PDT) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727268AbfE3B2O (ORCPT + 99 others); Wed, 29 May 2019 21:28:14 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38034 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726527AbfE3B2N (ORCPT ); Wed, 29 May 2019 21:28:13 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A40A083F44; Thu, 30 May 2019 01:28:13 +0000 (UTC) Received: from [10.72.12.96] (ovpn-12-96.pek2.redhat.com [10.72.12.96]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4323B19C70; Thu, 30 May 2019 01:28:10 +0000 (UTC) Subject: Re: [PATCH] nbd: fix crash when the blksize is zero To: Mike Christie , josef@toxicpanda.com, axboe@kernel.dk, nbd@other.debian.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, atumball@redhat.com References: <20190527054438.13548-1-xiubli@redhat.com> <5CEED598.7080703@redhat.com> From: Xiubo Li Message-ID: <3a7e1aed-c07a-8338-73a5-8389a2fe78dc@redhat.com> Date: Thu, 30 May 2019 09:28:07 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: <5CEED598.7080703@redhat.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 30 May 2019 01:28:13 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/5/30 2:55, Mike Christie wrote: > On 05/27/2019 12:44 AM, xiubli@redhat.com wrote: >> From: Xiubo Li >> >> This will allow the blksize to be set zero and then use 1024 as >> default. >> >> Signed-off-by: Xiubo Li >> --- >> drivers/block/nbd.c | 21 ++++++++++++++++++--- >> 1 file changed, 18 insertions(+), 3 deletions(-) >> >> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c >> index 053958a..4c1de1c 100644 >> --- a/drivers/block/nbd.c >> +++ b/drivers/block/nbd.c >> @@ -135,6 +135,8 @@ struct nbd_cmd { >> >> #define NBD_MAGIC 0x68797548 >> >> +#define NBD_DEF_BLKSIZE 1024 >> + >> static unsigned int nbds_max = 16; >> static int max_part = 16; >> static struct workqueue_struct *recv_workqueue; >> @@ -1237,6 +1239,14 @@ static void nbd_clear_sock_ioctl(struct nbd_device *nbd, >> nbd_config_put(nbd); >> } >> >> +static bool nbd_is_valid_blksize(unsigned long blksize) >> +{ >> + if (!blksize || !is_power_of_2(blksize) || blksize < 512 || >> + blksize > PAGE_SIZE) >> + return false; >> + return true; >> +} >> + >> /* Must be called with config_lock held */ >> static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, >> unsigned int cmd, unsigned long arg) >> @@ -1252,8 +1262,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, >> case NBD_SET_SOCK: >> return nbd_add_socket(nbd, arg, false); >> case NBD_SET_BLKSIZE: >> - if (!arg || !is_power_of_2(arg) || arg < 512 || >> - arg > PAGE_SIZE) >> + if (!arg) >> + arg = NBD_DEF_BLKSIZE; >> + if (!nbd_is_valid_blksize(arg)) >> return -EINVAL; >> nbd_size_set(nbd, arg, >> div_s64(config->bytesize, arg)); >> @@ -1333,7 +1344,7 @@ static struct nbd_config *nbd_alloc_config(void) >> atomic_set(&config->recv_threads, 0); >> init_waitqueue_head(&config->recv_wq); >> init_waitqueue_head(&config->conn_wait); >> - config->blksize = 1024; >> + config->blksize = NBD_DEF_BLKSIZE; >> atomic_set(&config->live_connections, 0); >> try_module_get(THIS_MODULE); >> return config; >> @@ -1769,6 +1780,10 @@ static int nbd_genl_connect(struct sk_buff *skb, struct genl_info *info) >> if (info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]) { >> u64 bsize = >> nla_get_u64(info->attrs[NBD_ATTR_BLOCK_SIZE_BYTES]); >> + if (!bsize) >> + bsize = NBD_DEF_BLKSIZE; >> + if (!nbd_is_valid_blksize(bsize)) >> + return -EINVAL; > You can't only return here. You need to also drop the mutex, do > nbd_put, and drop config_refs reference. > > Maybe you want to move this check to the beginning of the function with > the NBD_ATTR_SIZE_BYTES sanity check since the error handling is easier > there. Yeah, right. I saw your resend patch of this and that looks good to me. Thanks BRs Xiubo > >> nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize)); >> } >> if (info->attrs[NBD_ATTR_TIMEOUT]) { >>