IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> 在使用 emul_linux 时"executable stack as shared object"等问题的解决, mprotect patch for compat_linux in OpenBSD 4.2
猫猫草
post 2009-08-12 00:36:31, Wed
Post #1


猫猫猫
***

Group: Power Cat
Posts: 626
Joined: 2006-12-8
Member No.: 2



Origin from:

http://www.mail-archive.com/misc@openbsd.org/msg51249.html

引用
mprotect patch for compat_linux in OpenBSD 4.2

Matthew Szudzik
Sun, 11 Nov 2007 08:07:08 -0800

As some of you may recall, there are Linux binaries (such as Mathematica)
which will not run under OpenBSD's Linux emulation because the
Linux mprotect system-call uses flags that are not supported by OpenBSD.
To remedy this problem, I had posted a patch for OpenBSD 4.1

http://marc.info/?l=openbsd-misc&m=118275040601937

I have included a new version of that patch, updated for OpenBSD 4.2, at
the end of this message.

After applying the patch to the OpenBSD 4.2 source-code, be sure to run
make in the directory

/usr/src/sys/compat/linux/

If you forget to run that special makefile, then the patch will fail.

As before, I should mention that I am not the author of the majority of
the code in this patch. Instead, it has been ported from the
corresponding NetBSD patches


http://cvsweb.netbsd.org/bsdweb.cgi/src/sy...40&r2=1.141


http://cvsweb.netbsd.org/bsdweb.cgi/src/sy....16&r2=1.17

http://cvsweb.netbsd.org/bsdweb.cgi/src/sy....61&r2=1.62

The OpenBSD 4.2 patch follows:


代码
--- /usr/src/sys/compat/linux/linux_misc.c.orig Wed Feb 14 18:07:51 2007
+++ /usr/src/sys/compat/linux/linux_misc.c      Sun Nov 11 02:40:01 2007
@@ -718,6 +718,70 @@

}

+int
+linux_sys_mprotect(p, v, retval)
+       struct proc *p;
+       void *v;
+       register_t *retval;
+{
+       struct linux_sys_mprotect_args /* {
+               syscallarg(const void *) start;
+               syscallarg(unsigned long) len;
+               syscallarg(int) prot;
+       } */ *uap = v;
+       struct vm_map_entry *entry;
+       struct vm_map *map;
+       vaddr_t end, start, len, stacklim;
+       int prot, grows;
+
+       start = (vaddr_t)SCARG(uap, start);
+       len = round_page(SCARG(uap, len));
+       prot = SCARG(uap, prot);
+       grows = prot & (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP);
+       prot &= ~grows;
+       end = start + len;
+
+       if (start & PAGE_MASK)
+               return EINVAL;
+       if (end < start)
+               return EINVAL;
+       if (end == start)
+               return 0;
+
+       if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
+               return EINVAL;
+       if (grows == (LINUX_PROT_GROWSDOWN | LINUX_PROT_GROWSUP))
+               return EINVAL;
+
+       map = &p->p_vmspace->vm_map;
+       vm_map_lock(map);
+       if (!uvm_map_lookup_entry(map, start, &entry) || entry->start > start) {
+               vm_map_unlock(map);
+               return ENOMEM;
+       }
+
+       /*
+        * Approximate the behaviour of PROT_GROWS{DOWN,UP}.
+        */
+
+       stacklim = (vaddr_t)p->p_p->ps_limit->pl_rlimit[RLIMIT_STACK].rlim_cur;
+       if (grows & LINUX_PROT_GROWSDOWN) {
+               if (USRSTACK - stacklim <= start && start < USRSTACK) {
+                       start = USRSTACK - stacklim;
+               } else {
+                       start = entry->start;
+               }
+       } else if (grows & LINUX_PROT_GROWSUP) {
+               if (USRSTACK <= end && end < USRSTACK + stacklim) {
+                       end = USRSTACK + stacklim;
+               } else {
+                       end = entry->end;
+               }
+       }
+       vm_map_unlock(map);
+       return uvm_map_protect(map, start, end, prot, FALSE);
+}
+
/*
  * This code is partly stolen from src/lib/libc/gen/times.c
  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
--- /usr/src/sys/compat/linux/linux_mmap.h.orig Wed Apr 17 05:23:56 1996
+++ /usr/src/sys/compat/linux/linux_mmap.h      Sat Nov 10 23:21:24 2007
@@ -38,6 +38,8 @@
#define LINUX_PROT_READ                0x01
#define LINUX_PROT_WRITE       0x02
#define LINUX_PROT_EXEC                0x04
+#define LINUX_PROT_GROWSDOWN   0x01000000
+#define LINUX_PROT_GROWSUP     0x02000000

#define LINUX_MAP_SHARED       0x0001
#define LINUX_MAP_PRIVATE      0x0002
--- /usr/src/sys/compat/linux/syscalls.master.orig      Mon Jun 18 20:33:13 2007
+++ /usr/src/sys/compat/linux/syscalls.master   Sat Nov 10 23:21:24 2007
@@ -223,7 +223,8 @@
123    STD             { int linux_sys_modify_ldt(void); }
#endif
124    STD             { int linux_sys_adjtimex(void); }
-125    NOARGS          { int sys_mprotect(caddr_t addr, int len, int prot); }
+125    STD             { int linux_sys_mprotect(const void *start, \
+                           unsigned long len, int prot); }
126    STD             { int linux_sys_sigprocmask(int how, \
                            linux_old_sigset_t *set, linux_old_sigset_t *oset);
}
127    STD             { int linux_sys_create_module(void); }
Go to the top of the page
 
+Quote Post
猫猫草
post 2009-08-19 15:58:41, Wed
Post #2


猫猫猫
***

Group: Power Cat
Posts: 626
Joined: 2006-12-8
Member No.: 2



方法2:

对每一个出问题的动态链接库文件,在 Linux 环境下执行

execstack -c <库文件名>

即可
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 2024-10-17 05:04