From 2a08a8041178387d87aed82fe3e8c5071887969d Mon Sep 17 00:00:00 2001 From: kib Date: Fri, 15 Jun 2012 10:38:14 +0000 Subject: [PATCH] MFC r226342 (by marcel): In elf32_trans_prot() and when compiling for amd64 or ia64, add PROT_EXECUTE when PROT_READ is needed. By default i386 allows execution when reading is allowed and JDK 1.4.x depends on that. MFC r226343 (by marcel): In sys_obreak() and when compiling for amd64 or ia64, when the process is ILP32 (i.e. i386) grant execute permissions by default. The JDK 1.4.x depends on being able to execute from the heap on i386. MFC r226347 (by marcel): In freebsd32_mmap() and when compiling for amd64 or ia64, also ask for execute permissions when read permissions are wanted. This is needed for JDK 1.4.x on i386. MFC r226348 (by marcel): Wrap mprotect(2). MFC r226349 (by marcel): Wrap mprotect(2) so that we can add execute permissions when read permissions are requested. This is needed on amd64 and ia64 for JDK 1.4.x. MFC r226353 (by marcel): Use PTRIN(). MFC r226388: Control the execution permission of the readable segments for i386 binaries on the amd64 and ia64 with the sysctl, instead of unconditionally enabling it. MFC note: the syscall tables were regenerated in r226349 and committed together with changes to non-generated files. The merge includes syscall tables regenerated after the merge, for stable/9. git-svn-id: svn://svn.freebsd.org/base/stable/9@237134 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/compat/freebsd32/freebsd32_misc.c | 20 +++++++++++++++++++ sys/compat/freebsd32/freebsd32_proto.h | 7 +++++++ sys/compat/freebsd32/freebsd32_syscall.h | 2 +- sys/compat/freebsd32/freebsd32_syscalls.c | 2 +- sys/compat/freebsd32/freebsd32_sysent.c | 2 +- .../freebsd32/freebsd32_systrace_args.c | 6 +++--- sys/compat/freebsd32/syscalls.master | 2 +- sys/kern/imgact_elf.c | 14 +++++++++++++ sys/sys/sysent.h | 4 ++++ sys/vm/vm_unix.c | 14 +++++++++++-- 10 files changed, 64 insertions(+), 9 deletions(-) diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c index 5237c7022..aff280ad0 100644 --- a/sys/compat/freebsd32/freebsd32_misc.c +++ b/sys/compat/freebsd32/freebsd32_misc.c @@ -436,6 +436,21 @@ freebsd32_mmap_partial(struct thread *td, vm_offset_t start, vm_offset_t end, } #endif +int +freebsd32_mprotect(struct thread *td, struct freebsd32_mprotect_args *uap) +{ + struct mprotect_args ap; + + ap.addr = PTRIN(uap->addr); + ap.len = uap->len; + ap.prot = uap->prot; +#if defined(__amd64__) || defined(__ia64__) + if (i386_read_exec && (ap.prot & PROT_READ) != 0) + ap.prot |= PROT_EXEC; +#endif + return (sys_mprotect(td, &ap)); +} + int freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap) { @@ -520,6 +535,11 @@ freebsd32_mmap(struct thread *td, struct freebsd32_mmap_args *uap) } #endif +#if defined(__amd64__) || defined(__ia64__) + if (i386_read_exec && (prot & PROT_READ)) + prot |= PROT_EXEC; +#endif + ap.addr = (void *) addr; ap.len = len; ap.prot = prot; diff --git a/sys/compat/freebsd32/freebsd32_proto.h b/sys/compat/freebsd32/freebsd32_proto.h index 58b373afa..4adb8d07e 100644 --- a/sys/compat/freebsd32/freebsd32_proto.h +++ b/sys/compat/freebsd32/freebsd32_proto.h @@ -76,6 +76,11 @@ struct freebsd32_execve_args { char argv_l_[PADL_(u_int32_t *)]; u_int32_t * argv; char argv_r_[PADR_(u_int32_t *)]; char envv_l_[PADL_(u_int32_t *)]; u_int32_t * envv; char envv_r_[PADR_(u_int32_t *)]; }; +struct freebsd32_mprotect_args { + char addr_l_[PADL_(const void *)]; const void * addr; char addr_r_[PADR_(const void *)]; + char len_l_[PADL_(size_t)]; size_t len; char len_r_[PADR_(size_t)]; + char prot_l_[PADL_(int)]; int prot; char prot_r_[PADR_(int)]; +}; struct freebsd32_setitimer_args { char which_l_[PADL_(u_int)]; u_int which; char which_r_[PADR_(u_int)]; char itv_l_[PADL_(struct itimerval32 *)]; struct itimerval32 * itv; char itv_r_[PADR_(struct itimerval32 *)]; @@ -593,6 +598,7 @@ int freebsd32_recvfrom(struct thread *, struct freebsd32_recvfrom_args *); int freebsd32_sigaltstack(struct thread *, struct freebsd32_sigaltstack_args *); int freebsd32_ioctl(struct thread *, struct freebsd32_ioctl_args *); int freebsd32_execve(struct thread *, struct freebsd32_execve_args *); +int freebsd32_mprotect(struct thread *, struct freebsd32_mprotect_args *); int freebsd32_setitimer(struct thread *, struct freebsd32_setitimer_args *); int freebsd32_getitimer(struct thread *, struct freebsd32_getitimer_args *); int freebsd32_select(struct thread *, struct freebsd32_select_args *); @@ -952,6 +958,7 @@ int freebsd7_freebsd32_shmctl(struct thread *, struct freebsd7_freebsd32_shmctl_ #define FREEBSD32_SYS_AUE_freebsd32_execve AUE_EXECVE #define FREEBSD32_SYS_AUE_ofreebsd32_fstat AUE_FSTAT #define FREEBSD32_SYS_AUE_ofreebsd32_getpagesize AUE_NULL +#define FREEBSD32_SYS_AUE_freebsd32_mprotect AUE_MPROTECT #define FREEBSD32_SYS_AUE_freebsd32_setitimer AUE_SETITIMER #define FREEBSD32_SYS_AUE_freebsd32_getitimer AUE_GETITIMER #define FREEBSD32_SYS_AUE_freebsd32_select AUE_SELECT diff --git a/sys/compat/freebsd32/freebsd32_syscall.h b/sys/compat/freebsd32/freebsd32_syscall.h index d48f556e4..09a4af2ca 100644 --- a/sys/compat/freebsd32/freebsd32_syscall.h +++ b/sys/compat/freebsd32/freebsd32_syscall.h @@ -80,7 +80,7 @@ /* 71 is obsolete ommap */ #define FREEBSD32_SYS_vadvise 72 #define FREEBSD32_SYS_munmap 73 -#define FREEBSD32_SYS_mprotect 74 +#define FREEBSD32_SYS_freebsd32_mprotect 74 #define FREEBSD32_SYS_madvise 75 /* 76 is obsolete vhangup */ /* 77 is obsolete vlimit */ diff --git a/sys/compat/freebsd32/freebsd32_syscalls.c b/sys/compat/freebsd32/freebsd32_syscalls.c index 7f92c1923..83b3dc4d4 100644 --- a/sys/compat/freebsd32/freebsd32_syscalls.c +++ b/sys/compat/freebsd32/freebsd32_syscalls.c @@ -84,7 +84,7 @@ const char *freebsd32_syscallnames[] = { "obs_ommap", /* 71 = obsolete ommap */ "vadvise", /* 72 = vadvise */ "munmap", /* 73 = munmap */ - "mprotect", /* 74 = mprotect */ + "freebsd32_mprotect", /* 74 = freebsd32_mprotect */ "madvise", /* 75 = madvise */ "obs_vhangup", /* 76 = obsolete vhangup */ "obs_vlimit", /* 77 = obsolete vlimit */ diff --git a/sys/compat/freebsd32/freebsd32_sysent.c b/sys/compat/freebsd32/freebsd32_sysent.c index c3bfe4099..de1a53cd0 100644 --- a/sys/compat/freebsd32/freebsd32_sysent.c +++ b/sys/compat/freebsd32/freebsd32_sysent.c @@ -121,7 +121,7 @@ struct sysent freebsd32_sysent[] = { { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 71 = obsolete ommap */ { AS(ovadvise_args), (sy_call_t *)sys_ovadvise, AUE_O_VADVISE, NULL, 0, 0, 0, SY_THR_STATIC }, /* 72 = vadvise */ { AS(munmap_args), (sy_call_t *)sys_munmap, AUE_MUNMAP, NULL, 0, 0, 0, SY_THR_STATIC }, /* 73 = munmap */ - { AS(mprotect_args), (sy_call_t *)sys_mprotect, AUE_MPROTECT, NULL, 0, 0, 0, SY_THR_STATIC }, /* 74 = mprotect */ + { AS(freebsd32_mprotect_args), (sy_call_t *)freebsd32_mprotect, AUE_MPROTECT, NULL, 0, 0, 0, SY_THR_STATIC }, /* 74 = freebsd32_mprotect */ { AS(madvise_args), (sy_call_t *)sys_madvise, AUE_MADVISE, NULL, 0, 0, 0, SY_THR_STATIC }, /* 75 = madvise */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 76 = obsolete vhangup */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 77 = obsolete vlimit */ diff --git a/sys/compat/freebsd32/freebsd32_systrace_args.c b/sys/compat/freebsd32/freebsd32_systrace_args.c index 034f08ea4..88bdc705e 100644 --- a/sys/compat/freebsd32/freebsd32_systrace_args.c +++ b/sys/compat/freebsd32/freebsd32_systrace_args.c @@ -464,9 +464,9 @@ systrace_args(int sysnum, void *params, uint64_t *uarg, int *n_args) *n_args = 2; break; } - /* mprotect */ + /* freebsd32_mprotect */ case 74: { - struct mprotect_args *p = params; + struct freebsd32_mprotect_args *p = params; uarg[0] = (intptr_t) p->addr; /* const void * */ uarg[1] = p->len; /* size_t */ iarg[2] = p->prot; /* int */ @@ -3749,7 +3749,7 @@ systrace_setargdesc(int sysnum, int ndx, char *desc, size_t descsz) break; }; break; - /* mprotect */ + /* freebsd32_mprotect */ case 74: switch(ndx) { case 0: diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master index 58928a80d..cb22f8b09 100644 --- a/sys/compat/freebsd32/syscalls.master +++ b/sys/compat/freebsd32/syscalls.master @@ -172,7 +172,7 @@ 72 AUE_O_VADVISE NOPROTO { int ovadvise(int anom); } vadvise \ ovadvise_args int 73 AUE_MUNMAP NOPROTO { int munmap(void *addr, size_t len); } -74 AUE_MPROTECT NOPROTO { int mprotect(const void *addr, \ +74 AUE_MPROTECT STD { int freebsd32_mprotect(const void *addr, \ size_t len, int prot); } 75 AUE_MADVISE NOPROTO { int madvise(void *addr, size_t len, \ int behav); } diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index 7c442c511..baa4a2cd7 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -123,6 +123,14 @@ SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, nxstack, CTLFLAG_RW, &__elfN(nxstack), 0, __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack"); +#if __ELF_WORD_SIZE == 32 +#if defined(__amd64__) || defined(__ia64__) +int i386_read_exec = 0; +SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0, + "enable execution from readable segments"); +#endif +#endif + static Elf_Brandinfo *elf_brand_list[MAX_BRANDS]; #define trunc_page_ps(va, ps) ((va) & ~(ps - 1)) @@ -1671,6 +1679,12 @@ __elfN(trans_prot)(Elf_Word flags) prot |= VM_PROT_WRITE; if (flags & PF_R) prot |= VM_PROT_READ; +#if __ELF_WORD_SIZE == 32 +#if defined(__amd64__) || defined(__ia64__) + if (i386_read_exec && (flags & PF_R)) + prot |= VM_PROT_EXECUTE; +#endif +#endif return (prot); } diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h index 6a4b485ce..d916cf138 100644 --- a/sys/sys/sysent.h +++ b/sys/sys/sysent.h @@ -151,6 +151,10 @@ extern struct sysentvec null_sysvec; extern struct sysent sysent[]; extern const char *syscallnames[]; +#if defined(__amd64__) || defined(__ia64__) +extern int i386_read_exec; +#endif + #define NO_SYSCALL (-1) struct module; diff --git a/sys/vm/vm_unix.c b/sys/vm/vm_unix.c index e08aa6a61..253ab775d 100644 --- a/sys/vm/vm_unix.c +++ b/sys/vm/vm_unix.c @@ -36,6 +36,8 @@ * @(#)vm_unix.c 8.1 (Berkeley) 6/11/93 */ +#include "opt_compat.h" + /* * Traditional sbrk/grow interface to VM */ @@ -49,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include @@ -75,7 +78,7 @@ sys_obreak(td, uap) struct vmspace *vm = td->td_proc->p_vmspace; vm_offset_t new, old, base; rlim_t datalim, vmemlim; - int rv; + int prot, rv; int error = 0; boolean_t do_map_wirefuture; @@ -134,9 +137,16 @@ sys_obreak(td, uap) goto done; } PROC_UNLOCK(td->td_proc); +#endif + prot = VM_PROT_RW; +#ifdef COMPAT_FREEBSD32 +#if defined(__amd64__) || defined(__ia64__) + if (i386_read_exec && SV_PROC_FLAG(td->td_proc, SV_ILP32)) + prot |= VM_PROT_EXECUTE; +#endif #endif rv = vm_map_insert(&vm->vm_map, NULL, 0, old, new, - VM_PROT_RW, VM_PROT_ALL, 0); + prot, VM_PROT_ALL, 0); if (rv != KERN_SUCCESS) { #ifdef RACCT PROC_LOCK(td->td_proc); -- 2.45.0