git: 097f6d7a5733 - main - link_elf_obj: fix an off-by-one in the symbol string table index check

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Sun, 08 Nov 2026 16:45:20 UTC
The branch main has been updated by markj:

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

commit 097f6d7a573369503ef5b8503fb2b721a6f66d85
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-08 16:45:01 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-08 16:45:04 +0000

    link_elf_obj: fix an off-by-one in the symbol string table index check
    
    link_elf_load_file() checks the symbol table's sh_link against
    e_shnum with ">" instead of ">=", so a malformed object whose sh_link
    equals e_shnum makes the linker read one entry past the section header
    array before the sh_type check rejects it.  Use ">=", as
    link_elf_link_preload() already does.
    
    Reviewed by:    kib
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D61055
---
 sys/kern/link_elf_obj.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sys/kern/link_elf_obj.c b/sys/kern/link_elf_obj.c
index e80b2798c37f..63078e225dcf 100644
--- a/sys/kern/link_elf_obj.c
+++ b/sys/kern/link_elf_obj.c
@@ -899,7 +899,7 @@ link_elf_load_file(linker_class_t cls, const char *filename,
 		error = ENOEXEC;
 		goto out;
 	}
-	if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
+	if (symstrindex < 0 || symstrindex >= hdr->e_shnum ||
 	    shdr[symstrindex].sh_type != SHT_STRTAB) {
 		link_elf_error(filename, "file has invalid symbol strings");
 		error = ENOEXEC;