git: cff4d5da065b - releng/15.2 - imgact_elf: Pass program headers to the sv_protect hook

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 00:00:15 UTC
The branch releng/15.2 has been updated by cperciva:

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

commit cff4d5da065bbd72199ae9a98bd4e50f75f4bd44
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2026-11-25 15:17:44 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-26 23:59:19 +0000

    imgact_elf: Pass program headers to the sv_protect hook
    
    We handle program headers beyond the first page of the file, but
    arm64_exec_protect() wasn't updated to handle this.  Pass the program
    header array directly to the sv_protect hook.
    
    Also fix a pre-existing bug: imgp->et_dyn_addr isn't the right base
    address to use for the SV_INTERP case.
    
    Approved by:    re (cperciva)
    Reported by:    alc
    Reviewed by:    alc, kib
    Fixes:          804daf166406 ("imgact_elf: read program headers if not contained in the first page")
    Fixes:          670af174e27b ("arm64: Enable BTI in the kernel ELF loader")
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D61999
    
    (cherry picked from commit 884e1a00151d181e5dd1ba9d83c1adcffd25e5e8)
---
 sys/arm64/arm64/elf_machdep.c | 10 ++++++----
 sys/kern/imgact_elf.c         |  4 ++--
 sys/sys/sysent.h              |  3 ++-
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/sys/arm64/arm64/elf_machdep.c b/sys/arm64/arm64/elf_machdep.c
index 9e8d781f5ea1..998c39c10ed4 100644
--- a/sys/arm64/arm64/elf_machdep.c
+++ b/sys/arm64/arm64/elf_machdep.c
@@ -73,7 +73,8 @@ struct arm64_addr_mask elf64_addr_mask = {
 struct arm64_addr_mask elf64_addr_mask_14;
 #endif
 
-static void arm64_exec_protect(struct image_params *, int);
+static void arm64_exec_protect(struct image_params *, unsigned long,
+    const void *, int);
 
 static struct sysentvec elf64_freebsd_sysvec = {
 	.sv_size	= SYS_MAXSYSCALL,
@@ -365,7 +366,8 @@ gnu_property_cb(const Elf_Note *note, void *arg0, bool *res)
 }
 
 static void
-arm64_exec_protect(struct image_params *imgp, int flags __unused)
+arm64_exec_protect(struct image_params *imgp, unsigned long base_addr,
+    const void *_phdr, int flags __unused)
 {
 	const Elf_Ehdr *hdr;
 	const Elf_Phdr *phdr;
@@ -378,7 +380,7 @@ arm64_exec_protect(struct image_params *imgp, int flags __unused)
 		return;
 
 	hdr = (const Elf_Ehdr *)imgp->image_header;
-	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
+	phdr = (const Elf_Phdr *)_phdr;
 
 	found = false;
 	for (i = 0; i < hdr->e_phnum; i++) {
@@ -396,7 +398,7 @@ arm64_exec_protect(struct image_params *imgp, int flags __unused)
 		if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
 			continue;
 
-		sva = phdr[i].p_vaddr + imgp->et_dyn_addr;
+		sva = phdr[i].p_vaddr + base_addr;
 		eva = sva + phdr[i].p_memsz;
 		pmap_bti_set(vmspace_pmap(imgp->proc->p_vmspace), sva, eva);
 	}
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index c91d985d5d77..0d681e8ff14b 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -883,7 +883,7 @@ __elfN(load_file)(struct thread *td, const char *file, u_long *addr,
 		goto fail;
 
 	if (imgp->proc->p_sysent->sv_protect != NULL)
-		imgp->proc->p_sysent->sv_protect(imgp, SVP_INTERP);
+		imgp->proc->p_sysent->sv_protect(imgp, rbase, phdr, SVP_INTERP);
 
 	*addr = base_addr;
 	*entry = (unsigned long)hdr->e_entry + rbase;
@@ -1413,7 +1413,7 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
 	imgp->entry_addr = entry;
 
 	if (sv->sv_protect != NULL)
-		sv->sv_protect(imgp, SVP_IMAGE);
+		sv->sv_protect(imgp, imgp->et_dyn_addr, phdr, SVP_IMAGE);
 
 	if (interp != NULL) {
 		VOP_UNLOCK(imgp->vp);
diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h
index 05c1074fde70..855e3f477e83 100644
--- a/sys/sys/sysent.h
+++ b/sys/sys/sysent.h
@@ -152,7 +152,8 @@ struct sysentvec {
 	vm_offset_t	sv_fxrng_gen_offset;
 	void		(*sv_onexec_old)(struct thread *td);
 	int		(*sv_onexec)(struct proc *, struct image_params *);
-	void		(*sv_protect)(struct image_params *, int);
+	void		(*sv_protect)(struct image_params *, unsigned long,
+			    const void *, int);
 	void		(*sv_onexit)(struct proc *);
 	void		(*sv_ontdexit)(struct thread *td);
 	int		(*sv_setid_allowed)(struct thread *td,