Index: sys/kern/kern_exec.c =================================================================== --- sys/kern/kern_exec.c +++ sys/kern/kern_exec.c @@ -361,7 +361,6 @@ struct ucred *oldcred; struct uidinfo *euip = NULL; register_t *stack_base; - int error, i; struct image_params image_params, *imgp; struct vattr attr; int (*img_first)(struct image_params *); @@ -380,6 +379,8 @@ #ifdef HWPMC_HOOKS struct pmckern_procexec pe; #endif + int error, i, orig_osrel; + uint32_t orig_fctl0; static const char fexecv_proc_title[] = "(fexecv)"; imgp = &image_params; @@ -405,6 +406,8 @@ imgp->attr = &attr; imgp->args = args; oldcred = p->p_ucred; + orig_osrel = imgp->proc->p_osrel; + orig_fctl0 = imgp->proc->p_fctl0; #ifdef MAC error = mac_execve_enter(imgp, mac_p); @@ -868,6 +871,11 @@ SDT_PROBE1(proc, , , exec__success, args->fname); exec_fail_dealloc: + if (error != 0) { + p->p_osrel = orig_osrel; + p->p_fctl0 = orig_fctl0; + } + if (imgp->firstpage != NULL) exec_unmap_first_page(imgp); Index: sys/sys/elf_common.h =================================================================== --- sys/sys/elf_common.h +++ sys/sys/elf_common.h @@ -784,6 +784,7 @@ /* NT_FREEBSD_FEATURE_CTL desc[0] bits */ #define NT_FREEBSD_FCTL_ASLR_DISABLE 0x00000001 #define NT_FREEBSD_FCTL_PROTMAX_DISABLE 0x00000002 +#define NT_FREEBSD_FCTL_STKGAP_DISABLE 0x00000004 /* Values for n_type. Used in core files. */ #define NT_PRSTATUS 1 /* Process status. */ Index: sys/vm/vm_map.c =================================================================== --- sys/vm/vm_map.c +++ sys/vm/vm_map.c @@ -69,6 +69,7 @@ #include #include +#include #include #include #include @@ -4137,7 +4138,8 @@ addrbos + max_ssize > vm_map_max(map) || addrbos + max_ssize <= addrbos) return (KERN_INVALID_ADDRESS); - sgp = (curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ? 0 : + sgp = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 || + (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 : (vm_size_t)stack_guard_page * PAGE_SIZE; if (sgp >= max_ssize) return (KERN_INVALID_ARGUMENT); @@ -4274,7 +4276,8 @@ } else { return (KERN_FAILURE); } - guard = (curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 ? 0 : + guard = ((curproc->p_flag2 & P2_STKGAP_DISABLE) != 0 || + (curproc->p_fctl0 & NT_FREEBSD_FCTL_STKGAP_DISABLE) != 0) ? 0 : gap_entry->next_read; max_grow = gap_entry->end - gap_entry->start; if (guard > max_grow) Index: tools/tools/controlelf/controlelf.c =================================================================== --- tools/tools/controlelf/controlelf.c +++ tools/tools/controlelf/controlelf.c @@ -63,7 +63,9 @@ static struct ControlFeatures featurelist[] = { { "aslr", NT_FREEBSD_FCTL_ASLR_DISABLE, "Disable ASLR" }, - { "protmax", NT_FREEBSD_FCTL_PROTMAX_DISABLE,"Disable implicit PROT_MAX" }, + { "protmax", NT_FREEBSD_FCTL_PROTMAX_DISABLE, + "Disable implicit PROT_MAX" }, + { "stackgap", NT_FREEBSD_FCTL_STKGAP_DISABLE, "Disable stack gap" }, }; static struct option long_opts[] = {