From 367bbda713c5a89d501c79ac2340822078923ddf Mon Sep 17 00:00:00 2001 From: Masamichi Takagi Date: Thu, 16 Apr 2020 20:46:46 +0900 Subject: [PATCH] mcexec: Fix resolving library path for LD_PRELOAD Fixes: 8ee1d61d "Revert "Detect hang of McKernel in mcexec"" Fixes: b87ac8b8 "reproductible builds: remove most install paths in c code" Change-Id: I8ef9ab81cd0a41ccd0e227ebc3e45c0745c150e9 --- executer/user/mcexec.c | 54 ++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/executer/user/mcexec.c b/executer/user/mcexec.c index b40d7466..343e6fc4 100644 --- a/executer/user/mcexec.c +++ b/executer/user/mcexec.c @@ -1875,39 +1875,51 @@ static ssize_t find_libdir(char *libdir, size_t len) ssize_t rc; size_t linelen = 0; char *line = NULL; - char *ihklib, *slash; + char *slash; + char path[PATH_MAX]; + char cmd[PATH_MAX]; - filep = fopen("/proc/self/maps", "r"); - if (!filep) { + rc = readlink("/proc/self/exe", path, sizeof(path)); + if (rc < 0) { rc = -errno; - fprintf(stderr, "could not open /proc/self/maps: %zd\n", -rc); - return rc; + fprintf(stderr, "readlink /proc/self/exe: %ld\n", -rc); + goto out; + } else if (rc >= sizeof(path)) { + strcpy(path, "/proc/self/exe"); + } else { + path[rc] = '\0'; } - while ((rc = getline(&line, &linelen, filep)) > 0) { - ihklib = strstr(line, "libihk.so"); - if (ihklib) - break; - } - if (!ihklib) { - fprintf(stderr, "mcexec does not have libihk.so loaded?\n"); - rc = -ENOENT; + rc = snprintf(cmd, sizeof(cmd), + "objdump -x %s | awk '/RPATH/ { print $2 }'", + path); + if (rc >= sizeof(cmd)) { + rc = -ERANGE; goto out; } + filep = popen(cmd, "r"); + if (!filep) { + rc = -errno; + fprintf(stderr, "objdump /proc/self/exe: %ld\n", -rc); + goto out; + } + + rc = getline(&line, &linelen, filep); + if (rc <= 0) { + rc = -errno; + fprintf(stderr, "RPATH not found: %ld\n", -rc); + goto out; + } + line[rc - 1] = 0; + slash = strchr(line, '/'); if (!slash) { rc = -EINVAL; goto out; } - /* leave / iff root of filesystem */ - if (slash + 1 != ihklib) - ihklib--; - - *ihklib = 0; - - rc = snprintf(libdir, len, "%s", slash); + rc = snprintf(libdir, len, "%s", line); if (rc > len) { rc = -ERANGE; @@ -1915,7 +1927,7 @@ static ssize_t find_libdir(char *libdir, size_t len) } out: - fclose(filep); + pclose(filep); free(line); return rc; }