From 26d9f973d8691eccc098ded7326137d6f76ad243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corvin=20K=C3=B6hne?= Date: Fri, 12 May 2023 07:37:32 +0200 Subject: [PATCH] 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 45c4a885ae9..e845c70950b 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 {