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

glibc: Add an environment variable for specifying an NSS search path #111194

Draft
wants to merge 1 commit into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nixos/modules/services/system/nscd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ in

wantedBy = [ "nss-lookup.target" "nss-user-lookup.target" ];

environment = { LD_LIBRARY_PATH = nssModulesPath; };
environment = { NIX_GLIBC_NSS_PATH = nssModulesPath; };

restartTriggers = [
config.environment.etc.hosts.source
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/libraries/glibc/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ stdenv.mkDerivation ({
})

./fix-x64-abi.patch

./nss-path.patch
]
++ lib.optional stdenv.hostPlatform.isMusl ./fix-rpc-types-musl-conflicts.patch
++ lib.optional stdenv.buildPlatform.isDarwin ./darwin-cross-build.patch;
Expand Down
6 changes: 6 additions & 0 deletions pkgs/development/libraries/glibc/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ callPackage ./common.nix { inherit stdenv; } {
])
]);

preHook =
''
outName=$(basename $out)
NIX_CFLAGS_COMPILE+=" -DDEFAULT_NSS_PATH=\"${dirOf builtins.storeDir}/run/glibc-nss-path/''${outName:0:32}:${dirOf builtins.storeDir}/run/glibc-nss-path\"";
'';

# When building glibc from bootstrap-tools, we need libgcc_s at RPATH for
# any program we run, because the gcc will have been placed at a new
# store path than that determined when built (as a source for the
Expand Down
50 changes: 50 additions & 0 deletions pkgs/development/libraries/glibc/nss-path.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
diff -ru -x '*~' glibc-2.32-orig/nss/nsswitch.c glibc-2.32/nss/nsswitch.c
--- glibc-2.32-orig/nss/nsswitch.c 2020-08-05 04:17:00.000000000 +0200
+++ glibc-2.32/nss/nsswitch.c 2021-01-29 19:40:50.730883061 +0100
@@ -349,6 +349,35 @@
__nss_shlib_revision);

ni->library->lib_handle = __libc_dlopen (shlib_name);
+
+ if (ni->library->lib_handle == NULL)
+ {
+ const char *nss_path = __libc_secure_getenv ("NIX_GLIBC_NSS_PATH");
+ if (!nss_path)
+ nss_path = DEFAULT_NSS_PATH;
+
+ const char *pos = nss_path;
+
+ while (*pos)
+ {
+ const char *end = __strchrnul(pos, ':');
+ if (pos != end)
+ {
+ char shlib_path[1024];
Copy link
Member

Choose a reason for hiding this comment

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

How about:

Suggested change
+ char shlib_path[1024];
+ char shlib_path[PATH_MAX];

Not that it PATH_MAX is not a lie..

+ size_t shlib_pathlen = (end - pos) + 1 + strlen (shlib_name) + 1;
+ if (shlib_pathlen < sizeof(shlib_path))
+ {
+ __stpcpy (__stpcpy (__stpncpy (shlib_path, pos, end - pos), "/"), shlib_name);
Copy link
Member

Choose a reason for hiding this comment

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

Is memcpy not better here given that we already calulate the string length?

Copy link
Member

Choose a reason for hiding this comment

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

Alternative would be good old snprintf...

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm just following the surrounding code here, didn't want to deviate from the coding style.

+ ni->library->lib_handle = __libc_dlopen (shlib_path);
+ if (ni->library->lib_handle != NULL)
+ break;
+ }
+ if (!*end) break;
+ pos = end + 1;
+ }
+ }
+ }
+
if (ni->library->lib_handle == NULL)
{
/* Failed to load the library. */
diff -ru -x '*~' glibc-2.32-orig/sysdeps/generic/unsecvars.h glibc-2.32/sysdeps/generic/unsecvars.h
--- glibc-2.32-orig/sysdeps/generic/unsecvars.h 2020-08-05 04:17:00.000000000 +0200
+++ glibc-2.32/sysdeps/generic/unsecvars.h 2021-01-29 15:38:37.067684060 +0100
@@ -27,6 +27,7 @@
"LOCPATH\0" \
"MALLOC_TRACE\0" \
"NIS_PATH\0" \
+ "NIX_GLIBC_NSS_PATH\0" \
"NLSPATH\0" \
"RESOLV_HOST_CONF\0" \
"RES_OPTIONS\0" \