Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Oct 2023 16:05:47 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 7ea866eb14f8 - main - nvme: Fix memory leak in pt ioctl commands
Message-ID:  <202310021605.392G5lmI053060@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=7ea866eb14f8ec869a525442c03228b6701e1dab

commit 7ea866eb14f8ec869a525442c03228b6701e1dab
Author:     David Sloan <david.sloan@eideticom.com>
AuthorDate: 2023-09-07 16:22:21 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-10-02 15:50:14 +0000

    nvme: Fix memory leak in pt ioctl commands
    
    When running nvme passthrough commands through the ioctl interface
    memory is mapped with vmapbuf() but not unmapped. This results in leaked
    memory whenever a process executes an nvme passthrough command with a
    data buffer. This can be replicated with a simple c function (error
    checks skipped for brevity):
    
    void leak_memory(int nvme_ns_fd, uint16_t nblocks) {
            struct nvme_pt_command pt = {
                    .cmd = {
                            .opc = NVME_OPC_READ,
                            .cdw12 = nblocks - 1,
                    },
                    .len = nblocks * 512, // Assumes devices with 512 byte lba
                    .is_read = 1, // Reads and writes should both trigger leak
            }
            void *buf;
    
            posix_memalign(&buf, nblocks * 512);
            pt.buf = buf;
            ioctl(nvme_ns_fd, NVME_PASSTHROUGH_COMMAND, &pt);
            free(buf);
    }
    
    Signed-off-by: David Sloan <david.sloan@eideticom.com>
    
    PR:             273626
    Reviewed by:    imp, markj
    MFC after:      1 week
---
 sys/dev/nvme/nvme_ctrlr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sys/dev/nvme/nvme_ctrlr.c b/sys/dev/nvme/nvme_ctrlr.c
index 30a5ee81b2a4..ef4d7daa6efa 100644
--- a/sys/dev/nvme/nvme_ctrlr.c
+++ b/sys/dev/nvme/nvme_ctrlr.c
@@ -1334,8 +1334,9 @@ nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr,
 		mtx_sleep(pt, mtx, PRIBIO, "nvme_pt", 0);
 	mtx_unlock(mtx);
 
-err:
 	if (buf != NULL) {
+		vunmapbuf(buf);
+err:
 		uma_zfree(pbuf_zone, buf);
 		PRELE(curproc);
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202310021605.392G5lmI053060>