From nobody Wed Oct 02 20:38:26 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XJmq73m3Wz5Y444; Wed, 02 Oct 2024 20:38:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XJmq72DgHz4gK5; Wed, 2 Oct 2024 20:38:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727901507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yucrtEe3Stqhl8+v/gVPdZgmnG57fY/pVOgsAsAmI18=; b=vqnJrgPR7aIH52/lY57AoIx29vcgh4nKI+NwEwZaBaok5T4xQCRoPKwhXsN/23TIcPulVI 5qNE6fh9SOohgrgEe0JsqL3Vdc2PAOEEuIuMSCoYKYTlCaEKdCwj6yTFiwxTpOiLLq5uWG jEBynct/n5Uf8IFHKKUdajM3KBuAg5C+V+GOrOz2X2eWCpfbGO7qEFm+cS/LGb56OPt5Xh 7HWgb3GleUf1W1EzgD5PVj5XjKXc2gvoStg47jxwMq+jXufLWsQmYGxZ7hya4k+MIY4k0x oF4Ckl/DuDuNmkX1Vhpr1CAwSao0uvKKSTNMmLCuW/82N01E66ZFviJTnS0KiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727901507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yucrtEe3Stqhl8+v/gVPdZgmnG57fY/pVOgsAsAmI18=; b=m3hf6qDbvgBlDk7kv92xrsvNOuF0yCUL6ihLslYMLD/PbBhhj7dMAHZpCrkdr/ZqkPYiXT 0Bz35wgA5QKj+vz9eYNti256m3Sbe4whzdDP2q1S2EhaIe2vAM/zQOT6OAwCmhgXQZQgQ2 ZiNDfmruhWGlQwpjHFOw1KLRSFJppRAGtAu4/xtf7Q4LZ4ckW6mreMrOUlJmwmG9aKx7xM LO5hbCNqRa3w+I07voSsgyfjKhkB2Nl4PpKtZyrVzfW+qqWj+Mk4njhia3iYbSjHjQ8s8P CVH6GFzhtaQBD2nSc1VseeGyJyU0ZSXfNOI9oi/EHLJBnFcxz60Ac7p/iAs/PA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727901507; a=rsa-sha256; cv=none; b=cqbvaj3LTTUq/GbzlFvMbykm4ZJ7tpY3PkYqQGEOwT9tSPq2XABSzf+hsVIJSrxUNWl6o1 5GJHJnweTDf5Ka6exTAyMO3syCewNl/s5MIL2w6UzDhQFzlaceAk+odRoOJNpjGE1JPvNr Yj0hZ9U/0bIO/c6g2sRYZPfLT3WFR98sbVUpZA0KNeAx55oAt7YA1EmxmtIXSAXn1xjKie 9q8vobPoViFQ4HgSHcleC3mmXUnqvLrprYMb/y78ZQCmZNa9V4TVlUvOkT/mM+uJJMpiLW lx2aYxK043jf2Z/9JyAUvOxMgwId+1gDRk0ZAzvj6bKFdl6ljhIHo2nOnz6X9A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XJmq700pbzLM3; Wed, 2 Oct 2024 20:38:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 492KcQ7T030358; Wed, 2 Oct 2024 20:38:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 492KcQ92030355; Wed, 2 Oct 2024 20:38:26 GMT (envelope-from git) Date: Wed, 2 Oct 2024 20:38:26 GMT Message-Id: <202410022038.492KcQ92030355@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 6eb7879f4261 - stable/13 - bhyve: avoid TOCTOU on iov_len in virtio_vq_recordon() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 6eb7879f426129aa38f4e8b0d57ab7456e4eb351 Auto-Submitted: auto-generated The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=6eb7879f426129aa38f4e8b0d57ab7456e4eb351 commit 6eb7879f426129aa38f4e8b0d57ab7456e4eb351 Author: Pierre Pronchery AuthorDate: 2024-08-27 13:57:32 +0000 Commit: Ed Maste CommitDate: 2024-10-02 20:37:49 +0000 bhyve: avoid TOCTOU on iov_len in virtio_vq_recordon() Avoid a race condition when accessing guest memory, by reading memory contents only once. This has also been applied to _vq_record() in sys/dev/beri/virtio/virtio.c, as per markj@'s suggestion. Reported by: Synacktiv Reviewed by: markj Security: HYP-10 Sponsored by: The Alpha-Omega Project Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D45735 (cherry picked from commit 869d760cb9d7a307faa2fbe8c1c2b238a81b74d4) (cherry picked from commit ed03c309908687bdb9f71dc6d9c9c8a92c54fc20) --- sys/dev/beri/virtio/virtio.c | 11 ++++++++--- usr.sbin/bhyve/virtio.c | 9 +++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sys/dev/beri/virtio/virtio.c b/sys/dev/beri/virtio/virtio.c index c249d1c9d37b..7302bc39d799 100644 --- a/sys/dev/beri/virtio/virtio.c +++ b/sys/dev/beri/virtio/virtio.c @@ -107,12 +107,17 @@ paddr_unmap(void *phys, uint32_t size) static inline void _vq_record(uint32_t offs, int i, volatile struct vring_desc *vd, struct iovec *iov, int n_iov, uint16_t *flags) { + uint32_t len; + uint64_t addr; + if (i >= n_iov) return; - iov[i].iov_base = paddr_map(offs, be64toh(vd->addr), - be32toh(vd->len)); - iov[i].iov_len = be32toh(vd->len); + len = atomic_load_32(&vd->len); + addr = atomic_load_64(&vd->addr); + iov[i].iov_base = paddr_map(offs, be64toh(addr), + be32toh(len)); + iov[i].iov_len = be32toh(len); if (flags != NULL) flags[i] = be16toh(vd->flags); } diff --git a/usr.sbin/bhyve/virtio.c b/usr.sbin/bhyve/virtio.c index 5294da670677..d11f5683e7b7 100644 --- a/usr.sbin/bhyve/virtio.c +++ b/usr.sbin/bhyve/virtio.c @@ -215,10 +215,15 @@ static inline void _vq_record(int i, struct vring_desc *vd, struct vmctx *ctx, struct iovec *iov, int n_iov, struct vi_req *reqp) { + uint32_t len; + uint64_t addr; + if (i >= n_iov) return; - iov[i].iov_base = paddr_guest2host(ctx, vd->addr, vd->len); - iov[i].iov_len = vd->len; + len = atomic_load_32(&vd->len); + addr = atomic_load_64(&vd->addr); + iov[i].iov_len = len; + iov[i].iov_base = paddr_guest2host(ctx, addr, len); if ((vd->flags & VRING_DESC_F_WRITE) == 0) reqp->readable++; else