Add a sv_copyout_auxargs() hook in sysentvec.

Change the FreeBSD ELF ABIs to use this new hook to copyout ELF auxv
instead of doing it in the sv_fixup hook.  In particular, this new
hook allows the stack space to be allocated at the same time the auxv
values are copied out to userland.  This allows us to avoid wasting
space for unused auxv entries as well as not having to recalculate
where the auxv vector is by walking back up over the argv and
environment vectors.

Reviewed by:	brooks, emaste
Tested on:	amd64 (amd64 and i386 binaries), i386, mips, mips64
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D22355
This commit is contained in:
John Baldwin 2019-11-15 18:42:13 +00:00
parent 310399ac72
commit e353233118
17 changed files with 34 additions and 26 deletions

View File

@ -68,6 +68,7 @@ struct sysentvec elf64_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -75,6 +75,7 @@ struct sysentvec elf32_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -92,6 +92,7 @@ static struct sysentvec elf32_freebsd_sysvec = {
.sv_usrstack = FREEBSD32_USRSTACK,
.sv_psstrings = FREEBSD32_PS_STRINGS,
.sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
.sv_copyout_auxargs = elf32_freebsd_copyout_auxargs,
.sv_copyout_strings = freebsd32_copyout_strings,
.sv_setregs = freebsd32_setregs,
.sv_fixlimit = NULL, // XXX

View File

@ -76,6 +76,7 @@ static struct sysentvec elf64_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -3195,14 +3195,8 @@ freebsd32_copyout_strings(struct image_params *imgp)
if (imgp->sysent->sv_stackgap != NULL)
imgp->sysent->sv_stackgap(imgp, (u_long *)&vectp);
if (imgp->auxargs) {
/*
* Allocate room on the stack for the ELF auxargs
* array. It has up to AT_COUNT entries.
*/
vectp -= howmany(AT_COUNT * sizeof(Elf32_Auxinfo),
sizeof(*vectp));
}
if (imgp->auxargs)
imgp->sysent->sv_copyout_auxargs(imgp, (u_long *)&vectp);
/*
* Allocate room for the argv[] and env vectors including the

View File

@ -114,6 +114,7 @@ struct sysentvec ia32_freebsd_sysvec = {
.sv_usrstack = FREEBSD32_USRSTACK,
.sv_psstrings = FREEBSD32_PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = elf32_freebsd_copyout_auxargs,
.sv_copyout_strings = freebsd32_copyout_strings,
.sv_setregs = ia32_setregs,
.sv_fixlimit = ia32_fixlimit,

View File

@ -70,6 +70,7 @@ struct sysentvec elf32_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -1289,7 +1289,7 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
addr = et_dyn_addr;
/*
* Construct auxargs table (used by the fixup routine)
* Construct auxargs table (used by the copyout_auxargs routine)
*/
elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_NOWAIT);
if (elf_auxargs == NULL) {
@ -1323,16 +1323,13 @@ ret:
#define suword __CONCAT(suword, __ELF_WORD_SIZE)
int
__elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
void
__elfN(freebsd_copyout_auxargs)(struct image_params *imgp, u_long *base)
{
Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
Elf_Auxinfo *argarray, *pos;
Elf_Addr *base, *auxbase;
int error;
u_long auxlen;
base = (Elf_Addr *)*stack_base;
auxbase = base + imgp->args->argc + 1 + imgp->args->envc + 1;
argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP,
M_WAITOK | M_ZERO);
@ -1376,11 +1373,18 @@ __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
imgp->auxargs = NULL;
KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs"));
error = copyout(argarray, auxbase, sizeof(*argarray) * AT_COUNT);
auxlen = sizeof(*argarray) * (pos - argarray);
*base -= auxlen;
copyout(argarray, (void *)*base, auxlen);
free(argarray, M_TEMP);
if (error != 0)
return (error);
}
int
__elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
{
Elf_Addr *base;
base = (Elf_Addr *)*stack_base;
base--;
if (suword(base, imgp->args->argc) == -1)
return (EFAULT);

View File

@ -1637,14 +1637,8 @@ exec_copyout_strings(struct image_params *imgp)
if (imgp->sysent->sv_stackgap != NULL)
imgp->sysent->sv_stackgap(imgp, (u_long *)&vectp);
if (imgp->auxargs) {
/*
* Allocate room on the stack for the ELF auxargs
* array. It has up to AT_COUNT entries.
*/
vectp -= howmany(AT_COUNT * sizeof(Elf_Auxinfo),
sizeof(*vectp));
}
if (imgp->auxargs)
imgp->sysent->sv_copyout_auxargs(imgp, (u_long *)&vectp);
/*
* Allocate room for the argv[] and env vectors including the

View File

@ -71,6 +71,7 @@ struct sysentvec elf64_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,
@ -125,6 +126,7 @@ struct sysentvec elf32_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -94,6 +94,7 @@ struct sysentvec elf32_freebsd_sysvec = {
.sv_usrstack = FREEBSD32_USRSTACK,
.sv_psstrings = FREEBSD32_PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = freebsd32_copyout_strings,
.sv_setregs = freebsd32_exec_setregs,
.sv_fixlimit = NULL,

View File

@ -87,6 +87,7 @@ struct sysentvec elf32_freebsd_sysvec = {
.sv_errtbl = NULL,
.sv_transtrap = NULL,
.sv_fixup = __elfN(freebsd_fixup),
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_sendsig = sendsig,
.sv_sigcode = sigcode32,
.sv_szsigcode = &szsigcode32,

View File

@ -74,6 +74,7 @@ struct sysentvec elf64_freebsd_sysvec_v1 = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs_funcdesc,
.sv_fixlimit = NULL,
@ -111,6 +112,7 @@ struct sysentvec elf64_freebsd_sysvec_v2 = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_ALL,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -79,6 +79,7 @@ struct sysentvec elf64_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -75,6 +75,7 @@ static struct sysentvec elf64_freebsd_sysvec = {
.sv_usrstack = USRSTACK,
.sv_psstrings = PS_STRINGS,
.sv_stackprot = VM_PROT_READ | VM_PROT_WRITE,
.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
.sv_copyout_strings = exec_copyout_strings,
.sv_setregs = exec_setregs,
.sv_fixlimit = NULL,

View File

@ -99,6 +99,7 @@ int __elfN(freebsd_fixup)(register_t **, struct image_params *);
int __elfN(coredump)(struct thread *, struct vnode *, off_t, int);
size_t __elfN(populate_note)(int, void *, void *, size_t, void **);
void __elfN(stackgap)(struct image_params *, u_long *);
void __elfN(freebsd_copyout_auxargs)(struct image_params *, u_long *);
/* Machine specific function to dump per-thread information. */
void __elfN(dump_thread)(struct thread *, void *, size_t *);

View File

@ -110,6 +110,7 @@ struct sysentvec {
/* function to dump core, or NULL */
int (*sv_imgact_try)(struct image_params *);
void (*sv_stackgap)(struct image_params *, u_long *);
void (*sv_copyout_auxargs)(struct image_params *, u_long *);
int sv_minsigstksz; /* minimum signal stack size */
vm_offset_t sv_minuser; /* VM_MIN_ADDRESS */
vm_offset_t sv_maxuser; /* VM_MAXUSER_ADDRESS */