Module Name: src
Committed By: rin
Date: Fri Aug 4 07:21:57 UTC 2023
Modified Files:
src/sys/arch/i386/stand/lib: exec_multiboot2.c
Log Message:
x86/multiboot2: Fix short read for 64-bit ELF headers
XXX document this
At the moment, this cannot affect NetBSD/amd64, as we have not
supported multiboot for kernel side.
Found by GCC12.
To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/i386/stand/lib/exec_multiboot2.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/arch/i386/stand/lib/exec_multiboot2.c
diff -u src/sys/arch/i386/stand/lib/exec_multiboot2.c:1.5 src/sys/arch/i386/stand/lib/exec_multiboot2.c:1.6
--- src/sys/arch/i386/stand/lib/exec_multiboot2.c:1.5 Wed Jul 21 23:16:08 2021
+++ src/sys/arch/i386/stand/lib/exec_multiboot2.c Fri Aug 4 07:21:57 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: exec_multiboot2.c,v 1.5 2021/07/21 23:16:08 jmcneill Exp $ */
+/* $NetBSD: exec_multiboot2.c,v 1.6 2023/08/04 07:21:57 rin Exp $ */
/*
* Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -976,7 +976,10 @@ mbi_elf_sections(struct multiboot_packag
{
size_t len = 0;
struct multiboot_tag_elf_sections *mbt = buf;
- Elf_Ehdr ehdr;
+ union {
+ Elf32_Ehdr e32;
+ Elf64_Ehdr e64;
+ } ehdr;
int class;
Elf32_Ehdr *ehdr32 = NULL;
Elf64_Ehdr *ehdr64 = NULL;
@@ -991,21 +994,21 @@ mbi_elf_sections(struct multiboot_packag
/*
* Check this is a ELF header
*/
- if (memcmp(&ehdr.e_ident, ELFMAG, SELFMAG) != 0)
+ if (memcmp(&ehdr.e32.e_ident, ELFMAG, SELFMAG) != 0)
goto out;
- class = ehdr.e_ident[EI_CLASS];
+ class = ehdr.e32.e_ident[EI_CLASS];
switch (class) {
case ELFCLASS32:
- ehdr32 = (Elf32_Ehdr *)&ehdr;
+ ehdr32 = &ehdr.e32;
shnum = ehdr32->e_shnum;
shentsize = ehdr32->e_shentsize;
shstrndx = ehdr32->e_shstrndx;
shoff = ehdr32->e_shoff;
break;
case ELFCLASS64:
- ehdr64 = (Elf64_Ehdr *)&ehdr;
+ ehdr64 = &ehdr.e64;
shnum = ehdr64->e_shnum;
shentsize = ehdr64->e_shentsize;
shstrndx = ehdr64->e_shstrndx;