From nobody Wed Feb 11 08:40:33 2026 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1652735916; cv=none; d=zohomail.com; s=zohoarc; b=QiG/cFRzKGrQlsR9xh95Nbax+tykY1Y0nYScde+BZmgc4+s3ycehzzq+PcLKrA/+fXHWLZ2ZoUnxukixxUMtGp2hJwd4jWxyhBbd5+dTPo76QV2i0MRxXlVewxVBa5AfwWOEkMARJkUpypb+4l72Z59ZXawtCrK3sQo3XSL80Tw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652735916; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2D8zJoKITBIvDeR7b/dV2fYboZZQxIF/D34DE4tt2Q8=; b=YJ3L/PHskIYF7BnH8Y5K29N4WH/1PoXfEFxpMAtKCSB3E5sisQMHziKVrVTTGzOJh8s3H7njdcgKhYv8BjcoJaVLBr9/E6WTRks8Cl4JxBreJAwPd2WuKyMlzMovvKdfHE9qTVk66JhtcUc+Vhov6NFmhxlLgBqVxSZHy11PJHU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1652735916961585.8517068802622; Mon, 16 May 2022 14:18:36 -0700 (PDT) Received: from localhost ([::1]:37392 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nqi6t-0000DE-G7 for importer2@patchew.org; Mon, 16 May 2022 17:18:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58972) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqhiL-0007UB-7I for qemu-devel@nongnu.org; Mon, 16 May 2022 16:53:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:49744) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nqhiJ-0006by-GV for qemu-devel@nongnu.org; Mon, 16 May 2022 16:53:12 -0400 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-BhpqoKfxOjWFpwk1SOGWRw-1; Mon, 16 May 2022 16:53:09 -0400 Received: by mail-ej1-f69.google.com with SMTP id ga27-20020a1709070c1b00b006f43c161da4so6356488ejc.7 for ; Mon, 16 May 2022 13:53:09 -0700 (PDT) Received: from redhat.com ([2.55.131.38]) by smtp.gmail.com with ESMTPSA id o23-20020aa7dd57000000b0042abfe32ac8sm604613edw.30.2022.05.16.13.53.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 13:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652734390; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2D8zJoKITBIvDeR7b/dV2fYboZZQxIF/D34DE4tt2Q8=; b=WNsr4jToCUfW1/SG0d28ICuG9bjITUQXYpDX6V8U0+9JHAEx2euBQN6G0VXzcys9zi6Zi4 NOFuPMvCOTVpJ3IAU6Rg8iyVL04PgjGZTn58dtFbqRgXx2PBoZHCIqbAfiMZNOHqvVJA0l PJWrE9gzm73qHP/+QCcyIITY0asO0gA= X-MC-Unique: BhpqoKfxOjWFpwk1SOGWRw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=2D8zJoKITBIvDeR7b/dV2fYboZZQxIF/D34DE4tt2Q8=; b=XqwpBns0Jr4RcBKKWUYVRwG8H0Qbmm10pn/uq0a9E1UYaaZEU6QvI4+VTM/UhQGCwG mUdThpVswcVRopys5y1BRHyAmgS2XUbshe4MQzrTJNACL6rQG3ebLqZERG4sRbhQMCJG W6srVzoGKazKcLXMG2u4KUEEafhaq6dZTApqp+BzJSa5Jy2C65eVm1vzwioPQeWkRe7J a/EJhsVyahHtNTkWyg15J6bn7jFf6TrOe1Y+cjGLcLjDShRoBLJz55cEZHZmDf5FaPUJ Ntpkb12Psjr3j8D9Uplm6tkGswSK3RWmfOoIg+guI5xtutlCUlCZEC2ngD+jg6mWuXN4 F1sg== X-Gm-Message-State: AOAM533Y3f+FvwhWbGJB0lV0FRt3jDeBkub1eO6tOmqRWf8kqMXBdf1h 5jo5S4Ia0gfyQMJiUzKyi4fD0Aw7M/l9urmVLx9hth8nzxeINz+TRG8C/aQ6wbV7o64ybnYrhi7 12J5fS+FzVlgcIMntCSAyl7Nf6ckah/XCNvxucyqkAXiHelyVaVQKMN08AyTM X-Received: by 2002:aa7:ce82:0:b0:42a:b4a1:992b with SMTP id y2-20020aa7ce82000000b0042ab4a1992bmr6312974edv.327.1652734387904; Mon, 16 May 2022 13:53:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1AVRY423OLFDv2JEh1snHSt1CYknel9N5sMZSvfC3lYVpSU5VbddzFNps/ha/HqEFoV+2Xg== X-Received: by 2002:aa7:ce82:0:b0:42a:b4a1:992b with SMTP id y2-20020aa7ce82000000b0042ab4a1992bmr6312958edv.327.1652734387664; Mon, 16 May 2022 13:53:07 -0700 (PDT) Date: Mon, 16 May 2022 16:53:04 -0400 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Eugenio =?utf-8?B?UMOpcmV6?= Subject: [PULL v2 43/86] vhost: Track descriptor chain in private at SVQ Message-ID: <20220516204913.542894-44-mst@redhat.com> References: <20220516204913.542894-1-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: <20220516204913.542894-1-mst@redhat.com> X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1652735918453100001 From: Eugenio P=C3=A9rez The device could have access to modify them, and it definitely have access when we implement packed vq. Harden SVQ maintaining a private copy of the descriptor chain. Other fields like buffer addresses are already maintained sepparatedly. Signed-off-by: Eugenio P=C3=A9rez Message-Id: <20220512175747.142058-2-eperezma@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/virtio/vhost-shadow-virtqueue.h | 6 ++++++ hw/virtio/vhost-shadow-virtqueue.c | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-vi= rtqueue.h index e5e24c536d..c132c994e9 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -53,6 +53,12 @@ typedef struct VhostShadowVirtqueue { /* Next VirtQueue element that guest made available */ VirtQueueElement *next_guest_avail_elem; =20 + /* + * Backup next field for each descriptor so we can recover securely, n= ot + * needing to trust the device access. + */ + uint16_t *desc_next; + /* Next head to expose to the device */ uint16_t shadow_avail_idx; =20 diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index 1e5cfe2af6..1d6552b0fe 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -138,6 +138,7 @@ static void vhost_vring_write_descs(VhostShadowVirtqueu= e *svq, hwaddr *sg, for (n =3D 0; n < num; n++) { if (more_descs || (n + 1 < num)) { descs[i].flags =3D flags | cpu_to_le16(VRING_DESC_F_NEXT); + descs[i].next =3D cpu_to_le16(svq->desc_next[i]); } else { descs[i].flags =3D flags; } @@ -145,10 +146,10 @@ static void vhost_vring_write_descs(VhostShadowVirtqu= eue *svq, hwaddr *sg, descs[i].len =3D cpu_to_le32(iovec[n].iov_len); =20 last =3D i; - i =3D cpu_to_le16(descs[i].next); + i =3D cpu_to_le16(svq->desc_next[i]); } =20 - svq->free_head =3D le16_to_cpu(descs[last].next); + svq->free_head =3D le16_to_cpu(svq->desc_next[last]); } =20 static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, @@ -336,7 +337,6 @@ static void vhost_svq_disable_notification(VhostShadowV= irtqueue *svq) static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq, uint32_t *len) { - vring_desc_t *descs =3D svq->vring.desc; const vring_used_t *used =3D svq->vring.used; vring_used_elem_t used_elem; uint16_t last_used; @@ -365,7 +365,7 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowV= irtqueue *svq, return NULL; } =20 - descs[used_elem.id].next =3D svq->free_head; + svq->desc_next[used_elem.id] =3D svq->free_head; svq->free_head =3D used_elem.id; =20 *len =3D used_elem.len; @@ -540,8 +540,9 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIOD= evice *vdev, svq->vring.used =3D qemu_memalign(qemu_real_host_page_size(), device_s= ize); memset(svq->vring.used, 0, device_size); svq->ring_id_maps =3D g_new0(VirtQueueElement *, svq->vring.num); + svq->desc_next =3D g_new0(uint16_t, svq->vring.num); for (unsigned i =3D 0; i < svq->vring.num - 1; i++) { - svq->vring.desc[i].next =3D cpu_to_le16(i + 1); + svq->desc_next[i] =3D cpu_to_le16(i + 1); } } =20 @@ -574,6 +575,7 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq) virtqueue_detach_element(svq->vq, next_avail_elem, 0); } svq->vq =3D NULL; + g_free(svq->desc_next); g_free(svq->ring_id_maps); qemu_vfree(svq->vring.desc); qemu_vfree(svq->vring.used); --=20 MST