From 3aebd936a5f26c465c6ee1cb566bd3739a732847 Mon Sep 17 00:00:00 2001 From: Lucio AndrĂ©s Illanes Albornoz (arab, vxp) Date: Mar 07 2016 15:24:55 +0000 Subject: Folowup to last commit (adds minimal mprotect(2); fixes Irssi (part 2.)) --- diff --git a/psxscl.local.patch b/psxscl.local.patch index 491b7cc..1b3b9c0 100644 --- a/psxscl.local.patch +++ b/psxscl.local.patch @@ -134,17 +134,4 @@ index a623d2a..80dd4a9 100644 + NT_SEC_RESERVE, image->info.hfile))) return __execve_return(tlca,0,target,-ENOEXEC); - ---- psxscl/src/mman/_mmap.c.orig 2016-03-07 14:29:56.539765500 +0100 -+++ psxscl/src/mman/_mmap.c 2016-03-07 14:37:21.460223800 +0100 -@@ -95,7 +95,9 @@ - return (void *)__psx_sig_epilog(m.tlca,-EINVAL,EPSXONLY); - - /* protection */ -- if (__psx_convert_flags_to_native( -+ if (!prot) -+ m.cprot = NT_PAGE_NOACCESS; -+ else if (__psx_convert_flags_to_native( - __mmap_section_prot, - prot,&m.cprot,0)) - return (void *)__psx_sig_epilog(m.tlca,-EINVAL,EPSXONLY); + diff --git a/psxscl.local@lucio-vm.local.patch b/psxscl.local@lucio-vm.local.patch index 86ef312..1ef8615 100644 --- a/psxscl.local@lucio-vm.local.patch +++ b/psxscl.local@lucio-vm.local.patch @@ -11,3 +11,192 @@ index 924b1b8..02846ba 100644 #endif int32_t __psx_init_cwd(void) +diff --git a/project/common.mk b/project/common.mk +index 4390578..fa6df57 100644 +--- a/project/common.mk ++++ b/project/common.mk +@@ -102,6 +102,7 @@ COMMON_SRCS = \ + src/kernel/_systime.c \ + src/kernel/_uname.c \ + src/mman/_mmap.c \ ++ src/mman/_mprotect.c \ + src/mman/_mremap.c \ + src/mman/_munmap.c \ + src/mount/_mount.c \ +diff --git a/src/init/psx_dev_wip.c b/src/init/psx_dev_wip.c +index fe319bc..3915590 100644 +--- a/src/init/psx_dev_wip.c ++++ b/src/init/psx_dev_wip.c +@@ -89,6 +89,7 @@ static void __populate_syscall_vtbl(void) + + /* mman */ + set_syscall_pointer(mmap); ++ set_syscall_pointer(mprotect); + set_syscall_pointer(mremap); + set_syscall_pointer(munmap); + +@@ -263,6 +264,7 @@ static void __populate_strace_vtbl(void) + + /* mman */ + set_strace_pointer(mmap); ++ set_strace_pointer(mprotect); + set_strace_pointer(mremap); + set_strace_pointer(munmap); + +diff --git a/src/internal/psx.h b/src/internal/psx.h +index 1791bda..21efa46 100644 +--- a/src/internal/psx.h ++++ b/src/internal/psx.h +@@ -97,6 +97,7 @@ typedef intptr_t __sys_routine(uname)(struct __utsname *); + + /* mman */ + typedef void * __sys_routine(mmap)(void * addr, size_t length, int prot, int flags, int,off_t offset); ++typedef intptr_t __sys_routine(mprotect)(void * addr, size_t length, int prot); + typedef void * __sys_routine(mremap)(void * mapaddr, size_t mapsize, size_t newsize, int flags); + typedef intptr_t __sys_routine(munmap)(void * addr, size_t length); + +@@ -270,6 +271,7 @@ __sys_interface(uname); + + /* mman */ + __sys_interface(mmap); ++__sys_interface(mprotect); + __sys_interface(mremap); + __sys_interface(munmap); + +diff --git a/src/internal/psx_strace.c b/src/internal/psx_strace.c +index 6d39093..45e7ecd 100644 +--- a/src/internal/psx_strace.c ++++ b/src/internal/psx_strace.c +@@ -859,6 +859,22 @@ void * __strace_mmap(void * addr,size_t length,int prot,int flags,int fd,off_t o + + } + ++intptr_t __strace_mprotect(void * addr,size_t length,int prot) ++{ ++ struct __strace_params params = { ++ __STRACE_MPROTECT,3, ++ {__STRACE_SINTEGER,0}, ++ {{__STRACE_POINTER,(intptr_t)addr}, ++ {__STRACE_SINTEGER,length}, ++ {__STRACE_SINTEGER,prot}}}; ++ ++ if (!addr) params.params[0].type = __STRACE_SINTEGER; ++ params.ret.value = (intptr_t)__sys_mprotect(addr,length,prot); ++ __strace(¶ms); ++ return (void *)params.ret.value; ++ ++} ++ + void * __strace_mremap(void * mapaddr, size_t mapsize, size_t newsize, int flags) + { + struct __strace_params params = { +diff --git a/src/internal/psx_strace.h b/src/internal/psx_strace.h +index 67eecce..293cfdf 100644 +--- a/src/internal/psx_strace.h ++++ b/src/internal/psx_strace.h +@@ -60,6 +60,7 @@ enum __strace_sysids { + __STRACE_SYSINFO, + __STRACE_UNAME, + __STRACE_MMAP, ++ __STRACE_MPROTECT, + __STRACE_MREMAP, + __STRACE_MUNMAP, + __STRACE_MOUNT, +@@ -197,6 +198,7 @@ enum __strace_sysids { + "sysinfo", \ + "uname", \ + "mmap", \ ++ "mprotect", \ + "mremap", \ + "munmap", \ + "mount", \ +@@ -346,6 +348,7 @@ __strace_interface(uname); + + /* mman */ + __strace_interface(mmap); ++__strace_interface(mprotect); + __strace_interface(mremap); + __strace_interface(munmap); + +diff --git a/src/mman/_mmap.c b/src/mman/_mmap.c +index 13762e3..27ea942 100644 +--- a/src/mman/_mmap.c ++++ b/src/mman/_mmap.c +@@ -95,7 +95,9 @@ void * __sys_mmap( + return (void *)__psx_sig_epilog(m.tlca,-EINVAL,EPSXONLY); + + /* protection */ +- if (__psx_convert_flags_to_native( ++ if (!prot) ++ m.cprot = NT_PAGE_NOACCESS; ++ else if (__psx_convert_flags_to_native( + __mmap_section_prot, + prot,&m.cprot,0)) + return (void *)__psx_sig_epilog(m.tlca,-EINVAL,EPSXONLY); +diff --git a/src/mman/_mprotect.c b/src/mman/_mprotect.c +new file mode 100644 +index 0000000..7ac46d3 +--- /dev/null ++++ b/src/mman/_mprotect.c +@@ -0,0 +1,61 @@ ++/********************************************************/ ++/* psxscl: a thread-safe system call layer library */ ++/* Copyright (C) 2013--2016 Z. Gilboa */ ++/* Released under GPLv2 and GPLv3; see COPYING.PSXSCL. */ ++/********************************************************/ ++ ++#include ++#include "psx_systypes.h" ++#include "psx_tlca.h" ++#include "psx_errno.h" ++#include "psx_flags.h" ++#include "psx_mman.h" ++#include "psx_signal.h" ++#include "psx_ofd.h" ++#include "psx_acl.h" ++#include "psx.h" ++ ++static const struct __flag_set __mmap_section_prot[] = { ++ {PROT_READ, NT_PAGE_READONLY}, ++ {PROT_WRITE, NT_PAGE_READWRITE}, ++ {PROT_EXEC, NT_PAGE_EXECUTE}, ++ {PROT_NONE, 0}, ++ {0, 0}}; ++ ++ ++__psx_api ++intptr_t __sys_mprotect( ++ void * addr, ++ size_t length, ++ int prot) ++{ ++ int32_t status; ++ struct __psx_tlca * tlca; ++ struct __psx_ctx * ctx; ++ uint32_t cprot, protect_type_old; ++ ++ /* prolog */ ++ tlca = __tlca_self(); ++ if (!(ctx = __tlca_shared_ctx(tlca))) return 0; ++ __psx_sig_prolog(tlca); ++ ++ ++ /* protection */ ++ if (!prot) ++ cprot = NT_PAGE_NOACCESS; ++ else if (__psx_convert_flags_to_native( ++ __mmap_section_prot, ++ prot,&cprot,0)) ++ return __psx_sig_epilog(tlca,-EINVAL,EPSXONLY); ++ ++ if (cprot & NT_PAGE_READWRITE) ++ cprot = NT_PAGE_READWRITE; ++ ++ if ((status = __ntapi->zw_protect_virtual_memory( ++ NT_CURRENT_PROCESS_HANDLE, ++ &addr, &length, ++ cprot, &protect_type_old))) ++ return __psx_sig_epilog(tlca,-ENOMEM,status); ++ ++ return __psx_sig_epilog(tlca,0,status); ++}