git: 4e2c2842e6f7 - main - link_elf_obj: validate symbol name offsets

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

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

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

    link_elf_obj: validate symbol name offsets
    
    A symbol name offset (st_name) indexes the string table in several
    places without being checked against ddbstrcnt, so a corrupt object
    causes an out-of-bounds read.  Check that the string table is
    NUL-terminated and that every st_name lies within it, in both the
    preload and file paths, and reject the object with ENOEXEC.
    
    Reviewed by:    kib
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D61322
---
 sys/kern/link_elf_obj.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/sys/kern/link_elf_obj.c b/sys/kern/link_elf_obj.c
index 24eaba8f2ace..a4871900c5c4 100644
--- a/sys/kern/link_elf_obj.c
+++ b/sys/kern/link_elf_obj.c
@@ -340,6 +340,25 @@ link_elf_protect(elf_file_t ef)
 	    round_page((vm_offset_t)ef->address + ef->lf.size), gapprot);
 }
 
+static int
+link_elf_obj_check_symtab(elf_file_t ef, const char *filename)
+{
+	const Elf_Sym *symp;
+	long i;
+
+	if (ef->ddbstrcnt == 0 || ef->ddbstrtab[ef->ddbstrcnt - 1] != '\0') {
+		link_elf_error(filename, "corrupt symbol string table");
+		return (ENOEXEC);
+	}
+	for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
+		if (symp->st_name >= ef->ddbstrcnt) {
+			link_elf_error(filename, "corrupt symbol name offset");
+			return (ENOEXEC);
+		}
+	}
+	return (0);
+}
+
 static int
 link_elf_link_preload(linker_class_t cls, const char *filename,
     linker_file_t *result)
@@ -483,6 +502,9 @@ link_elf_link_preload(linker_class_t cls, const char *filename,
 	ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
 	ef->ddbstrcnt = shdr[symstrindex].sh_size;
 	ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
+	error = link_elf_obj_check_symtab(ef, filename);
+	if (error != 0)
+		goto out;
 	ef->shstrcnt = shdr[shstrindex].sh_size;
 	ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
 
@@ -949,6 +971,9 @@ link_elf_load_file(linker_class_t cls, const char *filename,
 		error = EINVAL;
 		goto out;
 	}
+	error = link_elf_obj_check_symtab(ef, filename);
+	if (error != 0)
+		goto out;
 
 	/* Do we have a string table for the section names?  */
 	if (hdr->e_shstrndx >= hdr->e_shnum) {