From nobody Wed Sep 04 20:54:14 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 4WzZVG6rBKz5VMX5; Wed, 04 Sep 2024 20:54:14 +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 4WzZVG5mGmz4QDB; Wed, 4 Sep 2024 20:54:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1725483254; 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=I7cE7jIQWUCXO6H+A3s80+xfxHfKEe/rm1o2dz2jwmQ=; b=OIOl1KTJ051MCOwPlTVTt5711tvYo2FEXsolwd1oWgAvBvDh344+3CtIy9K4eOJykgSGzs WWs8JfqI2gEh+FIZkDaK/Y2+LQibL/Wr6GV82v5XEygt/e7rVPBDor8bnQ/4Xc+2D70thJ KCCi7xs3Lg2dWqGWhBnK56u51ri+4eYkNshaZll+V/cxf8e4IfecHtnZnic1csE6o13ltn DVnjxIX3xnoTg+L+A8S7QB5b9OHP/eekfC0hkZBj4k2UNS2FdRsxsLAkaj+xAzaGvPrFCO 0s0GXAn2PkWLxitqT00dOEWeAYeZW+GNIuNc+pakEvM8F+CIeQjsRyW3HDrN+w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1725483254; a=rsa-sha256; cv=none; b=IYq9HhXDjENCdvF4YBoLm1dILr/BmI9+8IpW/2wcOYFWEaFAW87wKLaHbv1Iw59jKvcFPU qg0kujxt+IsuaOw02u4XyJ5Vnlu41I2FGqtTd6vciShsEktC4Ofz2ooxE/ZT7IcCunRc0s cFi86f5LMKqwQaaCjb2Lel8mfB4KKRQaC/4DT08NUxuj02A1GCS6j7FuVHL5Hedrg2qCvf mWo1NnJOx9pYvRdD0P7FdRkviecGooq2sNjIoRLO3TJMP3ejIPEraH1VKWgLMRaE5c/0cC PXW8iB8JtiX2cagrQWAcUo4k6twS1oYGThiPZCzaqlzQSGEhbr6OER6LUsCSUg== 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=1725483254; 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=I7cE7jIQWUCXO6H+A3s80+xfxHfKEe/rm1o2dz2jwmQ=; b=htW0m1Lr+uUgmN6KcKzIpmvvSqkVMaNgTyidj3FcxnkFaTC7A6UscnyqPLhtX/C9MvUmxc Oh68dCyEp7zO4GUXtlxhU1bZ6lq+ulB1FWATyWTQa38J2/yb29XLkkTzA08qzUHlis/xCF mN7jmVo2Xoktq4YEgEIvMIiNARXHIBKVQ4liYfV9I3YGNeCCN1v0ptvT8CZh39lZC5/M0b f5BZib9DRIoVeM7EtX849Mm+prr8yzuE4fMVOYS/EUnhusGa1MEM2J8KOeMJbQwumdDIb+ 08GB8UR4psWQZCdJGLLODaVptG8E/geYxDljMfAmPL014FUKoO/VepQLmjA6cA== 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 4WzZVG5NMCzgDK; Wed, 4 Sep 2024 20:54:14 +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 484KsELV033783; Wed, 4 Sep 2024 20:54:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 484KsEdF033780; Wed, 4 Sep 2024 20:54:14 GMT (envelope-from git) Date: Wed, 4 Sep 2024 20:54:14 GMT Message-Id: <202409042054.484KsEdF033780@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: 0a271608b328 - releng/14.0 - ctl: fix Use-After-Free in ctl_write_buffer 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/14.0 X-Git-Reftype: branch X-Git-Commit: 0a271608b328aa554bf9bea1a1108825cad89eec Auto-Submitted: auto-generated The branch releng/14.0 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=0a271608b328aa554bf9bea1a1108825cad89eec commit 0a271608b328aa554bf9bea1a1108825cad89eec Author: Alan Somers AuthorDate: 2024-09-04 14:38:11 +0000 Commit: Ed Maste CommitDate: 2024-09-04 20:54:02 +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) (cherry picked from commit 29937d7a1a0a3061c6ae12b5b35cc32b03829501) Approved by: so --- 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 6faecb0da0a4..004e86900342 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 9a87345015fa..85f4f6137810 100644 --- a/sys/cam/ctl/ctl_private.h +++ b/sys/cam/ctl/ctl_private.h @@ -410,6 +410,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;