From 6d391f9ef2d2588ae2faf0efb73bd1ac934b063a Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sun, 14 Jan 2018 23:54:49 -0600 Subject: readlink: Intercept readlink("/proc/self/exe") This allows programs run through gcompat to fork and re-exec themselves. It fixes readlink("/proc/self/exe") to return the executable's absolute path, instead of musl's path. Signed-off-by: Samuel Holland --- Makefile | 1 + libgcompat/readlink.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 libgcompat/readlink.c diff --git a/Makefile b/Makefile index 5e5a12e..b6bee29 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ LIBGCOMPAT_SRC = \ libgcompat/netdb.c \ libgcompat/pthread.c \ libgcompat/pwd.c \ + libgcompat/readlink.c \ libgcompat/resolv.c \ libgcompat/resource.c \ libgcompat/setjmp.c \ diff --git a/libgcompat/readlink.c b/libgcompat/readlink.c new file mode 100644 index 0000000..63def96 --- /dev/null +++ b/libgcompat/readlink.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include + +#ifndef LINKER +#error LINKER must be defined +#endif + +static char exe[PATH_MAX], *linker; +static ssize_t (*real_readlink)(const char *, char *, size_t); + +ssize_t readlink(const char *path, char *buf, size_t len) +{ + if (real_readlink == NULL) { + real_readlink = dlsym(RTLD_NEXT, "readlink"); + if (real_readlink == NULL) { + return -1; + } + } + + if (!strcmp(path, "/proc/self/exe")) { + int fd; + + if (exe[0] == '\0') { + if (linker == NULL) { + linker = realpath(LINKER, NULL); + if (linker == NULL) { + return -1; + } + } + if (real_readlink(path, exe, sizeof(exe)) < 1) { + goto fail; + } + if (!strcmp(exe, linker)) { + char c; + int arg = 0; + ssize_t arglen; + + fd = open("/proc/self/cmdline", + O_RDONLY | O_CLOEXEC); + if (fd < 0) { + goto fail; + } + /* Skip the --argv0/--preload ldso args. + * This number must be kept in sync with the + * argument order in loader/loader.c */ + while (arg < 5) { + if (read(fd, &c, 1) != 1) { + goto fail_close; + } + if (c == '\0') { + ++arg; + } + } + /* Read the executable path from the cmdline. */ + arglen = read(fd, exe, sizeof(exe)); + if (arglen < 1) { + goto fail_close; + } + close(fd); + /* Ensure the path exists, fits, and has NUL. */ + if (exe[0] == '\0') { + goto fail; + } + if (strnlen(exe, arglen) == (size_t) arglen) { + goto fail; + } + } + } + + return stpncpy(buf, exe, len) - buf; + + fail_close: + close(fd); + fail: + exe[0] = '\0'; + return -1; + } + + return real_readlink(path, buf, len); +} -- cgit v1.2.3-60-g2f50