Skip to contentSkip to conversation

erofs-utils: lib: follow symlinked directories in tar entry paths

PATCH New Yaroslav Borbat 1 participant 1 message Sep 30 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: follow symlinked directories in tar entry paths

From: Yaroslav Borbat <[email protected]>

Some OCI layers store entries under a symlinked directory, e.g. ./lib64/foo with ./lib64 -> usr/lib64. erofs_rebuild_get_dentry() fails with -ENOTDIR on them, so containerd's erofs differ cannot unpack such layers.

Fix it by resolving the rest of the path against the symlink target, as tar extraction does.

---
 lib/rebuild.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
lib/rebuild.c +55 -0
index ebac1c5..7030e67 100644
@@ -82,8 +82,58 @@ struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name)
 	return NULL;
 }
 
+/* the same limit as MAXSYMLINKS in the kernel */
+#define EROFS_REBUILD_MAXSYMLINKS	40
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *pwd,
+		char *path, bool aufs, bool *whout, bool *opq, bool to_head,
+		unsigned int nlinks);
+
+/*
+ * Some OCI image layers store entries under a symlinked directory, e.g.
+ * `lib64/libfoo.so` with `lib64 -> usr/lib64`.  Tar extraction follows such
+ * symlinks, so resolve the rest of the path against the symlink target in
+ * the same way.  Absolute targets are resolved against the root of the tree
+ * and `..` never goes above it; symlink loops end up with -ELOOP.
+ */
+static struct erofs_dentry *erofs_rebuild_follow_link(struct erofs_inode *dir,
+		const char *link, char *slash, bool aufs, bool *whout,
+		bool *opq, bool to_head, unsigned int nlinks)
+{
+	const char *rest = slash + 1;
+	struct erofs_dentry *d;
+	char *path;
+
+	*slash = '/';
+	while (*rest == '/')
+		++rest;
+	if (!*rest)
+		return ERR_PTR(-ENOTDIR);
+	if (nlinks >= EROFS_REBUILD_MAXSYMLINKS)
+		return ERR_PTR(-ELOOP);
+
+	if (*link == '/')
+		while (dir->i_parent && dir->i_parent != dir)
+			dir = dir->i_parent;
+
+	if (asprintf(&path, "%s/%s", link, rest) < 0)
+		return ERR_PTR(-ENOMEM);
+	d = __erofs_rebuild_get_dentry(dir, path, aufs, whout, opq, to_head,
+				       nlinks + 1);
+	free(path);
+	return d;
+}
+
 struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 		char *path, bool aufs, bool *whout, bool *opq, bool to_head)
+{
+	return __erofs_rebuild_get_dentry(pwd, path, aufs, whout, opq,
+					  to_head, 0);
+}
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *pwd,
+		char *path, bool aufs, bool *whout, bool *opq, bool to_head,
+		unsigned int nlinks)
 {
 	struct erofs_dentry *d = NULL;
 	char *s = path;
@@ -123,6 +173,11 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 			d = erofs_d_lookup(pwd, s);
 			if (d) {
 				if (d->type != EROFS_FT_DIR) {
+					if (slash && d->type == EROFS_FT_SYMLINK &&
+					    d->inode && d->inode->i_link)
+						return erofs_rebuild_follow_link(pwd,
+							d->inode->i_link, slash, aufs,
+							whout, opq, to_head, nlinks);
 					if (slash)
 						return ERR_PTR(-ENOTDIR);
 				} else if (to_head) {
Signature · 1 line
2.55.0