Execute code in process's stack, on recent Linux

馋奶兔 提交于 2019-12-10 15:47:56

问题


I want to use ptrace to write a piece of binary code in a running process's stack. However, this causes segmentation fault (signal 11).

I can make sure the %eip register stores the pointer to the first instruction that I want to execute in the stack. I guess there is some mechanism that linux protects the stack data to be executable.

So, does anyone know how to disable such protection for stack. Specifically, I'm trying Fedora 15.

Thanks a lot!


After reading all replies, I tried execstack, which really makes code in stack executable. Thank you all!


回答1:


This is probably due to the NX bit on modern processors. You may be able to disable this for your program using execstack.

http://advosys.ca/viewpoints/2009/07/disabling-the-nx-bit-for-specific-apps/

http://linux.die.net/man/8/execstack




回答2:


As already mentioned it is due to the NX bit. But it is possible. I know for sure that gcc uses it itself for trampolines (which are a workaround to make e.g. function pointers of nested functions). I dont looked at the detailes, but I would recommend a look at the gcc code. Search in the sources for the architecture specific macro TARGET_ASM_TRAMPOLINE_TEMPLATE, there you should see how they do it.

EDIT: A quick google for that macro, gave me the hint: mprotect is used to change the permissions of the memory page. Also be carefull when you generate date and execute it - you maybe have in addition to flush the instruction cache.



来源:https://stackoverflow.com/questions/6482759/execute-code-in-processs-stack-on-recent-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!