Moving a stack to a specific location

How to transfer my stack to a specific virtual address? For example, I would like my stack to be 40960 in size and start at 0x355480. I tried playing with setcontext, but I would like to know if there is a โ€œstandardโ€ way to do this:

ucontext_t cont; bool flag = false; getcontext (&cont); if(!flag){ void* a = mmap((void*)0x34B000, 81920, PROT_EXEC | PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); cont.uc_mcontext.gregs[REG_ESP] = 0x355000; flag = true; setcontext(&cont); } 
+5
source share
1 answer

The code below sets the stack pointer around mmap, which we did earlier, and allows us to work with it in the main2 function:

 bool flag = false; int argc2; char ** argv2; int main2(){ ... } int main(int argc, char ** argv){ argc2 = argc; argv2 = argv; ucontext_t cont; getcontext (&cont); if(!flag){ void* a = mmap((void*)0x34B000, 81920, PROT_EXEC | PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); if(a == MAP_FAILED){ printf("mmapfail"); return 1; } cont.uc_mcontext.gregs[REG_ESP] = 0x355000; flag = true; setcontext(&cont); } else{ exit(main2()); } } 
-2
source

Source: https://habr.com/ru/post/1247167/


All Articles