From nobody Wed Sep 4 15:51:05 2024 X-Original-To: dev-commits-src-branches@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 4WzRmV5q9Sz5V7tD; Wed, 04 Sep 2024 15:51:06 +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 4WzRmT3gWhz4TYT; Wed, 4 Sep 2024 15:51:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725465065; 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=5eoMaVS5m0cfr401kU6BYu7Ri5+fgMSz9zENCzsnBho=; b=dsetOy6ElzJoTb6MuRpQ98fMaZvBvNXRDab7cIaXIz8rmeSUZW8H2EVevfRO3odp2iF1lc bV8idhXVvVdvoARSsMqaM0HycffdVEiYniesoqBP7cB5GUkpS243muETc0smPXUyEc21I0 hZxbXEYthCtT4Q9rQcw/oVlVUGBxmLcRX/irT7yDTmw16OBHv54BXXPBbpbL+fsQYDAZqO sRIgCjnrq/eDqghuJ/z03+BZv7rtdc4VJu8PytiR2gO9CP1pD6sgb/IOUHJMi2ls/bCsTI C1H/qJYnQoQJNZ5aeTyJkkOmgg5vzpn+txsbI/CL4aO3YfS6mQvUZFidp2DpDw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725465065; a=rsa-sha256; cv=none; b=gbNWWz+OrxtR5f2UylUKwS4eMTdb6psq8+xDkc5zP0f3+L1Oo0BNpwn0cvj4TYPOBF2EL8 aEtPaT/1uQSDEGNV9Tsqym7C8Gq7AgjG/qmoHm7f1XYbppcQj9zo4NW7/KQD57HJFM+umq eB87g50aPy2KwmWil49/ly78y4hfsmfgpBTVjKibVYyArh11GtEWK5cqPiqZVafBiJlrms gRIhThCdjjBCMSfx+eBmFj/HIbgDyCxjNr4MI+ukSYeeDAMFhmv753zPIGIzEptVflfG7k iGU+NLNwIgxZqA0eGp+xq5HsZQj9y//hZ2eck5qVqwaX4vsZs8ziOTuaoE91XA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725465065; 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=5eoMaVS5m0cfr401kU6BYu7Ri5+fgMSz9zENCzsnBho=; b=tWIF77V1uc2iKmigKyJqxbnn7ymNtHdpZVshP2sfP/3iRRMKDpV10bQ4goQlkZKq0/INNl 7BIPaEynmgsurJqqnxD3BbZgRHD7S6i5XnvXtNzORfKTi8QzGKbtrQ+O6470qoUBo0h6Ex 6ycccv4BIOIRtJXabS7jGWOpGsN/5E2tq8z4Gw6n1i2T9xc0zNCb7CuYGW+42qrVPWxZ6c sBxT4WVZkNobSrdSsxv7zSJvRg/JuO/70eLfTbQJTNUTA8uzf86j3LBTTzDLKRaVtbSG9N aDLtIpRY/KTm/3JsKrTAhUxp9c9jmIKFoCH4HIz9FYTL6JFyJgrIGprNBVOvww== 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 4WzRmT3HqTzX2Z; Wed, 4 Sep 2024 15:51:05 +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 484Fp5sw015481; Wed, 4 Sep 2024 15:51:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 484Fp5A1015478; Wed, 4 Sep 2024 15:51:05 GMT (envelope-from git) Date: Wed, 4 Sep 2024 15:51:05 GMT Message-Id: <202409041551.484Fp5A1015478@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: 29937d7a1a0a - stable/14 - ctl: fix Use-After-Free in ctl_write_buffer List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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/14 X-Git-Reftype: branch X-Git-Commit: 29937d7a1a0a3061c6ae12b5b35cc32b03829501 Auto-Submitted: auto-generated The branch stable/14 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=29937d7a1a0a3061c6ae12b5b35cc32b03829501 commit 29937d7a1a0a3061c6ae12b5b35cc32b03829501 Author: Alan Somers AuthorDate: 2024-09-04 14:38:11 +0000 Commit: Ed Maste CommitDate: 2024-09-04 14:59:52 +0000 ctl: fix Use-After-Free in ctl_write_buffer The virtio_scsi device allows a guest VM to directly send SCSI commands to the kernel driver exposed on /dev/cam/ctl. This setup makes the vulnerability directly accessible from VMs through the pci_virtio_scsi bhyve device. The function ctl_write_buffer sets the CTL_FLAG_ALLOCATED flag, causing the kern_data_ptr to be freed when the command finishes processing. However, the buffer is still stored in lun->write_buffer, leading to a Use-After-Free vulnerability. Since the buffer needs to persist indefinitely, so it can be accessed by READ BUFFER, do not set CTL_FLAG_ALLOCATED. Reported by: Synacktiv Reviewed by: Pierre Pronchery Reviewed by: jhb Security: FreeBSD-SA-24:11.ctl Security: CVE-2024-45063 Security: HYP-03 Sponsored by: Axcient Sponsored by: The Alpha-Omega Project Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D46424 (cherry picked from commit 670b582db6cb827a8760df942ed8af0020a0b4d0) --- sys/cam/ctl/ctl.c | 19 +++++++++++-------- sys/cam/ctl/ctl_private.h | 8 ++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/sys/cam/ctl/ctl.c b/sys/cam/ctl/ctl.c index 1d5e13ad4671..b449e05889ec 100644 --- a/sys/cam/ctl/ctl.c +++ b/sys/cam/ctl/ctl.c @@ -5673,21 +5673,24 @@ ctl_write_buffer(struct ctl_scsiio *ctsio) return (CTL_RETVAL_COMPLETE); } + if (lun->write_buffer == NULL) { + lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, + M_CTL, M_WAITOK); + } + /* - * If we've got a kernel request that hasn't been malloced yet, - * malloc it and tell the caller the data buffer is here. + * If this kernel request hasn't started yet, initialize the data + * buffer to the correct region of the LUN's write buffer. Note that + * this doesn't set CTL_FLAG_ALLOCATED since this points into a + * persistent buffer belonging to the LUN rather than a buffer + * dedicated to this request. */ - if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { - if (lun->write_buffer == NULL) { - lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, - M_CTL, M_WAITOK); - } + if (ctsio->kern_data_ptr == NULL) { ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; ctsio->kern_data_len = len; ctsio->kern_total_len = len; ctsio->kern_rel_offset = 0; ctsio->kern_sg_entries = 0; - ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; ctsio->be_move_done = ctl_config_move_done; ctl_datamove((union ctl_io *)ctsio); diff --git a/sys/cam/ctl/ctl_private.h b/sys/cam/ctl/ctl_private.h index 04846f80e913..db8e748ec014 100644 --- a/sys/cam/ctl/ctl_private.h +++ b/sys/cam/ctl/ctl_private.h @@ -411,6 +411,14 @@ struct ctl_lun { uint8_t pr_res_type; int prevent_count; uint32_t *prevent; + + /* + * The READ_BUFFER and WRITE_BUFFER commands permit access to a logical + * data buffer associated with a LUN. Accesses to the data buffer do + * not affect data stored on the storage medium. To support this, + * allocate a buffer on first use that persists until the LUN is + * destroyed. + */ uint8_t *write_buffer; struct ctl_devid *lun_devid; TAILQ_HEAD(tpc_lists, tpc_list) tpc_lists;