git: 49204732f67c - main - cam: Fix XPT_MMC_IO buffer unmap order in cam_periph_unmapmem()

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 05 Nov 2026 14:51:52 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=49204732f67c32922a7ccc71f3b45f0d1d194cd2

commit 49204732f67c32922a7ccc71f3b45f0d1d194cd2
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-22 06:33:54 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-05 14:51:26 +0000

    cam: Fix XPT_MMC_IO buffer unmap order in cam_periph_unmapmem()
    
    XPT_MMC_IO maps two nested buffers: cmd.data, then cmd.data->data whose
    address is derived from cmd.data.  cam_periph_unmapmem() restored them in
    forward order, so it put cmd.data back to its userspace value and then
    dereferenced that pointer to reach cmd.data->data from kernel context,
    panicking in vm_fault_lookup().
    
    Unmap in reverse order, mirroring how the mappings were created.  This is
    a no-op for single-buffer func_codes and for the other two-buffer cases
    (XPT_DEV_MATCH, XPT_SMP_IO), whose buffers are independent.
    
    The bug is only reachable via the pass(4) userspace path, which maps and
    unmaps CCB buffers; in-kernel MMCCAM consumers never call
    cam_periph_{map,unmap}mem().  Triggered by a userspace SDIO CMD53
    transfer from sdiotool(8) through pass(4).
    
    MFC after:      1 week
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D61021
---
 sys/cam/cam_periph.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/sys/cam/cam_periph.c b/sys/cam/cam_periph.c
index d79977249cf2..3de3ae327dc9 100644
--- a/sys/cam/cam_periph.c
+++ b/sys/cam/cam_periph.c
@@ -1111,7 +1111,13 @@ cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo)
 	}
 
 	error = 0;
-	for (i = 0; i < numbufs; i++) {
+	/*
+	 * Unmap in reverse order.  XPT_MMC_IO maps cmd.data and then
+	 * cmd.data->data; the second pointer lives inside the first
+	 * mapping, so it must be processed before the first mapping is
+	 * removed.
+	 */
+	for (i = numbufs - 1; i >= 0; i--) {
 		if (mapinfo->bp[i]) {
 			/* unmap the buffer */
 			vunmapbuf(mapinfo->bp[i]);