git: eb8d8798a0eb - stable/13 - bhyve: Avoid unlikely truncation of the blockif ident strings.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 26 Jan 2023 20:27:15 UTC
The branch stable/13 has been updated by jhb:

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

commit eb8d8798a0eb49e58fb8f38ef33c3eabec9e7071
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-11-29 01:09:15 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-01-26 20:23:58 +0000

    bhyve: Avoid unlikely truncation of the blockif ident strings.
    
    The ident string for NVMe and VirtIO block deivces do not contain the
    bus, and the various fields can potentially use up to three characters
    when printed as unsigned values (full range of uint8_t) even if not
    likely in practice.
    
    Reviewed by:    corvink, chuck
    Differential Revision:  https://reviews.freebsd.org/D37488
    
    (cherry picked from commit 5d805962ca9347bbf62750452c4c980decb94793)
---
 usr.sbin/bhyve/pci_ahci.c         | 4 ++--
 usr.sbin/bhyve/pci_nvme.c         | 4 ++--
 usr.sbin/bhyve/pci_virtio_block.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/usr.sbin/bhyve/pci_ahci.c b/usr.sbin/bhyve/pci_ahci.c
index 4571f4caeffb..09b996476774 100644
--- a/usr.sbin/bhyve/pci_ahci.c
+++ b/usr.sbin/bhyve/pci_ahci.c
@@ -2422,7 +2422,7 @@ pci_ahci_hd_legacy_config(nvlist_t *nvl, const char *opts)
 static int
 pci_ahci_init(struct vmctx *ctx __unused, struct pci_devinst *pi, nvlist_t *nvl)
 {
-	char bident[sizeof("XX:XX:XX")];
+	char bident[sizeof("XXX:XXX:XXX")];
 	char node_name[sizeof("XX")];
 	struct blockif_ctxt *bctxt;
 	struct pci_ahci_softc *sc;
@@ -2469,7 +2469,7 @@ pci_ahci_init(struct vmctx *ctx __unused, struct pci_devinst *pi, nvlist_t *nvl)
 		 * Attempt to open the backing image. Use the PCI slot/func
 		 * and the port number for the identifier string.
 		 */
-		snprintf(bident, sizeof(bident), "%d:%d:%d", pi->pi_slot,
+		snprintf(bident, sizeof(bident), "%u:%u:%u", pi->pi_slot,
 		    pi->pi_func, p);
 
 		bctxt = blockif_open(port_nvl, bident);
diff --git a/usr.sbin/bhyve/pci_nvme.c b/usr.sbin/bhyve/pci_nvme.c
index 571a6a9fda62..edbac73c7ed5 100644
--- a/usr.sbin/bhyve/pci_nvme.c
+++ b/usr.sbin/bhyve/pci_nvme.c
@@ -3152,7 +3152,7 @@ pci_nvme_read(struct vmctx *ctx __unused, int vcpu __unused,
 static int
 pci_nvme_parse_config(struct pci_nvme_softc *sc, nvlist_t *nvl)
 {
-	char bident[sizeof("XX:X:X")];
+	char bident[sizeof("XXX:XXX")];
 	const char *value;
 	uint32_t sectsz;
 
@@ -3226,7 +3226,7 @@ pci_nvme_parse_config(struct pci_nvme_softc *sc, nvlist_t *nvl)
 			return (-1);
 		}
 	} else {
-		snprintf(bident, sizeof(bident), "%d:%d",
+		snprintf(bident, sizeof(bident), "%u:%u",
 		    sc->nsc_pi->pi_slot, sc->nsc_pi->pi_func);
 		sc->nvstore.ctx = blockif_open(nvl, bident);
 		if (sc->nvstore.ctx == NULL) {
diff --git a/usr.sbin/bhyve/pci_virtio_block.c b/usr.sbin/bhyve/pci_virtio_block.c
index 2b2e6a412a5a..06d2a9671c0b 100644
--- a/usr.sbin/bhyve/pci_virtio_block.c
+++ b/usr.sbin/bhyve/pci_virtio_block.c
@@ -453,7 +453,7 @@ static int
 pci_vtblk_init(struct vmctx *ctx __unused, struct pci_devinst *pi,
     nvlist_t *nvl)
 {
-	char bident[sizeof("XX:X:X")];
+	char bident[sizeof("XXX:XXX")];
 	struct blockif_ctxt *bctxt;
 	const char *path, *serial;
 	MD5_CTX mdctx;
@@ -465,7 +465,7 @@ pci_vtblk_init(struct vmctx *ctx __unused, struct pci_devinst *pi,
 	/*
 	 * The supplied backing file has to exist
 	 */
-	snprintf(bident, sizeof(bident), "%d:%d", pi->pi_slot, pi->pi_func);
+	snprintf(bident, sizeof(bident), "%u:%u", pi->pi_slot, pi->pi_func);
 	bctxt = blockif_open(nvl, bident);
 	if (bctxt == NULL) {
 		perror("Could not open backing file");