From nobody Fri May 3 04:55:18 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1604944784; cv=none; d=zohomail.com; s=zohoarc; b=grpbeiuA2ve3KUOcnikmTlQ/VDGD/3FuYGpcW+HwCSxQPE05gUD5jIa8/NLCGoWLi7aNNTUoIaMPkbytBXcYDYuT2H1jLfbl9F7ZjedJ8kXyjKtkSnExormfeAPh0DCO5FmYunWj8BEDGQQmcBxasEYGXCIAnhXeM49AF1oxMTA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604944784; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=4HKnjMevd+nRs52G0Nuu3zrCgW4gQKmlKEyCNxRzAy0=; b=gpO2a8PXxtKgXnDyGoYLyGz0+WmaOtB5F6OM/QxWcTUC+BXyz0oOo/2MqNBdPskMZG8LoKrgofQpgMxyp87b10AYY9lPSlW2AJf2NMGaM+EuiRgJytb9TG4Irhe0rGxtBxEOwtXHdOTBYxKVsrvkpJyaYk5J3L3sg+HbE7s7KE8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 160494478487455.40282089610855; Mon, 9 Nov 2020 09:59:44 -0800 (PST) Received: from localhost ([::1]:37452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kcBSB-0007WR-Ir for importer@patchew.org; Mon, 09 Nov 2020 12:59:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kcBD7-0005qo-33 for qemu-devel@nongnu.org; Mon, 09 Nov 2020 12:44:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:40872) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kcBD5-0002Dc-2x for qemu-devel@nongnu.org; Mon, 09 Nov 2020 12:44:08 -0500 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-426-GL3j7vqINYO34O3W5Q9qDw-1; Mon, 09 Nov 2020 12:44:04 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 885CD1084C89; Mon, 9 Nov 2020 17:44:03 +0000 (UTC) Received: from localhost (ovpn-114-110.ams2.redhat.com [10.36.114.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD4AC614F5; Mon, 9 Nov 2020 17:43:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1604943846; 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; bh=4HKnjMevd+nRs52G0Nuu3zrCgW4gQKmlKEyCNxRzAy0=; b=T/3KDEjIbexarrxpLBOhU0wZVzu/BEgBUbcGIekFsyQpcusJPQ7l+hDAnBFLQbGlUxBqV9 HbvH+wofNsMofS/ESR4ZM6PXqEJg0nhqFr2G0r0b4joYweXo2olcqmtPjr8lAlhAo0zqYU ysnTN1JOBd5jWrF4P1ly4pY9/I4XMhU= X-MC-Unique: GL3j7vqINYO34O3W5Q9qDw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH for-5.2] vhost-user: fix VHOST_USER_ADD/REM_MEM_REG truncation Date: Mon, 9 Nov 2020 17:43:55 +0000 Message-Id: <20201109174355.1069147-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/09 01:25:23 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Cornelia Huck , Christian Borntraeger , Raphael Norwitz , Stefan Hajnoczi , "Michael S. Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" QEMU currently truncates the mmap_offset field when sending VHOST_USER_ADD_MEM_REG and VHOST_USER_REM_MEM_REG messages. The struct layout looks like this: typedef struct VhostUserMemoryRegion { uint64_t guest_phys_addr; uint64_t memory_size; uint64_t userspace_addr; uint64_t mmap_offset; } VhostUserMemoryRegion; typedef struct VhostUserMemRegMsg { uint32_t padding; /* WARNING: there is a 32-bit hole here! */ VhostUserMemoryRegion region; } VhostUserMemRegMsg; The payload size is calculated as follows when sending the message in hw/virtio/vhost-user.c: msg->hdr.size =3D sizeof(msg->payload.mem_reg.padding) + sizeof(VhostUserMemoryRegion); This calculation produces an incorrect result of only 36 bytes. sizeof(VhostUserMemRegMsg) is actually 40 bytes. The consequence of this is that the final field, mmap_offset, is truncated. This breaks x86_64 TCG guests on s390 hosts. Other guest/host combinations may get lucky if either of the following holds: 1. The guest memory layout does not need mmap_offset !=3D 0. 2. The host is little-endian and mmap_offset <=3D 0xffffffff so the truncation has no effect. Fix this by extending the existing 32-bit padding field to 64-bit. Now the padding reflects the actual compiler padding. This can be verified using pahole(1). Also document the layout properly in the vhost-user specification. The vhost-user spec did not document the exact layout. It would be impossible to implement the spec without looking at the QEMU source code. Existing vhost-user frontends and device backends continue to work after this fix has been applied. The only change in the wire protocol is that QEMU now sets hdr.size to 40 instead of 36. If a vhost-user implementation has a hardcoded size check for 36 bytes, then it will fail with new QEMUs. Both QEMU and DPDK/SPDK don't check the exact payload size, so they continue to work. Fixes: f1aeb14b0809e313c74244d838645ed25e85ea63 ("Transmit vhost-user memor= y regions individually") Cc: Raphael Norwitz Cc: Cornelia Huck Cc: Michael S. Tsirkin Cc: Christian Borntraeger Signed-off-by: Stefan Hajnoczi Reviewed-by: Cornelia Huck Reviewed-by: Raphael Norwitz --- docs/interop/vhost-user.rst | 21 +++++++++++++++++++-- contrib/libvhost-user/libvhost-user.h | 2 +- hw/virtio/vhost-user.c | 5 ++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 988f154144..6d4025ba6a 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -131,6 +131,23 @@ A region is: =20 :mmap offset: 64-bit offset where region starts in the mapped memory =20 +Single memory region description +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++---------+---------------+------+--------------+-------------+ +| padding | guest address | size | user address | mmap offset | ++---------+---------------+------+--------------+-------------+ + +:padding: 64-bit + +:guest address: a 64-bit guest address of the region + +:size: a 64-bit size + +:user address: a 64-bit user address + +:mmap offset: 64-bit offset where region starts in the mapped memory + Log description ^^^^^^^^^^^^^^^ =20 @@ -1281,7 +1298,7 @@ Master message types ``VHOST_USER_ADD_MEM_REG`` :id: 37 :equivalent ioctl: N/A - :slave payload: memory region + :slave payload: single memory region description =20 When the ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol feature has been successfully negotiated, this message is submitted @@ -1296,7 +1313,7 @@ Master message types ``VHOST_USER_REM_MEM_REG`` :id: 38 :equivalent ioctl: N/A - :slave payload: memory region + :slave payload: single memory region description =20 When the ``VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS`` protocol feature has been successfully negotiated, this message is submitted diff --git a/contrib/libvhost-user/libvhost-user.h b/contrib/libvhost-user/= libvhost-user.h index a1539dbb69..7d47f1364a 100644 --- a/contrib/libvhost-user/libvhost-user.h +++ b/contrib/libvhost-user/libvhost-user.h @@ -136,7 +136,7 @@ typedef struct VhostUserMemory { } VhostUserMemory; =20 typedef struct VhostUserMemRegMsg { - uint32_t padding; + uint64_t padding; VhostUserMemoryRegion region; } VhostUserMemRegMsg; =20 diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 9c5b4f7fbc..2fdd5daf74 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -149,7 +149,7 @@ typedef struct VhostUserMemory { } VhostUserMemory; =20 typedef struct VhostUserMemRegMsg { - uint32_t padding; + uint64_t padding; VhostUserMemoryRegion region; } VhostUserMemRegMsg; =20 @@ -800,8 +800,7 @@ static int vhost_user_add_remove_regions(struct vhost_d= ev *dev, uint64_t shadow_pcb[VHOST_USER_MAX_RAM_SLOTS] =3D {}; int nr_add_reg, nr_rem_reg; =20 - msg->hdr.size =3D sizeof(msg->payload.mem_reg.padding) + - sizeof(VhostUserMemoryRegion); + msg->hdr.size =3D sizeof(msg->payload.mem_reg); =20 /* Find the regions which need to be removed or added. */ scrub_shadow_regions(dev, add_reg, &nr_add_reg, rem_reg, &nr_rem_reg, --=20 2.28.0