mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] dynlink.c: locate ld-musl-<arch>.path better.
@ 2022-03-20 14:19 Harmen Stoppels
  0 siblings, 0 replies; only message in thread
From: Harmen Stoppels @ 2022-03-20 14:19 UTC (permalink / raw)
  To: musl

Fixes an issue where the dynamic loader can't locate the
`ld-musl-<arch>.path` file when the interpreter is not a normalized
path. The following works after this commit:

```
./path/to/ld-musl-<arch>.so.1 ./exe
/absolute/path/to////ld-musl-<arch>.so.1 ./exe
/absolute/path/to/./ld-musl-<arch>.so.1 ./exe
```

The `ld-musl-<arch>.path` file is now located by simply appending
`/../etc/ld-musl-<arch>.path` to the directory name of `ldso.name`, or
falls back to `/etc/ld-musl-<arch>.path` when no directory separator is
found in `ldso.name`.

This means that there's currently one limitation where

```
PATH="/absolute/path/to/loader/dir:$PATH" ld-musl-<arch.so.1> ./exe
```

still fails to locate `ld-musl-<arch>.path` relative to the dynamic
loader's path.
---
 ldso/dynlink.c | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index 5b9c8be4..5a508ddf 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -1061,25 +1061,18 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
 		}
 		if (fd == -1) {
 			if (!sys_path) {
-				char *prefix = 0;
-				size_t prefix_len;
-				if (ldso.name[0]=='/') {
-					char *s, *t, *z;
-					for (s=t=z=ldso.name; *s; s++)
-						if (*s=='/') z=t, t=s;
-					prefix_len = z-ldso.name;
-					if (prefix_len < PATH_MAX)
-						prefix = ldso.name;
+				/* etc_ldso_path is <dir of ld.so>/../etc/ld-musl-<arch>.path when
+				 * we have a path for ld.so, otherwise fall back to
+				 * /etc/ld-musl-<arch>.path. */
+				char suffix[] = "/etc/ld-musl-" LDSO_ARCH ".path";
+				char *ldso_dir = strrchr(ldso.name, '/');
+				size_t prefix_len = ldso_dir ? ldso_dir - ldso.name + 3 : 0;
+				char etc_ldso_path[prefix_len + sizeof suffix];
+				if (ldso_dir) {
+					memcpy(etc_ldso_path, ldso.name, prefix_len - 2);
+					memcpy(etc_ldso_path + prefix_len - 2, "..", 2);
 				}
-				if (!prefix) {
-					prefix = "";
-					prefix_len = 0;
-				}
-				char etc_ldso_path[prefix_len + 1
-					+ sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
-				snprintf(etc_ldso_path, sizeof etc_ldso_path,
-					"%.*s/etc/ld-musl-" LDSO_ARCH ".path",
-					(int)prefix_len, prefix);
+				memcpy(etc_ldso_path + prefix_len, suffix, sizeof suffix);
 				fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC);
 				if (fd>=0) {
 					size_t n = 0;
--
2.25.1



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-20 14:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-20 14:19 [musl] [PATCH] dynlink.c: locate ld-musl-<arch>.path better Harmen Stoppels

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).