sqfstar: improve tar parser stability #309
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These commits fix different issues in sqfstar's tar parser and partially
depend on each other to mitigate the discussed issues fully:
Signed integer overflows are undefined behaviour and should be avoided.
From a C perspective, I do not care too much about them, but they can
lead to crashes later on in the code, e.g. because sqfstar does not
expect to encouter actually negative numbers for sizes later on.
Commits:
Proof of Concepts:
This crashes sqfstar because of negative sizes:
The crash occurs because the parsed number -2 leads to a read of 0 bytes
and a fragment added into the fragment processor. The processor itself
tries to create a checksum and eventually reads past the end of
allocated memory.
This triggers a signed integer overflow (compile with UBSAN):
This triggers an out ouf boundary read:
It is not covered by ASAN and hard to see in reality as a crash.
In fact, it took an OpenBSD system with its guard pages to prove that
out of boundary access can occur.
The sqfstar code has some unique traits in it which allows different
views on the same tar archive with different tools, e.g. GNU tar,
libarchive, docker (Go), and so on. It is possible to create a tar
archive which sneaks in special information into a squash filesystem
which otherwise is not detected. Sylab's singularity utilizes sqfstar
for conversions from OCI containers to sqfs. By making sqfstar behave
more like the other tar parsers and stay closer to the POSIX standard,
such attacks will be harder to be performed.
Commits:
Proof of Concepts:
This file can be parsed by GNU tar and libarchive, not by sqfstar:
This file sneaks in a different link into sqfs from tar:
You can see that the poc.sqfs contains a link to a different target than
the tar file implies with GNU tar or libarchive's bsdtar.
The sparse map parser allows a targeted out of boundary write with
attacker controlled data. Together with heap spraying due to leaking
memory if pax keywords are supplied multiple times, this might allow
an attacker to perform elevated attacks. Together with number 2, this
can be targeted specifically against sqfstar without triggering issues
with other tar implementations.
Commit:
Proof of Concept (compile with ASAN):
This allows an attacker to write multiples of 16 bytes beyond a multiple
of 16 bytes of allocated memory.
Some values might overflow system data types, e.g. times. These commits
are merely cleanups.
Commits: