Skip to content

AWS SDK breaks bultin:fetchurl builder and overrides OpenSSL engine  #5947

Open

Description

@st8ed

Describe the bug

Setting up any S3 store substituter causes builtin:fetchurl builder to freeze indefinitely and silently switches global OpenSSL engine to S2N (AWS's simplified and audited implementation of TLS).

I encountered the bug when boostrapping nixpkgs.glibc and struggled with its reproducibility as builtins:fetchurl is currently used on a rare occasion to boot actual fetchurl, which itself is not affected. As it turns out, the bug also modifies OpenSSL global state and could surface someplace where any cryptographic operation is involved.

Steps To Reproduce

The following self-sustained shell command reproduces the problem (sudo is essential to override substituters):

sudo nix build \
    --expr 'import <nix/fetchurl.nix> { url = "https://example.com/file.tar.xz"; sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; }' \
    --option sandbox false \
    --option substituters 's3://REDACTED' \
    --debug --verbose

After failed attempts to access inexistent REDACTED bucket build freezes indefinitely with the following output:

...
sandbox setup: closing leaked FD 24
sandbox setup: closing leaked FD 25
building '/nix/store/5psjg9667p5wm7shh3jpfxk5rsghr18r-file.tar.xz.drv'...
waiting for children
building of '/nix/store/5psjg9667p5wm7shh3jpfxk5rsghr18r-file.tar.xz.drv!out' from .drv file: read 457 bytes
download thread waiting for 10000 ms
downloading 'https://example.com/file.tar.xz'...
starting download of https://example.com/file.tar.xz
curl: Couldn't find host example.com in the /etc/nix/netrc file; using defaults
waiting for children
building of '/nix/store/5psjg9667p5wm7shh3jpfxk5rsghr18r-file.tar.xz.drv!out' from .drv file: read 152 bytes
curl: Connected to example.com (93.184.216.34) port 443 (#0)
waiting for children

Disabling substituters or leaving only non-S3 substituters restores correct behavior (error 404). Explicitly added for completeness sandbox option actually makes no difference.

Expected behavior

The actual execution of builtin:fetchurl shouldn't be affected by the choice of substituters and shouldn't get stuck.

nix-env --version output

nix-env (Nix) 2.6.0pre20220118_4af88a4

Additional context

I found out that the forked Nix build process inside namespaced sandbox is stuck in an AWS TLS implementation of OpenSSL in this loop at s2n-tls/main/utils/s2n_random.c:245 trying to read from closed file descriptor for /dev/urandom. entropy_fd is assigned there before nix splits processes and it still holds some positive value, but it's no longer related to /dev/urandom. On my system entropy_fd happens to really point to libCURL's opened socket and it can't produce enough output to bootstrap S2N's random generator, although I believe in some cases it can.

S2N takes place instead of ordinary OpenSSL implementation (engine) once AWS::InitAPI is called and libs2n.so is loaded, and as it seems to me there's no way to prevent it from modifying global state as it calls ENGINE_set_default from s2n_init().

Commenting closeMostFDs call allows Nix to finish the download attempt (with 404 error in the example), so it confirms that the problem is due to closed 'leaked' file descriptors.

I attempted different approaches to fix this.

Patch S2N

Using getrandom() instead of /dev/urandom actually solves the issue, but I doubt it can be merged in upstream as this feature is relatively new. Nevertheless, I did an overlay to demonstrate.

A much more thorough approach is to ensure proper management of /dev/urandom descriptors the way it is done in OpenSSL. It could be borrowed and implemented in S2N, but I'm not sure if there are other simplifications in S2N such as this, so this way OpenSSL library seems to me more appealing with its maturity and a better fit for Nix's cross-platform purposes. So perhaps S2N shouldn't be used in the first place.

Get rid of S2N altogether in favor of OpenSSL

If this is acceptable, I think it would be better if maintainers of aws-sdk-cpp could add an appropriate interface to build a S2N-less version of the sdk.

I implemented fix in my fork. I'm happy to PR if it's okay.

Activity

  1. edolstra commented on Jan 20, 2022

    @edolstra
    Member

    I didn't know there was such a thing as a global SSL engine on Linux! It's quite bad that aws-sdk-cpp changes the behaviour of libcurl.

    Aws-sdk-cpp should definitely check whether the urandom FD is still valid, because it's a big potential security bug if it ends up reading from a non-random file.

    We could disable closeMostFDs() for builtin builders, though that would slightly increase the attack surface (e.g. if you can get the builtin builder to execute arbitrary code, you can write to the parent's file descriptors).

    If this is acceptable, I think it would be better if maintainers of aws-sdk-cpp could add an appropriate interface to build a S2N-less version of the sdk.

    I'm confused, isn't that what the -DBYO_CRYPTO=ON flag does?

  2. st8ed commented on Jan 20, 2022

    @st8ed
    Author

    Aws-sdk-cpp should definitely check whether the urandom FD is still valid, because it's a big potential security bug if it ends up reading from a non-random file.

    My thoughts exactly. Luckily, it seems it's used only to seed random generator with no direct ability to retrieve the read value.

    We could disable closeMostFDs() for builtin builders, though that would slightly increase the attack surface (e.g. if you can get the builtin builder to execute arbitrary code, you can write to the parent's file descriptors).

    I did so and got segfault'ed somewhere in finalizers/destructors. Didn't investigate it thoroughly because this approach seems too radical to me.

    I even added /dev/urandom as a symbolic exception in closeMostFDs(), but then I thought it's not very nice to adjust to some library I personally don't even plan to use. Although I really appreciate AWS contribution and spent resources to develop and audit their own TLS implementation.

    I'm confused, isn't that what the -DBYO_CRYPTO=ON flag does?

    Yes, it is. But it's not as trivial as one CMake flag. One needs to simultaneously pass it to multiple aws-sdk packages and link them together because they provide different function implementations and linking simply fails due to missing symbols if only some of packages are built with this flag. And any missed package can still bring lib2sn.so to Nix.

    Hard-coding it into Nix flake/derivative can lead to eventually missing some new aws-sdk component, which will be then linked with libs2n.so and the issue will silently come back.

    So I propose some interfacing like aws-sdk-cpp.override { withS2N = false; } or aws-sdk-cpp.no-s2n in nixpkgs. I'm not quite sure if such interface will better fit in aws-sdk-cpp itself, or aws-crt-cpp or even ./all-packages.nix.

  3. mupdt commented on Mar 28, 2023

    @mupdt
    Contributor

    We just ran into this bug and had to work around it. We worked it around by adding an exception in the closeMostFDs logic so that it doesn't close /dev/urandom.

    The workaround doesn't feel right so a proper fix would be super welcome in the long term. @edolstra, @st8ed, would you say that we should go ahead with the BYO_CRYPTO fix?

    Side note: At first we did not realize it's the same bug, so we dug deep into the bug and came to exactly the same conclusions as @st8ed.

  4. matshch commented on Dec 14, 2023

    @matshch

    We also ran into this issue while trying to bootstrap our own S3 cache. It looks like it wasn't reported to the upstream, so I took the opportunity to file the issue: aws/s2n-tls#4324.

    @edolstra, we are ready to open a pull request with some workaround, but it requires decision which one of the discussed above to implement.

  5. 27 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions