From owner-svn-src-all@freebsd.org Fri Dec 18 23:14:42 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2EBED4B0A7E; Fri, 18 Dec 2020 23:14:42 +0000 (UTC) (envelope-from kib@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CyPp93l91z3qFX; Fri, 18 Dec 2020 23:14:41 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 72DA713250; Fri, 18 Dec 2020 23:14:41 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0BINEfVn010664; Fri, 18 Dec 2020 23:14:41 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0BINEeA3010657; Fri, 18 Dec 2020 23:14:40 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202012182314.0BINEeA3010657@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Fri, 18 Dec 2020 23:14:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r368772 - in head: sys/compat/freebsd32 sys/kern sys/sys usr.bin/elfctl X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: in head: sys/compat/freebsd32 sys/kern sys/sys usr.bin/elfctl X-SVN-Commit-Revision: 368772 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Dec 2020 23:14:42 -0000 Author: kib Date: Fri Dec 18 23:14:39 2020 New Revision: 368772 URL: https://svnweb.freebsd.org/changeset/base/368772 Log: Add ELF flag to disable ASLR stack gap. Also centralize and unify checks to enable ASLR stack gap in a new helper exec_stackgap(). PR: 239873 Sponsored by: The FreeBSD Foundation MFC after: 1 week Modified: head/sys/compat/freebsd32/freebsd32_misc.c head/sys/kern/imgact_elf.c head/sys/kern/kern_exec.c head/sys/sys/elf_common.h head/sys/sys/imgact.h head/usr.bin/elfctl/elfctl.c Modified: head/sys/compat/freebsd32/freebsd32_misc.c ============================================================================== --- head/sys/compat/freebsd32/freebsd32_misc.c Fri Dec 18 23:11:27 2020 (r368771) +++ head/sys/compat/freebsd32/freebsd32_misc.c Fri Dec 18 23:14:39 2020 (r368772) @@ -3424,8 +3424,7 @@ freebsd32_copyout_strings(struct image_params *imgp, u destp = rounddown2(destp, sizeof(uint32_t)); ustringp = destp; - if (imgp->sysent->sv_stackgap != NULL) - imgp->sysent->sv_stackgap(imgp, &destp); + exec_stackgap(imgp, &destp); if (imgp->auxargs) { /* Modified: head/sys/kern/imgact_elf.c ============================================================================== --- head/sys/kern/imgact_elf.c Fri Dec 18 23:11:27 2020 (r368771) +++ head/sys/kern/imgact_elf.c Fri Dec 18 23:14:39 2020 (r368772) @@ -2818,8 +2818,6 @@ __elfN(stackgap)(struct image_params *imgp, uintptr_t uintptr_t range, rbase, gap; int pct; - if ((imgp->map_flags & MAP_ASLR) == 0) - return; pct = __elfN(aslr_stack_gap); if (pct == 0) return; Modified: head/sys/kern/kern_exec.c ============================================================================== --- head/sys/kern/kern_exec.c Fri Dec 18 23:11:27 2020 (r368771) +++ head/sys/kern/kern_exec.c Fri Dec 18 23:14:39 2020 (r368772) @@ -1534,6 +1534,17 @@ exec_args_get_begin_envv(struct image_args *args) return (args->endp); } +void +exec_stackgap(struct image_params *imgp, uintptr_t *dp) +{ + if (imgp->sysent->sv_stackgap == NULL || + (imgp->proc->p_fctl0 & (NT_FREEBSD_FCTL_ASLR_DISABLE | + NT_FREEBSD_FCTL_ASG_DISABLE)) != 0 || + (imgp->map_flags & MAP_ASLR) == 0) + return; + imgp->sysent->sv_stackgap(imgp, dp); +} + /* * Copy strings out to the new process address space, constructing new arg * and env vector tables. Return a pointer to the base so that it can be used @@ -1624,8 +1635,7 @@ exec_copyout_strings(struct image_params *imgp, uintpt destp = rounddown2(destp, sizeof(void *)); ustringp = destp; - if (imgp->sysent->sv_stackgap != NULL) - imgp->sysent->sv_stackgap(imgp, &destp); + exec_stackgap(imgp, &destp); if (imgp->auxargs) { /* Modified: head/sys/sys/elf_common.h ============================================================================== --- head/sys/sys/elf_common.h Fri Dec 18 23:11:27 2020 (r368771) +++ head/sys/sys/elf_common.h Fri Dec 18 23:14:39 2020 (r368772) @@ -797,6 +797,7 @@ typedef struct { #define NT_FREEBSD_FCTL_STKGAP_DISABLE 0x00000004 #define NT_FREEBSD_FCTL_WXNEEDED 0x00000008 #define NT_FREEBSD_FCTL_LA48 0x00000010 +#define NT_FREEBSD_FCTL_ASG_DISABLE 0x00000020 /* ASLR STACK GAP Disable */ /* Values for n_type. Used in core files. */ #define NT_PRSTATUS 1 /* Process status. */ Modified: head/sys/sys/imgact.h ============================================================================== --- head/sys/sys/imgact.h Fri Dec 18 23:11:27 2020 (r368771) +++ head/sys/sys/imgact.h Fri Dec 18 23:14:39 2020 (r368772) @@ -122,6 +122,7 @@ int exec_copyin_args(struct image_args *, const char * char **, char **); int exec_copyin_data_fds(struct thread *, struct image_args *, const void *, size_t, const int *, size_t); +void exec_stackgap(struct image_params *imgp, uintptr_t *dp); int pre_execve(struct thread *td, struct vmspace **oldvmspace); void post_execve(struct thread *td, int error, struct vmspace *oldvmspace); #endif Modified: head/usr.bin/elfctl/elfctl.c ============================================================================== --- head/usr.bin/elfctl/elfctl.c Fri Dec 18 23:11:27 2020 (r368771) +++ head/usr.bin/elfctl/elfctl.c Fri Dec 18 23:14:39 2020 (r368772) @@ -68,6 +68,7 @@ static struct ControlFeatures featurelist[] = { { "stackgap", NT_FREEBSD_FCTL_STKGAP_DISABLE, "Disable stack gap" }, { "wxneeded", NT_FREEBSD_FCTL_WXNEEDED, "Requires W+X mappings" }, { "la48", NT_FREEBSD_FCTL_LA48, "amd64: Limit user VA to 48bit" }, + { "aslrstkgap", NT_FREEBSD_FCTL_ASG_DISABLE, "Disable ASLR stack gap" }, }; static struct option long_opts[] = {