git: d99bbd1f1601 - stable/13 - Add a link to the Elf_Brandinfo into the struc proc.

From: Dmitry Chagin <dchagin_at_FreeBSD.org>
Date: Fri, 17 Jun 2022 19:32:13 UTC
The branch stable/13 has been updated by dchagin:

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

commit d99bbd1f1601bcc604058d9381793a0113450744
Author:     Dmitry Chagin <dchagin@FreeBSD.org>
AuthorDate: 2022-06-17 19:30:26 +0000
Commit:     Dmitry Chagin <dchagin@FreeBSD.org>
CommitDate: 2022-06-17 19:30:26 +0000

    Add a link to the Elf_Brandinfo into the struc proc.
    
    To allow the ABI to make a dicision based on the Brandinfo add a link
    to the Elf_Brandinfo into the struct proc. Add a note that the high 8 bits
    of Elf_Brandinfo flags is private to the ABI.
    
    Note to MFC: it breaks KBI.
    
    Reviewed by:            kib, markj
    Differential Revision:  https://reviews.freebsd.org/D30918
    MFC after:              2 weeks
    
    (cherry picked from commit 615f22b2fb6d999005473abcb771c6bf25118dfe)
---
 sys/kern/imgact_elf.c | 1 +
 sys/kern/kern_exec.c  | 4 ++++
 sys/kern/kern_fork.c  | 1 +
 sys/sys/imgact_elf.h  | 1 +
 sys/sys/proc.h        | 2 ++
 5 files changed, 9 insertions(+)

diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index f81f565cacc9..260454e4b9f6 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -1301,6 +1301,7 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
 	error = exec_new_vmspace(imgp, sv);
 
 	imgp->proc->p_sysent = sv;
+	imgp->proc->p_elf_brandinfo = brand_info;
 
 	vmspace = imgp->proc->p_vmspace;
 	map = &vmspace->vm_map;
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index b001c413d6e7..9dbafe52119a 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -409,6 +409,7 @@ do_execve(struct thread *td, struct image_args *args, struct mac *mac_p,
 #endif
 	int error, i, orig_osrel;
 	uint32_t orig_fctl0;
+	Elf_Brandinfo *orig_brandinfo;
 	size_t freepath_size;
 	static const char fexecv_proc_title[] = "(fexecv)";
 
@@ -443,6 +444,7 @@ do_execve(struct thread *td, struct image_args *args, struct mac *mac_p,
 	oldcred = p->p_ucred;
 	orig_osrel = p->p_osrel;
 	orig_fctl0 = p->p_fctl0;
+	orig_brandinfo = p->p_elf_brandinfo;
 
 #ifdef MAC
 	error = mac_execve_enter(imgp, mac_p);
@@ -544,6 +546,7 @@ interpret:
 
 	imgp->proc->p_osrel = 0;
 	imgp->proc->p_fctl0 = 0;
+	imgp->proc->p_elf_brandinfo = NULL;
 
 	/*
 	 * Implement image setuid/setgid.
@@ -940,6 +943,7 @@ exec_fail_dealloc:
 	if (error != 0) {
 		p->p_osrel = orig_osrel;
 		p->p_fctl0 = orig_fctl0;
+		p->p_elf_brandinfo = orig_brandinfo;
 	}
 
 	if (imgp->firstpage != NULL)
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 16af36635db2..46f14e75212a 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -531,6 +531,7 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread *
 	p2->p_fd = fd;
 	p2->p_fdtol = fdtol;
 	p2->p_pd = pd;
+	p2->p_elf_brandinfo = p1->p_elf_brandinfo;
 
 	if (p1->p_flag2 & P2_INHERIT_PROTECTED) {
 		p2->p_flag |= P_PROTECTED;
diff --git a/sys/sys/imgact_elf.h b/sys/sys/imgact_elf.h
index e721a9623af2..94a912e99167 100644
--- a/sys/sys/imgact_elf.h
+++ b/sys/sys/imgact_elf.h
@@ -91,6 +91,7 @@ typedef struct {
 	Elf_Brandnote *brand_note;
 	boolean_t	(*header_supported)(struct image_params *,
 	    int32_t *, uint32_t *);
+		/* High 8 bits of flags is private to the ABI */
 #define	BI_CAN_EXEC_DYN		0x0001
 #define	BI_BRAND_NOTE		0x0002	/* May have note.ABI-tag section. */
 #define	BI_BRAND_NOTE_MANDATORY	0x0004	/* Must have note.ABI-tag section. */
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 96bd2cc8a6ce..38675248b22d 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -725,6 +725,8 @@ struct proc {
 	TAILQ_HEAD(, kq_timer_cb_data)	p_kqtim_stop;	/* (c) */
 	struct vnode	*p_textdvp;	/* (b) Dir containing textvp. */
 	char		*p_binname;	/* (b) Binary hardlink name. */
+	void		*p_elf_brandinfo; /* (x) Elf_Brandinfo, NULL for
+						 non ELF binaries. */
 };
 
 #define	p_session	p_pgrp->pg_session