Skip to content

glibc: Add a NIX_LD_SO_CACHE environment variable to glibc#248777

Open
Atry wants to merge 2 commits intoNixOS:masterfrom
Atry:NIX_LD_SO_CACHE
Open

glibc: Add a NIX_LD_SO_CACHE environment variable to glibc#248777
Atry wants to merge 2 commits intoNixOS:masterfrom
Atry:NIX_LD_SO_CACHE

Conversation

@Atry
Copy link
Copy Markdown
Contributor

@Atry Atry commented Aug 12, 2023

Description of changes

This PR adds a NIX_LD_SO_CACHE environment variable to glibc, so that users can specify a fallback library path built from ldconfig, fixing #327854

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.11 Release Notes (or backporting 23.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@Atry Atry changed the title Add a NIX_LD_SO_CACHE environment variable to glibc (fix #902) Add a NIX_LD_SO_CACHE environment variable to glibc (fix https://github.com/NixOS/nix/issues/902) Aug 12, 2023
@Atry Atry changed the title Add a NIX_LD_SO_CACHE environment variable to glibc (fix https://github.com/NixOS/nix/issues/902) Add a NIX_LD_SO_CACHE environment variable to glibc (fix http://nixos/nix#902) Aug 12, 2023
@Atry Atry changed the title Add a NIX_LD_SO_CACHE environment variable to glibc (fix http://nixos/nix#902) Add a NIX_LD_SO_CACHE environment variable to glibc (fix nixos/nix#902) Aug 12, 2023
@Atry Atry changed the title Add a NIX_LD_SO_CACHE environment variable to glibc (fix nixos/nix#902) Add a NIX_LD_SO_CACHE environment variable to glibc Aug 12, 2023
@Atry Atry requested review from Ma27 and copumpkin August 12, 2023 20:33
@Atry Atry changed the title Add a NIX_LD_SO_CACHE environment variable to glibc (glibc): Add a NIX_LD_SO_CACHE environment variable to glibc Aug 12, 2023
@ofborg ofborg bot added the 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch. label Aug 12, 2023
@ofborg ofborg bot requested a review from edolstra August 12, 2023 22:10
@ofborg ofborg bot added 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Aug 12, 2023
@Atry
Copy link
Copy Markdown
Contributor Author

Atry commented Aug 12, 2023

I have tested this PR in https://github.com/Atry/nix-ld-so-cache-test, it resolves cachix/devenv#773

@Atry Atry changed the title (glibc): Add a NIX_LD_SO_CACHE environment variable to glibc glibc: Add a NIX_LD_SO_CACHE environment variable to glibc Aug 13, 2023
@Atry Atry mentioned this pull request Aug 13, 2023
13 tasks
@RaitoBezarius
Copy link
Copy Markdown
Member

Hello there, please ensure you read the CONTRIBUTING guide, this is a change touching glibc, therefore mass rebuilding, therefore it should target at least staging, please read the CONTRIBUTING guide on how to perform retargeting.

On the changes themselves, I am not so sure about the triviality of adding a new environment variable, this makes us diverge from vanilla glibc, while it's true, it fixes a pain point, any new attempt should take into account the previous attempts at this, e.g. NIX_LD_LIBRARY_PATH, etc. (please look for the PRs, I don't have the number in mind).

Furthermore, I am not sure how this interacts with #207893 which is clearly a change that benefits a larger number of folks.

Either case, I don't feel good about this PR without having enough of our expert matter looking into it, I will add them as reviewers, please understand that it may take some time to proceed regarding this.

@ghost
Copy link
Copy Markdown

ghost commented Aug 21, 2023

Please don't add any new ways for getenv() to influence library loading.

This PR definitely creates a trivial exploit, since you didn't add NIX_LD_SO_CACHE to UNSECURE_ENVVARS in sysdeps/generic/unsecvars.h.

Even if you do add it to that list, it is far from clear that it does not create other exploits. We don't really have the resources to do this kind of analysis, and we would have to repeat the analysis on every glibc version bump.

Please don't add any new ways for getenv() to influence library loading.

@Atry
Copy link
Copy Markdown
Contributor Author

Atry commented Aug 22, 2023

Do you suggest that the only secure way to add an environment variable for the interpreter is to add it to upstream glibc and make ensure that the upstream maintainer is aware of it in version bumping, like #248547 ?

@RaitoBezarius
Copy link
Copy Markdown
Member

Do you suggest that the only secure way to add an environment variable for the interpreter is to add it to upstream glibc and make ensure that the upstream maintainer is aware of it in version bumping, like #248547 ?

I would agree with @amjoseph-nixpkgs analysis here, it seems saner to move it to a "non vanilla glibc" variant that can be used "exceptionally" rather than "default", or, this needs to be upstreamed or brought forward to glibc folks.

We do know some stuff, but we are not glibc developers, and if anything, we probably know how wicked those things can be.

Furthermore, I am not comfortable creating a precedent to accept this type of change in vanilla glibc.
I am more convinced by external approaches as https://github.com/Mic92/nix-ld and bringing them in a meaningful way to make them actually usable.

@Atry
Copy link
Copy Markdown
Contributor Author

Atry commented Aug 22, 2023

nix-ld is a nice tool, but it does not solve #327854 , especially when a nix binary loads a shared library, which does not have runpaths to nix store.

Vanilla Python packages in manylinux2014 ABI do not have runpaths.

@RaitoBezarius
Copy link
Copy Markdown
Member

RaitoBezarius commented Aug 22, 2023

nix-ld is a nice tool, but it does not solve NixOS/nixpkgs#327854 , especially when a nix binary loads a shared library, which does not have runpaths to nix store.

Vanilla Python packages in manylinux2014 ABI do not have runpaths.

If you have a Nix-built binary which does not have its RUNPATH completely filled and set, there's a bigger problem here. The toolchain was not correctly set for success.

You have two worlds:

  • Nix-built binaries: correct-by-construction, they have all their RUNPATH set properly.
  • External binaries on NixOS: will read the FHS dynamic loader, with nix-ld, they will just work because nix-ld is exactly that: a compatibility FHS dynamic loader.

If you need more than this model, you have to explain why you are sitting in the "in-between" which is hard to control.

If by this, you mean running Nix's Python and mixing with manylinux2014 ABI binaries, hah, I would argue that the fact that it won't work by default, is a feature to me. Python PyPI is full of malware and wheels is an opaque binary format unreviewed by PyPA, NixOS is nice to thwart any attempt to run arbitrary code that I don't trust.

If folks want this model to work out of the box, I would urge them to build a second package set for those, I am definitely against them by default.

@Mic92
Copy link
Copy Markdown
Member

Mic92 commented Aug 22, 2023

I think the authors intention here is to extend interpreters library path. This is not covered by nix-ld yet. Let's take for example installing a native python extension with pip that needs an additional native library. Just now one can set LD_LIBRARY_PATH to fix this but than this LD_LIBRARY_PATH is also picked up by other applications where it overrides the RPATH of our programs. This can lead to crashes if incompatible libraries are loaded (i.e. library compiled against different glibc versions). I haven't checked yet the implications of NIX_LD_SO_CACHE, but some environment variable that has lower priority than the RPATH of the binary would be great.

@RaitoBezarius
Copy link
Copy Markdown
Member

I think the authors intention here is to extend interpreters library path. This is not covered by nix-ld yet. Let's take for example installing a native python extension with pip that needs an additional native library. Just now one can set LD_LIBRARY_PATH to fix this but than this LD_LIBRARY_PATH is also picked up by other applications where it overrides the RPATH of our programs. This can lead to crashes if incompatible libraries are loaded (i.e. library compiled against different glibc versions). I haven't checked yet the implications of NIX_LD_SO_CACHE, but some environment variable that has lower priority than the RPATH of the binary would be great.

That's what the linked PR #248547 offers I believe.

@Atry
Copy link
Copy Markdown
Contributor Author

Atry commented Aug 23, 2023

Yes, this PR and #248547 are very similar

@SomeoneSerge
Copy link
Copy Markdown
Contributor

SomeoneSerge commented Sep 10, 2023

You have two worlds:

Nix-built binaries: correct-by-construction, they have all their RUNPATH set properly.
...

...have their runpath set properly, unless they use OpenGL or CUDA. There's at least one in-between world: "Nix-built in not-a-NixOS runtime"

I think nix-ld is an absolutely wonderful approach to solve "FHS-on-NixOS" (definitely preferable to fhs user namespaces): it's a decent UX, and it only affects applications that actually expect an FHS environment. However, OpenGL/CUDA/etc programs built by Nix aren't quite "correct" or "cross-platform" today. They are usable outside NixOS with the help of extra tools, but if we wanted to actually support FHS environments, we'd have to patch ld.so more?

P.S. I'm still unfamiliar with #207893 and with how far it's progressed

P.P.S. @amjoseph-nixpkgs what is your opinion about nix-ld? It does, in principle, expose a new variable...

@ghost
Copy link
Copy Markdown

ghost commented Oct 27, 2023

"Nix-built in not-a-NixOS runtime"

Nitpick: this should be "Nix-built in FHS runtime". There are plenty of non-NixOS nixpkgs-based systems out there. Spectrum is one.

P.P.S. @amjoseph-nixpkgs what is your opinion about nix-ld? It does, in principle, expose a new variable...

I haven't tried nix-ld myself, but it appears to be an example of the approach that I prefer: don't modify the "standard" linker/elf-interpreter; instead provide a modified one that is used if/when necessary.

@SomeoneSerge
Copy link
Copy Markdown
Contributor

Nitpick: this should be "Nix-built in FHS runtime"

👍🏻 ❤️

I haven't tried nix-ld myself, but it appears to be an example of the approach that I prefer: don't modify the "standard" linker/elf-interpreter; instead provide a modified one that is used if/when necessary.

Yes and no: from the point of view of the FHS app, nix-ld "replaces" /lib64/ld-linux-x86-64.so.2, which is easy because it's linked impurely. We, otoh, link a concrete loader, and that's exactly what would (and should) be used in the FHS environment as in any other environment, so we likely want it to support an escape hatch in the lines of LD_FALLBACK_LIBRARY_PATH.

So, what does the process of "talking to glibc people" look like if we want one of these solutions upstreamed?

@RaitoBezarius
Copy link
Copy Markdown
Member

RaitoBezarius commented Oct 27, 2023 via email

@Atry
Copy link
Copy Markdown
Contributor Author

Atry commented Oct 27, 2023

I'd recommend sending an email explaining the whole problem and situation and trying to convince them of re-introducing something like RPATH (not RUNPATH).

It was the nix community's decision to use RUNPATH instead of RPATH. GNU's dynamic linker supports both.

@RaitoBezarius
Copy link
Copy Markdown
Member

RaitoBezarius commented Oct 27, 2023 via email

@nixos-discourse
Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/on-nixpkgs-and-the-ai-follow-up-to-2023-nix-developer-dialogues/37087/2

@wegank wegank added 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 2.status: merge conflict This PR has merge conflicts with the target branch labels Mar 19, 2024
@nixpkgs-ci nixpkgs-ci bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 26, 2025
@nixpkgs-ci nixpkgs-ci bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux-stdenv This PR causes stdenv to rebuild on Linux and must target a staging branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants