unlink
Reading time: 3 minutes
tip
Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE)
Soutenir HackTricks
- Vérifiez les plans d'abonnement !
- Rejoignez le 💬 groupe Discord ou le groupe telegram ou suivez nous sur Twitter 🐦 @hacktricks_live.
- Partagez des astuces de hacking en soumettant des PRs au HackTricks et HackTricks Cloud dépôts github.
Code
c
// From https://github.com/bminor/glibc/blob/master/malloc/malloc.c
/* Take a chunk off a bin list. */
static void
unlink_chunk (mstate av, mchunkptr p)
{
if (chunksize (p) != prev_size (next_chunk (p)))
malloc_printerr ("corrupted size vs. prev_size");
mchunkptr fd = p->fd;
mchunkptr bk = p->bk;
if (__builtin_expect (fd->bk != p || bk->fd != p, 0))
malloc_printerr ("corrupted double-linked list");
fd->bk = bk;
bk->fd = fd;
if (!in_smallbin_range (chunksize_nomask (p)) && p->fd_nextsize != NULL)
{
if (p->fd_nextsize->bk_nextsize != p
|| p->bk_nextsize->fd_nextsize != p)
malloc_printerr ("corrupted double-linked list (not small)");
// Added: If the FD is not in the nextsize list
if (fd->fd_nextsize == NULL)
{
if (p->fd_nextsize == p)
fd->fd_nextsize = fd->bk_nextsize = fd;
else
// Link the nexsize list in when removing the new chunk
{
fd->fd_nextsize = p->fd_nextsize;
fd->bk_nextsize = p->bk_nextsize;
p->fd_nextsize->bk_nextsize = fd;
p->bk_nextsize->fd_nextsize = fd;
}
}
else
{
p->fd_nextsize->bk_nextsize = p->bk_nextsize;
p->bk_nextsize->fd_nextsize = p->fd_nextsize;
}
}
}
Explication Graphique
Vérifiez cette excellente explication graphique du processus unlink :
 (1) (1) (1) (1) (1).png)
https://ctf-wiki.mahaloz.re/pwn/linux/glibc-heap/implementation/figure/unlink_smallbin_intro.png
Vérifications de Sécurité
- Vérifiez si la taille indiquée du chunk est la même que le prev_size indiqué dans le chunk suivant
- Vérifiez également que
P->fd->bk == P
etP->bk->fw == P
- Si le chunk n'est pas petit, vérifiez que
P->fd_nextsize->bk_nextsize == P
etP->bk_nextsize->fd_nextsize == P
Fuites
Un chunk non lié ne nettoie pas les adresses allouées, donc avoir accès à rad, il est possible de fuir certaines adresses intéressantes :
Fuites Libc :
- Si P est situé au début de la liste doublement chaînée,
bk
pointera versmalloc_state
dans libc - Si P est situé à la fin de la liste doublement chaînée,
fd
pointera versmalloc_state
dans libc - Lorsque la liste doublement chaînée ne contient qu'un seul chunk libre, P est dans la liste doublement chaînée, et à la fois
fd
etbk
peuvent fuir l'adresse à l'intérieur demalloc_state
.
Fuites de Heap :
- Si P est situé au début de la liste doublement chaînée,
fd
pointera vers un chunk disponible dans le heap - Si P est situé à la fin de la liste doublement chaînée,
bk
pointera vers un chunk disponible dans le heap - Si P est dans la liste doublement chaînée, à la fois
fd
etbk
pointeront vers un chunk disponible dans le heap
tip
Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE)
Soutenir HackTricks
- Vérifiez les plans d'abonnement !
- Rejoignez le 💬 groupe Discord ou le groupe telegram ou suivez nous sur Twitter 🐦 @hacktricks_live.
- Partagez des astuces de hacking en soumettant des PRs au HackTricks et HackTricks Cloud dépôts github.