I have an exercise that asks of me to produce a seg.fault. In my understanding i can do that by overflowing the buffer. So all i need to do is provide an input(Name) bigger than a certain size(covering the return address). So if buf,i and c hold 52 Bytes and ebp 4,then the return address should be after 56 bytes. So if i give an input bigger than 56, it should produce a seg.fault. Is my thinking correct ? I tried with those numbers but it still runs and exit correctly.(UNIX-32bit)
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#define BUFSIZE 44
char grade = '3';
char Name[BUFSIZE];
void readString(char *s) {
char buf[BUFSIZE];
int i = 0;
int c;
while (1) {
c = fgetc(stdin);
if ((c == EOF) || (c == '\n'))
break;
buf[i++] = c;
}
buf[i] = 0;
for (i = 0; i < BUFSIZE; i++)
s[i] = buf[i];
return;
}
int main(void) {
mprotect((void*)((unsigned int)Name & 0xfffff000), 1,
PROT_READ | PROT_WRITE | PROT_EXEC);
printf("What is your name?\n");
readString(Name);
exit(0)
}
int ohno = *((int*)NULL); printf("%d", ohno);