Search K
Appearance
Appearance
Other ways to support HackTricks:
When you free memory in a program using glibc, different "bins" are used to manage the memory chunks. Here's a simplified explanation of two common scenarios: unsorted bins and fastbins.
When you free a memory chunk that's not a fast chunk, it goes to the unsorted bin. This bin acts like a list where new freed chunks are added to the front (the "head"). When you request a new chunk of memory, the allocator looks at the unsorted bin from the back (the "tail") to find a chunk that's big enough. If a chunk from the unsorted bin is bigger than what you need, it gets split, with the front part being returned and the remaining part staying in the bin.
Example:
a
), then 250 bytes (b
), the free a
and request again 250 bytes (c
).a
, it goes to the unsorted bin.a
at the tail and splits it, returning the part that fits your request and keeping the rest in the bin. c
will be pointing to the previous a
and filled with the a's
.char *a = malloc(300);
char *b = malloc(250);
free(a);
char *c = malloc(250);
Fastbins are used for small memory chunks. Unlike unsorted bins, fastbins add new chunks to the head, creating a last-in-first-out (LIFO) behavior. If you request a small chunk of memory, the allocator will pull from the fastbin's head.
Example:
a
, b
, c
, d
).char *a = malloc(20);
char *b = malloc(20);
char *c = malloc(20);
char *d = malloc(20);
free(a);
free(b);
free(c);
free(d);
a = malloc(20); // d
b = malloc(20); // c
c = malloc(20); // b
d = malloc(20); // a