diff options
author | Dawid Gorecki <dgr@semihalf.com> | 2021-10-13 19:03:37 +0000 |
---|---|---|
committer | Marcin Wojtas <mw@FreeBSD.org> | 2021-10-15 08:21:55 +0000 |
commit | a97d697122da2bfb0baae5f0939d118d119dae33 (patch) | |
tree | 7abb348391cfe24c345e5fc5c5b22dc0f248221e | |
parent | 889b56c8cd84c9a9f2d9e3b019c154d6f14d9021 (diff) | |
download | src-a97d697122da2bfb0baae5f0939d118d119dae33.tar.gz src-a97d697122da2bfb0baae5f0939d118d119dae33.zip |
kern_exec: Add kern.stacktop sysctl.
With stack gap enabled top of the stack is moved down by a random
amount of bytes. Because of that some multithreaded applications
which use kern.usrstack sysctl to calculate address of stacks for
their threads can fail. Add kern.stacktop sysctl, which can be used
to retrieve address of the stack after stack gap is applied to it.
Returns value identical to kern.usrstack for processes which have
no stack gap.
Reviewed by: kib
Obtained from: Semihalf
Sponsored by: Stormshield
MFC after: 1 month
Differential Revision: https://reviews.freebsd.org/D31897
-rw-r--r-- | sys/kern/kern_exec.c | 31 | ||||
-rw-r--r-- | sys/sys/sysctl.h | 1 |
2 files changed, 31 insertions, 1 deletions
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index 9dceebdd8441..a9998471dd14 100644 --- a/sys/kern/kern_exec.c +++ b/sys/kern/kern_exec.c @@ -119,6 +119,7 @@ SYSCTL_INT(_kern, OID_AUTO, coredump_pack_vmmapinfo, CTLFLAG_RWTUN, static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); +static int sysctl_kern_stacktop(SYSCTL_HANDLER_ARGS); static int sysctl_kern_stackprot(SYSCTL_HANDLER_ARGS); static int do_execve(struct thread *td, struct image_args *args, struct mac *mac_p, struct vmspace *oldvmspace); @@ -133,6 +134,10 @@ SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD| CTLFLAG_CAPRD|CTLFLAG_MPSAFE, NULL, 0, sysctl_kern_usrstack, "LU", "Top of process stack"); +SYSCTL_PROC(_kern, KERN_STACKTOP, stacktop, CTLTYPE_ULONG | CTLFLAG_RD | + CTLFLAG_CAPRD | CTLFLAG_MPSAFE, NULL, 0, sysctl_kern_stacktop, "LU", + "Top of process stack with stack gap."); + SYSCTL_PROC(_kern, OID_AUTO, stackprot, CTLTYPE_INT|CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, 0, sysctl_kern_stackprot, "I", "Stack memory permissions"); @@ -191,7 +196,31 @@ sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) #endif error = SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, sizeof(p->p_sysent->sv_usrstack)); - return error; + return (error); +} + +static int +sysctl_kern_stacktop(SYSCTL_HANDLER_ARGS) +{ + vm_offset_t stacktop; + struct proc *p; + int error; + + p = curproc; +#ifdef SCTL_MASK32 + if (req->flags & SCTL_MASK32) { + unsigned int val; + + val = (unsigned int)(p->p_sysent->sv_usrstack - + p->p_vmspace->vm_stkgap); + error = SYSCTL_OUT(req, &val, sizeof(val)); + } else +#endif + { + stacktop = p->p_sysent->sv_usrstack - p->p_vmspace->vm_stkgap; + error = SYSCTL_OUT(req, &stacktop, sizeof(stacktop)); + } + return (error); } static int diff --git a/sys/sys/sysctl.h b/sys/sys/sysctl.h index 71a34652ff44..b455fec7209f 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -976,6 +976,7 @@ TAILQ_HEAD(sysctl_ctx_list, sysctl_ctx_entry); #define KERN_HOSTUUID 36 /* string: host UUID identifier */ #define KERN_ARND 37 /* int: from arc4rand() */ #define KERN_MAXPHYS 38 /* int: MAXPHYS value */ +#define KERN_STACKTOP 39 /* int: USRSTACK - stack gap */ /* * KERN_PROC subtypes */ |