HeapOverflow
HeapOverflow
Introduction to Heap
&
Heap - Overflow
• The heap is not managed automatically for you and is not as tightly managed by the CPU. It is more
like a free-floating region of memory.
2
Module Code | Module Name | Lecture Title | Lecturer
Stack vs Heap
In a stack, the allocation and deallocation are automatically done by whereas, in heap, it
needs to be done by the programmer manually.
Stack accesses local variables only while Heap allows you to access variables globally.
Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any
random order.
2
Module Code | Module Name | Lecture Title | Lecturer
Dynamic Memory Allocation in C
malloc() - The name "malloc" stands for memory allocation.
The malloc() function reserves a block of memory of the specified number of bytes. And it returns
a pointer of void which can be casted into pointers of any form.
Syntax of malloc()
ptr = (castType*) malloc(size);
Ex:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And the pointer ptr
holds the address of the first byte in the allocated memory.
2
Module Code | Module Name | Lecture Title | Lecturer
Calloc()
The malloc() function allocates memory and leaves the memory uninitialized, whereas the calloc()
function allocates memory and initializes all bits to zero.
Syntax of calloc()
ptr = (castType*) calloc(n, size);
Ex:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of the float.
2
Module Code | Module Name | Lecture Title | Lecturer
free()
“free” method in C is used to dynamically de-allocate the memory.
Syntax
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
realloc()
If the dynamically allocated memory is insufficient or more than required, you can change the
size of previously allocated memory using the realloc() function.
Syntax
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
2
Module Code | Module Name | Lecture Title | Lecturer
Heap Allocation
Any heap allocation and reallocation requires raw byte counter and returns a pointer the
beginning of the piece of memory requested.
2
Module Code | Module Name | Lecture Title | Lecturer
mmap() and brk()
❑ mmap - Mmap() creates a new mapping in the virtual address space of the calling process.
❑ brk - The brk() and sbrk() functions are used to change dynamically the amount of space
allocated for the calling process's data segment. The change is made by resetting the
process's break value and allocating the appropriate amount of space.
2
Module Code | Module Name | Lecture Title | Lecturer
mmap() and brk()
2
Module Code | Module Name | Lecture Title | Lecturer
Heap and Stack
In gdb, the "info proc map" command shows how memory is used
2
Module Code | Module Name | Lecture Title | Lecturer
In gdb, “Info files” command list all the sections and their
addresses.
2
Module Code | Module Name | Lecture Title | Lecturer
A Simple Example
fp point to nowinner()
2
Module Code | Module Name | Lecture Title | Lecturer
2
Module Code | Module Name | Lecture Title | Lecturer
2
Module Code | Module Name | Lecture Title | Lecturer
Targeted Exploit
2
Module Code | Module Name | Lecture Title | Lecturer