Repository navigation
fix(libutil): apply only the specified filter to decompress archive - #10341
Conversation
|
With this preliminary patch |
|
I've added a nixos test that targets the exact case encountered in #10254. I'm pretty sure that a nixos test is the best way to approach this. I've verified that without 1826726 this test fails. |
Do a |
|
Done in a separate commit. Will squash relevant commits after review. Separate commits should be easier to read |
thufschmitt
left a comment
There was a problem hiding this comment.
Thanks! That looks good
Left a few comments, but mostly nitpicks that you can freely ignore
| dontUnpack = true; | ||
| nativeBuildInputs = with pkgs; [ zstd ]; | ||
| buildPhase = '' | ||
| tar --zstd -cf archive.tar.zst ${nixpkgs} |
There was a problem hiding this comment.
Can we use a small ad-hoc set of files here rather than nixpkgs? That could make a pretty big difference in the duration of the test
| ${fileCmd} -b $archive_path --extension | grep "zst" | ||
| ! ${fileCmd} -b $archive_path --extension | grep "tar" |
There was a problem hiding this comment.
(nit, untested)
| ${fileCmd} -b $archive_path --extension | grep "zst" | |
| ! ${fileCmd} -b $archive_path --extension | grep "tar" | |
| [[ ${fileCmd} --brief --mime-type $archive_path == "application/zstd" ]] |
Makes the test a bit more precise and explicit
| std::string compressionMethod; | ||
| ArchiveDecompressionSource(Source & src, std::string compressionMethod = "") |
There was a problem hiding this comment.
(nit)
| std::string compressionMethod; | |
| ArchiveDecompressionSource(Source & src, std::string compressionMethod = "") | |
| std::optional<std::string> compressionMethod; | |
| ArchiveDecompressionSource(Source & src, std::optional<std::string> compressionMethod = "") |
Doesn't change much but makes the intent slightly more explicit
There was a problem hiding this comment.
Huh, I've changed to using std::optional and I'm seeing a very curious bug. Here method is passed as a const ref:
std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Sink & nextSink)
{
if (method == "none" || method == "")
return std::make_unique<NoneSink>(nextSink);
else if (method == "br")
return std::make_unique<BrotliDecompressionSink>(nextSink);
else
return sourceToSink([&](Source & source) {
auto decompressionSource = std::make_unique<ArchiveDecompressionSource>(source, method);
decompressionSource->drainInto(nextSink);
});
}However, there's certainly some UB going on here because a const ref method becomes an empty string here:
return sourceToSink([&](Source & source) {
// here on my machine exception is actually thrown. Though this should never happen
// (by simple inspection) in a well-defined program.
if (method.empty()) throw std::runtime_error{"VERY BAD UB AND DATARACES"};
auto decompressionSource = std::make_unique<ArchiveDecompressionSource>(source, method);
decompressionSource->drainInto(nextSink);
});But this is a courotine created here: src/libstore/filetransfer.cc:163. encoding is passed by reference, but it looks
like another thread races on the same variable and sets it to an empty string here: src/libstore/filetransfer.cc:208.
Isn't nix built with thread sanitizer to actually catch such blatant UB? Not only is this a datarace (maybe a race on the same thread between different tasks/coroutines), but also a modification of a supposedly const ref.
The correct thing to do here is obviosly take the string by value. But such bugs are really better diagnosed with sanitizers
There was a problem hiding this comment.
Would it maybe be beneficial to open an issue/rfc to introduce tests under address/memory/ub/thread sanitizers. I would not be surprised it this is happening in other places as well...
There was a problem hiding this comment.
It would be great if someone could reproduce this issue to make sure I'm not braindead and not going crazy
There was a problem hiding this comment.
I've pinned down what's going on. There was either a dangling reference/race. Capturing method by value into the lambda passed to the coroutine resolves the issue:
return sourceToSink([method, &nextSink](Source & source) {
auto decompressionSource = std::make_unique<ArchiveDecompressionSource>(source, method);
decompressionSource->drainInto(nextSink);
});So yeah, that's been my mistake all along.
There was a problem hiding this comment.
Nice investigation (and sorry for the headaches it caused you).
Regarding
Would it maybe be beneficial to open an issue/rfc to introduce tests under address/memory/ub/thread sanitizers. I would not be surprised it this is happening in other places as well...
100x yes! I opened #10144 for that last month, but didn't have time to finish it. Let me know if you're interested in picking it up :)
Run clang-format on compression.{cc,hh} and tarfile{cc,hh}.
This way follow-up patches will be formatted properly and have
easier to read diffs.
This patch makes `makeDecompressionSink` strip only a single layer of compression specified via method. This fixes erroneous decompression of doubly-compressed NARs fetched with curl.
Add an integration test with a compressing proxy (nginx is used). This test verifies that fetched archives do not get decompressed excessively.
Motivation
This series of patches fixes erroneous decompression of files that are received
from the web with some compression applied. This can occur for substitution with
reverse proxies or fetching compressed tarballs from the web with compressing proxies.
Context
This PR fixes bugs #10275 and #10254.
Priorities and Process
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.