Skip to content

Ticket #5227: assorted tarfs fixes - #5228

Open
zyv wants to merge 17 commits into
masterfrom
5227_tarfs_fixes
Open

zyv wants to merge 17 commits into
masterfrom
5227_tarfs_fixes

Conversation

@zyv

@zyv zyv commented Nov 23, 2026

Copy link
Copy Markdown
Member

Proposed changes

I have performed an investigation with Opus and found the cause of the reported issue, as well as a number of other related crashes in our code. Full investigation report below; I looked at the tests and at the code, and both make sense to me.

Checklist

  • I have referenced the issue(s) resolved by this PR (if any)
  • I have signed-off my contribution with git commit --amend -s
  • Lint and unit tests pass locally with my changes (make indent && make check)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

Full investigation results

Problem

The tarfs code has five errors in the sparse file code:

  1. The archive type stays TAR_USTAR. tarfs sets the type one time, from the first header.
    A pax extended header has the same magic as a ustar header.
    Thus, tarfs does not change the type to TAR_POSIX when it finds an extended header.
    For TAR_USTAR, tarfs does not look for sparse files.
  2. pax_sparse_member_p() uses && where it must use ||.
    The formats 0.x have a map, but no version. The format 1.0 has a version, but no map in the header.
    Thus, the function is always FALSE.
  3. The data offset is incorrect. tarfs records the data offset before it reads the blocks
    in front of the data. In the format 1.0, these blocks contain the sparse map.
    In the old GNU and star formats, they are extension headers for more than 4 chunks.
    Thus, tarfs reads the contents of these files from an incorrect position.
  4. mc crashes with the format 0.0. In this format, each chunk has a GNU.sparse.offset keyword
    and then a GNU.sparse.numbytes keyword. tarfs expects the opposite sequence.
    Thus, it writes the first offset to the chunk at index −1 of an empty map.
    This code runs for each extended header. Thus, errors 1 and 2 do not prevent the crash.
  5. The chunks are not rounded up to whole blocks. In the archive, each chunk starts at a block boundary.
    tar_calc_sparse_offsets() adds the remainder of the chunk size as a number of blocks, not as one block.
    Thus, after a chunk with a size that is not a multiple of 512 bytes,
    tarfs reads all the subsequent chunks from an incorrect position.
    Usual tools on usual file systems write such a chunk only at the end of the file. Thus, real archives seldom show this error.

Errors 1 and 2 cause the problem in the issue. Errors 3 to 5 are in the same code path.
Errors 3 and 5 can give incorrect data when you copy a file from the archive.

Cause

tarfs is a manual port of GNU tar. All five errors occur only in the mc port:

Error GNU tar
1 Finds the format for each member, after it reads the extended headers (current_format in decode_header()).
2 sparse_map_avail > 0 || sparse_major > 0.
3 Does not apply. GNU tar reads the archive in sequence and does not keep a data offset.
4 sparse_offset_decoder() sets the offset of the next chunk. sparse_numbytes_decoder() sets its size and completes it.
5 Does not apply. GNU tar reads the chunks in sequence. After each chunk, it goes to the next block (set_next_block_after() in sparse_extract_region()).

For errors 1, 2 and 4, GNU tar 1.34, GNU tar 1.35 and the current GNU tar master (4e76fbad, 2026-11-22)
have the same correct code. Errors 3 and 5 are in code that only mc has, because tarfs can seek in a file.
The next sync must keep these points:

  • mc uses one archive type. GNU tar uses one format for each member.
  • mc uses sparse_map->len. GNU tar uses sparse_map_avail.

Solution

The patch series has five commits on top of master (f3e4a92):

Commit Error Change
tests: add tarfs tests for pax sparse members — New test tests/src/vfs/tar/tar_open_archive for the formats 0.0, 0.1 and 1.0, and for a chunk that does not fill its last block.
tarfs: detect sparse members in pax archives 1, 2 Change the type from TAR_USTAR to TAR_POSIX at an extended header. Use || in pax_sparse_member_p().
tarfs: read sparse data after the sparse map 3 tar_sparse_skip_file() gives the data offset after the map and the extension headers.
tarfs: fix crash on the pax sparse format 0.0 4 The offset starts a chunk, and the size completes it. tarfs rejects other sequences.
tarfs: round sparse chunks up to whole blocks 5 Add one block for the remainder of the chunk size.

@zyv
zyv requested a review from mc-worker November 23, 2026 12:04
@github-actions github-actions Bot added needs triage Needs triage by maintainers prio: medium Has the potential to affect progress labels Nov 23, 2026
@github-actions github-actions Bot added this to the Future Releases milestone Nov 23, 2026
@zyv zyv added area: vfs Virtual File System support and removed needs triage Needs triage by maintainers labels Nov 23, 2026
@zyv zyv modified the milestones: Future Releases, 4.9.0 Nov 23, 2026

@ossilator ossilator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3d8d77e: without more context, it's not obvious why TAR_POSIX should be the correct constant. this "problem" is shared between the commit message and the code comment. this might or might not be worth addressing ...

the autotest addition is weird. does the first commit add tests that fail until the subsequent commits, or does it test only things that already work, and the fixes are thus not tested? i'm being too lazy to check which one it is 😇, but both cases would be suboptimal.

Comment thread src/vfs/tar/tar-xheader.c Resolved
Comment thread src/vfs/tar/tar-xheader.c Resolved
@zyv

zyv commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

the autotest addition is weird. does the first commit add tests that fail until the subsequent commits

Yes, this is exactly my intention. I want to see in the CI that all regression tests on all platforms uniformly fail until the fixes are implemented.

i'm being too lazy to check which one it is 😇,

Screenshot 2026-11-23 at 14 05 27

but both cases would be suboptimal.

Why?

@zyv

zyv commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

3d8d77e: without more context, it's not obvious why TAR_POSIX should be the correct constant. this "problem" is shared between the commit message and the code comment.

See extended PR description:

  1. The archive type stays TAR_USTAR. tarfs sets the type one time, from the first header.
    A pax extended header has the same magic as a ustar header.
    Thus, tarfs does not change the type to TAR_POSIX when it finds an extended header.
    For TAR_USTAR, tarfs does not look for sparse files.

So the correct logic is as follows: treat archive as ustar first, change to POSIX if an extended header is encountered.

@ossilator

Copy link
Copy Markdown
Contributor

See extended PR description:

yes, obviously. but i'm trying to encourage the policy "make commit messages self-contained" (cf. https://wiki.qt.io/Commit_Policy 8.2.3).

but both cases would be suboptimal.

Why?

because it's non-atomic, which sucks for various reasons.

one could fix this by temporarily marking the tests as expected failures, but git's policy somewhat convincingly argues against that approach.

I want to see in the CI that all regression tests on all platforms uniformly fail until the fixes are implemented.

that's reasonable, but i'd limit this state to private branches and maybe PRs that are still drafts.

@zyv

zyv commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

because it's non-atomic, which sucks for various reasons.

And what are these reasons?

one could fix this by temporarily marking the tests as expected failures, but git's policy somewhat convincingly argues against that approach.

In the Python world, I would mark tests as xfail and then flip the marks in the commits that fix it, but it's unavailable in check, and workarounds are even more laborious and annoying. I don't see git's argument as convincing; they argue that it's harder for them to review, and for me, it's easier.

@ossilator

ossilator commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

And what are these reasons?

oh, c'mon. you can really google that yourself, but i'm sure you actually only need to remember.

they argue that it's harder for them to review, and for me, it's easier.

i don't believe that; there is no plausible reason why this should be the case when you actually take the reviewer role.
what i can believe is that creating and testing series is easier that way. but prospectively, the commits will be looked at many more times. it's a trade-off, as usual.

@zyv

zyv commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

And what are these reasons?

oh, c'mon. you can really google that yourself, but i'm sure you actually only need to remember.

Why would I put work into actively searching for supporting arguments for something I don't believe is true and/or relevant?

they argue that it's harder for them to review, and for me, it's easier.

i don't believe that; there is no plausible reason why this should be the case when you actually take the reviewer role. what i can believe is that creating and testing series is easier that way. but prospectively, the commits will be looked at many more times. it's a trade-off, as usual.

That's fine; it is not my goal to make you believe everything I say; it's enough for me to believe it myself. When my beliefs are changed by your argument, I tend to act on that if able.

@zyv zyv changed the title 5227 tarfs fixes Ticket #5227: assorted tarfs fixes Nov 25, 2026
@zyv

zyv commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

New commits after the review:

  • vfs: give generated archive directories the time of the archive.
    This fixes the 1970 regression that @Andron001 found, with a deterministic time.

  • tarfs: apply a directory entry that comes after its contents.
    Before, such a directory kept generated attributes, so a 0700 directory was copied out as 0755. This is an old problem.

And since we're touching the same code area:

  • tarfs: don't allocate the sparse map by the count from the archive.
    A crafted count aborted mc, and 3d8d77e made the pax 1.0 path reachable. The map now grows with the chunks that are read.
  • tarfs: don't abort on headers that don't fit into memory.
    g_try_malloc() instead of g_malloc() for x/X/g/L/K header data.

I have reported this to GNU tar already, but they don't seem to care; at least I haven't got any reaction to my report. It occurred to me that those issues can be fixed "elegantly" in our code without any hard bounds:

https://lists.gnu.org/archive/html/bug-tar/2026-10/msg00004.html

@Andron001

Copy link
Copy Markdown

Tested on FreeBSD 15.1 with a large (>150 MB) real-world tar archive:
the 1970 regression is gone, synthesized directories now carry the
archive file's time and it does not change on re-entry. No crashes,
archives open fine. Thank you!

@zyv

zyv commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

/rebase

zyv added 9 commits November 30, 2026 18:30
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 <yury@shurup.com>
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: #5227

Assisted-By: Claude Opus 5.5
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
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 <yury@shurup.com>
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 <yury@shurup.com>
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 <yury@shurup.com>
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 <yury@shurup.com>
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 <yury@shurup.com>
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 eb1375b 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 #5227 (comment)

Assisted-By: Claude Opus 5.5
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
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 <yury@shurup.com>
zyv added 3 commits November 30, 2026 19:42
Check the map before decode_num(), because this check is cheaper.

Assisted-By: Claude Opus 5.5
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
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 <yury@shurup.com>
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 #5227

Assisted-By: Claude Opus 5.5
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
@zyv
zyv force-pushed the 5227_tarfs_fixes branch from 8dafbb6 to addf8f7 Compare November 30, 2026 19:49
zyv added 5 commits November 30, 2026 19:59
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 #5227

Assisted-By: Claude Opus 5.5
Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
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 <yury@shurup.com>
…hive

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 <yury@shurup.com>
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 <yury@shurup.com>
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 <yury@shurup.com>
@zyv

zyv commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

@mc-worker I've added a commit to fix the review comment I overlooked, and then I noticed your FIXMEs. I thought it makes sense to fix those as well, while we are at it. Unfortunately, fixing the error messages revealed more crashes when testing. So I added more commits to fix these crashes. Now I stop looking. Our tarfs is perfect.

Comment thread src/vfs/tar/tar.c
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"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be more natural to say "Unexpected end of archive file". or just "Truncated archive".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: vfs Virtual File System support prio: medium Has the potential to affect progress

Development

Successfully merging this pull request may close these issues.

tarfs: fails to open a large tar archive that GNU tar and bsdtar read fine

4 participants