Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xmlsec: add library path to lt_dladdsearchdir #68488

Merged
merged 2 commits into from Jul 29, 2020

Conversation

B4dM4n
Copy link
Contributor

@B4dM4n B4dM4n commented Sep 11, 2019

Motivation for this change

When linking to libxmlsec, the linking binary/library has to ensure that the xmlsec $out/lib directory is in the LD_LIBRARY_PATH, to esure libxmlsec can find and load the crypto libraries. xmlsec1 uses wrapProgram for this, but when an intermediate library is linking libxmlsec, it has no control over the LD_LIBRARY_PATH (like python-xmlsec ).

Adding $out/lib to lt_dladdsearchdir during library initialization works around LD_LIBRARY_PATH and ensures the crypto libraries can always be found.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nix-review --run "nix-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.
Notify maintainers

This makes it easier to use libxmlsec in other libraries like
python-xmlsec.
@matthewbauer
Copy link
Member

I think it would be good if can avoid patching like this. These patches usually end up breaking between version updates. I think we might be able to just add $out/lib to RPATH for $out/lib/libxmlsec.* and $out/bin/xmlsec, and put that in the postInstal...

@B4dM4n
Copy link
Contributor Author

B4dM4n commented Sep 19, 2019

Modifying the RPATH didn't work for me.

The xmlsec crypto libraries aren't linked dynamically, but get loaded with lt_dlopenext. This function uses either a libtool integrated loader, which can be influenced via lt_dladdsearchdir, LD_LIBRARY_PATH and LTDL_LIBRARY_PATH or it uses dlopen which can be influenced via LD_LIBRARY_PATH and the RPATH of the library that calls dlopen, which is libltdl.so in this case.

I tested the dlopen variant by appending $out/lib to the libxmlsec1.so library RPATH and straceing the xmlsec1 binary (without wrapProgram "$out/bin/xmlsec1"), which already includes $out/lib in it's RPATH. (strace log: https://pastebin.com/2ZBVWWbK)

It only tries to load libxmlsec1-openssl.so from /nix/store/xxx-glibc-2.27/lib/ (except the sane path, which is my LD_LIBRARY_PATH), which is the only RPATH entry of libltdl.so.
So dlopen and RPATH don't work together when used in a library.

These are the only options I could find so far.

@stale
Copy link

stale bot commented Jun 1, 2020

Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the
    related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on
    irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 1, 2020
@austinbutler
Copy link
Member

austinbutler commented Jul 3, 2020

I'm also trying to use python-xmlsec and it can't load the crypto library:

❯ python -c "import xmlsec"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
xmlsec.Error: (1, 'cannot load crypto library for xmlsec.')

@jtojnar in researching this problem I've seen your name pop up on related issues elsewhere.

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 3, 2020
@austinbutler
Copy link
Member

austinbutler commented Jul 3, 2020

Looks like pysaml2 also has trouble with xmlsec and does patching:

src = ./hardcode-xmlsec1-path.patch;

@jtojnar
Copy link
Contributor

jtojnar commented Jul 3, 2020

The changes look reasonable. Future updates can break any customizations and I prefer when the breakage is loud so that we can be aware of it and fix it. Since this was not caught by the project tests adding a derivation checking if this still works to passthru.tests would be a good idea.


Looks like xmlSecCryptoDLLoadLibrary only takes crypto library name like openssl or nss and then it constructs libxmlsec1-nss library name, which is lt_dlopened

  1. https://github.com/lsh123/xmlsec/blob/c882d225f87194a8d457ad61ee23ff4befdb86c7/src/dl.c#L442
  2. https://github.com/lsh123/xmlsec/blob/c882d225f87194a8d457ad61ee23ff4befdb86c7/src/dl.c#L451
  3. https://github.com/lsh123/xmlsec/blob/c882d225f87194a8d457ad61ee23ff4befdb86c7/src/dl.c#L118
  4. https://github.com/lsh123/xmlsec/blob/c882d225f87194a8d457ad61ee23ff4befdb86c7/src/dl.c#L252

so alternately, we could hard-code the absolute path within xmlSecCryptoDLLibraryConstructFilename. That might have the benefit that any future changes would complain loudly by breaking tests but it would require us to include the file extension too.

@B4dM4n
Copy link
Contributor Author

B4dM4n commented Jul 14, 2020

Looks like pysaml2 also has trouble with xmlsec and does patching:

pysaml2 only uses the xmlsec1 binary, so nothing would change there.

Since this was not caught by the project tests adding a derivation checking if this still works to passthru.tests would be a good idea.

The tests seem to only run the xmlsec1 binary and library loading is made possible by setting LD_LIBRARY_PATH in the Makefile.

Adding a passthru.tests check for library loading is a good idea. I'm currently working on such a test.

so alternately, we could hard-code the absolute path within xmlSecCryptoDLLibraryConstructFilename. That might have the benefit that any future changes would complain loudly by breaking tests but it would require us to include the file extension too.

I'm not sure if hardcoding the paths has an advantage over embedding the lib path. If the tests can ensure that library loading works, both should fail equally fast on a breaking change.

The benefit of embedding the lib path would be that dependent packages can still modify the search_dir and load their own libraries instead of the embedded ones.

@matthewbauer matthewbauer merged commit c2ea731 into NixOS:master Jul 29, 2020
@B4dM4n B4dM4n deleted the xmlsec-dlsearchdir branch July 30, 2020 06:58
@glittershark
Copy link
Member

@B4dM4n did you ever end up packaging python-xmlsec? I'm trying to get it built right now and running into dlopen issues

@glittershark
Copy link
Member

I'm also trying to use python-xmlsec and it can't load the crypto library:

❯ python -c "import xmlsec"
Traceback (most recent call last):
File "", line 1, in
xmlsec.Error: (1, 'cannot load crypto library for xmlsec.')

this is the exact issue I'm getting, even with this patch

@glittershark
Copy link
Member

never mind, I'm actually just running an older version of nixpkgs than when this was merged

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

Successfully merging this pull request may close these issues.

None yet

6 participants