realpath-workaround.patch 1.53 KB
Newer Older
1 2
Musl realpath() implementation currently depends on /proc which is
not available when setting up pivot root. For the time being just
3 4 5
fallback to a naive normalization algorithm originated from
VoidLinux' xbps. If there was path that would have required advanced
normalizing as provided by realpath() the following parse_mountinfo()
6 7
will fail.

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

diff --git bind-mount.c.orig bind-mount.c
index 045fa0e..d05b540 100644
--- bind-mount.c.orig
+++ bind-mount.c
@@ -23,6 +23,28 @@
 #include "utils.h"
 #include "bind-mount.h"
 
+#ifndef __GLIBC__
+static char *
+normpath(char *path)
+{
+  char *seg = NULL, *p = NULL;
+
+  for (p = path, seg = NULL; *p; p++) {
+    if (strncmp(p, "/../", 4) == 0 || strncmp(p, "/..", 4) == 0) {
+      memmove(seg ? seg : p, p+3, strlen(p+3) + 1);
+      return normpath(path);
+    } else if (strncmp(p, "/./", 3) == 0 || strncmp(p, "/.", 3) == 0) {
+      memmove(p, p+2, strlen(p+2) + 1);
+    } else if (strncmp(p, "//", 2) == 0 || strncmp(p, "/", 2) == 0) {
+      memmove(p, p+1, strlen(p+1) + 1);
+    }
+    if (*p == '/')
+      seg = p;
+  }
+  return path;
+}
+#endif
+
 static char *
 skip_token (char *line, bool eat_whitespace)
 {
@@ -397,7 +419,11 @@ bind_mount (int           proc_fd,
43 44 45
      path, so to find it in the mount table we need to do that too. */
   resolved_dest = realpath (dest, NULL);
   if (resolved_dest == NULL)
46 47 48 49 50
+#ifdef __GLIBC__
     return 2;
+#else
+    resolved_dest = normpath(strdup(dest));
+#endif
51 52 53
 
   mount_tab = parse_mountinfo (proc_fd, resolved_dest);
   if (mount_tab[0].mountpoint == NULL)