From nobody Mon Feb 9 03:51:31 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1639677328; cv=none; d=zohomail.com; s=zohoarc; b=C2qSdc9LnLGqx2Iua8GcmnT8t1lSbLlOPy/kHeKg/Dn/2F6tPQI27kzUYeQtgNNXxKM50agXPgExTwGK4nIF93WOUK7QrM+7eRDZyYI8ieGGkDHZsYMroc5oTa4cpKJnD5bgR5jB7OPFaEzvSb9EXMqEEM3n9t0X85Lh0ubIqsk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639677328; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=EBEhhvwmrK3INOwJ0KjtvMl1VgesDG36wHzC7bVJ4nc=; b=XGztkE5haQp1Gdbl7Y2unmp3IrY8K6pIfk5wO/MYitEviOGy11IxpWLig/nU9yf2JCL97JRznMk1VfRRA3x7oRGoX8YlUunKhmqT3Wr4iIu7oLv67Mr9JgPC4t2qz+QNMHmZ8fLxguNgmylcCSDrdCpbixahuz1usjXyMU42808= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1639677328567942.8782746956524; Thu, 16 Dec 2021 09:55:28 -0800 (PST) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-148-DV4HSQChN12VpnHEPVnDlA-1; Thu, 16 Dec 2021 12:55:26 -0500 Received: by mail-wm1-f70.google.com with SMTP id b75-20020a1c804e000000b0034569bde713so1602942wmd.9 for ; Thu, 16 Dec 2021 09:55:25 -0800 (PST) Return-Path: Return-Path: Received: from localhost.localdomain (174.red-83-50-185.dynamicip.rima-tde.net. [83.50.185.174]) by smtp.gmail.com with ESMTPSA id w8sm5509969wre.106.2021.12.16.09.55.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 09:55:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639677327; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EBEhhvwmrK3INOwJ0KjtvMl1VgesDG36wHzC7bVJ4nc=; b=Kg+iOy7ECK4wX4yBQQD7NEcrp7cgByqmBDOjhkJiuXjbMLSl6YGrgboxL7la9Hsvnhkewl 1UBnPuO0+TD2BSw59G6yuEtO1uV5pYk0FlZPkGyQye4r1kd6vOfrLh4EO35PKGgq7zVBN/ EX5z0xR5g6Z9c8WftZ4zOcQgDOQCPgU= X-MC-Unique: DV4HSQChN12VpnHEPVnDlA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EBEhhvwmrK3INOwJ0KjtvMl1VgesDG36wHzC7bVJ4nc=; b=dxJaitSW1bJjQ5VZ8YvjKkALDW1M+oAHNByqKFMj5NEVLshb7LLiVtUHble+m9O4CR asr44gbUyfdlbY/0n8J9+YcLC3Fgvi4aeSdib7fI+QFMmS+iZ4l1/rYpMdsPeoNmR8lQ 3yeadMkQo7lDWq6Hc/DQ2U+ZRNJKlx2mp3TzoAiKubsnwBebMiSPvSuLBvhG0Ml1Q7V4 IaXHeqgNH9uMUWJm9JKxeYFNINQrzuqysOFYiAkOXgYQ0xYOaKZDLRhKdTUUI86LvL6W 41zIPtZsgr9q3sWE9dgTxde/K8OeqIqWXVfmDtV+2jcDHSoB6G9vZ/zuf6ivz9IGu/6R b59A== X-Gm-Message-State: AOAM530/MDPW4bkBzRXXq63Li2bmhRRr69RkbfOVLMtOapwp+Q5qBW83 WyzJXNBeCIWx4A1vgbIsG0URxygSPPc7eDUGH3kGVLD7+22LootoI52KU8Ve1lM9JSci5y1oBAO a8/rAlxfW1S66oQ== X-Received: by 2002:a5d:6da2:: with SMTP id u2mr10123205wrs.273.1639677325001; Thu, 16 Dec 2021 09:55:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJyKq/OOw3vRa7EiIkjaYarlgIoTYwbR/cYUfPpK/bcCcI/DiN36NRfauduaSSnaUZAePdgY8w== X-Received: by 2002:a5d:6da2:: with SMTP id u2mr10123187wrs.273.1639677324855; Thu, 16 Dec 2021 09:55:24 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Klaus Jensen , Qiuhao Li , "Edgar E . Iglesias" , Keith Busch , Li Qiang , Jason Wang , Bandan Das , David Hildenbrand , qemu-block@nongnu.org, Peter Xu , Mauro Matteo Cascella , Peter Maydell , Alexander Bulekov , Darren Kenny , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH 1/2] hw/nvme/ctrl: Do not ignore DMA access errors Date: Thu, 16 Dec 2021 18:55:09 +0100 Message-Id: <20211216175510.884749-2-philmd@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211216175510.884749-1-philmd@redhat.com> References: <20211216175510.884749-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1639677329161100001 dma_buf_read/dma_buf_write() return a MemTxResult type. Do not discard it, propagate the DMA error to the caller. Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Keith Busch --- hw/nvme/ctrl.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index fa410a179a6..604ed0aea0d 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -1147,15 +1147,16 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, ui= nt8_t *ptr, uint32_t len, =20 if (sg->flags & NVME_SG_DMA) { const MemTxAttrs attrs =3D MEMTXATTRS_UNSPECIFIED; + MemTxResult res; uint64_t residual; =20 if (dir =3D=3D NVME_TX_DIRECTION_TO_DEVICE) { - dma_buf_write(ptr, len, &residual, &sg->qsg, attrs); + res =3D dma_buf_write(ptr, len, &residual, &sg->qsg, attrs); } else { - dma_buf_read(ptr, len, &residual, &sg->qsg, attrs); + res =3D dma_buf_read(ptr, len, &residual, &sg->qsg, attrs); } =20 - if (unlikely(residual)) { + if (unlikely(residual) || res !=3D MEMTX_OK) { trace_pci_nvme_err_invalid_dma(); return NVME_INVALID_FIELD | NVME_DNR; } --=20 2.33.1 From nobody Mon Feb 9 03:51:31 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=philmd@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1639677335; cv=none; d=zohomail.com; s=zohoarc; b=dQQcAIJNJ3uUu9i7VFb0cS5GL40/FgKw/wo9pnDwBx+0GlOtxudhr7Tp1HCg0z1t41IklVMmUTApL0WZN7lTrCiooFGHGW3K8agLkLKCICxZxBS+20NnDeL1roubDCFaQUGAKVMNpbfJ2K+DHNKtXh5DCLx4WN8n1l/b4fK6P2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1639677335; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=UGIjOqreiR3wf0mzjMXHC2g8ApWAQ49FKn1AAebFmwc=; b=VlvwxZFKycVfTolW8C8K43Y+prUzI4uOfMMfGxMoA0hjjqJD8j0LNjszKjSJnxIIjiJwSOjVNpZAhn2hO7VP5VAXq+C1mOyZaO8wGbLJuFeMpb1TDyvkyOt0JUNj+KMogl5S4cLsZmg0Efcu1IQunVVvXEoQgJ/ZDg1EbXuuFhA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=philmd@redhat.com; dmarc=pass header.from= (p=none dis=none) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1639677335470822.9976167960955; Thu, 16 Dec 2021 09:55:35 -0800 (PST) Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-414-eY6-khDMOYeXV7OVebA_5g-1; Thu, 16 Dec 2021 12:55:33 -0500 Received: by mail-wm1-f70.google.com with SMTP id v190-20020a1cacc7000000b003456d598510so1606186wme.6 for ; Thu, 16 Dec 2021 09:55:33 -0800 (PST) Return-Path: Return-Path: Received: from localhost.localdomain (174.red-83-50-185.dynamicip.rima-tde.net. [83.50.185.174]) by smtp.gmail.com with ESMTPSA id t17sm9238842wmq.15.2021.12.16.09.55.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Dec 2021 09:55:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639677334; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UGIjOqreiR3wf0mzjMXHC2g8ApWAQ49FKn1AAebFmwc=; b=fMvri973R2LrYH5GvNGRqE/Fhhn4+iEcbU8VKK0BSHn/HOr2mb6s9X6ijWSk5M50h56dc9 lIOs29Butih4+QtL3AZHLedvuzsH0ZWMqror0Zb9NGM5qmJL53w0UPZNlchn5ZoQE3Lswp 8jTPNMSvny9K6M9d921/Wf1oiwF23RM= X-MC-Unique: eY6-khDMOYeXV7OVebA_5g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UGIjOqreiR3wf0mzjMXHC2g8ApWAQ49FKn1AAebFmwc=; b=YWR5eX8pMIBq3dGddX+bR87R/M6JR3dgzDxdPXVdduI9/eMu+aCCCRGPR+9BbDLKLS gXQhq7+v6p/+SLzpwIehxLusJxJC8r3EoqVYeEM7rOgPFuV+fnAge5jCHBQ8DtZju/Jd tSMUp8jhDkMYBftnfkeJSemDWxcv/mDpOhgvI1G3Ml2oyRtTqnQyAW9zShiDYIaWnzgD 23FeW9/ffG1Tb1jRoNFVaB7ypnixScehCA9d+iLu1dU6s8Ia8PcupmRJnuUmpt1DclpT YE7NJ9Ui6Na+8vR4La1dCQdkWy/JcBgbHdyZ8dn6kNQPW2rLdvqzLDFXIdUOK8urKEXv 0KAw== X-Gm-Message-State: AOAM531geArWQhRSzykWqlGsyKpc3i2CGKej2tbr7D3sIaUpewmrAtSz dS6g1mydR+pDKwWj7Q6Bz7uGfhIGIBBkYPn0d7GMstG5ZlNcFIz7lQmDYBT5AvUgvPaAde+IXkH YBOe24SVQ9Uh0Pw== X-Received: by 2002:a05:6000:15ca:: with SMTP id y10mr10179921wry.642.1639677329942; Thu, 16 Dec 2021 09:55:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJxfnK03Kv0K9CSC7sl1OfJrbOKYMl9wq8FDK48j9kAeeeUDkNhtP8urE/K/I3TexWuHcKVuSg== X-Received: by 2002:a05:6000:15ca:: with SMTP id y10mr10179895wry.642.1639677329708; Thu, 16 Dec 2021 09:55:29 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Klaus Jensen , Qiuhao Li , "Edgar E . Iglesias" , Keith Busch , Li Qiang , Jason Wang , Bandan Das , David Hildenbrand , qemu-block@nongnu.org, Peter Xu , Mauro Matteo Cascella , Peter Maydell , Alexander Bulekov , Darren Kenny , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH 2/2] hw/nvme/ctrl: Prohibit DMA accesses to devices (CVE-2021-3929) Date: Thu, 16 Dec 2021 18:55:10 +0100 Message-Id: <20211216175510.884749-3-philmd@redhat.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211216175510.884749-1-philmd@redhat.com> References: <20211216175510.884749-1-philmd@redhat.com> MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=philmd@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1639677336773100001 Async DMA requests might access MMIO regions and re-program the NVMe controller internal registers while DMA requests are still scheduled or in flight. Avoid that by prohibing the controller to access non-memories regions. The bug has been audited looking at the following report from Qiuhao Li: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D793444=3D=3DERROR: AddressSanitizer: heap-use-after-free on address= 0x616000026198 WRITE of size 2 at 0x616000026198 thread T0 #0 0x55d64d672178 in nvme_process_sq hw/nvme/ctrl.c:5556:25 #1 0x55d64f3b3fde in timerlist_run_timers util/qemu-timer.c:573:9 #2 0x55d64f3b430c in qemu_clock_run_timers util/qemu-timer.c:587:12 0x616000026198 is located 24 bytes inside of 624-byte region [0x616000026= 180,0x6160000263f0) freed by thread T0 here: #1 0x7f9e20a0ddac in g_free (/lib64/libglib-2.0.so.0+0x56dac) #2 0x55d64d661ec2 in nvme_ctrl_reset hw/nvme/ctrl.c:5578:13 #3 0x55d64d65b5e4 in nvme_write_bar hw/nvme/ctrl.c:5824:13 #4 0x55d64d658f70 in nvme_mmio_write hw/nvme/ctrl.c:6174:9 #5 0x55d64e36f413 in memory_region_write_accessor softmmu/memory.c:49= 2:5 #6 0x55d64e36ed51 in access_with_adjusted_size softmmu/memory.c:554:18 #7 0x55d64e36d666 in memory_region_dispatch_write softmmu/memory.c:15= 04:16 #8 0x55d64e33e8ee in flatview_write_continue softmmu/physmem.c:2812:23 #9 0x55d64e32d0eb in flatview_write softmmu/physmem.c:2854:12 #10 0x55d64e32cba8 in address_space_write softmmu/physmem.c:2950:18 #11 0x55d64e32d417 in address_space_rw softmmu/physmem.c:2960:16 #12 0x55d64cd207e2 in dma_memory_rw_relaxed include/sysemu/dma.h:89:12 #13 0x55d64cd2054a in dma_memory_rw include/sysemu/dma.h:132:12 #14 0x55d64cd1c922 in dma_buf_rw softmmu/dma-helpers.c:312:16 #15 0x55d64cd1c2e1 in dma_buf_read softmmu/dma-helpers.c:327:12 #16 0x55d64d638aab in nvme_tx hw/nvme/ctrl.c:1156:19 #17 0x55d64d6a72f4 in nvme_c2h hw/nvme/ctrl.c:1191:12 #18 0x55d64d6b7554 in nvme_fw_log_info hw/nvme/ctrl.c:4142:12 #19 0x55d64d6ab5e8 in nvme_get_log hw/nvme/ctrl.c:4294:16 #20 0x55d64d6740d5 in nvme_admin_cmd hw/nvme/ctrl.c:5499:16 #21 0x55d64d6720a3 in nvme_process_sq hw/nvme/ctrl.c:5554:13 #22 0x55d64f3b3fde in timerlist_run_timers util/qemu-timer.c:573:9 previously allocated by thread T0 here: #1 0x7f9e20a115e0 in g_malloc0 (/lib64/libglib-2.0.so.0+0x5a5e0) #2 0x55d64d661856 in nvme_start_ctrl hw/nvme/ctrl.c:5718:5 #3 0x55d64d65b503 in nvme_write_bar hw/nvme/ctrl.c:5815:17 #4 0x55d64d658f70 in nvme_mmio_write hw/nvme/ctrl.c:6174:9 #5 0x55d64e36f413 in memory_region_write_accessor softmmu/memory.c:49= 2:5 #6 0x55d64e36ed51 in access_with_adjusted_size softmmu/memory.c:554:18 #7 0x55d64e36d666 in memory_region_dispatch_write softmmu/memory.c:15= 04:16 #8 0x55d64e33e8ee in flatview_write_continue softmmu/physmem.c:2812:23 #9 0x55d64e32d0eb in flatview_write softmmu/physmem.c:2854:12 #10 0x55d64e32cba8 in address_space_write softmmu/physmem.c:2950:18 SUMMARY: AddressSanitizer: heap-use-after-free hw/nvme/ctrl.c:5556:25 in = nvme_process_sq Shadow bytes around the buggy address: 0x0c2c7fffcbe0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcbf0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc00: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc10: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc20: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa =3D>0x0c2c7fffcc30: fd fd fd[fd]fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc40: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc50: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc60: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd 0x0c2c7fffcc70: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fa fa 0x0c2c7fffcc80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Heap left redzone: fa Freed heap region: fd =3D=3D793444=3D=3DABORTING Fixes: CVE-2021-3929 Reported-by: Qiuhao Li Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Keith Busch --- hw/nvme/ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index 604ed0aea0d..2be2c340b34 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -1146,7 +1146,7 @@ static uint16_t nvme_tx(NvmeCtrl *n, NvmeSg *sg, uint= 8_t *ptr, uint32_t len, assert(sg->flags & NVME_SG_ALLOC); =20 if (sg->flags & NVME_SG_DMA) { - const MemTxAttrs attrs =3D MEMTXATTRS_UNSPECIFIED; + const MemTxAttrs attrs =3D { .memory =3D true }; MemTxResult res; uint64_t residual; =20 --=20 2.33.1