From nobody Tue Oct 29 18:53:37 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 4XdKCj71jpz5bnQQ; Tue, 29 Oct 2024 18:53:37 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XdKCj6Rfkz45kx; Tue, 29 Oct 2024 18:53:37 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730228017; 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=hgdJtlik4ryKbn/oLyfAeQTv15O0nB3u6MKPxwX3ga0=; b=Gq/ytOA62Ai0QnilmyEXzJIn/5n90hCpOKBGO2mjjjPo+s5GQJEj/bbK+QKMb0xjKRWdpw cP9+rroTzNs1v35ABN3qqR06n7az5EyVRhL1sForuFUM1WzJc4HCSSzyHWJtx4pYdW6Xqw T7GAkw6I7FK5IXdG15aVddJMS6h2i1+k5RYWkv3ocTHa0z+zwivx3v+kCQ6QGXiTizFLiE T3UI4RhayqItxSdTthrlFqtDGGw4q4cmOwOkO7bdVIf1BcAIeJzO2TvAreBevvNY4jYksc AQHL6K8sP24ObnOgMpZeQUZsJ18taeTHLQM+zoArcy1OSL39/GSHcIGBnob/0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730228017; 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=hgdJtlik4ryKbn/oLyfAeQTv15O0nB3u6MKPxwX3ga0=; b=jg+zWKTBqlck40WPvUrj1dv1MveNnP9Z1P3kjgDC+xx9TLZhZZ+VcWxe1J5AETwVEao3R2 FGEmeSqjFo6dFxz/QXIl4ymyxCVjAGVUHSRpy7a+lt4SADUhO1LF3FN6ybJZZNSnOHEs2i WGjLg1U6ZSR1rgzAXWh6a2YZBWIjwzQPE5qSDaC9Ua7UPDVLJ8UzfUsGqgWI4JY8nNAk3o zHgMesPdPYwyh4zVOD6kU8urH8pFqV+Au4n06A/z2nfwQq/nWo4RzFZNzCUd/1752xAvFN OG0x1UfIEG99CWPviMrtw9MsmMZGD0r5+8lpyTplH9apYB+tTCN9cHygOHIDSQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1730228017; a=rsa-sha256; cv=none; b=xG6ItCRe4nbXp4laiZ+i0vpQlJQEJzJXqaPtO2I4AyhuzAZSKHg20hTfZBLrlF0AmdsGbP N82gAryy8PHOOnZ2EbjhjOGbfJ3hvXBFfjtF0YkTxgB9vqpSoa759QdCKBYPk5RIXbP9VT 1dckqlOVdA/UeSlid2tE3P7jjH5egRvi8ZXSLYSfs93G9GpGuo47u+oZKkxcsJgGHxkfZN 09fSc2Z5WusYkhCkXU5WzlnFb+NATiM9SLZo7OcXzrEfAAepi79IwIetGuTPu2qS9gvE06 4IwTmCkQbhNqzm2ac1I+zfSkPlKE+JuC2MD6W/LTb1XzL26s6JmuNNjVT0QY2g== 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 4XdKCj62xCzbk9; Tue, 29 Oct 2024 18:53:37 +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 49TIrbws035068; Tue, 29 Oct 2024 18:53:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49TIrbjO035065; Tue, 29 Oct 2024 18:53:37 GMT (envelope-from git) Date: Tue, 29 Oct 2024 18:53:37 GMT Message-Id: <202410291853.49TIrbjO035065@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: 296083d6bdb3 - releng/13.3 - 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/releng/13.3 X-Git-Reftype: branch X-Git-Commit: 296083d6bdb31c5a7720540f2c44644f87245590 Auto-Submitted: auto-generated The branch releng/13.3 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=296083d6bdb31c5a7720540f2c44644f87245590 commit 296083d6bdb31c5a7720540f2c44644f87245590 Author: Pierre Pronchery AuthorDate: 2024-08-27 13:57:32 +0000 Commit: Ed Maste CommitDate: 2024-10-29 18:52:46 +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 Security: FreeBSD-SA-24:17.bhyve Approved by: so 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) (cherry picked from commit 6eb7879f426129aa38f4e8b0d57ab7456e4eb351) --- 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