From nobody Tue Oct 29 18:49:52 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 4XdK7N2HQCz5bn9Z; Tue, 29 Oct 2024 18:49:52 +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 4XdK7N11Q6z43mw; Tue, 29 Oct 2024 18:49:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730227792; 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=kYa5v1Csjzm7nT36qlednuoHmZigDwhz16ePD6UcWUI=; b=fizRN/nwQ7dbhBcGBBJR3NXFFobQfUiqJ0djlcWBGKfFuS1g6et0qz1pkDuEAfj4H7TC/1 BX/337xC3qrdjXMXzVH+uu+ohnqeEeqDfAAkjekPe4pYfX48DmwE/h4GSfnbzQ3npLRerE a9FlmMeUXidvd2w+H1Y9+3bXHzsm5aS7FyGGkxlrOniEiIIMBHP+IDhiTanw659T7Tu4tf ZsJJHtM5AI5K7L0NzdqKVJwN/8eA5DjlCUsdtdDY5fm6fKA2xuTppmQ4GgM7kt7Ab0UZ+C i1BYKOrrODahL0BKwPVLD1ow5Kvxv389Eub86v9bog3G47PUICZ9dbyiPK9AWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730227792; 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=kYa5v1Csjzm7nT36qlednuoHmZigDwhz16ePD6UcWUI=; b=slmEZizKKbNCKyiisNjyEdHjxPP0r8462RYOuDf3/u873oeEYpHylrXlzpn+EHqcFux68t aqL/j8IpeYT7vrUG2TaIXfHfYYs//7KK/nnidiXCA81Zv5atbNIoLSRQHTGSZerFDG+pVf zhvvPurZC51FX1C/weA1M5fZDzQ0q8xIJ9mqbGyXJWIHreZXI0uBADEJyKA6C2GaSAHlnQ W1ztUqbGdO3Q5kbs25U8HO5L/+xHiX0Q2PmxFMPv+bQprNxh0v+oOeSsFkuwm34i78vz3s 2rK1/Nf8xXDAbXG8lg0LsMS2PKDwcKsBWecw01uDNcygq8zzX9AdciEaFXl9Ww== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1730227792; a=rsa-sha256; cv=none; b=dRLSWHpdb91Lxs49li/SjSmUZ7cIWle1SYY3aKEm/49xuM8fFFSJC5PxJGUEN/H60UKynh FuPaTNrD9gfRwJWzkpAGvhXBQ857TqgruNuXCQUb97x3Y/E7A8LYpD3CwMQBdZzJvliV3P YOMmAq8ADfMXwNxiPOuwSlN+WGc7oqBjBFsnOrv+CDzHNQkXm9zYpIS0ndJy8kQUWf8I4y V0vxJP/Lph/QSO1mv6SLaUUPoL68QlRE3bqcTtUCsfMr9wGhMJXv9S/JFcMez/TlmXhKPK pJXjYjYufMri19oGRrRm1JYS+MmCj3Mw2MDbaYRok+Rzh8bLWu0uhA5yJ17k/Q== 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 4XdK7N0d5yzb7F; Tue, 29 Oct 2024 18:49:52 +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 49TInqg2018551; Tue, 29 Oct 2024 18:49:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49TInq6T018548; Tue, 29 Oct 2024 18:49:52 GMT (envelope-from git) Date: Tue, 29 Oct 2024 18:49:52 GMT Message-Id: <202410291849.49TInq6T018548@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: 1c48a9b47821 - releng/13.4 - 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.4 X-Git-Reftype: branch X-Git-Commit: 1c48a9b47821de7c4d0dbe28de112b525470e209 Auto-Submitted: auto-generated The branch releng/13.4 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=1c48a9b47821de7c4d0dbe28de112b525470e209 commit 1c48a9b47821de7c4d0dbe28de112b525470e209 Author: Pierre Pronchery AuthorDate: 2024-08-27 13:57:32 +0000 Commit: Ed Maste CommitDate: 2024-10-29 18:46:43 +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