From patchwork Tue May 18 15:04:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 12264973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,MIME_BASE64_TEXT, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A9ECC433B4 for ; Tue, 18 May 2021 15:04:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E4516108B for ; Tue, 18 May 2021 15:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344081AbhERPFj (ORCPT ); Tue, 18 May 2021 11:05:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33415 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343903AbhERPFj (ORCPT ); Tue, 18 May 2021 11:05:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621350260; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=+bQ9SJNaGOJTT/Ip80Zm/iZNd8qbzj+zVIqxvcFO7kk=; b=ILlMI3q4qZlrnSHC34onWRzM8pMVORuGF9Z2Y0ICE0UwlWDiqOl8YvWELRQwitN563UoK1 YMvHXtEdGsvqNY9OBjDGnHV59C1Q4WXgeBsojlFjUUZXWMLHYphg/vQHrp0fQNgJkNK6bU p0QzRp/+R3JNi2qd2pa0j6FRHS0Pr6c= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-151-1QKLHYpSO2i-ljSdkmC-mw-1; Tue, 18 May 2021 11:04:19 -0400 X-MC-Unique: 1QKLHYpSO2i-ljSdkmC-mw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D21048049CD; Tue, 18 May 2021 15:04:17 +0000 (UTC) Received: from localhost (ovpn-115-196.ams2.redhat.com [10.36.115.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 369EA5D9C0; Tue, 18 May 2021 15:04:17 +0000 (UTC) From: Stefan Hajnoczi To: linux-block@vger.kernel.org Cc: jasowang@redhat.com, "Michael S. Tsirkin" , Christoph Hellwig , Xie Yongji , Stefan Hajnoczi Subject: [PATCH] virtio-blk: limit seg_max to a safe value Date: Tue, 18 May 2021 16:04:15 +0100 Message-Id: <20210518150415.152730-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org The struct virtio_blk_config seg_max value is read from the device and incremented by 2 to account for the request header and status byte descriptors added by the driver. In preparation for supporting untrusted virtio-blk devices, protect against integer overflow and limit the value to a safe maximum (SG_MAX_SEGMENTS). Signed-off-by: Stefan Hajnoczi --- drivers/block/virtio_blk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index b9fa3ef5b57c..4dfd5fc7aeea 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -728,7 +728,10 @@ static int virtblk_probe(struct virtio_device *vdev) if (err || !sg_elems) sg_elems = 1; - /* We need an extra sg elements at head and tail. */ + /* Prevent integer overflows and excessive blk-mq req cmd_size */ + sg_elems = min_t(u32, sg_elems, SG_MAX_SEGMENTS); + + /* We need extra sg elements at head and tail. */ sg_elems += 2; vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL); if (!vblk) {