summaryrefslogtreecommitdiff
path: root/libgcompat
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2018-01-14 23:54:49 -0600
committerSamuel Holland <samuel@sholland.org>2018-01-15 00:43:49 -0600
commit6d391f9ef2d2588ae2faf0efb73bd1ac934b063a (patch)
tree2e8b0038a6ff22006346f54f8ef6ac811e0377e3 /libgcompat
parentbf7102a303ec86f2d9bc9fd451db78a6815ca175 (diff)
downloadgcompat-6d391f9ef2d2588ae2faf0efb73bd1ac934b063a.tar.gz
gcompat-6d391f9ef2d2588ae2faf0efb73bd1ac934b063a.tar.bz2
gcompat-6d391f9ef2d2588ae2faf0efb73bd1ac934b063a.tar.xz
gcompat-6d391f9ef2d2588ae2faf0efb73bd1ac934b063a.zip
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 <samuel@sholland.org>
Diffstat (limited to 'libgcompat')
-rw-r--r--libgcompat/readlink.c85
1 files changed, 85 insertions, 0 deletions
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 <dlfcn.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#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);
+}