/proc/self/maps ( , @AndrewHenle, pmap):
char* get_stack_bounds() {
FILE* maps = fopen("/proc/self/maps", "r");
static char line[256];
while(!feof(maps)) {
fgets(line, 255, maps);
if(strstr(line, "[stack]")) {
char* space = strchr(line, ' ');
*space = '\0';
fclose(maps);
return line;
}
}
fclose(maps);
return NULL;
}
unsigned long get_stack_right() {
char* bounds = get_stack_bounds();
bounds = strchr(bounds, '-') + 1;
return strtol(bounds, NULL, 16);
}
main():
printf("&result: %p delta: %ld\n", &result,
get_stack_right() - ((unsigned long) &result));
:
> ./a.out 104747
&result: 0x7fff3347c7f8 delta: 6152
0
> ./a.out 174580
&result: 0x7fffe43c9b38 delta: 5320
0
> ./a.out 174580
&result: 0x7fff26ad2b28 delta: 9432
Segmentation fault (core dumped)
> ./a.out 174580
&result: 0x7fff145aa5a8 delta: 6744
0
> ./a.out 174580
&result: 0x7fff74fff0b8 delta: 12104
Segmentation fault (core dumped)
, delta ( result ) .
, main() , , _start() crt1.o( - ), .
.
fs/binfmt_elf_fdpic.c :
sp = arch_align_stack(bprm->p);
arch_align_stack() x86:
unsigned long arch_align_stack(unsigned long sp)
{
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
sp -= get_random_int() % 8192;
return sp & ~0xf;
}