Access memory under the stack on linux

This program accesses the memory below the stack.

I would suggest getting segfault or just nulwhen exiting stack restrictions, but I can see the actual data. (It is assumed that 100kb below the stack pointer is outside the boundaries of the stack)

Or does the system really allow me to see the memory below the stack? Shouldn't there be kernel level protection from this, or does this only apply to allocated memory?

Edit: With a 1024*127lower char pointer, it randomly executes segfaults or starts, so the stack is not fixed 8 MB, and it seems to be a little coincidental too.

#include <stdio.h>

int main(){
  char * x;
  int a;
  for( x = (char *)&x-1024*127; x<(char *)(&x+1); x++){
    a = *x & 0xFF;
    printf("%p = 0x%02x\n",x,a);
  }
}

: . segfaults 1024*127, f , segfault, (All 0x00):

#include <stdio.h>

int main(){
  char * x;
  int a;
  for( x = (char *)(&x); x>(char *)&x-1024*1024; x--){
    a = *x & 0xFF;
    printf("%p = 0x%02x\n",x,a);
  }
}
+4
4

.

( 4 x86). : . . :

  • , . ( ).

  • , . , . , (, ), , , " X Y".

  • (.. ).

  • (, ). .

, 4 , 4 , 1048576 . ; . (.. main()), , :

  • . .

  • (.. ). .

  • , .

  • .

4 . , , /proc/ (pid)/maps, . (a) (b) (ASLR), , .

, :

*(unsigned char *)0x12345678

, , , , . , SIGSEGV . ( , JIT), , . , - ASLR, , .

+3

, , , , . - , .

. I. e., , , .. , . , , , .

, . , , , - . /.


, undefined. , &x-1024*127 x, undefined.

+1

undefined C. , , , . , ; , segfault . promises .

, undefined, . , C , . , , , , .

. ( , , .)

, , ( , ...).

+1

This behavior is undefined in C. You gain access to an arbitrary memory address, which, depending on the platform, may or may not be on the stack. It may or may not be in memory, which this user can access; if not, you will get segfault or the like. Absolutely no promises anyway.

Do not do that. (If you are one of five or six people who have legal grounds for this, you already know this and do not need our advice.)

0
source

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


All Articles