Make the array pointed to by AT_PAGESIZES auxv properly aligned.

Also, remove the expression which calculated the location of the
strings for a new image and grown over the time to be
non-comprehensible.  Instead, calculate the offsets by steps, which
also makes fixing the alignments much cleaner.

Reported and reviewed by:	alc
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
This commit is contained in:
kib 2014-03-19 12:35:04 +00:00
parent b3534c26d0
commit b236080eb1
2 changed files with 51 additions and 43 deletions

View File

@ -2822,7 +2822,8 @@ freebsd32_copyout_strings(struct image_params *imgp)
{ {
int argc, envc, i; int argc, envc, i;
u_int32_t *vectp; u_int32_t *vectp;
char *stringp, *destp; char *stringp;
uintptr_t destp;
u_int32_t *stack_base; u_int32_t *stack_base;
struct freebsd32_ps_strings *arginfo; struct freebsd32_ps_strings *arginfo;
char canary[sizeof(long) * 8]; char canary[sizeof(long) * 8];
@ -2844,35 +2845,34 @@ freebsd32_copyout_strings(struct image_params *imgp)
szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
else else
szsigcode = 0; szsigcode = 0;
destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - destp = (uintptr_t)arginfo;
roundup(execpath_len, sizeof(char *)) -
roundup(sizeof(canary), sizeof(char *)) -
roundup(sizeof(pagesizes32), sizeof(char *)) -
roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
/* /*
* install sigcode * install sigcode
*/ */
if (szsigcode != 0) if (szsigcode != 0) {
copyout(imgp->proc->p_sysent->sv_sigcode, destp -= szsigcode;
((caddr_t)arginfo - szsigcode), szsigcode); destp = rounddown2(destp, sizeof(uint32_t));
copyout(imgp->proc->p_sysent->sv_sigcode, (void *)destp,
szsigcode);
}
/* /*
* Copy the image path for the rtld. * Copy the image path for the rtld.
*/ */
if (execpath_len != 0) { if (execpath_len != 0) {
imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len; destp -= execpath_len;
copyout(imgp->execpath, (void *)imgp->execpathp, imgp->execpathp = destp;
execpath_len); copyout(imgp->execpath, (void *)destp, execpath_len);
} }
/* /*
* Prepare the canary for SSP. * Prepare the canary for SSP.
*/ */
arc4rand(canary, sizeof(canary), 0); arc4rand(canary, sizeof(canary), 0);
imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len - destp -= sizeof(canary);
sizeof(canary); imgp->canary = destp;
copyout(canary, (void *)imgp->canary, sizeof(canary)); copyout(canary, (void *)destp, sizeof(canary));
imgp->canarylen = sizeof(canary); imgp->canarylen = sizeof(canary);
/* /*
@ -2880,11 +2880,15 @@ freebsd32_copyout_strings(struct image_params *imgp)
*/ */
for (i = 0; i < MAXPAGESIZES; i++) for (i = 0; i < MAXPAGESIZES; i++)
pagesizes32[i] = (uint32_t)pagesizes[i]; pagesizes32[i] = (uint32_t)pagesizes[i];
imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len - destp -= sizeof(pagesizes32);
roundup(sizeof(canary), sizeof(char *)) - sizeof(pagesizes32); destp = rounddown2(destp, sizeof(uint32_t));
copyout(pagesizes32, (void *)imgp->pagesizes, sizeof(pagesizes32)); imgp->pagesizes = destp;
copyout(pagesizes32, (void *)destp, sizeof(pagesizes32));
imgp->pagesizeslen = sizeof(pagesizes32); imgp->pagesizeslen = sizeof(pagesizes32);
destp -= ARG_MAX - imgp->args->stringspace;
destp = rounddown2(destp, sizeof(uint32_t));
/* /*
* If we have a valid auxargs ptr, prepare some room * If we have a valid auxargs ptr, prepare some room
* on the stack. * on the stack.
@ -2904,13 +2908,14 @@ freebsd32_copyout_strings(struct image_params *imgp)
vectp = (u_int32_t *) (destp - (imgp->args->argc + vectp = (u_int32_t *) (destp - (imgp->args->argc +
imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) * imgp->args->envc + 2 + imgp->auxarg_size + execpath_len) *
sizeof(u_int32_t)); sizeof(u_int32_t));
} else } else {
/* /*
* The '+ 2' is for the null pointers at the end of each of * The '+ 2' is for the null pointers at the end of each of
* the arg and env vector sets * the arg and env vector sets
*/ */
vectp = (u_int32_t *) vectp = (u_int32_t *)(destp - (imgp->args->argc +
(destp - (imgp->args->argc + imgp->args->envc + 2) * sizeof(u_int32_t)); imgp->args->envc + 2) * sizeof(u_int32_t));
}
/* /*
* vectp also becomes our initial stack base * vectp also becomes our initial stack base
@ -2923,7 +2928,7 @@ freebsd32_copyout_strings(struct image_params *imgp)
/* /*
* Copy out strings - arguments and environment. * Copy out strings - arguments and environment.
*/ */
copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
/* /*
* Fill in "ps_strings" struct for ps, w, etc. * Fill in "ps_strings" struct for ps, w, etc.

View File

@ -1231,7 +1231,8 @@ exec_copyout_strings(imgp)
{ {
int argc, envc; int argc, envc;
char **vectp; char **vectp;
char *stringp, *destp; char *stringp;
uintptr_t destp;
register_t *stack_base; register_t *stack_base;
struct ps_strings *arginfo; struct ps_strings *arginfo;
struct proc *p; struct proc *p;
@ -1255,45 +1256,47 @@ exec_copyout_strings(imgp)
if (p->p_sysent->sv_szsigcode != NULL) if (p->p_sysent->sv_szsigcode != NULL)
szsigcode = *(p->p_sysent->sv_szsigcode); szsigcode = *(p->p_sysent->sv_szsigcode);
} }
destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - destp = (uintptr_t)arginfo;
roundup(execpath_len, sizeof(char *)) -
roundup(sizeof(canary), sizeof(char *)) -
roundup(szps, sizeof(char *)) -
roundup((ARG_MAX - imgp->args->stringspace), sizeof(char *));
/* /*
* install sigcode * install sigcode
*/ */
if (szsigcode != 0) if (szsigcode != 0) {
copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - destp -= szsigcode;
szsigcode), szsigcode); destp = rounddown2(destp, sizeof(void *));
copyout(p->p_sysent->sv_sigcode, (void *)destp, szsigcode);
}
/* /*
* Copy the image path for the rtld. * Copy the image path for the rtld.
*/ */
if (execpath_len != 0) { if (execpath_len != 0) {
imgp->execpathp = (uintptr_t)arginfo - szsigcode - execpath_len; destp -= execpath_len;
copyout(imgp->execpath, (void *)imgp->execpathp, imgp->execpathp = destp;
execpath_len); copyout(imgp->execpath, (void *)destp, execpath_len);
} }
/* /*
* Prepare the canary for SSP. * Prepare the canary for SSP.
*/ */
arc4rand(canary, sizeof(canary), 0); arc4rand(canary, sizeof(canary), 0);
imgp->canary = (uintptr_t)arginfo - szsigcode - execpath_len - destp -= sizeof(canary);
sizeof(canary); imgp->canary = destp;
copyout(canary, (void *)imgp->canary, sizeof(canary)); copyout(canary, (void *)destp, sizeof(canary));
imgp->canarylen = sizeof(canary); imgp->canarylen = sizeof(canary);
/* /*
* Prepare the pagesizes array. * Prepare the pagesizes array.
*/ */
imgp->pagesizes = (uintptr_t)arginfo - szsigcode - execpath_len - destp -= szps;
roundup(sizeof(canary), sizeof(char *)) - szps; destp = rounddown2(destp, sizeof(void *));
copyout(pagesizes, (void *)imgp->pagesizes, szps); imgp->pagesizes = destp;
copyout(pagesizes, (void *)destp, szps);
imgp->pagesizeslen = szps; imgp->pagesizeslen = szps;
destp -= ARG_MAX - imgp->args->stringspace;
destp = rounddown2(destp, sizeof(void *));
/* /*
* If we have a valid auxargs ptr, prepare some room * If we have a valid auxargs ptr, prepare some room
* on the stack. * on the stack.
@ -1318,8 +1321,8 @@ exec_copyout_strings(imgp)
* The '+ 2' is for the null pointers at the end of each of * The '+ 2' is for the null pointers at the end of each of
* the arg and env vector sets * the arg and env vector sets
*/ */
vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc + 2) * vectp = (char **)(destp - (imgp->args->argc + imgp->args->envc
sizeof(char *)); + 2) * sizeof(char *));
} }
/* /*
@ -1334,7 +1337,7 @@ exec_copyout_strings(imgp)
/* /*
* Copy out strings - arguments and environment. * Copy out strings - arguments and environment.
*/ */
copyout(stringp, destp, ARG_MAX - imgp->args->stringspace); copyout(stringp, (void *)destp, ARG_MAX - imgp->args->stringspace);
/* /*
* Fill in "ps_strings" struct for ps, w, etc. * Fill in "ps_strings" struct for ps, w, etc.