Repository navigation
Conversation
ossilator
left a comment
There was a problem hiding this comment.
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.
See extended PR description:
So the correct logic is as follows: treat archive as |
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).
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.
that's reasonable, but i'd limit this state to private branches and maybe PRs that are still drafts. |
And what are these reasons?
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. |
oh, c'mon. you can really google that yourself, but i'm sure you actually only need to remember.
i don't believe that; there is no plausible reason why this should be the case when you actually take the reviewer role. |
Why would I put work into actively searching for supporting arguments for something I don't believe is true and/or relevant?
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. |
|
New commits after the review:
And since we're touching the same code area:
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 |
|
Tested on FreeBSD 15.1 with a large (>150 MB) real-world tar archive: |
|
/rebase |
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>
71c6844 to
286ccf9
Compare
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>
8dafbb6 to
addf8f7
Compare
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>
|
@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. |
| 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")); |
There was a problem hiding this comment.
it would be more natural to say "Unexpected end of archive file". or just "Truncated archive".

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
git commit --amend -smake indent && make check)Full investigation results
Problem
The tarfs code has five errors in the sparse file code:
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_POSIXwhen it finds an extended header.For
TAR_USTAR, tarfs does not look for sparse files.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.
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.
GNU.sparse.offsetkeywordand then a
GNU.sparse.numbyteskeyword. 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.
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:
current_formatindecode_header()).sparse_map_avail > 0 || sparse_major > 0.sparse_offset_decoder()sets the offset of the next chunk.sparse_numbytes_decoder()sets its size and completes it.set_next_block_after()insparse_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:
sparse_map->len. GNU tar usessparse_map_avail.Solution
The patch series has five commits on top of
master(f3e4a92):tests/src/vfs/tar/tar_open_archivefor the formats 0.0, 0.1 and 1.0, and for a chunk that does not fill its last block.TAR_USTARtoTAR_POSIXat an extended header. Use||inpax_sparse_member_p().tar_sparse_skip_file()gives the data offset after the map and the extension headers.