git: 26d9f973d869 - main - bhyve: error out if fwcfg user file isn't read completely

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Wed, 17 May 2023 13:40:52 UTC
The branch main has been updated by corvink:

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

commit 26d9f973d8691eccc098ded7326137d6f76ad243
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2023-05-12 05:37:32 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-05-17 13:39:37 +0000

    bhyve: error out if fwcfg user file isn't read completely
    
    At the moment, fwcfg reads the file once at startup and passes these
    data to the guest. Therefore, we should always read the whole file.
    Otherwise we should error out.
    
    Additionally, GCC12 complains that the comparison whether
    fwcfg_file->size is lower than 0 is always false due to the limited
    range of data type.
    
    Reviewed by:            markj
    Fixes:                  ca14781c8170f3517ae79e198c0c880dbc3142dd ("bhyve: add cmdline option for user defined fw_cfg items")
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D40076
---
 usr.sbin/bhyve/qemu_fwcfg.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/usr.sbin/bhyve/qemu_fwcfg.c b/usr.sbin/bhyve/qemu_fwcfg.c
index 45c4a885ae96..e845c70950b1 100644
--- a/usr.sbin/bhyve/qemu_fwcfg.c
+++ b/usr.sbin/bhyve/qemu_fwcfg.c
@@ -524,6 +524,7 @@ qemu_fwcfg_parse_cmdline_arg(const char *opt)
 	struct qemu_fwcfg_user_file *fwcfg_file;
 	struct stat sb;
 	const char *opt_ptr, *opt_end;
+	ssize_t bytes_read;
 	int fd;
 	
 	fwcfg_file = malloc(sizeof(*fwcfg_file));
@@ -593,16 +594,14 @@ qemu_fwcfg_parse_cmdline_arg(const char *opt)
 			close(fd);
 			return (ENOMEM);
 		}
-		fwcfg_file->size = read(fd, fwcfg_file->data, sb.st_size);
-		if ((ssize_t)fwcfg_file->size < 0) {
+		bytes_read = read(fd, fwcfg_file->data, sb.st_size);
+		if (bytes_read < 0 || bytes_read != sb.st_size) {
 			warn("Unable to read file \"%s\"", opt_ptr);
 			free(fwcfg_file->data);
 			close(fd);
 			return (-1);
-		} else if (fwcfg_file->size < sb.st_size) {
-			warnx("Only read %u bytes of file \"%s\"",
-			    fwcfg_file->size, opt_ptr);
 		}
+		fwcfg_file->size = bytes_read;
 
 		close(fd);
 	} else {