git: e4b9fec671b6 - main - link_elf: validate the section headers in link_elf_load_file()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 11 Nov 2026 16:49:57 UTC
The branch main has been updated by markj:
URL: https://cgit.FreeBSD.org/src/commit/?id=e4b9fec671b6f45ab18c6292be7586a7d5d9b85f
commit e4b9fec671b6f45ab18c6292be7586a7d5d9b85f
Author: Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-11 15:04:28 +0000
Commit: Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-11 16:49:42 +0000
link_elf: validate the section headers in link_elf_load_file()
e_shentsize isn't checked, and e_shstrndx, sh_link and sh_name are used
unchecked. Do what link_elf_obj.c does.
MFC after: 1 week
Reviewed by: kib
Differential Revision: https://reviews.freebsd.org/D61389
---
sys/kern/link_elf.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/sys/kern/link_elf.c b/sys/kern/link_elf.c
index c49ecea0bace..785233ec4213 100644
--- a/sys/kern/link_elf.c
+++ b/sys/kern/link_elf.c
@@ -1098,6 +1098,7 @@ link_elf_load_file(linker_class_t cls, const char* filename,
int symtabindex;
int symstrindex;
int shstrindex;
+ int shstrsz;
int symcnt;
int strcnt;
char *shstrs;
@@ -1105,6 +1106,7 @@ link_elf_load_file(linker_class_t cls, const char* filename,
shdr = NULL;
lf = NULL;
shstrs = NULL;
+ shstrsz = 0;
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
flags = FREAD;
@@ -1376,6 +1378,11 @@ link_elf_load_file(linker_class_t cls, const char* filename,
nbytes = hdr->e_shnum * hdr->e_shentsize;
if (nbytes == 0 || hdr->e_shoff == 0)
goto nosyms;
+ if (hdr->e_shentsize != sizeof(Elf_Shdr)) {
+ link_elf_error(filename, "Unsupported section header size");
+ error = ENOEXEC;
+ goto out;
+ }
shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
error = vn_rdwr(UIO_READ, nd.ni_vp,
(caddr_t)shdr, nbytes, hdr->e_shoff,
@@ -1386,6 +1393,12 @@ link_elf_load_file(linker_class_t cls, const char* filename,
/* Read section string table */
shstrindex = hdr->e_shstrndx;
+ if (shstrindex >= hdr->e_shnum) {
+ link_elf_error(filename,
+ "file has an invalid section name string table");
+ error = ENOEXEC;
+ goto out;
+ }
if (shstrindex != 0 && shdr[shstrindex].sh_type == SHT_STRTAB &&
shdr[shstrindex].sh_size != 0) {
nbytes = shdr[shstrindex].sh_size;
@@ -1395,6 +1408,21 @@ link_elf_load_file(linker_class_t cls, const char* filename,
td->td_ucred, NOCRED, &resid, td);
if (error)
goto out;
+ shstrsz = nbytes;
+ if (shstrs[shstrsz - 1] != '\0') {
+ link_elf_error(filename,
+ "corrupt section name string table");
+ error = ENOEXEC;
+ goto out;
+ }
+ for (i = 0; i < hdr->e_shnum; i++) {
+ if (shdr[i].sh_name >= shstrsz) {
+ link_elf_error(filename,
+ "corrupt section name offset");
+ error = ENOEXEC;
+ goto out;
+ }
+ }
}
symtabindex = -1;
@@ -1413,6 +1441,12 @@ link_elf_load_file(linker_class_t cls, const char* filename,
}
if (symtabindex < 0 || symstrindex < 0)
goto nosyms;
+ if (symstrindex >= hdr->e_shnum ||
+ shdr[symstrindex].sh_type != SHT_STRTAB) {
+ link_elf_error(filename, "file has invalid symbol strings");
+ error = ENOEXEC;
+ goto out;
+ }
symcnt = shdr[symtabindex].sh_size;
ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);