git: f7c24ef1e9a4 - main - camcontrol: Don't close the CAM device in nvme_get_data()

From: Kenneth D. Merry <ken_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 19:14:14 UTC
The branch main has been updated by ken:

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

commit f7c24ef1e9a47b889eaaae8514c61e95d9738cf8
Author:     Kenneth D. Merry <ken@FreeBSD.org>
AuthorDate: 2026-11-09 18:47:01 +0000
Commit:     Kenneth D. Merry <ken@FreeBSD.org>
CommitDate: 2026-11-09 18:59:13 +0000

    camcontrol: Don't close the CAM device in nvme_get_data()
    
    nvme_get_data() closed the supplied CAM device on every error
    path, but its callers either pass the device that main() opened
    and closes, or close the device themselves, so an error resulted
    in a double cam_close_device() and a double free.  Move the error
    path close into print_dev_nvme(), the one caller that opens its
    own device, and leave the device alone in nvme_get_data().
    
    This predates the recent camcontrol NVMe additions, but they added
    more callers.
    
    Reported by:    llm-postcr
    Fixes:          6ee13c54dad0
    MFC after:      1 week
    Sponsored by:   Spectra Logic
    Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
---
 sbin/camcontrol/camcontrol.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/sbin/camcontrol/camcontrol.c b/sbin/camcontrol/camcontrol.c
index 28a2d39606a6..8a93206a3d7a 100644
--- a/sbin/camcontrol/camcontrol.c
+++ b/sbin/camcontrol/camcontrol.c
@@ -813,7 +813,6 @@ nvme_get_data(struct cam_device *dev, int nvme_data_type, void *data,
 	ccb = cam_getccb(dev);
 	if (ccb == NULL) {
 		warnx("couldn't allocate CCB");
-		cam_close_device(dev);
 		return (1);
 	}
 
@@ -840,7 +839,6 @@ nvme_get_data(struct cam_device *dev, int nvme_data_type, void *data,
 	return 0;
 
 bailout_error:
-	cam_close_device(dev);
 	return (1);
 }
 
@@ -858,8 +856,10 @@ print_dev_nvme(struct device_match_result *dev_result, char *tmpstr)
 		return (1);
 	}
 
-	if (nvme_get_data(dev, CDAI_TYPE_NVME_CNTRL, &cdata, sizeof(cdata)))
+	if (nvme_get_data(dev, CDAI_TYPE_NVME_CNTRL, &cdata, sizeof(cdata))) {
+		cam_close_device(dev);
 		return (1);
+	}
 
 	cam_strvis(vendor, cdata.mn, sizeof(cdata.mn), sizeof(vendor));
 	cam_strvis(product, cdata.fr, sizeof(cdata.fr), sizeof(product));