Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 20 Apr 2026 15:49:37 +0000
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Cc:        Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
Subject:   git: 91f03cde6604 - main - libvmmapi: Check for allocation failure in vm_vcpu_open()
Message-ID:  <69e64b11.32c6e.3addcdea@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by markj:

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

commit 91f03cde6604fdb940f30d81e4860118ee07f4b3
Author:     Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
AuthorDate: 2026-04-20 15:14:16 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-04-20 15:14:49 +0000

    libvmmapi: Check for allocation failure in vm_vcpu_open()
    
    vm_vcpu_open() really should check the value returned from malloc() and
    return NULL on failure. Also, all users of vm_vcpu_open() need to check
    the returned value for NULL, too.
    
    Reviewed by:    corvink, markj
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D56346
---
 lib/libvmmapi/vmmapi.c         |  4 ++++
 usr.sbin/bhyve/bhyverun.c      | 13 +++++++++++++
 usr.sbin/bhyvectl/bhyvectl.c   |  7 +++++++
 usr.sbin/bhyveload/bhyveload.c |  2 ++
 4 files changed, 26 insertions(+)

diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index ede46dce73b3..99d0a1ec7e39 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -2,6 +2,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2011 NetApp, Inc.
+ * Copyright (c) 2026 Hans Rosenfeld
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -218,6 +219,9 @@ vm_vcpu_open(struct vmctx *ctx, int vcpuid)
 	struct vcpu *vcpu;
 
 	vcpu = malloc(sizeof(*vcpu));
+	if (vcpu == NULL)
+		return (vcpu);
+
 	vcpu->ctx = ctx;
 	vcpu->vcpuid = vcpuid;
 	return (vcpu);
diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index 9db62972467c..994f0f1fef21 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -2,6 +2,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2011 NetApp, Inc.
+ * Copyright (c) 2026 Hans Rosenfeld
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -917,6 +918,12 @@ main(int argc, char *argv[])
 	}
 
 	bsp = vm_vcpu_open(ctx, BSP);
+	if (bsp == NULL) {
+		fprintf(stderr, "Unable to open boot VCPU: %s",
+		    strerror(errno));
+		exit(BHYVE_EXIT_ERROR);
+	}
+
 	max_vcpus = num_vcpus_allowed(ctx, bsp);
 	if (guest_ncpus > max_vcpus) {
 		fprintf(stderr, "%d vCPUs requested but only %d available\n",
@@ -935,6 +942,12 @@ main(int argc, char *argv[])
 			vcpu_info[vcpuid].vcpu = bsp;
 		else
 			vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid);
+
+		if (vcpu_info[vcpuid].vcpu == NULL) {
+			fprintf(stderr, "Unable to open VCPU %d: %s", vcpuid,
+			    strerror(errno));
+			exit(BHYVE_EXIT_ERROR);
+		}
 	}
 
 	if (bhyve_init_platform(ctx, bsp) != 0)
diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
index 8c37b670ab2e..96768383d4ca 100644
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -2,6 +2,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2011 NetApp, Inc.
+ * Copyright (c) 2026 Hans Rosenfeld
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -408,6 +409,12 @@ main(int argc, char *argv[])
 		exit(1);
 	}
 	vcpu = vm_vcpu_open(ctx, vcpuid);
+	if (vcpu == NULL) {
+		fprintf(stderr,
+		    "vm_vcpu_open: %s vcpu %d could not be opened: %s\n",
+		    vmname, vcpuid, strerror(errno));
+		exit(1);
+	}
 
 	error = 0;
 	if (!error && memsize)
diff --git a/usr.sbin/bhyveload/bhyveload.c b/usr.sbin/bhyveload/bhyveload.c
index 4cc566f334c3..3b416b7a5ad5 100644
--- a/usr.sbin/bhyveload/bhyveload.c
+++ b/usr.sbin/bhyveload/bhyveload.c
@@ -892,6 +892,8 @@ main(int argc, char** argv)
 	}
 
 	vcpu = vm_vcpu_open(ctx, BSP);
+	if (vcpu == NULL)
+		err(1, "vm_vcpu_open");
 
 	caph_cache_catpages();
 	if (caph_enter() < 0)


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69e64b11.32c6e.3addcdea>