git: 8644c3bfe240 - main - link_elf_obj: bounds-check a relocation section's sh_info

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

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

commit 8644c3bfe2406d00235c4d242022fc7bd61e2276
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-08 15:43:44 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-08 16:18:13 +0000

    link_elf_obj: bounds-check a relocation section's sh_info
    
    sh_info is used to index the section header array without checking it
    against e_shnum, so a malformed object can make the linker read out of
    bounds.  Reject such a file with ENOEXEC in both the preload and kldload
    paths.
    
    MFC after:      1 week
    Reviewed by:    kib, markj
    Differential Revision:  https://reviews.freebsd.org/D60937
---
 sys/kern/link_elf_obj.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/sys/kern/link_elf_obj.c b/sys/kern/link_elf_obj.c
index 511a44006f03..5be78aa147bd 100644
--- a/sys/kern/link_elf_obj.c
+++ b/sys/kern/link_elf_obj.c
@@ -397,6 +397,14 @@ link_elf_link_preload(linker_class_t cls, const char *filename,
 	symtabindex = -1;
 	symstrindex = -1;
 	for (i = 0; i < hdr->e_shnum; i++) {
+		if ((shdr[i].sh_type == SHT_REL ||
+		    shdr[i].sh_type == SHT_RELA) &&
+		    shdr[i].sh_info >= hdr->e_shnum) {
+			link_elf_error(filename,
+			    "file has an invalid relocation section");
+			error = ENOEXEC;
+			goto out;
+		}
 		switch (shdr[i].sh_type) {
 		case SHT_PROGBITS:
 		case SHT_NOBITS:
@@ -836,6 +844,14 @@ link_elf_load_file(linker_class_t cls, const char *filename,
 	for (i = 0; i < hdr->e_shnum; i++) {
 		if (shdr[i].sh_size == 0)
 			continue;
+		if ((shdr[i].sh_type == SHT_REL ||
+		    shdr[i].sh_type == SHT_RELA) &&
+		    shdr[i].sh_info >= hdr->e_shnum) {
+			link_elf_error(filename,
+			    "file has an invalid relocation section");
+			error = ENOEXEC;
+			goto out;
+		}
 		switch (shdr[i].sh_type) {
 		case SHT_PROGBITS:
 		case SHT_NOBITS: