Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3244487pxb; Mon, 18 Apr 2022 20:25:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwV4Uodzg06tRJJcB56CaAEXepaNSmiLqQgn+I2WBlwVml7X73J6eEBZ/3coDdPcZZWIlHo X-Received: by 2002:a05:6402:d7:b0:413:673:ba2f with SMTP id i23-20020a05640200d700b004130673ba2fmr15138299edu.29.1650338714635; Mon, 18 Apr 2022 20:25:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650338714; cv=none; d=google.com; s=arc-20160816; b=HGs02RzNML3LdOMxQrEVMmnJmXNOBmVsPEaBDmApETDMoQa9QLoyru+ZnR8X/fIeXM JdBjg1lnsZDfWb+3dPcuGrsjoiR8hSoehItvkc9odI7N1QMfze79zYg5jkbxJTIuq2Hl Lg0dzibqZzyznxitM3oneXcJGF7Ea8B4eHvzH8POkRItWYgJouITWYUaquxT3YQfd7pU a8Y/1Gk+7v/jnjrYIknSvxBje7ED9WoDHNMsGRgK6LwQNC7haKs3StwF0XStNoGmQlpE 1zekUrrziME/Hm1OHQKNjqWwiKhVgDZOZxZOPBJy+3S++bmC1X2JOyM54DrAiP2TLK/P fByA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=X6c54j+61Etf/0PMsEaQL4qbxoaJaA+lINuosrEtlXA=; b=DxPXa7Y2S0rhjj8ZgT0PUOqEFrGhxqtkWBWxXgy/5hSaFY7JH/XFq7KoJ95GVj0Jjp bInkvwop9iVPQnVnjB7zv6ZD+VPgHRZwxEXrTN6uOKENHR0cIHAiAs5QAG2C+TxkHwV0 GbFYJcfmqtIB44ikRsQWq4OmtoScnGTuuWbptoHLuE8neI/ERfLh8pHcTN1Ajd2z4VtX +6ipNGAkoHKUJf/TYnuVrDwM+Xds7hJ/Q9sbkV0zyOUQHApBFo2/FRks/lep9PByZnp4 Re+4b+IhANb1Z1Gwyn14SIbL2lc6ZwlSymYWmqtoIafOpuchgYUcM57T/gbX7aw+Cayl mjMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fyqZHoqr; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dt3-20020a170907728300b006e8701e7d69si8481275ejc.85.2022.04.18.20.24.48; Mon, 18 Apr 2022 20:25:14 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fyqZHoqr; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243507AbiDRNd5 (ORCPT + 99 others); Mon, 18 Apr 2022 09:33:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241235AbiDRNHA (ORCPT ); Mon, 18 Apr 2022 09:07:00 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6E722A26A; Mon, 18 Apr 2022 05:47:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 519B76124A; Mon, 18 Apr 2022 12:47:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59C67C385A1; Mon, 18 Apr 2022 12:47:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286043; bh=ErH+l6hXTN8Q0qGt8gkT3zRW+oYajTQEEk1rs7dX0yU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fyqZHoqrEMGGyn01GN4LT6+i5IyksFOss0N6U4QYtw9fmP9MWYFwkyoUbnmR8kSoD 5+52sqfFBAFUPf8Ayg8rIY7O+jw+qyoaTcy5DFuxC4UENE6tGy6444qnr/0/w86WCm BywUXoOlBodEDZIUisqGWS3IT/ykmmcyIDGVtx/k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xie Yongji , "Michael S. Tsirkin" , Jens Axboe , Lee Jones Subject: [PATCH 4.14 012/284] virtio-blk: Use blk_validate_block_size() to validate block size Date: Mon, 18 Apr 2022 14:09:53 +0200 Message-Id: <20220418121211.046677405@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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: Xie Yongji commit 57a13a5b8157d9a8606490aaa1b805bafe6c37e1 upstream. The block layer can't support a block size larger than page size yet. And a block size that's too small or not a power of two won't work either. If a misconfigured device presents an invalid block size in configuration space, it will result in the kernel crash something like below: [ 506.154324] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 506.160416] RIP: 0010:create_empty_buffers+0x24/0x100 [ 506.174302] Call Trace: [ 506.174651] create_page_buffers+0x4d/0x60 [ 506.175207] block_read_full_page+0x50/0x380 [ 506.175798] ? __mod_lruvec_page_state+0x60/0xa0 [ 506.176412] ? __add_to_page_cache_locked+0x1b2/0x390 [ 506.177085] ? blkdev_direct_IO+0x4a0/0x4a0 [ 506.177644] ? scan_shadow_nodes+0x30/0x30 [ 506.178206] ? lru_cache_add+0x42/0x60 [ 506.178716] do_read_cache_page+0x695/0x740 [ 506.179278] ? read_part_sector+0xe0/0xe0 [ 506.179821] read_part_sector+0x36/0xe0 [ 506.180337] adfspart_check_ICS+0x32/0x320 [ 506.180890] ? snprintf+0x45/0x70 [ 506.181350] ? read_part_sector+0xe0/0xe0 [ 506.181906] bdev_disk_changed+0x229/0x5c0 [ 506.182483] blkdev_get_whole+0x6d/0x90 [ 506.183013] blkdev_get_by_dev+0x122/0x2d0 [ 506.183562] device_add_disk+0x39e/0x3c0 [ 506.184472] virtblk_probe+0x3f8/0x79b [virtio_blk] [ 506.185461] virtio_dev_probe+0x15e/0x1d0 [virtio] So let's use a block layer helper to validate the block size. Signed-off-by: Xie Yongji Acked-by: Michael S. Tsirkin Link: https://lore.kernel.org/r/20211026144015.188-5-xieyongji@bytedance.com Signed-off-by: Jens Axboe Signed-off-by: Lee Jones Signed-off-by: Greg Kroah-Hartman --- drivers/block/virtio_blk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -822,9 +822,17 @@ static int virtblk_probe(struct virtio_d err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE, struct virtio_blk_config, blk_size, &blk_size); - if (!err) + if (!err) { + err = blk_validate_block_size(blk_size); + if (err) { + dev_err(&vdev->dev, + "virtio_blk: invalid block size: 0x%x\n", + blk_size); + goto out_free_tags; + } + blk_queue_logical_block_size(q, blk_size); - else + } else blk_size = queue_logical_block_size(q); /* Use topology information if available */