Hello maintainers!

Using tar and building it with the UBSAN sanitizer I have found a
runtime error in the extract_file() function. Running tar with the -xv
and --to-stdout arguments and passing a file with specific data
to stdin, an out-of-bounds error occurs.

The problem is the same as previously reported here:
https://lists.gnu.org/r/bug-tar/2025-06/msg00002.html

In short, we offset buffer[] field of the block union by a larger
number of bytes, than defined to buffer[] array in union block - 512.


Here is the stacktrace:

extract.c:1384:30: runtime error: index 1024 out of bounds for type 'char[512]'
#0 0x6433c12bf2b5 in extract_file /tar-test/src/extract.c:1384:30
#1 0x6433c12bb636 in extract_archive /tar-test/src/extract.c:1895:11
#2 0x6433c12dcd87 in read_and /tar-test/src/list.c:233:6
#3 0x6433c13195e4 in main /tar-test/src/tar.c:2894:7
#4 0x753990e53249 in __libc_start_call_main
csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#5 0x753990e53304 in __libc_start_main csu/../csu/libc-start.c:360:3
#6 0x6433c1264ac0 in _start (/tar-test/src/tar+0x62ac0)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior extract.c:1384:30


Since the buffer[] in the block union is the first field with char* type,
we can offset the pointer to the union rather than the members.

This diff fixes the error for me:

diff --git a/src/extract.c b/src/extract.c
index 5d89458a..8709ef79 100644
--- a/src/extract.c
+++ b/src/extract.c
@@ -1381,7 +1381,7 @@ extract_file (char *file_name, char typeflag)
size -= written;
set_next_block_after ((union block *)
- (data_block->buffer + written - 1));
+ ((char *)data_block + written - 1));
if (count != written)
{
if (!to_command_option)


Steps to reproduce:

1. Build project with UBSAN sanitizer
2. Create a file, that contains information, like this:
echo -ne 
"1\x2etar\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000644\x000000000\x000000000\x0000000024000\x0014625076416\x00010433\x0000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00ustar\x20\x20\x00root\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00root\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
> crash

1. Run tar as follows:

cat crash | ./src/tar -xv --to-stdout

4. Get an ubsan error

Reply via email to