Excuse the ads! We need some help to keep our site up.
UAF uses previously freed memory, which can cause a variety of negative consequences, from corrupting valid data to executing arbitrary code.
For example, if the program allocates two memories, frees the first memory, and then requests a similar amount of memory allocation, the previously freed memory is allocated.
And a has a pointer to the first allocated memory, and c also has a pointer to the same memory.
In other words, data can be changed and printed using the pointer of a.
This causes undefined behavior in the process.
- Saved the data after allocating memory as follows:
The memory is released, but the data stored in the memory is not initialized.
Therefore, Can be output the data in the memory pointed to by an after freeing the memory.
- The following code asks malloc () to allocate memory of 160 bytes and 256 bytes in size.
- The returned pointer is stored in "a" and "b".
- Free the first memory, request an allocation of 144byte memory, and the returned pointer is stored in "c".
- Copies a string into the corresponding memory and prints out the data in the memory pointed to by "a".
- Check the address of the memory stored in "a" at 0x4005c8, and confirm the release of the memory at 0x4005e6.
- Checks the address of the memory stored in "c" at 0x4005f0 and checks the data stored in that memory at 0x400616.
- Check for UAF at 0x40061d.
When you ask malloc() for memory allocation of size 160 bytes, the allocator returns 0x602010, whose address is stored in "a".
The memory is registered in the Unsorted bin if it is freed after allocating a different size of memory.
- If request malloc() for memory allocation of 144 bytes in size, the memory that was registered in the Unsorted bin is reallocated.
A request was made to allocate a memory whose size differs from the amount of memory placed in the unsorted bin, but the memory registered in the unsorted bin is returned.
In order for the allocator to use memory efficiently, it will preferentially use that memory if the size of the requested memory is less than or equal to the size of the memory placed in the Unsorted bin.
If the memory placed in the unsorted bin is large, the allocator allocates memory as much as the requested memory, and places the remaining memory in main_arena.last_remainder.
In this example, the memory placed in the unsorted bin is larger than the newly requested memory, so the memory of the unsorted bin is used to return the memory (0x602010).
After allocating the newly requested memory, the remaining size of memory is 16 bytes.
In this case, malloc will not be able to reuse the remaining memory, so it will return the amount of memory placed in the Unsorted bin (176) without partitioning the memory.
- The address of this allocated memory is stored in variable "c".
- The address is the same address stored in variable "a".
- Enter the string "Secret message" in the reallocated area.
- The data stored in "a"(0x602010) is output.
- If you request the output of the data stored in the variable "a", the string "Secret message" is output.
- The reason why the data is displayed is that the memory pointed to by the variable "a" is released but the value stored in the variable "a" is not initialized.
- That is, the variable "a" still has the address of the first allocated memory (0x602010).
- This allows you to check the data stored in variable "c" via variable "a".
- This example uses UAF-2.c.
- Check the allocated pointer at 0x4005c8 and check the data in the freed memory at 0x4005f5.
- Check the data output at 0x400601.
The address of allocated memory is 0x602010, and the memory is released after saving the data.
- The stored data is not initialized even after the memory is released.
- The variable "a" has an address that points to the freed memory and can output the data of that area.