Where does the stack for each program begin in memory?

Where does the stack of each program begin in memory?

I understand that there is a random address space definition that will randomly choose an address. If the option is disabled, does each program run from the same address?

What if we open two terminals and simultaneously launch two programs; will the system use the same starting address for the stacks of the two programs (by overwriting the previous program stack and loading the current program stack in the same place during a context switch)?

What if I run the program by calling the exec()

-family function like in the following example; will there be a different stack for this program and a different stack for the "vulnerable" program? Or will there just be another vulnerable stack on top of the caller's stack?

int main(int argc, char *argv[]) {
  char *buff, *ptr;
  int i;
  bsize  = atoi(argv[1]);

  if (!(buff = malloc(bsize))) {
    printf("Can't allocate memory.\n");
    exit(0);
  }
  for (i = 0; i < bsize; i+=4)
    buff[i] = '0';

  execl("/home/amulya/Desktop/CMPE209/HWs/HW2/vulnerable","vulnerable", buff, NULL);
  return(-1);
}

      

+2


source to share


2 answers


You need to learn about Virtual Memory . Yes, it is likely that if the OS does not have Address Space Layout Randomization (ASLR), all programs will have their own stack at the same VIRTUAL address. but this does not mean that the OS has to move the previous program stack just to switch the context to another program, because through virtual memory you just need to make sure that both programs have the same base VIRTUAL address, but each virtual address can have a different PHYSICAL location. (this full paragraph is completely OS dependent)



As for your second question, execve replaces the currently running program with the program to be executed, this includes replacing the current text and data segments as well as the stack, so the executed program will not see the previous program stack.

+5


source


The answer really depends on the OS and arch used. Appearsin which you are using the * nix variant and the odds are Linux.

For Linux, before randomization became standard, the default was just short where the kernel space started. On my x86 system the area used for the stack (ASLR disabled by default):bffea000 - c0000000

NOTE. the value I have provided is not always accurate for all systems, but what is for my system.

On modern Linux systems, the stack will have a fairly random address. You can check this by doing this several times in a row:



cat /proc/self/maps | grep "\[stack\]"

      

If the option is disabled, I expect all program stacks by default to be in the same location (end of user space).

Running a program with exec

replaces your address space with a new program; this will include the stack, so it will end up in the same place as any other program. Think about it: your shell program has to do fork/exec

to run the program exactly the same way your program would do ...

+2


source







All Articles