summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2020-12-18 23:14:39 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2020-12-18 23:14:39 +0000
commit673e2dd652156342009930cf1f7d15623e4a543a (patch)
tree765f627665335f3c58e4e245461589ddb0063a75
parentc5354d593df9a5b97b6c4b1d9999915d93c8e103 (diff)
downloadsrc-test2-673e2dd652156342009930cf1f7d15623e4a543a.tar.gz
src-test2-673e2dd652156342009930cf1f7d15623e4a543a.zip
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
Notes
Notes: svn path=/head/; revision=368772
-rw-r--r--sys/compat/freebsd32/freebsd32_misc.c3
-rw-r--r--sys/kern/imgact_elf.c2
-rw-r--r--sys/kern/kern_exec.c14
-rw-r--r--sys/sys/elf_common.h1
-rw-r--r--sys/sys/imgact.h1
-rw-r--r--usr.bin/elfctl/elfctl.c1
6 files changed, 16 insertions, 6 deletions
diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c
index 6b7fd2deae35..62fab95c68d1 100644
--- a/sys/compat/freebsd32/freebsd32_misc.c
+++ b/sys/compat/freebsd32/freebsd32_misc.c
@@ -3424,8 +3424,7 @@ freebsd32_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
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) {
/*
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index fc1b7bf1a53a..f6482bfcfb21 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -2818,8 +2818,6 @@ __elfN(stackgap)(struct image_params *imgp, uintptr_t *stack_base)
uintptr_t range, rbase, gap;
int pct;
- if ((imgp->map_flags & MAP_ASLR) == 0)
- return;
pct = __elfN(aslr_stack_gap);
if (pct == 0)
return;
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index b3fcbef0289b..6f7c01470804 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -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, uintptr_t *stack_base)
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) {
/*
diff --git a/sys/sys/elf_common.h b/sys/sys/elf_common.h
index 8166d352955c..5724397754e7 100644
--- a/sys/sys/elf_common.h
+++ b/sys/sys/elf_common.h
@@ -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. */
diff --git a/sys/sys/imgact.h b/sys/sys/imgact.h
index 5463b6f80d1c..cd130551c050 100644
--- a/sys/sys/imgact.h
+++ b/sys/sys/imgact.h
@@ -122,6 +122,7 @@ int exec_copyin_args(struct image_args *, const char *, enum uio_seg,
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
diff --git a/usr.bin/elfctl/elfctl.c b/usr.bin/elfctl/elfctl.c
index 591258fad729..e9b93e697ef7 100644
--- a/usr.bin/elfctl/elfctl.c
+++ b/usr.bin/elfctl/elfctl.c
@@ -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[] = {