Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Instructions From The Heap

Tags:

c++

c

Can I allocate a block on the heap, set its bytes to values that correspond to a function call and its parameters, then use the function call and dereference operators to execute that sequence?

like image 587
David Avatar asked Jul 20 '26 04:07

David


1 Answers

So if I read you right you want to dynamically create CPU assembly instructions on the heap and execute them. A bit like self-modifying code. In theory that's possible, but in practice maybe not.

The problem is that the heap is in a data segment, and CPU's/operating systems nowadays have measures to prevent exactly this kind of behavior (it's called the NX bit, or No-eXecute bit for x86 CPUs). If a segement is marked as NX, you can't execute code from it. This was invented to stop computer virusses from using buffer overflows to place exectuable code in data/heap/stack memory and then try the calling program to execute such code.

Note that DLL's and libraries are loaded in the code segment, which of course allows code execution.

like image 142
JvO Avatar answered Jul 21 '26 19:07

JvO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!