From nobody Thu Dec 30 15:36:38 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 33018191D841; Thu, 30 Dec 2021 15:36:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JPsnf5jsCz3s26; Thu, 30 Dec 2021 15:36:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A5A221B0C6; Thu, 30 Dec 2021 15:36:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BUFacWM011041; Thu, 30 Dec 2021 15:36:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BUFacFi011040; Thu, 30 Dec 2021 15:36:38 GMT (envelope-from git) Date: Thu, 30 Dec 2021 15:36:38 GMT Message-Id: <202112301536.1BUFacFi011040@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Marcin Wojtas Subject: git: 532d925b6f02 - stable/13 - kern_exec: Add kern.stacktop sysctl. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mw X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 532d925b6f02167132edb7b1715836041a6e46fc Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1640878598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NF+QhIsu/iAVCjbPhzmiFQp/HIQa2YCHiLJlG1uXaYw=; b=udyDkRhPlk0JSbx3dLPlPWHwsJD/RPjXF3P70KLFFiTv2MhYJy1w6kUUOnrq9UKIeeOMl3 RUumP5Y1PbTnw5xTUuiXI9Aot1qGrIu877zMKSUxUniaolKOF3qfVAVJcTvYpuUFW4AztC CgSxc8Wd1eYKI/zG2ht2JSu4NA7YZOKlAK6DQUo8Rtq3GCaqAnnFNVlW5YiirBPGNB7z6S 8J4r38XUi5I7nV+WCmcNxVv+lZZmEDArRZU14c7ntqZSfKVcCCTmFVvrP/dKWy08Yfl1eg 8+kFsHamkZF5xaPR84AsRlQp6y0t6Q1pwgICrUp9WAu2RR7qBY/HhePdADequQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1640878598; a=rsa-sha256; cv=none; b=FAXye4NGE+oqAJLid05LyAauXFaSSv7NmMWUS/pHngeJcr8lYmvhcLvsrr7eu4Q4sChWKb zlgz1uf7jFRa6t4yuSNyt33X67TbAQMIRr5ZuvIjHpAvMZi/pJL7pTOXrPu7PydOSm/BuS YNLKERxPMdugwDPp8PbSem7vitaJBzFj2qUx71emquVYAYh8LCkxyU1PHge+bXIr2HcfM2 fau3dcaXyT4i2q0+0P4juScVzV2JVg/c6pc+2CDkY/Rfpuj5IO1erOgowJF/NTE93HuzRf xU1nj500LTgwwxd/+eqc6jmCnuYwnZuc4FPwvhKw3rCFrNSbPEgpOuW39zIV2Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mw: URL: https://cgit.FreeBSD.org/src/commit/?id=532d925b6f02167132edb7b1715836041a6e46fc commit 532d925b6f02167132edb7b1715836041a6e46fc Author: Dawid Gorecki AuthorDate: 2021-10-13 19:03:37 +0000 Commit: Marcin Wojtas CommitDate: 2021-12-30 15:25:22 +0000 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 (cherry picked from commit a97d697122da2bfb0baae5f0939d118d119dae33) --- sys/kern/kern_exec.c | 31 ++++++++++++++++++++++++++++++- sys/sys/sysctl.h | 1 + 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c index a6f333eec9e2..612797c2e075 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 9e9bd723f4bd..c8c9dddc146d 100644 --- a/sys/sys/sysctl.h +++ b/sys/sys/sysctl.h @@ -982,6 +982,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 */