From nobody Sun Oct 26 01:45:00 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1516080091313221.982213112311; Mon, 15 Jan 2018 21:21:31 -0800 (PST) Received: from localhost ([::1]:35921 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebJga-0002pR-HK for importer@patchew.org; Tue, 16 Jan 2018 00:21:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48582) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebJAm-0000Ws-PN for qemu-devel@nongnu.org; Mon, 15 Jan 2018 23:48:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebJAj-00058b-Mn for qemu-devel@nongnu.org; Mon, 15 Jan 2018 23:48:32 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60422) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ebJAj-00058O-DM for qemu-devel@nongnu.org; Mon, 15 Jan 2018 23:48:29 -0500 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 mx1.redhat.com (Postfix) with ESMTPS id 89ABDA7E5; Tue, 16 Jan 2018 04:48:28 +0000 (UTC) Received: from redhat.com (ovpn-125-221.rdu2.redhat.com [10.10.125.221]) by smtp.corp.redhat.com (Postfix) with SMTP id 1868DA2D9E; Tue, 16 Jan 2018 04:48:28 +0000 (UTC) Date: Tue, 16 Jan 2018 06:48:27 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1516077852-7974-33-git-send-email-mst@redhat.com> References: <1516077852-7974-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1516077852-7974-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 16 Jan 2018 04:48:28 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 32/33] vhost-user: fix misaligned access to payload X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We currently take a pointer to a misaligned field of a packed structure. clang reports this as a build warning. A fix is to keep payload in a separate structure, and access is it from there using a vectored write. Signed-off-by: Michael S. Tsirkin --- hw/virtio/vhost-user.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 6ac3610..7930fd8 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -652,33 +652,34 @@ static void slave_read(void *opaque) { struct vhost_dev *dev =3D opaque; struct vhost_user *u =3D dev->opaque; - VhostUserMsg msg =3D { 0, }; + VhostUserHeader hdr =3D { 0, }; + VhostUserPayload payload =3D { 0, }; int size, ret =3D 0; =20 /* Read header */ - size =3D read(u->slave_fd, &msg, VHOST_USER_HDR_SIZE); + size =3D read(u->slave_fd, &hdr, VHOST_USER_HDR_SIZE); if (size !=3D VHOST_USER_HDR_SIZE) { error_report("Failed to read from slave."); goto err; } =20 - if (msg.hdr.size > VHOST_USER_PAYLOAD_SIZE) { + if (hdr.size > VHOST_USER_PAYLOAD_SIZE) { error_report("Failed to read msg header." - " Size %d exceeds the maximum %zu.", msg.hdr.size, + " Size %d exceeds the maximum %zu.", hdr.size, VHOST_USER_PAYLOAD_SIZE); goto err; } =20 /* Read payload */ - size =3D read(u->slave_fd, &msg.payload, msg.hdr.size); - if (size !=3D msg.hdr.size) { + size =3D read(u->slave_fd, &payload, hdr.size); + if (size !=3D hdr.size) { error_report("Failed to read payload from slave."); goto err; } =20 - switch (msg.hdr.request) { + switch (hdr.request) { case VHOST_USER_SLAVE_IOTLB_MSG: - ret =3D vhost_backend_handle_iotlb_msg(dev, &msg.payload.iotlb); + ret =3D vhost_backend_handle_iotlb_msg(dev, &payload.iotlb); break; case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG : ret =3D vhost_user_slave_handle_config_change(dev); @@ -692,15 +693,23 @@ static void slave_read(void *opaque) * REPLY_ACK feature handling. Other reply types has to be managed * directly in their request handlers. */ - if (msg.hdr.flags & VHOST_USER_NEED_REPLY_MASK) { - msg.hdr.flags &=3D ~VHOST_USER_NEED_REPLY_MASK; - msg.hdr.flags |=3D VHOST_USER_REPLY_MASK; + if (hdr.flags & VHOST_USER_NEED_REPLY_MASK) { + struct iovec iovec[2]; =20 - msg.payload.u64 =3D !!ret; - msg.hdr.size =3D sizeof(msg.payload.u64); =20 - size =3D write(u->slave_fd, &msg, VHOST_USER_HDR_SIZE + msg.hdr.si= ze); - if (size !=3D VHOST_USER_HDR_SIZE + msg.hdr.size) { + hdr.flags &=3D ~VHOST_USER_NEED_REPLY_MASK; + hdr.flags |=3D VHOST_USER_REPLY_MASK; + + payload.u64 =3D !!ret; + hdr.size =3D sizeof(payload.u64); + + iovec[0].iov_base =3D &hdr; + iovec[0].iov_len =3D VHOST_USER_HDR_SIZE; + iovec[1].iov_base =3D &payload; + iovec[1].iov_len =3D hdr.size; + + size =3D writev(u->slave_fd, iovec, ARRAY_SIZE(iovec)); + if (size !=3D VHOST_USER_HDR_SIZE + hdr.size) { error_report("Failed to send msg reply to slave."); goto err; } --=20 MST