Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp629225pxb; Tue, 5 Apr 2022 16:31:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJq2+3M4uvPGtbxSv0U8kARFE8kFt3TtA9bWkCRNF2okyIiIZikpEeaynxjK2vM8tUQoQC X-Received: by 2002:a63:7f15:0:b0:398:5224:9b52 with SMTP id a21-20020a637f15000000b0039852249b52mr4768611pgd.249.1649201514781; Tue, 05 Apr 2022 16:31:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649201514; cv=none; d=google.com; s=arc-20160816; b=Qa2rrxhJ6y8ZSvMy8BGGtKbUDM+ho6uvwTJGe3OvdzJNB0arKtRuNJjWApdWh4wqsS OVwXdhm8WBSgYyWeMpVKUZeQcd5d9wcnAvCBzj50NphIjFSAgmyhA3fqYyrmUGZkQsLJ HylOiUmwAmy6NEmNgPZDRDSDqWXsbx6tvurU8SKMWOiZKlh4hTVTop1Lcgo6fj00LQ9F vKHYFAn9qikpvzB1jzMH15S1tnGJhGvuf249LeufjnlvpMVIWl7nCKuzQuhkmzX1YvT5 /46dQpI1yhqC/No/q8j+A1i1kNfZFT9BwKmaaXV9qrcNanOyDimN6YvL78xXSVL9QGM/ +3cA== 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=jzDznmQZkRXWD74pmGyFkHXzcBo1x4KhQwcZ5c4QC8I=; b=Udq9AkpRzmsiwB1M4HDJL5Z/5QANOjoIGJb5ZEXvACV2Jan18l9QNLLdhaFjU67oGe OcIysT85kkTgrfgm33836SGrGty6LJYlIaXZBFS1v2mXyNcOKDVClZ4BR0E8B7f39/Ol RmA7S9gYfu6DM2ejX9IYN92dNgDX9OL31RhjxmUlxVZ+Aeay3hWvDmtjOAr8jpGU8PGo WFOobeOaSGiSgqgKkEnzhxvENEBpWrgcQxj/zNvCbqnrTvzXYXEogJGEt6rIz/+I83ga qn55gIlHM1/QcmbAQpTSD9IrHOekAKkJyUurLhhjoFn9c+B1R5jMkQ2nWTz3C1nXUVP0 o/ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RHB+L6X0; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e5-20020a17090ada0500b001bd14e01fcdsi2879862pjv.187.2022.04.05.16.31.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 16:31:54 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RHB+L6X0; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 394FBBD7F3; Tue, 5 Apr 2022 16:16:21 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379957AbiDEOPv (ORCPT + 99 others); Tue, 5 Apr 2022 10:15:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238400AbiDEJcc (ORCPT ); Tue, 5 Apr 2022 05:32:32 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73D1AB64; Tue, 5 Apr 2022 02:19:20 -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 10D326144D; Tue, 5 Apr 2022 09:19:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 276AFC385A0; Tue, 5 Apr 2022 09:19:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649150359; bh=1fG89xdKOt8ET6oufH5VJwXYBoTv7beuzBJOrhMCqqA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RHB+L6X0dSZVyceRSRobiVfCfJh+xJEnJqXXsb60cj9h8y3qpvl8aSEVNSdCF5tjC NKiumy08zcGgrxzak4h/MR2sf0YiaCX67uFyHcdYVzcIPny0uYmvEkTBPzoXZgFeHq 5GMkHx0ZiNB8KtN1rOkfQ4u4zPjO4YtWpVYCZoBk= 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 5.15 029/913] virtio-blk: Use blk_validate_block_size() to validate block size Date: Tue, 5 Apr 2022 09:18:11 +0200 Message-Id: <20220405070340.689812747@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 @@ -815,9 +815,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_cleanup_disk; + } + blk_queue_logical_block_size(q, blk_size); - else + } else blk_size = queue_logical_block_size(q); /* Use topology information if available */