Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3412906ybi; Sun, 26 May 2019 22:55:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzGR6YWstiZpY5QBr/ejtQmwRWMnTln3JZhahf2jbmn53HDrAZTfIrNjivi+x8yzsq5g7Ug X-Received: by 2002:a17:90a:228f:: with SMTP id s15mr28385834pjc.10.1558936535407; Sun, 26 May 2019 22:55:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558936535; cv=none; d=google.com; s=arc-20160816; b=aaZvevTrAU4KyQcYJD0zcDLsKwKy5UnBHDzt8D2q8ADNxdryGFVczBpyqzd2E2e1ha VNDYgPyvrNjD2Xd/msd1m722HHqvbowsHXgu3lvN6fqVvrTmv15L9vB+LTZIhTxs79iM MhzkJzqu6uHY3sOrPyFTF3FE+LO0wD4RourCJZht1Y7Eg+XbKzhXjf5pRBd9Xg78nWdL muBPOldyRHll4p04MFJccAJG/y98MLyYzmnRerPsZXC4fjaoDXVIEhL4SlPkn+imHvrf Gi3TId/SZbCgQaZPvs2Ss7ne9mgPABSz5VYr/JwVmqOtEPpy91UWv2dPJKtvnULP8CLS /yKA== 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:mime-version :message-id:date:subject:cc:to:from; bh=7/pIhVL1CI0YFNxXaULLevKoVcZZQ55PLLk1lBgUIqQ=; b=pEPDDvoukAb46meNEaCbVSX/WI090LEhIH4GgNxvhNX0HulNaKOeSQOREeUf2c5vrE +/7sQFyChzmWtXMYV/9gbnBaeKhdaU66Md3QRdojIxCImmWBT+s3kA3Ix1qrCL+3g6oE J/XdpEwAyq398FFe7ifKeD4rjLSAM3RI5lT7zl4x/978qQn+bDr2YYiu73W6krSYxLQO ZvAT7OehwCOiMQ8rgUzu9U7eX3Au2bOlW+JLPVObuGb60+zWq2OEYguh7ol8BDX+DQYh sdhTGPpavQelURhfezqDVApWyruIbjTnIB/EjnWueKhqhFqBH5WJl5f0hI+NFma2flFh 6iuA== 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 f6si12170204pgs.544.2019.05.26.22.55.19; Sun, 26 May 2019 22:55:35 -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 S1726055AbfE0FqL (ORCPT + 99 others); Mon, 27 May 2019 01:46:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45724 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725774AbfE0FqL (ORCPT ); Mon, 27 May 2019 01:46:11 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 22F1330820C9; Mon, 27 May 2019 05:46:11 +0000 (UTC) Received: from rhel3.localdomain (ovpn-12-147.pek2.redhat.com [10.72.12.147]) by smtp.corp.redhat.com (Postfix) with ESMTP id 30B5560C9B; Mon, 27 May 2019 05:46:07 +0000 (UTC) From: xiubli@redhat.com To: josef@toxicpanda.com, axboe@kernel.dk, nbd@other.debian.org Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, atumball@redhat.com, Xiubo Li Subject: [PATCH] nbd: fix crash when the blksize is zero Date: Mon, 27 May 2019 13:44:38 +0800 Message-Id: <20190527054438.13548-1-xiubli@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Mon, 27 May 2019 05:46:11 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; nbd_size_set(nbd, bsize, div64_u64(config->bytesize, bsize)); } if (info->attrs[NBD_ATTR_TIMEOUT]) { -- 1.8.3.1