From 652b4aeccfc85bd605acd6f9f25ce48b8c2096c0 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 23 Nov 2026 08:20:56 +0000 Subject: [PATCH 01/17] Ticket #5227: tests: add tarfs tests for pax sparse members Add a pax sparse file in the formats 0.0, 0.1 and 1.0 between two plain ustar files, the same as bsdtar writes them, and check that all members are listed, and that the sparse file is expanded correctly. For 1.0, also check a chunk that doesn't fill its last block. These tests fail until the fixes that follow are applied. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- configure.ac | 1 + tests/src/vfs/Makefile.am | 4 + tests/src/vfs/tar/Makefile.am | 19 ++ tests/src/vfs/tar/tar_open_archive.c | 285 +++++++++++++++++++++++++++ 4 files changed, 309 insertions(+) create mode 100644 tests/src/vfs/tar/Makefile.am create mode 100644 tests/src/vfs/tar/tar_open_archive.c diff --git a/configure.ac b/configure.ac index 3580ae80d..20cfeb1c0 100644 --- a/configure.ac +++ b/configure.ac @@ -712,6 +712,7 @@ tests/src/vfs/extfs/helpers-list/data/config.sh tests/src/vfs/extfs/helpers-list/misc/Makefile tests/src/vfs/ftpfs/Makefile tests/src/vfs/local/Makefile +tests/src/vfs/tar/Makefile ]) AC_OUTPUT diff --git a/tests/src/vfs/Makefile.am b/tests/src/vfs/Makefile.am index 9b6789c68..b36731d1f 100644 --- a/tests/src/vfs/Makefile.am +++ b/tests/src/vfs/Makefile.am @@ -12,3 +12,7 @@ endif if ENABLE_VFS_FTP SUBDIRS += ftpfs endif + +if ENABLE_VFS_TAR +SUBDIRS += tar +endif diff --git a/tests/src/vfs/tar/Makefile.am b/tests/src/vfs/tar/Makefile.am new file mode 100644 index 000000000..8b2e39881 --- /dev/null +++ b/tests/src/vfs/tar/Makefile.am @@ -0,0 +1,19 @@ +PACKAGE_STRING = "/src/vfs/tar" + +AM_CPPFLAGS = \ + $(GLIB_CFLAGS) \ + -I$(top_srcdir) \ + @CHECK_CFLAGS@ + +LDADD = \ + $(top_builddir)/src/libinternal.la \ + $(top_builddir)/lib/libmc.la \ + @CHECK_LIBS@ + +TESTS = \ + tar_open_archive + +check_PROGRAMS = $(TESTS) + +tar_open_archive_SOURCES = \ + tar_open_archive.c diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c new file mode 100644 index 000000000..7d6ab1b4e --- /dev/null +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -0,0 +1,285 @@ +/* + src/vfs/tar - tests for opening of tar archives + + Copyright (C) 2026 + Free Software Foundation, Inc. + + Written by: + Yury V. Zaytsev , 2026 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#define TEST_SUITE_NAME "/src/vfs/tar" + +#include "tests/mctest.h" + +#include // snprintf() +#include // unlink(), rmdir() + +#include "lib/strutil.h" +#include "src/vfs/local/local.h" + +#include "src/vfs/tar/tar.c" + +/* --------------------------------------------------------------------------------------------- */ + +// real size of the sparse file, much larger than the whole archive +#define SPARSE_SIZE (1024 * 1024) +// the last data chunk of the sparse file +#define CHUNK_OFFSET 2048 +#define CHUNK_DATA "hello\n" + +/* --------------------------------------------------------------------------------------------- */ +/* mocked functions */ +/* --------------------------------------------------------------------------------------------- */ + +// text of the last error message, if any +static char *message__text = NULL; + +/* @Mock */ +void +message (int flags, const char *title, const char *text, ...) +{ + va_list ap; + + (void) flags; + (void) title; + + g_free (message__text); + va_start (ap, text); + message__text = g_strdup_vprintf (text, ap); + va_end (ap); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @Before */ +static void +setup (void) +{ + str_init_strings (NULL); + + vfs_init (); + vfs_init_localfs (); + vfs_init_tarfs (); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @After */ +static void +teardown (void) +{ + vfs_shut (); + str_uninit_strings (); + + MC_PTR_FREE (message__text); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* Append data padded with zeros to the block boundary. */ +static void +append_data (GByteArray *archive, const char *data, size_t len) +{ + g_byte_array_append (archive, (const guint8 *) data, len); + while (archive->len % BLOCKSIZE != 0) + g_byte_array_append (archive, (const guint8 *) "", 1); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* Append an ustar header followed by its data. */ +static void +append_member (GByteArray *archive, const char *name, char typeflag, const char *data, size_t len) +{ + union block b; + unsigned int sum = 0; + size_t i; + + memset (&b, 0, sizeof (b)); + g_strlcpy (b.header.name, name, sizeof (b.header.name)); + strcpy (b.header.mode, "0000644"); + strcpy (b.header.uid, "0000000"); + strcpy (b.header.gid, "0000000"); + snprintf (b.header.size, sizeof (b.header.size), "%011o", (unsigned int) len); + strcpy (b.header.mtime, "00000000000"); + b.header.typeflag = typeflag; + memcpy (b.header.magic, TMAGIC, sizeof (b.header.magic)); + memcpy (b.header.version, "00", sizeof (b.header.version)); + + memset (b.header.chksum, ' ', sizeof (b.header.chksum)); + for (i = 0; i < sizeof (b); i++) + sum += (unsigned char) b.buffer[i]; + snprintf (b.header.chksum, sizeof (b.header.chksum), "%06o", sum); + + g_byte_array_append (archive, (const guint8 *) b.buffer, sizeof (b)); + append_data (archive, data, len); +} + +/* --------------------------------------------------------------------------------------------- */ + +/* @DataSource("test_tar_pax_sparse_ds") */ +static const struct test_tar_pax_sparse_ds +{ + const char *xheader; + const char *map; // sparse map stored in the data, if any + size_t head_len; // size of a chunk at offset 0 before the last chunk, if any +} test_tar_pax_sparse_ds[] = { + // GNU tar --sparse-version=0.0: each chunk is a pair of keywords in the extended header + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "26 GNU.sparse.offset=2048\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + NULL, + 0, + }, + // GNU tar --sparse-version=0.1: the map is in the extended header + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "25 GNU.sparse.map=2048,6\n" + "15 path=sparse\n", + NULL, + 0, + }, + // GNU tar and bsdtar default (1.0): the map precedes the chunk in the data + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "1\n2048\n6\n", + 0, + }, + // 1.0 with a chunk that doesn't fill its last block, which is padded in the archive + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "2\n0\n1000\n2048\n6\n", + 1000, + }, +}; + +/* @Test(dataSource = "test_tar_pax_sparse_ds") */ +START_PARAMETRIZED_TEST (test_tar_pax_sparse, test_tar_pax_sparse_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive, *content; + char *dir, *path, *archive_path, *joined; + vfs_path_t *vpath, *sparse_vpath; + DIR *d; + struct vfs_dirent *de; + GString *listing; + char *expected, *actual; + ssize_t n, total; + int fd; + + // given: a pax sparse file between two plain ustar files, as written by bsdtar + content = g_byte_array_new (); + if (data->map != NULL) + append_data (content, data->map, strlen (data->map)); + if (data->head_len != 0) + { + char *head = g_strnfill (data->head_len, 'x'); + + append_data (content, head, data->head_len); + g_free (head); + } + g_byte_array_append (content, (const guint8 *) CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + + archive = g_byte_array_new (); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "PaxHeader/sparse", XHDTYPE, data->xheader, strlen (data->xheader)); + append_member (archive, "GNUSparseFile.0/sparse", REGTYPE, (const char *) content->data, + content->len); + append_member (archive, "z", REGTYPE, "world\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + mctest_assert_not_null (d); + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + + expected = g_malloc0 (SPARSE_SIZE); + memset (expected, 'x', data->head_len); + memcpy (expected + CHUNK_OFFSET, CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + actual = g_malloc0 (SPARSE_SIZE + 1); + sparse_vpath = vfs_path_append_new (vpath, "sparse", (char *) NULL); + fd = mc_open (sparse_vpath, O_RDONLY); + mctest_assert_true (fd != -1); + for (total = 0; (n = mc_read (fd, actual + total, SPARSE_SIZE + 1 - total)) > 0; total += n) + ; + mc_close (fd); + + // then: all members are listed and the sparse file is expanded correctly + joined = g_string_free (listing, FALSE); + mctest_assert_null (message__text); + mctest_assert_str_eq (joined, "a sparse z "); + ck_assert_int_eq (total, SPARSE_SIZE); + mctest_assert_true (memcmp (actual, expected, SPARSE_SIZE) == 0); + + // cleanup + g_free (joined); + g_free (actual); + g_free (expected); + vfs_path_free (sparse_vpath, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); + g_byte_array_free (content, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + +int +main (void) +{ + TCase *tc_core; + + tc_core = tcase_create ("Core"); + + tcase_add_checked_fixture (tc_core, setup, teardown); + + // Add new tests here: *************** + mctest_add_parameterized_test (tc_core, test_tar_pax_sparse, test_tar_pax_sparse_ds); + // *********************************** + + return mctest_run_all (tc_core); +} + +/* --------------------------------------------------------------------------------------------- */ From eb4ef5591936618fe14187e3402ab41eaba0e8f6 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 23 Nov 2026 07:44:51 +0000 Subject: [PATCH 02/17] tarfs: detect sparse members in pax archives tarfs sets the archive type one time, from the first header. For a header with the ustar magic, tar_decode_header() selects TAR_POSIX if the member has an extended header, and TAR_USTAR if not. GNU tar does the same test in decode_header(), but for each member. In a pax archive, the first header is the extended header of the first member, which also has the ustar magic. tar_decode_header() examines this header before the extended header is read, so the type becomes TAR_USTAR. The XHDTYPE branch then sets TAR_POSIX only for TAR_UNKNOWN, so a pax archive stays TAR_USTAR, and sparse_select_optab() doesn't look for sparse members. Therefore, also change TAR_USTAR to TAR_POSIX in the XHDTYPE branch. Apart from the detection of sparse members, tarfs handles the two types the same. Also, pax_sparse_member_p() required both a sparse map and a major version. However, the formats 0.x only have the map in the extended header, and the format 1.0 only has the version, because the map is stored in the data. Like GNU tar, accept either. Without these changes, a pax sparse member was skipped by its real size instead of its archived size. If this pointed past the end of the archive, all following members silently disappeared from the listing. Otherwise, the archive failed to open without an error message. Fixes: https://github.com/MidnightCommander/mc/issues/5227 Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-sparse.c | 4 ++-- src/vfs/tar/tar.c | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/vfs/tar/tar-sparse.c b/src/vfs/tar/tar-sparse.c index 2989fc140..cbe244bba 100644 --- a/src/vfs/tar/tar-sparse.c +++ b/src/vfs/tar/tar-sparse.c @@ -522,8 +522,8 @@ star_get_sparse_info (tar_super_t *archive, struct tar_sparse_file *file) static gboolean pax_sparse_member_p (struct tar_sparse_file *file) { - return file->stat_info->sparse_map != NULL && file->stat_info->sparse_map->len > 0 - && file->stat_info->sparse_major > 0; + return (file->stat_info->sparse_map != NULL && file->stat_info->sparse_map->len > 0) + || file->stat_info->sparse_major > 0; } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 4d9c2ff99..e7be0b6db 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -684,7 +684,9 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) } else if (header->header.typeflag == XHDTYPE || header->header.typeflag == SOLARIS_XHDTYPE) { - if (arch->type == TAR_UNKNOWN) + // the first header of a pax archive is this extended header, which has the ustar + // magic: like GNU tar, treat an archive with an extended header as pax, not ustar + if (arch->type == TAR_UNKNOWN || arch->type == TAR_USTAR) arch->type = TAR_POSIX; if (!tar_xheader_read (arch, ¤t_stat_info.xhdr, header, OFF_FROM_HEADER (header->header.size))) From 711d7a55d876c0130f68b7aac2a85c0604d568a2 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 23 Nov 2026 07:44:58 +0000 Subject: [PATCH 03/17] tarfs: read sparse data after the sparse map tar_skip_member() took the data offset of a sparse member before tar_sparse_skip_file() read the blocks that precede the data: the sparse map in the pax format 1.0, and the extension headers in the old GNU and star formats, if there are more chunks than fit into the header. Thus, the contents of such files were read from a wrong offset. Take the offset after these blocks. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-internal.h | 3 ++- src/vfs/tar/tar-sparse.c | 4 +++- src/vfs/tar/tar.c | 7 ++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/vfs/tar/tar-internal.h b/src/vfs/tar/tar-internal.h index 65c067ba1..cd93bcbab 100644 --- a/src/vfs/tar/tar-internal.h +++ b/src/vfs/tar/tar-internal.h @@ -312,7 +312,8 @@ gboolean tar_skip_file (tar_super_t *archive, off_t size); /* tar-sparse.c */ gboolean tar_sparse_member_p (tar_super_t *archive, struct tar_stat_info *st); gboolean tar_sparse_fixup_header (tar_super_t *archive, struct tar_stat_info *st); -enum dump_status tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st); +enum dump_status tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st, + off_t *data_offset); /* tar-xheader.c */ gboolean tar_xheader_decode (struct tar_stat_info *st); diff --git a/src/vfs/tar/tar-sparse.c b/src/vfs/tar/tar-sparse.c index cbe244bba..e14cb5b5c 100644 --- a/src/vfs/tar/tar-sparse.c +++ b/src/vfs/tar/tar-sparse.c @@ -628,7 +628,7 @@ tar_sparse_fixup_header (tar_super_t *archive, struct tar_stat_info *st) /* --------------------------------------------------------------------------------------------- */ enum dump_status -tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st) +tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st, off_t *data_offset) { gboolean rc = TRUE; struct tar_sparse_file file; @@ -640,6 +640,8 @@ tar_sparse_skip_file (tar_super_t *archive, struct tar_stat_info *st) file.fd = -1; rc = sparse_decode_header (archive, &file); + // the data follows the sparse map or the extension headers, if any + *data_offset = BLOCKSIZE * tar_current_block_ordinal (archive); (void) tar_skip_file (archive, file.stat_info->archive_file_size - file.dumped_size); return (sparse_done (&file) && rc) ? dump_status_ok : dump_status_short; } diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index e7be0b6db..47312c258 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -271,13 +271,14 @@ tar_skip_member (tar_super_t *archive, struct vfs_s_inode *inode) if (current_stat_info.is_sparse) { - if (inode != NULL) - inode->data_offset = BLOCKSIZE * tar_current_block_ordinal (archive); + off_t data_offset = BLOCKSIZE * tar_current_block_ordinal (archive); - (void) tar_sparse_skip_file (archive, ¤t_stat_info); + (void) tar_sparse_skip_file (archive, ¤t_stat_info, &data_offset); if (inode != NULL) { + inode->data_offset = data_offset; + // use vfs_s_inode::user_data to keep the sparse map inode->user_data = current_stat_info.sparse_map; current_stat_info.sparse_map = NULL; From ed05b074d48cecafa8a869bc7d4f72e60b36a810 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 23 Nov 2026 08:03:49 +0000 Subject: [PATCH 04/17] tarfs: fix crash on the pax sparse format 0.0 In the format 0.0, every chunk of a sparse file is stored as a GNU.sparse.offset keyword followed by a GNU.sparse.numbytes keyword. However, sparse_offset_decoder() changed the last chunk of the map, and sparse_numbytes_decoder() appended a new one. So the first offset was written to the chunk at index -1 of an empty map, or of no map at all, and mc crashed on opening of such an archive. Like GNU tar, start a chunk with the offset and complete it with the size. Reject the keywords if they come in a different order. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-xheader.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/vfs/tar/tar-xheader.c b/src/vfs/tar/tar-xheader.c index d087f422d..b408eab88 100644 --- a/src/vfs/tar/tar-xheader.c +++ b/src/vfs/tar/tar-xheader.c @@ -750,15 +750,20 @@ static gboolean sparse_offset_decoder (struct tar_stat_info *st, const char *keyword, const char *arg, size_t size) { uintmax_t u; - struct sp_array *s; + struct sp_array s; (void) size; if (!decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword)) return FALSE; - s = &g_array_index (st->sparse_map, struct sp_array, st->sparse_map->len - 1); - s->offset = u; + // the offset starts a chunk, which needs GNU.sparse.numblocks before it + if (st->sparse_map == NULL) + return FALSE; + + s.offset = u; + s.numbytes = 0; + g_array_append_val (st->sparse_map, s); return TRUE; } @@ -769,16 +774,17 @@ sparse_numbytes_decoder (struct tar_stat_info *st, const char *keyword, const ch size_t size) { uintmax_t u; - struct sp_array s; (void) size; if (!decode_num (&u, arg, SIZE_MAX, keyword)) return FALSE; - s.offset = 0; - s.numbytes = u; - g_array_append_val (st->sparse_map, s); + // the size ends the chunk started by GNU.sparse.offset + if (st->sparse_map == NULL || st->sparse_map->len == 0) + return FALSE; + + g_array_index (st->sparse_map, struct sp_array, st->sparse_map->len - 1).numbytes = u; return TRUE; } From f985bea2c02b1e775b69a7829deada3c7a036e32 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 23 Nov 2026 08:20:36 +0000 Subject: [PATCH 05/17] tarfs: round sparse chunks up to whole blocks Every chunk of a sparse file starts on a block boundary in the archive. However, tar_calc_sparse_offsets() added the remainder of the chunk size as a number of blocks instead of one block. So, after a chunk the size of which isn't a multiple of the block size, the data of all following chunks was read from a wrong offset. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 47312c258..18395892d 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -251,7 +251,7 @@ tar_calc_sparse_offsets (struct vfs_s_inode *inode) sp = &g_array_index (sm, struct sp_array, i); sp->arch_offset = begin; - begin += BLOCKSIZE * (sp->numbytes / BLOCKSIZE + sp->numbytes % BLOCKSIZE); + begin += BLOCKSIZE * (sp->numbytes / BLOCKSIZE + (sp->numbytes % BLOCKSIZE != 0)); } } From 942fd1f35240eb4aa5bcec164cfc51af014bff44 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Wed, 25 Nov 2026 06:44:32 +0000 Subject: [PATCH 06/17] tarfs: don't allocate the sparse map by the count from the archive sparse_numblocks_decoder() for the pax formats 0.0 and 0.1, and pax_decode_header() for the format 1.0 preallocated the sparse map for as many chunks as the archive told. With a corrupt or crafted count, such as 4294967295, g_array_sized_new() tried to allocate 128 GiB, and GLib aborted mc. GNU tar has the same code with xcalloc() and exits with "memory exhausted". The format 1.0 path became reachable with the detection of pax sparse members. Also, if the map already existed, g_array_set_size() filled it up to the count with uninitialized chunks, and the real chunks came after them. GNU tar starts with an empty map in both places. Start with an empty map and let the chunks that the archive really has fill it. This doesn't limit the number of chunks: the map grows only with the chunks that are read, and the reading of a format 1.0 map stops at the first malformed entry, as before. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-sparse.c | 7 ++---- src/vfs/tar/tar-xheader.c | 4 ++-- tests/src/vfs/tar/tar_open_archive.c | 33 +++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/vfs/tar/tar-sparse.c b/src/vfs/tar/tar-sparse.c index e14cb5b5c..45d1c21ef 100644 --- a/src/vfs/tar/tar-sparse.c +++ b/src/vfs/tar/tar-sparse.c @@ -556,11 +556,8 @@ pax_decode_header (tar_super_t *archive, struct tar_sparse_file *file) return FALSE; } - if (file->stat_info->sparse_map == NULL) - file->stat_info->sparse_map = - g_array_sized_new (FALSE, FALSE, sizeof (struct sp_array), u); - else - g_array_set_size (file->stat_info->sparse_map, u); + if (file->stat_info->sparse_map != NULL) + g_array_set_size (file->stat_info->sparse_map, 0); sparse_map_len = u; diff --git a/src/vfs/tar/tar-xheader.c b/src/vfs/tar/tar-xheader.c index b408eab88..75b33d074 100644 --- a/src/vfs/tar/tar-xheader.c +++ b/src/vfs/tar/tar-xheader.c @@ -737,9 +737,9 @@ sparse_numblocks_decoder (struct tar_stat_info *st, const char *keyword, const c return FALSE; if (st->sparse_map == NULL) - st->sparse_map = g_array_sized_new (FALSE, FALSE, sizeof (struct sp_array), u); + st->sparse_map = g_array_new (FALSE, FALSE, sizeof (struct sp_array)); else - g_array_set_size (st->sparse_map, u); + g_array_set_size (st->sparse_map, 0); return TRUE; } diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 7d6ab1b4e..f7994af3f 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -137,8 +137,9 @@ append_member (GByteArray *archive, const char *name, char typeflag, const char static const struct test_tar_pax_sparse_ds { const char *xheader; - const char *map; // sparse map stored in the data, if any - size_t head_len; // size of a chunk at offset 0 before the last chunk, if any + const char *map; // sparse map stored in the data, if any + size_t head_len; // size of a chunk at offset 0 before the last chunk, if any + gboolean bad_map; // the map is malformed: check the listing only } test_tar_pax_sparse_ds[] = { // GNU tar --sparse-version=0.0: each chunk is a pair of keywords in the extended header { @@ -149,6 +150,7 @@ static const struct test_tar_pax_sparse_ds "15 path=sparse\n", NULL, 0, + FALSE, }, // GNU tar --sparse-version=0.1: the map is in the extended header { @@ -158,6 +160,7 @@ static const struct test_tar_pax_sparse_ds "15 path=sparse\n", NULL, 0, + FALSE, }, // GNU tar and bsdtar default (1.0): the map precedes the chunk in the data { @@ -167,6 +170,7 @@ static const struct test_tar_pax_sparse_ds "31 GNU.sparse.realsize=1048576\n", "1\n2048\n6\n", 0, + FALSE, }, // 1.0 with a chunk that doesn't fill its last block, which is padded in the archive { @@ -176,6 +180,28 @@ static const struct test_tar_pax_sparse_ds "31 GNU.sparse.realsize=1048576\n", "2\n0\n1000\n2048\n6\n", 1000, + FALSE, + }, + // 0.0 with a chunk count that doesn't fit into memory + { + "27 GNU.sparse.size=1048576\n" + "35 GNU.sparse.numblocks=4294967295\n" + "26 GNU.sparse.offset=2048\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + NULL, + 0, + FALSE, + }, + // 1.0 with a chunk count that doesn't fit into memory + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "4294967295\n2048\n6\n", + 0, + TRUE, }, }; @@ -246,7 +272,8 @@ START_PARAMETRIZED_TEST (test_tar_pax_sparse, test_tar_pax_sparse_ds) mctest_assert_null (message__text); mctest_assert_str_eq (joined, "a sparse z "); ck_assert_int_eq (total, SPARSE_SIZE); - mctest_assert_true (memcmp (actual, expected, SPARSE_SIZE) == 0); + if (!data->bad_map) + mctest_assert_true (memcmp (actual, expected, SPARSE_SIZE) == 0); // cleanup g_free (joined); From ad078c2ba49486b3c0b15080fa57a1d2d5343a60 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Wed, 25 Nov 2026 06:44:51 +0000 Subject: [PATCH 07/17] tarfs: don't abort on headers that don't fit into memory tar_xheader_read() for the extended headers 'x', 'X' and 'g', and the branch of tar_read_header() for the GNU long name 'L' and long link 'K' headers allocated the buffer by the size field of the header with g_malloc(). With a corrupt or crafted size, such as 2^60 bytes in base-256, the allocation failed, and GLib aborted mc. GNU tar has the same code with xmalloc() and exits with "memory exhausted". Use g_try_malloc() and fail to open the archive with an error message instead. This doesn't limit the size of the headers: if the system gives the memory, the archive is read as before, and if it has less data than the header tells, it is reported as truncated. For the global extended header, also don't decode the buffer if it wasn't read completely. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-xheader.c | 4 +- src/vfs/tar/tar.c | 12 +++- tests/src/vfs/tar/tar_open_archive.c | 83 +++++++++++++++++++++++++--- 3 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/vfs/tar/tar-xheader.c b/src/vfs/tar/tar-xheader.c index 75b33d074..71675491e 100644 --- a/src/vfs/tar/tar-xheader.c +++ b/src/vfs/tar/tar-xheader.c @@ -912,7 +912,9 @@ tar_xheader_read (tar_super_t *archive, struct xheader *xhdr, union block *p, of return FALSE; size = size_plus_1 - 1; xhdr->size = size; - xhdr->buffer = g_malloc (size_plus_1); + xhdr->buffer = g_try_malloc (size_plus_1); + if (xhdr->buffer == NULL) + return FALSE; xhdr->buffer[size] = '\0'; do diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 18395892d..8fd5cb90e 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -643,7 +643,13 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) size -= size % BLOCKSIZE; - header_copy = g_malloc (size + 1); + header_copy = g_try_malloc (size + 1); + if (header_copy == NULL) + { + message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); + status = HEADER_FAILURE; + goto ret; + } if (header->header.typeflag == GNUTYPE_LONGNAME) { @@ -706,8 +712,8 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) arch->type = TAR_POSIX; memset (&xhdr, 0, sizeof (xhdr)); - tar_xheader_read (arch, &xhdr, header, OFF_FROM_HEADER (header->header.size)); - ok = tar_xheader_decode_global (&xhdr); + ok = tar_xheader_read (arch, &xhdr, header, OFF_FROM_HEADER (header->header.size)) + && tar_xheader_decode_global (&xhdr); tar_xheader_destroy (&xhdr); if (!ok) diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index f7994af3f..69eed1c7e 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -103,13 +103,26 @@ append_data (GByteArray *archive, const char *data, size_t len) /* --------------------------------------------------------------------------------------------- */ +/* Fill in the checksum of a header. */ +static void +set_checksum (union block *b) +{ + unsigned int sum = 0; + size_t i; + + memset (b->header.chksum, ' ', sizeof (b->header.chksum)); + for (i = 0; i < sizeof (*b); i++) + sum += (unsigned char) b->buffer[i]; + snprintf (b->header.chksum, sizeof (b->header.chksum), "%06o", sum); +} + +/* --------------------------------------------------------------------------------------------- */ + /* Append an ustar header followed by its data. */ static void append_member (GByteArray *archive, const char *name, char typeflag, const char *data, size_t len) { union block b; - unsigned int sum = 0; - size_t i; memset (&b, 0, sizeof (b)); g_strlcpy (b.header.name, name, sizeof (b.header.name)); @@ -121,11 +134,7 @@ append_member (GByteArray *archive, const char *name, char typeflag, const char b.header.typeflag = typeflag; memcpy (b.header.magic, TMAGIC, sizeof (b.header.magic)); memcpy (b.header.version, "00", sizeof (b.header.version)); - - memset (b.header.chksum, ' ', sizeof (b.header.chksum)); - for (i = 0; i < sizeof (b); i++) - sum += (unsigned char) b.buffer[i]; - snprintf (b.header.chksum, sizeof (b.header.chksum), "%06o", sum); + set_checksum (&b); g_byte_array_append (archive, (const guint8 *) b.buffer, sizeof (b)); append_data (archive, data, len); @@ -293,6 +302,64 @@ END_PARAMETRIZED_TEST /* --------------------------------------------------------------------------------------------- */ +/* @DataSource("test_tar_huge_header_size_ds") */ +static const struct test_tar_huge_header_size_ds +{ + char typeflag; +} test_tar_huge_header_size_ds[] = { + { XHDTYPE }, { SOLARIS_XHDTYPE }, { XGLTYPE }, { GNUTYPE_LONGNAME }, { GNUTYPE_LONGLINK }, +}; + +/* @Test(dataSource = "test_tar_huge_header_size_ds") */ +START_PARAMETRIZED_TEST (test_tar_huge_header_size, test_tar_huge_header_size_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive; + union block *b; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + + // given: a header the data of which doesn't fit into memory + archive = g_byte_array_new (); + append_member (archive, "huge", data->typeflag, "", 0); + b = (union block *) archive->data; + // 2^60 bytes in base-256 + memset (b->header.size, 0, sizeof (b->header.size)); + b->header.size[0] = (char) 0x80; + b->header.size[4] = 0x10; + set_checksum (b); + append_member (archive, "a", REGTYPE, "hello\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + d = mc_opendir (vpath); + + // then: an error instead of an abort + mctest_assert_null (d); + mctest_assert_not_null (message__text); + + // cleanup + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + int main (void) { @@ -304,6 +371,8 @@ main (void) // Add new tests here: *************** mctest_add_parameterized_test (tc_core, test_tar_pax_sparse, test_tar_pax_sparse_ds); + mctest_add_parameterized_test (tc_core, test_tar_huge_header_size, + test_tar_huge_header_size_ds); // *********************************** return mctest_run_all (tc_core); From de5d1024d952b9192a3db3e427437f506a2d32a0 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Wed, 25 Nov 2026 07:10:53 +0000 Subject: [PATCH 08/17] vfs: give generated archive directories the time of the archive tarfs and cpiofs generate the parent directories of members that have no entry of their own in the archive. Up to 4.8.31, these directories had the time when the archive was read. Commit eb1375b65 replaced this time in vfs_s_default_stat() with vfs_zero_stat_times(), so since 4.8.32 they show "Jan 1 1970". When such a directory is copied out of the archive with "Preserve attributes", which is on by default, the copy also gets this date. The archive doesn't give a time for such directories, so any value is a choice. The epoch is deterministic, but it is a false date, and it is written to disk on copy. The current time is what GNU tar and bsdtar give such directories on extraction, but it changes each time the archive is read again. The time of the archive is deterministic as well, and it is a plausible date: an archive is usually written after its members. Also, the root directory of the archive already has this time, because tarfs and cpiofs make it from the stat of the archive. Thus, copy the times of the root directory to the directories that vfs_s_automake() generates. Only tarfs and cpiofs use it. Reported in https://github.com/MidnightCommander/mc/issues/5227#issuecomment-7132908131 Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- lib/vfs/direntry.c | 1 + tests/src/vfs/tar/tar_open_archive.c | 48 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/vfs/direntry.c b/lib/vfs/direntry.c index d61cd6f91..e3b169be2 100644 --- a/lib/vfs/direntry.c +++ b/lib/vfs/direntry.c @@ -117,6 +117,7 @@ vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int f *sep = '\0'; res = vfs_s_generate_entry (me, path, dir, (flags & FL_MKDIR) != 0 ? (0777 | S_IFDIR) : 0777); + vfs_copy_stat_times (&dir->super->root->st, &res->ino->st); vfs_s_insert_entry (me, dir, res); if (sep != NULL) diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 69eed1c7e..ba6c4a6a9 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -29,6 +29,7 @@ #include // snprintf() #include // unlink(), rmdir() +#include // utime() #include "lib/strutil.h" #include "src/vfs/local/local.h" @@ -360,6 +361,52 @@ END_PARAMETRIZED_TEST /* --------------------------------------------------------------------------------------------- */ +/* @Test */ +START_TEST (test_tar_parent_dirs) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + const struct utimbuf archive_time = { 1234567890, 1234567890 }; + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath, *dir_vpath; + struct stat st; + + // given: a member in a directory that has no entry of its own + archive = g_byte_array_new (); + append_member (archive, "dir/a", REGTYPE, "hello\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + ck_assert_int_eq (utime (path, &archive_time), 0); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + dir_vpath = vfs_path_append_new (vpath, "dir", (char *) NULL); + + // when + ck_assert_int_eq (mc_stat (dir_vpath, &st), 0); + + // then: the generated directory has the time of the archive + mctest_assert_true (S_ISDIR (st.st_mode)); + ck_assert_int_eq (st.st_mtime, archive_time.modtime); + + // cleanup + vfs_path_free (dir_vpath, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ + int main (void) { @@ -373,6 +420,7 @@ main (void) mctest_add_parameterized_test (tc_core, test_tar_pax_sparse, test_tar_pax_sparse_ds); mctest_add_parameterized_test (tc_core, test_tar_huge_header_size, test_tar_huge_header_size_ds); + tcase_add_test (tc_core, test_tar_parent_dirs); // *********************************** return mctest_run_all (tc_core); From 286ccf981021bc12593301775c5a27cc3b0cc249 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Wed, 25 Nov 2026 07:15:42 +0000 Subject: [PATCH 09/17] tarfs: apply a directory entry that comes after its contents If the members of a directory come before the entry of the directory itself, as in archives made in the order of find -depth, tarfs first generates the directory for the members. When the entry of the directory came, tar_insert_entry() found the generated directory and ignored the entry. Thus, the directory kept the generated attributes instead of its own mode, owner and time. For example, a directory with the mode 0700 in the archive was shown with 0755, and the copy with "Preserve attributes" made it readable for all users. GNU tar applies the entry on extraction, and cpiofs already does it as well. Like cpiofs, take the mode, the owner and the time of the entry for the directory that was generated before it. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar.c | 12 ++++++++++++ tests/src/vfs/tar/tar_open_archive.c | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 8fd5cb90e..e253b8922 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -565,7 +565,19 @@ tar_insert_entry (struct vfs_class *me, struct vfs_s_super *archive, union block { entry = VFS_SUBCLASS (me)->find_entry (me, parent, p, LINK_NO_FOLLOW, FL_NONE); if (entry != NULL) + { + if (S_ISDIR (entry->ino->st.st_mode)) + { + entry->ino->st.st_mode = current_stat_info.stat.st_mode; + entry->ino->st.st_uid = current_stat_info.stat.st_uid; + entry->ino->st.st_gid = current_stat_info.stat.st_gid; + vfs_zero_stat_times (&entry->ino->st); + entry->ino->st.st_mtime = current_stat_info.mtime.tv_sec; + entry->ino->st.st_atime = current_stat_info.atime.tv_sec; + entry->ino->st.st_ctime = current_stat_info.ctime.tv_sec; + } return HEADER_SUCCESS; + } } *inode = vfs_s_new_inode (me, archive, ¤t_stat_info.stat); diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index ba6c4a6a9..97012baa6 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -368,12 +368,15 @@ START_TEST (test_tar_parent_dirs) const struct utimbuf archive_time = { 1234567890, 1234567890 }; GByteArray *archive; char *dir, *path, *archive_path; - vfs_path_t *vpath, *dir_vpath; - struct stat st; + vfs_path_t *vpath, *dir_vpath, *late_vpath; + struct stat st, late_st; - // given: a member in a directory that has no entry of its own + // given: a member in a directory that has no entry of its own, + // and a member in a directory the entry of which comes after it archive = g_byte_array_new (); append_member (archive, "dir/a", REGTYPE, "hello\n", 6); + append_member (archive, "late/a", REGTYPE, "hello\n", 6); + append_member (archive, "late/", DIRTYPE, "", 0); g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); @@ -385,15 +388,22 @@ START_TEST (test_tar_parent_dirs) archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); vpath = vfs_path_from_str (archive_path); dir_vpath = vfs_path_append_new (vpath, "dir", (char *) NULL); + late_vpath = vfs_path_append_new (vpath, "late", (char *) NULL); // when ck_assert_int_eq (mc_stat (dir_vpath, &st), 0); + ck_assert_int_eq (mc_stat (late_vpath, &late_st), 0); - // then: the generated directory has the time of the archive + // then: the generated directory has the time of the archive, + // and the other one has the mode and the time of its own entry mctest_assert_true (S_ISDIR (st.st_mode)); ck_assert_int_eq (st.st_mtime, archive_time.modtime); + mctest_assert_true (S_ISDIR (late_st.st_mode)); + ck_assert_int_eq (late_st.st_mode & 07777, 0644); + ck_assert_int_eq (late_st.st_mtime, 0); // cleanup + vfs_path_free (late_vpath, TRUE); vfs_path_free (dir_vpath, TRUE); vfs_path_free (vpath, TRUE); g_free (archive_path); From 32fb823abab33b65a003ba0458fd856244542111 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 19:23:04 +0000 Subject: [PATCH 10/17] fixup! tarfs: fix crash on the pax sparse format 0.0 Check the map before decode_num(), because this check is cheaper. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-xheader.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/vfs/tar/tar-xheader.c b/src/vfs/tar/tar-xheader.c index 71675491e..9b848d313 100644 --- a/src/vfs/tar/tar-xheader.c +++ b/src/vfs/tar/tar-xheader.c @@ -754,13 +754,13 @@ sparse_offset_decoder (struct tar_stat_info *st, const char *keyword, const char (void) size; - if (!decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword)) - return FALSE; - // the offset starts a chunk, which needs GNU.sparse.numblocks before it if (st->sparse_map == NULL) return FALSE; + if (!decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword)) + return FALSE; + s.offset = u; s.numbytes = 0; g_array_append_val (st->sparse_map, s); @@ -777,13 +777,13 @@ sparse_numbytes_decoder (struct tar_stat_info *st, const char *keyword, const ch (void) size; - if (!decode_num (&u, arg, SIZE_MAX, keyword)) - return FALSE; - // the size ends the chunk started by GNU.sparse.offset if (st->sparse_map == NULL || st->sparse_map->len == 0) return FALSE; + if (!decode_num (&u, arg, SIZE_MAX, keyword)) + return FALSE; + g_array_index (st->sparse_map, struct sp_array, st->sparse_map->len - 1).numbytes = u; return TRUE; } From 65c14ad1b6e30e568a858664b69b8642a63ef86a Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 19:45:56 +0000 Subject: [PATCH 11/17] fixup! tarfs: fix crash on the pax sparse format 0.0 Add tests for the keywords that the decoders reject: an offset without GNU.sparse.numblocks, a size before its offset, and a malformed offset or size. Before this fix, three of them crashed mc or were accepted. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- tests/src/vfs/tar/tar_open_archive.c | 84 ++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 97012baa6..7248edd3f 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -143,6 +143,88 @@ append_member (GByteArray *archive, const char *name, char typeflag, const char /* --------------------------------------------------------------------------------------------- */ +/* @DataSource("test_tar_pax_sparse_bad_keywords_ds") */ +static const struct test_tar_pax_sparse_bad_keywords_ds +{ + const char *xheader; +} test_tar_pax_sparse_bad_keywords_ds[] = { + // 0.0 with an offset, but without GNU.sparse.numblocks before it + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.offset=2048\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + }, + // 0.0 with a size before its offset + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "25 GNU.sparse.numbytes=6\n" + "26 GNU.sparse.offset=2048\n" + "15 path=sparse\n", + }, + // 0.0 with a malformed offset + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "23 GNU.sparse.offset=x\n" + "25 GNU.sparse.numbytes=6\n" + "15 path=sparse\n", + }, + // 0.0 with a malformed size + { + "27 GNU.sparse.size=1048576\n" + "26 GNU.sparse.numblocks=1\n" + "26 GNU.sparse.offset=2048\n" + "26 GNU.sparse.numbytes=-1\n" + "15 path=sparse\n", + }, +}; + +/* @Test(dataSource = "test_tar_pax_sparse_bad_keywords_ds") */ +START_PARAMETRIZED_TEST (test_tar_pax_sparse_bad_keywords, test_tar_pax_sparse_bad_keywords_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + + // given: a pax sparse file with a malformed sparse map between two plain ustar files + archive = g_byte_array_new (); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "PaxHeader/sparse", XHDTYPE, data->xheader, strlen (data->xheader)); + append_member (archive, "GNUSparseFile.0/sparse", REGTYPE, CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + append_member (archive, "z", REGTYPE, "world\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + d = mc_opendir (vpath); + + // then: the archive is rejected instead of a crash + mctest_assert_null (d); + + // cleanup + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + /* @DataSource("test_tar_pax_sparse_ds") */ static const struct test_tar_pax_sparse_ds { @@ -427,6 +509,8 @@ main (void) tcase_add_checked_fixture (tc_core, setup, teardown); // Add new tests here: *************** + mctest_add_parameterized_test (tc_core, test_tar_pax_sparse_bad_keywords, + test_tar_pax_sparse_bad_keywords_ds); mctest_add_parameterized_test (tc_core, test_tar_pax_sparse, test_tar_pax_sparse_ds); mctest_add_parameterized_test (tc_core, test_tar_huge_header_size, test_tar_huge_header_size_ds); From addf8f7a002482fc001f62a5a217d9db009e7ed3 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 19:45:56 +0000 Subject: [PATCH 12/17] tarfs: show an error message on a cascade of bad headers If a header is bad, tar_open_archive() skips it and reads the next block. If this block is also bad, it stops and fails to open the archive. Some of the bad headers already show a message, but many of them don't, for example a bad checksum, or an extended header that can't be decoded. Then mc failed to open such an archive without an error message. Therefore, show "Inconsistent tar archive" when tarfs stops at the second bad header, as the FIXME asked. Reported in https://github.com/MidnightCommander/mc/issues/5227 Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar.c | 2 +- tests/src/vfs/tar/tar_open_archive.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index e253b8922..be0ee8081 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -1033,7 +1033,7 @@ tar_open_archive (struct vfs_s_super *archive, const vfs_path_t *vpath, case HEADER_END_OF_FILE: case HEADER_FAILURE: // We are in the middle of a cascade of errors. - // AB: FIXME: TODO: show an error message here + message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); return -1; default: diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 7248edd3f..95a4eec0e 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -209,8 +209,9 @@ START_PARAMETRIZED_TEST (test_tar_pax_sparse_bad_keywords, test_tar_pax_sparse_b // when d = mc_opendir (vpath); - // then: the archive is rejected instead of a crash + // then: the archive is rejected with an error message instead of a crash mctest_assert_null (d); + mctest_assert_not_null (message__text); // cleanup vfs_path_free (vpath, TRUE); From 47b6bdcb171b0d70c46f8159f7dac4ff1079e0b7 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 19:59:01 +0000 Subject: [PATCH 13/17] amend! tarfs: show an error message on a cascade of bad headers tarfs: show one error message for bad headers If a header is bad, tar_open_archive() skips it and reads the next block. If this block is also bad, it stops and fails to open the archive. Some of the bad headers had an error message in tar_read_header(), but many of them didn't, for example a bad checksum, or an extended header that can't be decoded. So mc listed the archive without the bad members, or failed to open it, without an error message. For the bad headers with a message, tar_open_archive() could then show the message of the next bad header as well. Therefore, show the error message only in tar_open_archive(), one time for each sequence of bad headers, at the first of them. If the archive ends in the bad header, tell it. As before, tarfs skips one bad header and stops at the second one, which resolves the two FIXMEs there. Reported in https://github.com/MidnightCommander/mc/issues/5227 Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar.c | 18 ++---- tests/src/vfs/tar/tar_open_archive.c | 97 ++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 16 deletions(-) diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index be0ee8081..30ac39490 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -648,7 +648,6 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) if (ckd_add (&size, current_stat_info.stat.st_size, 2 * BLOCKSIZE - 1)) { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); status = HEADER_FAILURE; goto ret; } @@ -658,7 +657,6 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) header_copy = g_try_malloc (size + 1); if (header_copy == NULL) { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); status = HEADER_FAILURE; goto ret; } @@ -685,7 +683,6 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) data_block = tar_find_next_block (arch); if (data_block == NULL) { - message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); status = HEADER_FAILURE; goto ret; } @@ -710,7 +707,6 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) if (!tar_xheader_read (arch, ¤t_stat_info.xhdr, header, OFF_FROM_HEADER (header->header.size))) { - message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); status = HEADER_FAILURE; goto ret; } @@ -730,7 +726,6 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) if (!ok) { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); status = HEADER_FAILURE; goto ret; } @@ -826,10 +821,7 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) status = tar_insert_entry (me, archive, header, &inode); if (status != HEADER_SUCCESS) - { - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); goto ret; - } if (recent_long_name == next_long_name) recent_long_name = NULL; @@ -1027,13 +1019,15 @@ tar_open_archive (struct vfs_s_super *archive, const vfs_path_t *vpath, case HEADER_ZERO_BLOCK: case HEADER_SUCCESS: - // Skipping to next header. - break; // AB: FIXME + // Skipping to next header: report it once for the whole cascade. + message (D_ERROR, MSG_ERROR, + hit_eof ? _ ("Unexpected EOF on archive file") + : _ ("Inconsistent tar archive")); + break; case HEADER_END_OF_FILE: case HEADER_FAILURE: - // We are in the middle of a cascade of errors. - message (D_ERROR, MSG_ERROR, _ ("Inconsistent tar archive")); + // We are in the middle of a cascade of errors, which is already reported. return -1; default: diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 95a4eec0e..6c4f55427 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -50,6 +50,8 @@ // text of the last error message, if any static char *message__text = NULL; +// number of error messages +static int message__count = 0; /* @Mock */ void @@ -60,6 +62,7 @@ message (int flags, const char *title, const char *text, ...) (void) flags; (void) title; + message__count++; g_free (message__text); va_start (ap, text); message__text = g_strdup_vprintf (text, ap); @@ -89,6 +92,7 @@ teardown (void) str_uninit_strings (); MC_PTR_FREE (message__text); + message__count = 0; } /* --------------------------------------------------------------------------------------------- */ @@ -209,9 +213,9 @@ START_PARAMETRIZED_TEST (test_tar_pax_sparse_bad_keywords, test_tar_pax_sparse_b // when d = mc_opendir (vpath); - // then: the archive is rejected with an error message instead of a crash + // then: the archive is rejected with one error message instead of a crash mctest_assert_null (d); - mctest_assert_not_null (message__text); + ck_assert_int_eq (message__count, 1); // cleanup vfs_path_free (vpath, TRUE); @@ -427,9 +431,9 @@ START_PARAMETRIZED_TEST (test_tar_huge_header_size, test_tar_huge_header_size_ds // when d = mc_opendir (vpath); - // then: an error instead of an abort + // then: one error message instead of an abort mctest_assert_null (d); - mctest_assert_not_null (message__text); + ck_assert_int_eq (message__count, 1); // cleanup vfs_path_free (vpath, TRUE); @@ -444,6 +448,90 @@ END_PARAMETRIZED_TEST /* --------------------------------------------------------------------------------------------- */ +/* @DataSource("test_tar_bad_header_ds") */ +static const struct test_tar_bad_header_ds +{ + char typeflag; // type of the members between "a" and "z" + int count; // number of these members + gboolean bad_checksum; // these members have a bad checksum + const char *listing; // NULL if the archive is rejected +} test_tar_bad_header_ds[] = { + // tarfs skips a bad header and reads the members after it + { REGTYPE, 1, TRUE, "a z " }, + // tarfs stops at two bad headers in sequence + { REGTYPE, 2, TRUE, NULL }, + // a hard link to a member that isn't in the archive + { LNKTYPE, 1, FALSE, "a z " }, +}; + +/* @Test(dataSource = "test_tar_bad_header_ds") */ +START_PARAMETRIZED_TEST (test_tar_bad_header, test_tar_bad_header_ds) +{ + static const guint8 zero_blocks[2 * BLOCKSIZE] = { 0 }; + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + GString *listing; + int i; + + // given: bad members between two good ones + archive = g_byte_array_new (); + append_member (archive, "a", REGTYPE, "hello\n", 6); + for (i = 0; i < data->count; i++) + { + append_member (archive, "bad", data->typeflag, "", 0); + if (data->bad_checksum) + archive->data[archive->len - BLOCKSIZE + offsetof (struct posix_header, chksum)] ^= 1; + } + append_member (archive, "z", REGTYPE, "world\n", 6); + g_byte_array_append (archive, zero_blocks, sizeof (zero_blocks)); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + if (d != NULL) + { + struct vfs_dirent *de; + + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + } + + // then: one error message, and the good members are listed, if the archive is opened + ck_assert_int_eq (message__count, 1); + if (data->listing == NULL) + { + mctest_assert_null (d); + } + else + { + mctest_assert_str_eq (listing->str, data->listing); + } + + // cleanup + g_string_free (listing, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + /* @Test */ START_TEST (test_tar_parent_dirs) { @@ -515,6 +603,7 @@ main (void) mctest_add_parameterized_test (tc_core, test_tar_pax_sparse, test_tar_pax_sparse_ds); mctest_add_parameterized_test (tc_core, test_tar_huge_header_size, test_tar_huge_header_size_ds); + mctest_add_parameterized_test (tc_core, test_tar_bad_header, test_tar_bad_header_ds); tcase_add_test (tc_core, test_tar_parent_dirs); // *********************************** From 0b7515d9cf8dc85f8ae4883fc93f157bc3398de4 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 20:16:51 +0000 Subject: [PATCH 14/17] fixup! tarfs: detect sparse members in pax archives A pax 1.0 sparse member can have no chunks: if the archive ends in the sparse map, or if the map has a count of 0. Then the sparse map was NULL, and tar_calc_sparse_offsets() crashed mc on opening of the archive. With an empty map, tar_lseek_sparse() read the chunk at index -1 on reading of the file. Give such a member an empty map, and seek to its data offset if it has no chunks: the whole member is a hole, as in GNU tar. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 30ac39490..08006367d 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -283,6 +283,10 @@ tar_skip_member (tar_super_t *archive, struct vfs_s_inode *inode) inode->user_data = current_stat_info.sparse_map; current_stat_info.sparse_map = NULL; + // without chunks, e.g. if the archive ends in the map, the whole member is a hole + if (inode->user_data == NULL) + inode->user_data = g_array_new (FALSE, FALSE, sizeof (struct sp_array)); + tar_calc_sparse_offsets (inode); } } @@ -1202,6 +1206,13 @@ tar_lseek_sparse (vfs_file_handler_t *fh, off_t offset) break; case 0: + if (sm->len == 0) + { + // without chunks, the whole member is a hole + offset = fh->ino->data_offset; + break; + } + chunk = &g_array_index (sm, struct sp_array, sm->len - 1); // FIXME: can we seek beyond tar archive EOF here? offset = chunk->arch_offset + chunk->numbytes; From 9aa4b41337e6e73587295a838859fe8477e2a4e6 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 20:16:51 +0000 Subject: [PATCH 15/17] fixup! tarfs: don't allocate the sparse map by the count from the archive Add a test for a pax 1.0 sparse map with a count of 0. Since this commit, the map of such a member stays NULL. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- tests/src/vfs/tar/tar_open_archive.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 6c4f55427..10627f4fd 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -300,6 +300,16 @@ static const struct test_tar_pax_sparse_ds 0, TRUE, }, + // 1.0 without chunks: the whole file is a hole, and the data in the archive is not used + { + "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n", + "0\n", + 0, + TRUE, + }, }; /* @Test(dataSource = "test_tar_pax_sparse_ds") */ From f371ec32c8116e8a18c00aa26a6f699284ed68f9 Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 20:17:58 +0000 Subject: [PATCH 16/17] tarfs: don't read old blocks after the end of the archive At the end of the archive, tar_flush_archive() reads an empty record: current_block and record_end are then both at the start of the buffer. But current_header still points into the buffer, at the position of the header in the last record. If this header is bad, for example an extended header the data of which is cut off, tar_open_archive() skips it with tar_set_next_block_after(), and current_block goes past record_end. tar_find_next_block() only checked for current_block == record_end, so it returned the old blocks of the previous records from the buffer, and tarfs read them as members again, and then the memory after the buffer. Such an archive failed to open, listed members twice, or crashed mc. GNU tar has the same code, but aborts in tar_set_next_block_after() if current_block goes past record_end. Instead, also treat this as the end of the record in tar_find_next_block(). Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-internal.c | 3 +- tests/src/vfs/tar/tar_open_archive.c | 60 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/vfs/tar/tar-internal.c b/src/vfs/tar/tar-internal.c index c8673e6c7..aa7b10c0f 100644 --- a/src/vfs/tar/tar-internal.c +++ b/src/vfs/tar/tar-internal.c @@ -510,7 +510,8 @@ off_from_header (const char *p, size_t s) union block * tar_find_next_block (tar_super_t *archive) { - if (current_block == record_end) + // after the end of the archive, tar_set_next_block_after() can go past the empty record + if (current_block >= record_end) { if (hit_eof) return NULL; diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 10627f4fd..7d7a9935d 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -542,6 +542,65 @@ END_PARAMETRIZED_TEST /* --------------------------------------------------------------------------------------------- */ +/* @Test */ +START_TEST (test_tar_eof_after_first_record) +{ + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + struct vfs_dirent *de; + GString *listing; + int i; + + // given: members that fill the first record of 20 blocks, a member, and an extended header the + // data of which is cut off by the end of the archive + archive = g_byte_array_new (); + for (i = 0; i < 10; i++) + { + char name[] = "a0"; + + name[1] = (char) ('0' + i); + append_member (archive, name, REGTYPE, "hello\n", 6); + } + append_member (archive, "b", REGTYPE, "", 0); + append_member (archive, "PaxHeader/c", XHDTYPE, "13 path=cccc\n", 13); + g_byte_array_set_size (archive, archive->len - BLOCKSIZE); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + mctest_assert_not_null (d); + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + + // then: one error message, and the members before the end are listed once + ck_assert_int_eq (message__count, 1); + mctest_assert_str_eq (listing->str, "a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 b "); + + // cleanup + g_string_free (listing, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_TEST + +/* --------------------------------------------------------------------------------------------- */ + /* @Test */ START_TEST (test_tar_parent_dirs) { @@ -614,6 +673,7 @@ main (void) mctest_add_parameterized_test (tc_core, test_tar_huge_header_size, test_tar_huge_header_size_ds); mctest_add_parameterized_test (tc_core, test_tar_bad_header, test_tar_bad_header_ds); + tcase_add_test (tc_core, test_tar_eof_after_first_record); tcase_add_test (tc_core, test_tar_parent_dirs); // *********************************** From 5779eced461dc5c50f727c090b308e6579220eaf Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Mon, 30 Nov 2026 20:20:12 +0000 Subject: [PATCH 17/17] tarfs: show an error message for a cut-off archive If the archive ended in the data of a member, tar_read_header() ended the archive without an error message, and listed the member with its size from the header. For a sparse member, tar_skip_member() didn't even check if its map or data was cut off. And if the archive ended after an extended header or a long name, before the header of the member, the archive also ended without an error message. Also, tar_seek_archive() seeks over the data in whole records, and a seek doesn't stop at the end of the file. So if the rest of the data after the current record was a whole number of records, tarfs didn't see the end of the archive at all. GNU tar has the same code. Therefore, show "Unexpected EOF on archive file" in these cases, and read the last record of the data instead of seeking over it. As before, the members up to the cut-off one are listed. In these cases, the headers are good, so tar_open_archive() shows no second message. Assisted-By: Claude Opus 5.5 Signed-off-by: Yury V. Zaytsev --- src/vfs/tar/tar-internal.c | 5 +- src/vfs/tar/tar.c | 13 +++ tests/src/vfs/tar/tar_open_archive.c | 154 +++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 2 deletions(-) diff --git a/src/vfs/tar/tar-internal.c b/src/vfs/tar/tar-internal.c index aa7b10c0f..782581fb4 100644 --- a/src/vfs/tar/tar-internal.c +++ b/src/vfs/tar/tar-internal.c @@ -158,8 +158,9 @@ tar_seek_archive (tar_super_t *archive, off_t size) if (size <= skipped) return 0; - // Compute number of records to skip - nrec = (size - skipped) / record_size; + // Compute number of records to skip, but read the last one: a seek doesn't stop at the end of + // a cut-off archive + nrec = (size - skipped - 1) / record_size; if (nrec == 0) return 0; diff --git a/src/vfs/tar/tar.c b/src/vfs/tar/tar.c index 08006367d..36e08fe3e 100644 --- a/src/vfs/tar/tar.c +++ b/src/vfs/tar/tar.c @@ -289,6 +289,9 @@ tar_skip_member (tar_super_t *archive, struct vfs_s_inode *inode) tar_calc_sparse_offsets (inode); } + + // FALSE if the map or the data is cut off + return !hit_eof; } else if (save_typeflag != DIRTYPE) { @@ -616,6 +619,12 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) current_header = header; if (header == NULL) { + // the archive is cut off after an extended header or a long name of a member + if (hit_eof + && (next_long_name != NULL || next_long_link != NULL + || current_stat_info.xhdr.buffer != NULL)) + message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); + status = HEADER_END_OF_FILE; goto ret; } @@ -836,7 +845,11 @@ tar_read_header (struct vfs_class *me, struct vfs_s_super *archive) if (tar_skip_member (arch, inode)) status = HEADER_SUCCESS; else if (hit_eof) + { + // the header is good, but the data is cut off: keep the member, and end the archive + message (D_ERROR, MSG_ERROR, _ ("Unexpected EOF on archive file")); status = HEADER_END_OF_FILE; + } else status = HEADER_FAILURE; } diff --git a/tests/src/vfs/tar/tar_open_archive.c b/tests/src/vfs/tar/tar_open_archive.c index 7d7a9935d..54ee729cc 100644 --- a/tests/src/vfs/tar/tar_open_archive.c +++ b/tests/src/vfs/tar/tar_open_archive.c @@ -601,6 +601,159 @@ END_TEST /* --------------------------------------------------------------------------------------------- */ +/* Append a pax 1.0 sparse member with a chunk of CHUNK_DATA at CHUNK_OFFSET. */ +static void +append_pax_sparse_member (GByteArray *archive) +{ + static const char xheader[] = "22 GNU.sparse.major=1\n" + "22 GNU.sparse.minor=0\n" + "26 GNU.sparse.name=sparse\n" + "31 GNU.sparse.realsize=1048576\n"; + static const char map[] = "1\n2048\n6\n"; + GByteArray *content; + + content = g_byte_array_new (); + append_data (content, map, sizeof (map) - 1); + g_byte_array_append (content, (const guint8 *) CHUNK_DATA, sizeof (CHUNK_DATA) - 1); + + append_member (archive, "PaxHeader/sparse", XHDTYPE, xheader, sizeof (xheader) - 1); + append_member (archive, "GNUSparseFile.0/sparse", REGTYPE, (const char *) content->data, + content->len); + g_byte_array_free (content, TRUE); +} + +/* --------------------------------------------------------------------------------------------- */ + +static void +cut_in_data (GByteArray *archive) +{ + char data[2000]; + + memset (data, 'x', sizeof (data)); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "m", REGTYPE, data, sizeof (data)); + g_byte_array_set_size (archive, archive->len - 2 * BLOCKSIZE); +} + +static void +cut_in_data_of_first (GByteArray *archive) +{ + char data[2000]; + + memset (data, 'x', sizeof (data)); + append_member (archive, "m", REGTYPE, data, sizeof (data)); + g_byte_array_set_size (archive, archive->len - 2 * BLOCKSIZE); +} + +static void +cut_in_seeked_data (GByteArray *archive) +{ + // the data starts at block 3: tarfs reads 17 blocks of it in the first record of 20 blocks, + // and seeks over the rest, which is one whole record + const size_t len = 17 * BLOCKSIZE + 20 * BLOCKSIZE; + char *data; + + data = g_malloc0 (len); + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "m", REGTYPE, data, len); + g_byte_array_set_size (archive, 20 * BLOCKSIZE); + g_free (data); +} + +static void +cut_in_sparse_data (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_pax_sparse_member (archive); + g_byte_array_set_size (archive, archive->len - BLOCKSIZE); +} + +static void +cut_in_sparse_map (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_pax_sparse_member (archive); + g_byte_array_set_size (archive, archive->len - 2 * BLOCKSIZE); +} + +static void +cut_after_xheader (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "PaxHeader/m", XHDTYPE, "10 path=m\n", 10); +} + +static void +cut_after_long_name (GByteArray *archive) +{ + append_member (archive, "a", REGTYPE, "hello\n", 6); + append_member (archive, "././@LongLink", GNUTYPE_LONGNAME, "m", 2); +} + +/* @DataSource("test_tar_cut_off_ds") */ +static const struct test_tar_cut_off_ds +{ + void (*build) (GByteArray *archive); + const char *listing; +} test_tar_cut_off_ds[] = { + { cut_in_data, "a m " }, + { cut_in_data_of_first, "m " }, + { cut_in_seeked_data, "a m " }, + { cut_in_sparse_data, "a sparse " }, + { cut_in_sparse_map, "a sparse " }, + { cut_after_xheader, "a " }, + { cut_after_long_name, "a " }, +}; + +/* @Test(dataSource = "test_tar_cut_off_ds") */ +START_PARAMETRIZED_TEST (test_tar_cut_off, test_tar_cut_off_ds) +{ + GByteArray *archive; + char *dir, *path, *archive_path; + vfs_path_t *vpath; + DIR *d; + struct vfs_dirent *de; + GString *listing; + + // given: an archive that ends before the end of its last member + archive = g_byte_array_new (); + data->build (archive); + + dir = g_dir_make_tmp ("mctest-XXXXXX", NULL); + mctest_assert_not_null (dir); + path = g_build_filename (dir, "test.tar", (char *) NULL); + mctest_assert_true ( + g_file_set_contents (path, (const char *) archive->data, (gssize) archive->len, NULL)); + archive_path = g_strconcat (path, PATH_SEP_STR "utar" VFS_PATH_URL_DELIMITER, (char *) NULL); + vpath = vfs_path_from_str (archive_path); + + // when + listing = g_string_new (NULL); + d = mc_opendir (vpath); + mctest_assert_not_null (d); + while ((de = mc_readdir (d)) != NULL) + g_string_append_printf (listing, "%s ", de->d_name); + mc_closedir (d); + + // then: one error message, and the members are listed up to the cut-off one + ck_assert_int_eq (message__count, 1); + mctest_assert_str_eq (message__text, "Unexpected EOF on archive file"); + mctest_assert_str_eq (listing->str, data->listing); + + // cleanup + g_string_free (listing, TRUE); + vfs_path_free (vpath, TRUE); + g_free (archive_path); + unlink (path); + g_free (path); + rmdir (dir); + g_free (dir); + g_byte_array_free (archive, TRUE); +} +END_PARAMETRIZED_TEST + +/* --------------------------------------------------------------------------------------------- */ + /* @Test */ START_TEST (test_tar_parent_dirs) { @@ -674,6 +827,7 @@ main (void) test_tar_huge_header_size_ds); mctest_add_parameterized_test (tc_core, test_tar_bad_header, test_tar_bad_header_ds); tcase_add_test (tc_core, test_tar_eof_after_first_record); + mctest_add_parameterized_test (tc_core, test_tar_cut_off, test_tar_cut_off_ds); tcase_add_test (tc_core, test_tar_parent_dirs); // ***********************************