git: f0683276b433 - main - link_elf_obj: bounds-check e_shstrndx in link_elf_load_file()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 23:43:32 UTC
The branch main has been updated by markj:

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

commit f0683276b4334f4b7e7faf73471a2afe937a9f5d
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-09 23:18:01 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-09 23:43:17 +0000

    link_elf_obj: bounds-check e_shstrndx in link_elf_load_file()
    
    e_shstrndx is used to index the section header array after only being
    checked against zero, so a malformed object can make the linker read up
    to about 64K entries past the end of shdr[].  Reject such a file with
    ENOEXEC, as link_elf_link_preload() already does.
    
    Reported by:    llm-postcr
    Reviewed by:    kib
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D61321
---
 sys/kern/link_elf_obj.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/sys/kern/link_elf_obj.c b/sys/kern/link_elf_obj.c
index 63078e225dcf..24eaba8f2ace 100644
--- a/sys/kern/link_elf_obj.c
+++ b/sys/kern/link_elf_obj.c
@@ -951,6 +951,12 @@ link_elf_load_file(linker_class_t cls, const char *filename,
 	}
 
 	/* Do we have a string table for the section names?  */
+	if (hdr->e_shstrndx >= hdr->e_shnum) {
+		link_elf_error(filename,
+		    "file has an invalid section name string table");
+		error = ENOEXEC;
+		goto out;
+	}
 	shstrindex = -1;
 	if (hdr->e_shstrndx != 0 &&
 	    shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {