git: ad8f475be2c5 - main - link_elf: bounds-check the DT_HASH offset

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

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

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

    link_elf: bounds-check the DT_HASH offset
    
    parse_dynamic() builds the symbol hash table at ef->address + d_ptr
    without checking that d_ptr lies within the mapped image, so a crafted
    DT_HASH entry causes an out-of-bounds read of the hash header.  Reject
    the object with ENOEXEC when the header does not fit in the image.
    
    Reviewed by:    kib
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D61323
---
 sys/kern/link_elf.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/sys/kern/link_elf.c b/sys/kern/link_elf.c
index d7aeefa25433..c04bd63f58ff 100644
--- a/sys/kern/link_elf.c
+++ b/sys/kern/link_elf.c
@@ -596,7 +596,14 @@ parse_dynamic(elf_file_t ef)
 		case DT_HASH:
 		{
 			/* From src/libexec/rtld-elf/rtld.c */
-			const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
+			const Elf_Hashelt *hashtab;
+
+			if (ef->lf.size != 0 &&
+			    (dp->d_un.d_ptr >= ef->lf.size ||
+			    ef->lf.size - dp->d_un.d_ptr <
+			    2 * sizeof(Elf_Hashelt)))
+				return (ENOEXEC);
+			hashtab = (const Elf_Hashelt *)
 			    (ef->address + dp->d_un.d_ptr);
 			ef->nbuckets = hashtab[0];
 			ef->nchains = hashtab[1];