Received: by 2002:a05:6a10:6d25:0:0:0:0 with SMTP id gq37csp1531689pxb; Sun, 12 Sep 2021 22:59:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrNFSo1XWoDNgIcns61lxLS0ER5pBCuoNKTqLJPNw0HDoCN7sBskoJRkGqTiMrBfFdj8+Z X-Received: by 2002:a17:906:9b1:: with SMTP id q17mr10876341eje.546.1631512773655; Sun, 12 Sep 2021 22:59:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631512773; cv=none; d=google.com; s=arc-20160816; b=XUR41PJuddJYOvv/1oLXK+BVunO9nAw6t01cym4E/+uH1iIPk9FzZwmfTrrKQfMkNr B8OT4vqzg66i03w5UR35IweoKbqPGK0a+tYz113+NJEYr1MPLj0Dv2htYGBfz59SgXTk IWRVOi/VeEVyX6oeEQu66EYKGcobC+7Fo/zl2E6q7jtG40eefFjqnqIljjrVvtHiB0GI /nSuM1uaGeNx17Rtet3zeYFB0/1I1X0tiTS0CSaLGe/jqug5lZ6eNsfJfEaktyaMDF6/ prsdzBaDf/LsCTMpB7/rZej5N4XCmCbjjiwdPzb/YOZI2tic4w6wErgnTOwNjIOHkvHq 1yjA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=59mlc5MAZl17Bwg0fnZF6E06RySu8Fbe/iP8dyny6Ao=; b=LPdw/VkyMVrCgKyzpChNmSGZK9+giId0HrUp1bxfJLZdyThF/PmytzD0j0HkO2v56n T19RSaTkqyatF6TyxmN5qRlGhvJsfFYWsdjByny60MlHrdR0h06GuTJfdMJgE0tQBcSM pVsPpBUGo9Q49Hz65WPau58VOqALseqnqNxcZ/+Cs9q0OoTdOcZMMLAy9ZF/S92AfbDd vP1aJ8h1TRYYQXIVPVDqdLZsQHTbrH0wZMGs443dXjcrYKW7KKfnZulS1uGv4u5Zq+kx F5QJcjlA210vpUTQwjxd5sx1tkMyoC01C8ikUQsDxt4aClOdaBpi3XhDxWY+d799oQan 2C3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gqfGxGRX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f16si3360499edf.48.2021.09.12.22.59.05; Sun, 12 Sep 2021 22:59:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gqfGxGRX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237261AbhIMF4q (ORCPT + 99 others); Mon, 13 Sep 2021 01:56:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29614 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237303AbhIMF4g (ORCPT ); Mon, 13 Sep 2021 01:56:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631512521; 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: in-reply-to:in-reply-to:references:references; bh=59mlc5MAZl17Bwg0fnZF6E06RySu8Fbe/iP8dyny6Ao=; b=gqfGxGRXWnu7gpXgj/89W0/4BloCQvOh4StusNfWjHGsi/N1FWPt517wEudV7KeJeKTsg+ cDub8Y5ies7KTJFBeLaUOy9VsWvHn9mgWZ9TmoNQDWQXHwMbT+44wUtm1588CKvUnq8O2H PkCIJ405uzxPccB8iQ/vyDcwwzoUG64= 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-602-JyQ73bKlN4WvJZxjyD7e5Q-1; Mon, 13 Sep 2021 01:55:17 -0400 X-MC-Unique: JyQ73bKlN4WvJZxjyD7e5Q-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 251B88145E7; Mon, 13 Sep 2021 05:55:16 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-146.pek2.redhat.com [10.72.13.146]) by smtp.corp.redhat.com (Postfix) with ESMTP id 759F85C23A; Mon, 13 Sep 2021 05:55:02 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, f.hetzelt@tu-berlin.de, david.kaplan@amd.com, konrad.wilk@oracle.com Subject: [PATCH 9/9] virtio_ring: validate used buffer length Date: Mon, 13 Sep 2021 13:53:53 +0800 Message-Id: <20210913055353.35219-10-jasowang@redhat.com> In-Reply-To: <20210913055353.35219-1-jasowang@redhat.com> References: <20210913055353.35219-1-jasowang@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch validate the used buffer length provided by the device before trying to use it. This is done by record the in buffer length in a new field in desc_state structure during virtqueue_add(), then we can fail the virtqueue_get_buf() when we find the device is trying to give us a used buffer length which is greater than the in buffer length. Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index d2ca0a7365f8..b8374a6144f3 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -69,6 +69,7 @@ struct vring_desc_state_split { void *data; /* Data for callback. */ struct vring_desc *indir_desc; /* Indirect descriptor, if any. */ + u64 buflen; /* In buffer length */ }; struct vring_desc_state_packed { @@ -76,6 +77,7 @@ struct vring_desc_state_packed { struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */ u16 num; /* Descriptor list length. */ u16 last; /* The last desc state in a list. */ + u64 buflen; /* In buffer length */ }; struct vring_desc_extra { @@ -490,6 +492,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, unsigned int i, n, avail, descs_used, prev, err_idx; int head; bool indirect; + u64 buflen = 0; START_USE(vq); @@ -571,6 +574,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE, indirect); + buflen += sg->length; } } /* Last one doesn't continue. */ @@ -605,6 +609,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq, /* Store token and indirect buffer state. */ vq->split.desc_state[head].data = data; + vq->split.desc_state[head].buflen = buflen; if (indirect) vq->split.desc_state[head].indir_desc = desc; else @@ -784,6 +789,11 @@ static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, BAD_RING(vq, "id %u is not a head!\n", i); return NULL; } + if (unlikely(*len > vq->split.desc_state[i].buflen)) { + BAD_RING(vq, "used len %d is larger than in buflen %lld\n", + *len, vq->split.desc_state[i].buflen); + return NULL; + } /* detach_buf_split clears data, so grab it now. */ ret = vq->split.desc_state[i].data; @@ -1062,6 +1072,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, unsigned int i, n, err_idx; u16 head, id; dma_addr_t addr; + u64 buflen = 0; head = vq->packed.next_avail_idx; desc = alloc_indirect_packed(total_sg, gfp); @@ -1089,6 +1100,8 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, desc[i].addr = cpu_to_le64(addr); desc[i].len = cpu_to_le32(sg->length); i++; + if (n >= out_sgs) + buflen += sg->length; } } @@ -1141,6 +1154,7 @@ static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq, vq->packed.desc_state[id].data = data; vq->packed.desc_state[id].indir_desc = desc; vq->packed.desc_state[id].last = id; + vq->packed.desc_state[id].buflen = buflen; vq->num_added += 1; @@ -1176,6 +1190,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, unsigned int i, n, c, descs_used, err_idx; __le16 head_flags, flags; u16 head, id, prev, curr, avail_used_flags; + u64 buflen = 0; START_USE(vq); @@ -1250,6 +1265,8 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, 1 << VRING_PACKED_DESC_F_AVAIL | 1 << VRING_PACKED_DESC_F_USED; } + if (n >= out_sgs) + buflen += sg->length; } } @@ -1268,6 +1285,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].data = data; vq->packed.desc_state[id].indir_desc = ctx; vq->packed.desc_state[id].last = prev; + vq->packed.desc_state[id].buflen = buflen; /* * A driver MUST NOT make the first descriptor in the list @@ -1455,6 +1473,11 @@ static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, BAD_RING(vq, "id %u is not a head!\n", id); return NULL; } + if (unlikely(*len > vq->packed.desc_state[id].buflen)) { + BAD_RING(vq, "used len %d is larger than in buflen %lld\n", + *len, vq->packed.desc_state[id].buflen); + return NULL; + } /* detach_buf_packed clears data, so grab it now. */ ret = vq->packed.desc_state[id].data; -- 2.25.1