git: 096e54258d15 - main - loader: i386: fix vectx handling in chain error paths

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 04:02:31 UTC
The branch main has been updated by kevans:

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

commit 096e54258d157bfa3a534fc46a23f41f00e17b82
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-26 04:00:54 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-26 04:01:03 +0000

    loader: i386: fix vectx handling in chain error paths
    
    Once vectx_open() succeeds, the stat, device, root device and read error
    paths all returned without freeing the context, leaking it.
    
    We also closed the fd before calling vectx_close(), which may need to
    read the remainder of the file to finish hashing it.  This is harmless
    as-is because we've always read the whole file by that point, but close
    it afterwards so that we aren't relying on that.
    
    Reviewed by:    sjg
    Sponsored by:   The FreeBSD Foundation
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D61840
---
 stand/i386/loader/chain.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/stand/i386/loader/chain.c b/stand/i386/loader/chain.c
index 13dd4377ea1d..02152533f936 100644
--- a/stand/i386/loader/chain.c
+++ b/stand/i386/loader/chain.c
@@ -103,12 +103,18 @@ command_chain(int argc, char *argv[])
 	if (argv[1][len-1] != ':') {
 		if (fstat(fd, &st) == -1) {
 			command_errmsg = "stat failed";
+#ifdef LOADER_VERIEXEC_VECTX
+			free(vctx);
+#endif
 			close(fd);
 			return (CMD_ERROR);
 		}
 		size = st.st_size;
 	} else if (strncmp(argv[1], "disk", 4) != 0) {
 		command_errmsg = "can only use disk device";
+#ifdef LOADER_VERIEXEC_VECTX
+		free(vctx);
+#endif
 		close(fd);
 		return (CMD_ERROR);
 	}
@@ -116,22 +122,29 @@ command_chain(int argc, char *argv[])
 	i386_getdev((void **)(&rootdev), argv[1], NULL);
 	if (rootdev == NULL) {
 		command_errmsg = "can't determine root device";
+#ifdef LOADER_VERIEXEC_VECTX
+		free(vctx);
+#endif
 		close(fd);
 		return (CMD_ERROR);
 	}
 
 	if (archsw.arch_readin(VECTX_HANDLE(fd), mem, size) != size) {
 		command_errmsg = "failed to read disk";
+#ifdef LOADER_VERIEXEC_VECTX
+		free(vctx);
+#endif
 		close(fd);
 		return (CMD_ERROR);
 	}
-	close(fd);
 #ifdef LOADER_VERIEXEC_VECTX
 	verror = vectx_close(vctx, __func__);
 	if (verror) {
+		close(fd);
 		return (CMD_ERROR);
 	}
 #endif
+	close(fd);
 	if (argv[1][len-1] == ':' &&
 	    *((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) {
 		command_errmsg = "wrong magic";