I am trying to write an example program to understand stack buffer overflow and i have the following program.
overflow.s:
.section .data
.section .text
.globl _start
_start:
call sum
movl %eax, %ebx
movl $15, %ebx
movl $1, %eax
int $0x80
.type sum, @function
sum:
pushl %ebp # save the current base pointer
movl %esp, %ebp # store current stack pointer to %ebp
subl $4, %esp # inc the stack pointer by 4 bytes for local variable
movl $5, -8(%ebp) # store value 5 from 8 bytes of %ebp 4 bytes beyond stack pointer
addl $5, -8(%ebp) # add 5 to the value store beyond of stack pointer
movl -8(%ebp), %eax # store the value in %eax
movl %ebp, %esp
popl %ebp
ret
assemble and link the program:
as -gstabs+ overflow.s -o oveflow.o
ld overflow.o -o overflow
./overflow
echo $?
15 <============= the result
I expected either i get some garbage or segfault. but it seems to work as expected. So in the sum function when i increment the stack pointer by 4 bytes and when i storing the value 5 8 bytes from base pointer, I was expecting this is a simulation of overflow. Is the above program wrong to be used an example of stack buffer overflow. ?