Skip to content

fix(libutil): apply only the specified filter to decompress archive - #10341

Merged
thufschmitt merged 3 commits into
NixOS:masterfrom
xokdvium:xokdvium/fix-double-decompression
Mar 30, 2024
Merged

thufschmitt merged 3 commits into
NixOS:masterfrom
xokdvium:xokdvium/fix-double-decompression

Conversation

@xokdvium

Copy link
Copy Markdown
Contributor

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.

@xokdvium
xokdvium requested a review from edolstra as a code owner March 27, 2024 14:53
@xokdvium
xokdvium marked this pull request as draft March 27, 2024 14:53
@xokdvium

Copy link
Copy Markdown
Contributor Author

With this preliminary patch nix-prefetch-url works as expected:

outputs/out/bin/nix-prefetch-url --print-path https://releases.warp.dev/stable/v0.2024.03.12.08.02.stable_01/warp-terminal-v0.2024.03.12.0
8.02.stable_01-1-x86_64.pkg.tar.zst --store ./store
1pb0dl4mbh7l9b0507bqqml5hinfapqdr74f2gqkrndpic28bdzn
/nix/store/apal6zrbwrmrvc677w4i60ks7p2n40xg-warp-terminal-v0.2024.03.12.08.02.stable_01-1-x86_64.pkg.tar.zst
file store/nix/store/apal6zrbwrmrvc677w4i60ks7p2n40xg-warp-terminal-v0.2024.03.12.08.02.stable_01-1-x86_64.pkg.tar.zst                    store/nix/store/apal6zrbwrmrvc677w4i60ks7p2n40xg-warp-terminal-v0.2024.03.12.08.02.stable_01-1-x86_64.pkg.tar.zst: Zstandard compressed data (v0.8+), Dictionary ID: None

@xokdvium
xokdvium marked this pull request as ready for review March 27, 2024 16:20
@xokdvium

Copy link
Copy Markdown
Contributor Author

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.
My concern is maintaining this test. I can't think of a way to verify that content-encoding is actually used without relying on verbose output (which supposedly can change on a whim). I've done the best I can to configure nginx to compress on the fly regardless of settings.

@edolstra

Copy link
Copy Markdown
Member

I can't think of a way to verify that content-encoding is actually used

Do a curl -v ... | grep content-encoding or something like that?

@xokdvium

Copy link
Copy Markdown
Contributor Author

Done in a separate commit. Will squash relevant commits after review. Separate commits should be easier to read

@thufschmitt thufschmitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! That looks good

Left a few comments, but mostly nitpicks that you can freely ignore

Comment thread tests/nixos/gzip-content-encoding.nix Outdated
dontUnpack = true;
nativeBuildInputs = with pkgs; [ zstd ];
buildPhase = ''
tar --zstd -cf archive.tar.zst ${nixpkgs}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Comment thread tests/nixos/gzip-content-encoding.nix Resolved
Comment thread tests/nixos/gzip-content-encoding.nix Outdated
Comment on lines +61 to +62
${fileCmd} -b $archive_path --extension | grep "zst"
! ${fileCmd} -b $archive_path --extension | grep "tar"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(nit, untested)

Suggested change
${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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread src/libutil/compression.cc Outdated
Comment on lines +41 to +42
std::string compressionMethod;
ArchiveDecompressionSource(Source & src, std::string compressionMethod = "")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(nit)

Suggested change
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

@xokdvium xokdvium Mar 29, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

@xokdvium xokdvium Mar 29, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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...

@xokdvium xokdvium Mar 29, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It would be great if someone could reproduce this issue to make sure I'm not braindead and not going crazy

@xokdvium xokdvium Mar 29, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 :)

Sergei Zimmerman added 3 commits March 30, 2024 01:29
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants