SlideShare une entreprise Scribd logo
1  sur  63
Télécharger pour lire hors ligne
Tcache in glibc
New mechanism of libc malloc

Angelboy
Outline
• New Structure

• Tcache

• Make Heap Exploitation Easy Again

• Weakness in tcache
Overview
• 增進記憶體管理理的效能

• 實作在 glibc 2.26 

• Ubuntu 17.10 之後
Outline
• New Structure

• Tcache

• Make Heap Exploitation Easy Again

• Weakness in tcache
New Structure
• tcache_entry

• 類似 fastbin 中的 chunk

• 在 freed 時,⽤用 linked list 串串起來來,指向的是 chunk data 的部分
New Structure
• tcache_perthread_struct

• ⼀一個 thread ⼀一個 tcache_perthread_struct 

• 在該 thread 第⼀一次 malloc 時初始化

• Count : 對應每個 tcache 中,chunk 的數量量
New Structure
• tcache_perthread_struct

• 根據⼤大⼩小分成多個不同的 tcache

• 只要是 smailbin 範圍⼤大⼩小的 chunk 都會使⽤用 tcache
Outline
• New Structure

• Tcache

• Make Heap Exploitation Easy Again

• Weakness in tcache
tcache
• 第⼀一次 malloc 時,會先 malloc ⼀一塊記憶體區塊,⽤用來來存放
tcache_perthread_struct

• 在之後 small bin chunk size 的 malloc 都會先以存放在 tcache 中的為
主,幾乎與 fastbin 相似

• 比較不同的事 fastbin 中的 fd 是指向 chunk 開頭位置

⽽而 tcache 中則是指向 user data 的部分,也就是 header 之後
tcache
• 當 free 掉⼀一塊 chunk 時,且 chunk ⼩小於 small bin size 時

• 以往來來說都會先放到 fastbin 或是 unsorted bin 中

• 在有 tcache 之後

• 會先放到相對應的 tcache 中,直到 tcache 被塞滿到 7 個

• 塞滿之後,其他的 chunk 就跟以前⼀一樣放到 fastbin 或是

unsorted bin中

• tcache 中的 chunk 不會合併(不取消 inused bit )
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
First time

malloc(0x60)
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
First time

malloc(0x60)
NULL
…
…
…
tcache count
User data
Heap
0x6030c0
header
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
free(0x6030c0)
NULL
…
…
…
tcache count
User data
Heap
0x6030c0
header
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
free(0x6030c0)
0x6030c0
…
…
…
… 1 ….
Heap
header
NULL (fd)
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
free(0x603130) (size:0x70)
0x6030c0
…
…
…
… 1 ….
Heap
header
NULL (fd)
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
free(0x6031a0) (size:0x70)
0x603130
…
…
…
… 2 ….
Heap
header
NULL (fd)
header
0x6030c0
0x6030b0
0x603120
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
free(xxx) (size:0x70) ……
0x6031a0
…
…
…
… 3 ….
Heap
header
NULL (fd)
header
0x6030c0
header
0x603130
0x6030b0
0x603120
0x603190
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
free(0x603cd0) (size:0x70)
0x6033360
…
…
…
… 7 ….
Heap
header
NULL (fd)
header
0x6030c0
header
0x6032e0
0x6030b0
0x603120
0x603350
……
x4
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
0x6033360
…
…
…
… 7 ….
Heap
header
NULL (fd)
header
0x6030b0
header
0x6032e0
0x6030b0
0x603110
0x603350
……
x4
header
NULL
free(0x603cd0) (size:0x70)
0x6033c0
tcache
• Malloc 時

• 優先從 tcache 取出,直到該 tcache 為空,才會從原本的 bin 開始找

• Tcache 為空時,如果 fastbin/smallbin/unsorted bin 有剛好 size 的
chunk 時,會先將該 fastbin/smallbin/unsroted bin 中的 chunk 填補⾄至
tcache 中,直到填滿為⽌止,再從 tcache 相對應的 tcache 中取出

• 所以在 bin 中,與在 tcache 中的順序會反過來來

• 但在 fastbin 中第⼀一個會先作為 victim 後⾯面的順序才是反的
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
0x6033360
…
…
…
… 7 ….
Heap
header
NULL (fd)
header
0x6030c0
header
0x6032e0
0x6030b0
0x603110
0x603350
……
x4
header
NULL
malloc(0x60)
0x603cc0
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
0x60332e0
…
…
…
… 6 ….
Heap
header
NULL (fd)
header
0x6030c0
header
0x603280
0x6030b0
0x603110
0x6032e0
……
x3
header
NULL
0x603cc0
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
0x60332e0
…
…
…
… 6 ….
Heap
header
NULL (fd)
header
0x6030c0
header
0x603270
0x6030b0
0x603110
0x6032e0
……
x3
header
NULL
malloc(0x60)…..x6
0x603cc0
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
NULL
Victim
malloc(0x60)
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
Return to user
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
0x603430
header
NULL
0x6033c0 0x603430
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
0x603430
malloc(0x60)
header
NULL
0x6033c0 0x603430
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
0x603430
malloc(0x60)
header
NULL
0x6033c0 0x603430
tcache 為空,但 fastbin 中還有,故從 fast bin 取出第⼀一個並填充
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x6033c0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
0x603430
malloc(0x60)
header
NULL
Victim = 0x6033c0 0x603430
tcache 為空,但 fastbin 中還有,故從 fast bin 取出第⼀一個並填充
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0x603430 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
NULL
malloc(0x60)
header
NULL
0x603430
只要還沒滿,且 bin 中還有,就會繼續填充
Victim = 0x6033c0
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x603440
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
malloc(0x60)
header
NULL
0x603430
Victim = 0x6033c0
tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x603440
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
header
NULL
0x603430
Victim = 0x6033c0
Return 0x6033d0 to user
Outline
• New Structure

• Tcache

• Make Heap Exploitation Easy Again

• Weakness in tcache
Weakness in tcache
• Malloc
Weakness in tcache
• Malloc

•沒有任何檢查
Weakness in tcache
• tcache corruption

• 利利⽤用任意 memory corruption 覆蓋 tcache 中的 next 

• 不需偽造任何 chunk 結構就可以拿到任意記憶體位置
tcache corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x603440
…
…
…
… 2 ….
Heap
header0x6030b0
header
NULL
Corrupt the linked list
header
0x6033c0
0x603430
0x6033c0
tcache corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x603440
…
…
…
… 2 ….
Heap
header0x6030b0
header
NULL
malloc(0x60)
header
0xdeadbeef
0x603430
0x6033c0
tcache corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0xdeadbedef
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
Return 0x603430 to user
header
0xdeadbeef
0x603430
0x6033c0
0xdeadbeef
tcache corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0xdeadbedef
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
malloc(0x60)
header
0xdeadbeef
0x603430
0x6033c0
0xdeadbeef
tcache corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0xdeadbedef
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
Return 0xdeadbeef to user
header
0xdeadbeef
0x603430
0x6033c0
0xdeadbeef
Weakness in tcache
• Free

• 在存入 tcache 之前會檢查

• size 的合法性

• size > MINSIZE && -size < p

• 位置是否有對⿑齊

• 必須對其 8 的倍數
Weakness in tcache
• Free

• Double free ? 

• Linked list 完整性?
Weakness in tcache
• Free

• Double free ? 

• Linked list 完整性?

•這些通通都沒有檢查
Overlap tcache
• 因為沒有檢查 double free 

• 所以我們可以不斷對同⼀一記憶體區塊 free 

• 我們可以利利⽤用此特性來來達成 fastbin attack 的效果,甚⾄至更更好⽤用,因為不
須符合 chunk 的結構

• 另外他也沒有檢查 tcache 中的 count
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
NULL
…
…
…
… 0 ….
Heap
header0x6030b0
header
NULL
free(0x6033d0)
0x6033c0
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
free(0x6033d0)
0x6033c0
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 1 ….
Heap
header0x6030b0
header
NULL
double free(0x6033d0)
0x6033c0
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 2 ….
Heap
header0x6030b0
header
0x6033d0
0x6033c0
malloc(0x60)
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 1 ….
Heap
header0x6030b0
header
0xdeadbeef
0x6033c0
Return 0x6033d0 to user
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 1 ….
Heap
header0x6030b0
header
0xdeadbeef
0x6033c0
malloc(0x60)
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 0 ….
Heap
header0x6030b0
header
0xdeadbeef
0x6033c0
Return 0x6033d0 to user
0xdeadbeef
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 0 ….
Heap
header0x6030b0
header
0xdeadbeef
0x6033c0
malloc(0x60)
0xdeadbeef
Overlap tcache
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 0 ….
Heap
header0x6030b0
header
0xdeadbeef
0x6033c0
malloc(0x60)
0xdeadbeef
Get 0xdeadbeef again
Small bin attack
• 在從 small bin 填充⾄至 tcache 時

• 需要對 small bin 做 unlink 

• 正常來來講,應該要對該 chunk 做 double linked list 的檢查

• 但是這邊卻沒檢查,所以可以做類似 unsorted bin attack 的效果

• 唯⼀一要注意的是 count 數要剛好滿,不然會導致無窮迴圈或者存取到
非法區段⽽而 segment fault
tcache perthread corruption
• 該結構掌管整個 tcache 機制

• 配合前⾯面漏洞洞複寫該結構

• 就可控制整個 tcache ,不管 malloc ⼤大⼩小多少,變成整個都是可控的

• 很多情況下只要利利⽤用前⾯面漏洞洞做 partial overwrite 就可以間接控制該結構
tcache perthread corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 2 ….
Heap
header0x6030b0
header
0x6033d0
0x6033c0
malloc(0x60)
tcache perthread corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 2 ….
Heap
header0x6030b0
header
0x603000
0x6033c0
Return 0x6033d0
tcache perthread corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 2 ….
Heap
header0x6030b0
header
0x603000
0x6033c0
malloc(0x60)
tcache perthread corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
…
…
…
… 2 ….
Heap
header0x6030b0
header
0x603000
0x6033c0
malloc(0x60)
0x603000
tcache perthread corruption
0x20size 0x30 0x40 …. 0x70 ….
fast bin
array
0. 0 0 …… 0 …..
0x6033d0
0x13371337
0xfacebooc
…
… 2 ….
Heap
header0x6030b0
header
0x603000
0x6033c0
malloc(0x200)
0xdeadbeef
Get 0xdeadbeef again
Reference
• http://tukan.farm/2017/07/08/tcache/
Q & A

Contenu connexe

Tendances

Linux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingLinux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingAngel Boy
 
Return to dlresolve
Return to dlresolveReturn to dlresolve
Return to dlresolveAngel Boy
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsoViller Hsiao
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)Angel Boy
 
MacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) ExploitationMacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) ExploitationAngel Boy
 
ROP 輕鬆談
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談hackstuff
 
TDOH x 台科 pwn課程
TDOH x 台科 pwn課程TDOH x 台科 pwn課程
TDOH x 台科 pwn課程Weber Tsai
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracingViller Hsiao
 
Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)Angel Boy
 
Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Angel Boy
 
Performance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedBrendan Gregg
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceBrendan Gregg
 
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytes
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytesWindows Kernel Exploitation : This Time Font hunt you down in 4 bytes
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytesPeter Hlavaty
 
Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)
Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)
Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)Takashi Takizawa
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Databricks
 
AVX-512(フォーマット)詳解
AVX-512(フォーマット)詳解AVX-512(フォーマット)詳解
AVX-512(フォーマット)詳解MITSUNARI Shigeo
 
10分で分かるLinuxブロックレイヤ
10分で分かるLinuxブロックレイヤ10分で分かるLinuxブロックレイヤ
10分で分かるLinuxブロックレイヤTakashi Hoshino
 
ELFの動的リンク
ELFの動的リンクELFの動的リンク
ELFの動的リンク7shi
 

Tendances (20)

Linux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend ProgramingLinux Binary Exploitation - Return-oritend Programing
Linux Binary Exploitation - Return-oritend Programing
 
Return to dlresolve
Return to dlresolveReturn to dlresolve
Return to dlresolve
 
twlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdsotwlkh-linux-vsyscall-and-vdso
twlkh-linux-vsyscall-and-vdso
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 
Execution
ExecutionExecution
Execution
 
MacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) ExploitationMacOS memory allocator (libmalloc) Exploitation
MacOS memory allocator (libmalloc) Exploitation
 
ROP 輕鬆談
ROP 輕鬆談ROP 輕鬆談
ROP 輕鬆談
 
TDOH x 台科 pwn課程
TDOH x 台科 pwn課程TDOH x 台科 pwn課程
TDOH x 台科 pwn課程
 
Linux kernel tracing
Linux kernel tracingLinux kernel tracing
Linux kernel tracing
 
Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)Windows 10 Nt Heap Exploitation (Chinese version)
Windows 10 Nt Heap Exploitation (Chinese version)
 
Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)
 
Performance Wins with BPF: Getting Started
Performance Wins with BPF: Getting StartedPerformance Wins with BPF: Getting Started
Performance Wins with BPF: Getting Started
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems Performance
 
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytes
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytesWindows Kernel Exploitation : This Time Font hunt you down in 4 bytes
Windows Kernel Exploitation : This Time Font hunt you down in 4 bytes
 
Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)
Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)
Unbound/NSD最新情報(OSC 2013 Tokyo/Spring)
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
 
AVX-512(フォーマット)詳解
AVX-512(フォーマット)詳解AVX-512(フォーマット)詳解
AVX-512(フォーマット)詳解
 
10分で分かるLinuxブロックレイヤ
10分で分かるLinuxブロックレイヤ10分で分かるLinuxブロックレイヤ
10分で分かるLinuxブロックレイヤ
 
Kernel Pool
Kernel PoolKernel Pool
Kernel Pool
 
ELFの動的リンク
ELFの動的リンクELFの動的リンク
ELFの動的リンク
 

Tcache Exploitation

  • 1. Tcache in glibc New mechanism of libc malloc
 Angelboy
  • 2. Outline • New Structure • Tcache • Make Heap Exploitation Easy Again • Weakness in tcache
  • 4. Outline • New Structure • Tcache • Make Heap Exploitation Easy Again • Weakness in tcache
  • 5. New Structure • tcache_entry • 類似 fastbin 中的 chunk • 在 freed 時,⽤用 linked list 串串起來來,指向的是 chunk data 的部分
  • 6. New Structure • tcache_perthread_struct • ⼀一個 thread ⼀一個 tcache_perthread_struct • 在該 thread 第⼀一次 malloc 時初始化 • Count : 對應每個 tcache 中,chunk 的數量量
  • 7. New Structure • tcache_perthread_struct • 根據⼤大⼩小分成多個不同的 tcache • 只要是 smailbin 範圍⼤大⼩小的 chunk 都會使⽤用 tcache
  • 8. Outline • New Structure • Tcache • Make Heap Exploitation Easy Again • Weakness in tcache
  • 9. tcache • 第⼀一次 malloc 時,會先 malloc ⼀一塊記憶體區塊,⽤用來來存放 tcache_perthread_struct • 在之後 small bin chunk size 的 malloc 都會先以存放在 tcache 中的為 主,幾乎與 fastbin 相似 • 比較不同的事 fastbin 中的 fd 是指向 chunk 開頭位置
 ⽽而 tcache 中則是指向 user data 的部分,也就是 header 之後
  • 10. tcache • 當 free 掉⼀一塊 chunk 時,且 chunk ⼩小於 small bin size 時 • 以往來來說都會先放到 fastbin 或是 unsorted bin 中 • 在有 tcache 之後 • 會先放到相對應的 tcache 中,直到 tcache 被塞滿到 7 個 • 塞滿之後,其他的 chunk 就跟以前⼀一樣放到 fastbin 或是
 unsorted bin中 • tcache 中的 chunk 不會合併(不取消 inused bit )
  • 11. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. First time
 malloc(0x60)
  • 12. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. First time
 malloc(0x60) NULL … … … tcache count User data Heap 0x6030c0 header
  • 13. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. free(0x6030c0) NULL … … … tcache count User data Heap 0x6030c0 header
  • 14. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. free(0x6030c0) 0x6030c0 … … … … 1 …. Heap header NULL (fd)
  • 15. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. free(0x603130) (size:0x70) 0x6030c0 … … … … 1 …. Heap header NULL (fd)
  • 16. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. free(0x6031a0) (size:0x70) 0x603130 … … … … 2 …. Heap header NULL (fd) header 0x6030c0 0x6030b0 0x603120
  • 17. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. free(xxx) (size:0x70) …… 0x6031a0 … … … … 3 …. Heap header NULL (fd) header 0x6030c0 header 0x603130 0x6030b0 0x603120 0x603190
  • 18. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. free(0x603cd0) (size:0x70) 0x6033360 … … … … 7 …. Heap header NULL (fd) header 0x6030c0 header 0x6032e0 0x6030b0 0x603120 0x603350 …… x4
  • 19. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. 0x6033360 … … … … 7 …. Heap header NULL (fd) header 0x6030b0 header 0x6032e0 0x6030b0 0x603110 0x603350 …… x4 header NULL free(0x603cd0) (size:0x70) 0x6033c0
  • 20. tcache • Malloc 時 • 優先從 tcache 取出,直到該 tcache 為空,才會從原本的 bin 開始找 • Tcache 為空時,如果 fastbin/smallbin/unsorted bin 有剛好 size 的 chunk 時,會先將該 fastbin/smallbin/unsroted bin 中的 chunk 填補⾄至 tcache 中,直到填滿為⽌止,再從 tcache 相對應的 tcache 中取出 • 所以在 bin 中,與在 tcache 中的順序會反過來來 • 但在 fastbin 中第⼀一個會先作為 victim 後⾯面的順序才是反的
  • 21. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. 0x6033360 … … … … 7 …. Heap header NULL (fd) header 0x6030c0 header 0x6032e0 0x6030b0 0x603110 0x603350 …… x4 header NULL malloc(0x60) 0x603cc0
  • 22. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. 0x60332e0 … … … … 6 …. Heap header NULL (fd) header 0x6030c0 header 0x603280 0x6030b0 0x603110 0x6032e0 …… x3 header NULL 0x603cc0
  • 23. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. 0x60332e0 … … … … 6 …. Heap header NULL (fd) header 0x6030c0 header 0x603270 0x6030b0 0x603110 0x6032e0 …… x3 header NULL malloc(0x60)…..x6 0x603cc0
  • 24. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. NULL … … … … 0 …. Heap header0x6030b0 header NULL Victim malloc(0x60)
  • 25. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. NULL … … … … 0 …. Heap header0x6030b0 header Return to user
  • 26. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. NULL … … … … 0 …. Heap header0x6030b0 header 0x603430 header NULL 0x6033c0 0x603430
  • 27. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. NULL … … … … 0 …. Heap header0x6030b0 header 0x603430 malloc(0x60) header NULL 0x6033c0 0x603430
  • 28. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. NULL … … … … 0 …. Heap header0x6030b0 header 0x603430 malloc(0x60) header NULL 0x6033c0 0x603430 tcache 為空,但 fastbin 中還有,故從 fast bin 取出第⼀一個並填充
  • 29. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x6033c0 ….. NULL … … … … 0 …. Heap header0x6030b0 header 0x603430 malloc(0x60) header NULL Victim = 0x6033c0 0x603430 tcache 為空,但 fastbin 中還有,故從 fast bin 取出第⼀一個並填充
  • 30. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0x603430 ….. NULL … … … … 0 …. Heap header0x6030b0 header NULL malloc(0x60) header NULL 0x603430 只要還沒滿,且 bin 中還有,就會繼續填充 Victim = 0x6033c0
  • 31. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x603440 … … … … 1 …. Heap header0x6030b0 header NULL malloc(0x60) header NULL 0x603430 Victim = 0x6033c0
  • 32. tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x603440 … … … … 1 …. Heap header0x6030b0 header NULL header NULL 0x603430 Victim = 0x6033c0 Return 0x6033d0 to user
  • 33. Outline • New Structure • Tcache • Make Heap Exploitation Easy Again • Weakness in tcache
  • 35. Weakness in tcache • Malloc •沒有任何檢查
  • 36. Weakness in tcache • tcache corruption • 利利⽤用任意 memory corruption 覆蓋 tcache 中的 next • 不需偽造任何 chunk 結構就可以拿到任意記憶體位置
  • 37. tcache corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x603440 … … … … 2 …. Heap header0x6030b0 header NULL Corrupt the linked list header 0x6033c0 0x603430 0x6033c0
  • 38. tcache corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x603440 … … … … 2 …. Heap header0x6030b0 header NULL malloc(0x60) header 0xdeadbeef 0x603430 0x6033c0
  • 39. tcache corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0xdeadbedef … … … … 1 …. Heap header0x6030b0 header NULL Return 0x603430 to user header 0xdeadbeef 0x603430 0x6033c0 0xdeadbeef
  • 40. tcache corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0xdeadbedef … … … … 1 …. Heap header0x6030b0 header NULL malloc(0x60) header 0xdeadbeef 0x603430 0x6033c0 0xdeadbeef
  • 41. tcache corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0xdeadbedef … … … … 1 …. Heap header0x6030b0 header NULL Return 0xdeadbeef to user header 0xdeadbeef 0x603430 0x6033c0 0xdeadbeef
  • 42. Weakness in tcache • Free • 在存入 tcache 之前會檢查 • size 的合法性 • size > MINSIZE && -size < p • 位置是否有對⿑齊 • 必須對其 8 的倍數
  • 43. Weakness in tcache • Free • Double free ? • Linked list 完整性?
  • 44. Weakness in tcache • Free • Double free ? • Linked list 完整性? •這些通通都沒有檢查
  • 45. Overlap tcache • 因為沒有檢查 double free • 所以我們可以不斷對同⼀一記憶體區塊 free • 我們可以利利⽤用此特性來來達成 fastbin attack 的效果,甚⾄至更更好⽤用,因為不 須符合 chunk 的結構 • 另外他也沒有檢查 tcache 中的 count
  • 46. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. NULL … … … … 0 …. Heap header0x6030b0 header NULL free(0x6033d0) 0x6033c0
  • 47. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 1 …. Heap header0x6030b0 header NULL free(0x6033d0) 0x6033c0
  • 48. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 1 …. Heap header0x6030b0 header NULL double free(0x6033d0) 0x6033c0
  • 49. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 2 …. Heap header0x6030b0 header 0x6033d0 0x6033c0 malloc(0x60)
  • 50. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 1 …. Heap header0x6030b0 header 0xdeadbeef 0x6033c0 Return 0x6033d0 to user
  • 51. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 1 …. Heap header0x6030b0 header 0xdeadbeef 0x6033c0 malloc(0x60)
  • 52. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 0 …. Heap header0x6030b0 header 0xdeadbeef 0x6033c0 Return 0x6033d0 to user 0xdeadbeef
  • 53. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 0 …. Heap header0x6030b0 header 0xdeadbeef 0x6033c0 malloc(0x60) 0xdeadbeef
  • 54. Overlap tcache 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 0 …. Heap header0x6030b0 header 0xdeadbeef 0x6033c0 malloc(0x60) 0xdeadbeef Get 0xdeadbeef again
  • 55. Small bin attack • 在從 small bin 填充⾄至 tcache 時 • 需要對 small bin 做 unlink • 正常來來講,應該要對該 chunk 做 double linked list 的檢查 • 但是這邊卻沒檢查,所以可以做類似 unsorted bin attack 的效果 • 唯⼀一要注意的是 count 數要剛好滿,不然會導致無窮迴圈或者存取到 非法區段⽽而 segment fault
  • 56. tcache perthread corruption • 該結構掌管整個 tcache 機制 • 配合前⾯面漏洞洞複寫該結構 • 就可控制整個 tcache ,不管 malloc ⼤大⼩小多少,變成整個都是可控的 • 很多情況下只要利利⽤用前⾯面漏洞洞做 partial overwrite 就可以間接控制該結構
  • 57. tcache perthread corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 2 …. Heap header0x6030b0 header 0x6033d0 0x6033c0 malloc(0x60)
  • 58. tcache perthread corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 2 …. Heap header0x6030b0 header 0x603000 0x6033c0 Return 0x6033d0
  • 59. tcache perthread corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 2 …. Heap header0x6030b0 header 0x603000 0x6033c0 malloc(0x60)
  • 60. tcache perthread corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 … … … … 2 …. Heap header0x6030b0 header 0x603000 0x6033c0 malloc(0x60) 0x603000
  • 61. tcache perthread corruption 0x20size 0x30 0x40 …. 0x70 …. fast bin array 0. 0 0 …… 0 ….. 0x6033d0 0x13371337 0xfacebooc … … 2 …. Heap header0x6030b0 header 0x603000 0x6033c0 malloc(0x200) 0xdeadbeef Get 0xdeadbeef again