Skip to contentSkip to conversation

erofs-utils: lib: follow symlinks on intermediate tar path components

PATCH Superseded [email protected] 2 participants 5 messages Jul 25 – Sep 20 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: follow symlinks on intermediate tar path components

From: Mikhail Malyshev <[email protected]>

mkfs.erofs --tar fails to convert layers that store files underneath a symlinked directory, a very common "usr-merge" layout in RHEL/UBI/Fedora based OCI images. For example a base layer contains

    lib64 -> usr/lib64        (symlink)
    usr/lib64/                (directory)
    lib64/libc.so.6           (regular file, path goes THROUGH the symlink)

erofs_rebuild_get_dentry() walks the path component by component and, when it hits the "lib64" component, finds an existing dentry that is a symlink rather than a directory. Since there are still components left to resolve it bails out with -ENOTDIR. In practice this surfaces as a bogus ~2 TiB image and "Could not format the device", making the whole layer unconvertible even though nothing is wrong with the tar.

A real filesystem (and containerd's walking differ, which applies the same layers via fs.RootPath) resolves such a path by following the symlink to its target directory. Do the same during tar rebuild: when an intermediate path component resolves to a symlink, follow it to the target directory (relative targets from the containing directory, absolute targets from the tree root), chasing chained symlinks up to a bounded depth to avoid loops.

---
 lib/rebuild.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 70 insertions(+), 2 deletions(-)
lib/rebuild.c +70 -2
index 108a464..febc95c 100644
@@ -82,8 +82,60 @@ struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name)
 	return NULL;
 }
 
-struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
-		char *path, bool aufs, bool *whout, bool *opq, bool to_head)
+/* Bound symlink chains during path resolution, like the kernel's MAXSYMLINKS. */
+#define EROFS_REBUILD_SYMLINK_DEPTH	40
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
+		struct erofs_inode *pwd, char *path, bool aufs,
+		bool *whout, bool *opq, bool to_head, unsigned int depth);
+
+/*
+ * Resolve the target of an intermediate symlink path component to its
+ * directory inode, following chained symlinks up to a bounded depth.  This
+ * lets tar layers that store files under a symlinked directory be converted
+ * the same way a real filesystem extracts them, e.g. usr-merge layouts where
+ * "/lib64 -> usr/lib64" coexists with entries such as "lib64/libc.so.6".
+ */
+static struct erofs_inode *erofs_rebuild_follow_link(struct erofs_inode *root,
+		struct erofs_inode *pwd, struct erofs_inode *link,
+		unsigned int depth)
+{
+	struct erofs_dentry *d;
+	struct erofs_inode *base;
+	bool dumb_wh, dumb_opq;
+	char *target;
+
+	if (depth >= EROFS_REBUILD_SYMLINK_DEPTH)
+		return ERR_PTR(-ELOOP);
+	if (!link->i_link)
+		return ERR_PTR(-ENOENT);
+
+	target = strdup(link->i_link);
+	if (!target)
+		return ERR_PTR(-ENOMEM);
+
+	/* absolute targets are resolved from the tree root */
+	base = target[0] == '/' ? root : pwd;
+	d = __erofs_rebuild_get_dentry(root, base, target, false,
+				       &dumb_wh, &dumb_opq, false, depth + 1);
+	free(target);
+	if (IS_ERR(d))
+		return ERR_CAST(d);
+	/* target referred to the root or the base directory itself ("/", ".") */
+	if (!d)
+		return base;
+	if (d->type == EROFS_FT_DIR)
+		return d->inode;
+	/* the target is itself a symlink: keep following the chain */
+	if (d->type == EROFS_FT_SYMLINK)
+		return erofs_rebuild_follow_link(root, d->inode->i_parent,
+						 d->inode, depth + 1);
+	return ERR_PTR(-ENOTDIR);
+}
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
+		struct erofs_inode *pwd, char *path, bool aufs,
+		bool *whout, bool *opq, bool to_head, unsigned int depth)
 {
 	struct erofs_dentry *d = NULL;
 	char *s = path;
@@ -121,6 +173,15 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 			}
 
 			d = erofs_d_lookup(pwd, s);
+			if (d && slash && d->type == EROFS_FT_SYMLINK) {
+				pwd = erofs_rebuild_follow_link(root, pwd,
+							d->inode, depth);
+				if (IS_ERR(pwd))
+					return ERR_CAST(pwd);
+				*slash = '/';
+				s = slash + 1;
+				continue;
+			}
 			if (d) {
 				if (d->type != EROFS_FT_DIR) {
 					if (slash)
@@ -152,6 +213,13 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 	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, pwd, path, aufs, whout, opq,
+					  to_head, 0);
+}
+
 static int erofs_rebuild_write_blob_index(struct erofs_sb_info *dst_sb,
 					  struct erofs_inode *inode)
 {
Signature · 1 line
2.43.0
#1 lore raw

Hi Mikhail,

Quoted 17 lines · On Sat, Jul 25, 2026 at 03:09:20PM +0000, [email protected] wrote:

From: Mikhail Malyshev <[email protected]>

mkfs.erofs --tar fails to convert layers that store files underneath a symlinked directory, a very common "usr-merge" layout in RHEL/UBI/Fedora based OCI images. For example a base layer contains

    lib64 -> usr/lib64        (symlink)
    usr/lib64/                (directory)
    lib64/libc.so.6           (regular file, path goes THROUGH the symlink)

erofs_rebuild_get_dentry() walks the path component by component and, when it hits the "lib64" component, finds an existing dentry that is a symlink rather than a directory. Since there are still components left to resolve it bails out with -ENOTDIR. In practice this surfaces as a bogus ~2 TiB image and "Could not format the device", making the whole layer unconvertible even though nothing is wrong with the tar.

Quoted 8 lines

A real filesystem (and containerd's walking differ, which applies the same layers via fs.RootPath) resolves such a path by following the symlink to its target directory. Do the same during tar rebuild: when an intermediate path component resolves to a symlink, follow it to the target directory (relative targets from the containing directory, absolute targets from the tree root), chasing chained symlinks up to a bounded depth to avoid loops.

Thanks for the patch and sorry for late reply.

Yes, I assume containerd will resolve the directories like this, but as a quick reference, could you give some pointer how tar utility works and posix documents this behavior?

I plan to release erofs-utils 1.9.3 without this patch soon, and it can be addressed in erofs-utils 1.9.4 version.

Thanks,
Gao Xiang

#2 lore raw

From: Mikhail Malyshev <[email protected]>

Quoted 1 line · Gao Xiang

Thanks for the patch and sorry for late reply.

No worries at all -- and sorry for _my_ slow reply, I was on vacation and only just got back to this.

Quoted 3 lines

Yes, I assume containerd will resolve the directories like this, but as a quick reference, could you give some pointer how tar utility works and posix documents this behavior?

Good question, I dug into it a bit.

On the tar side, I tried the exact case (a "lib64 -> usr/lib64" symlink followed by a "lib64/foo" entry). GNU tar (1.35) and busybox tar (1.37) both follow the symlink and write the file to usr/lib64/foo, leaving lib64 as a symlink -- the same result as this patch. bsdtar/libarchive is the odd one out: it refuses with "Cannot extract through symlink," but that is its ARCHIVE_EXTRACT_SECURE_SYMLINKS anti-symlink-attack default, not a different idea of what the path means.

POSIX-wise it is really just pathname resolution (IEEE Std 1003.1-2017 Base Definitions, 4.13): a symlink in a non-final path component is followed. GNU/busybox tar create the entry with a plain open(2), so the kernel resolves lib64/foo to usr/lib64/foo for them; the patch just does that same resolution by hand, since the tar tree builder has no kernel to do it.

What really pushed me to fix it, though, is matching the runtime that actually consumes these images. containerd's walking differ does exactly this -- it resolves each entry's parent with fs.RootPath ("evaluating and bounding any symlink to the root directory") and writes to the resolved target. So today mkfs.erofs --tar and containerd disagree: containerd converts these layers fine, while mkfs.erofs bails out with -ENOTDIR and produces the ~2 TiB image, which makes RHEL/UBI/Fedora usr-merge base layers unconvertible. And since the builder works on an in-memory tree (with absolute links re-rooted), following them cannot escape the image.

Quoted 2 lines

I plan to release erofs-utils 1.9.3 without this patch soon, and it can be addressed in erofs-utils 1.9.4 version.

1.9.4 is totally fine by me. Would you like me to fold these references (the tar behavior, the POSIX section, and the containerd fs.RootPath pointer) into the commit message? I am happy to send a v2 with them if that is easier for you to pick up.

Thanks!
Mikhail

#3 lore raw
In reply to [email protected]

Hi Mikhail,

Quoted 9 lines · On Sat, Jul 25, 2026 at 03:09:20PM +0000, [email protected] wrote:

From: Mikhail Malyshev <[email protected]>

mkfs.erofs --tar fails to convert layers that store files underneath a symlinked directory, a very common "usr-merge" layout in RHEL/UBI/Fedora based OCI images. For example a base layer contains

    lib64 -> usr/lib64        (symlink)
    usr/lib64/                (directory)
    lib64/libc.so.6           (regular file, path goes THROUGH the symlink)

Also my another question is that how these tars are generated, I guess it should be recorded as usr/lib64/libc.so.6, why does it need to record a path with a symlink component, that is confusing to me honestly.

Quoted 10 lines

erofs_rebuild_get_dentry() walks the path component by component and, when it hits the "lib64" component, finds an existing dentry that is a symlink rather than a directory. Since there are still components left to resolve it bails out with -ENOTDIR. In practice this surfaces as a bogus ~2 TiB image and "Could not format the device", making the whole layer unconvertible even though nothing is wrong with the tar.

A real filesystem (and containerd's walking differ, which applies the same layers via fs.RootPath) resolves such a path by following the symlink to

It would be better to summarize the various tar behaviors here too as you mentioned in the previous reply.

Quoted 24 lines

its target directory. Do the same during tar rebuild: when an intermediate path component resolves to a symlink, follow it to the target directory (relative targets from the containing directory, absolute targets from the tree root), chasing chained symlinks up to a bounded depth to avoid loops.

---
 lib/rebuild.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 70 insertions(+), 2 deletions(-)
lib/rebuild.c +4 -2
index 108a464..febc95c 100644
@@ -82,8 +82,60 @@ struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name)
 	return NULL;
 }
 
-struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
-		char *path, bool aufs, bool *whout, bool *opq, bool to_head)
+/* Bound symlink chains during path resolution, like the kernel's MAXSYMLINKS. */
+#define EROFS_REBUILD_SYMLINK_DEPTH	40
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,

lib/rebuild.c struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name) · line 98

Quoted 10 lines
lib/rebuild.c +10 -0
+		struct erofs_inode *pwd, char *path, bool aufs,
+		bool *whout, bool *opq, bool to_head, unsigned int depth);
+
+/*
+ * Resolve the target of an intermediate symlink path component to its
+ * directory inode, following chained symlinks up to a bounded depth.  This
+ * lets tar layers that store files under a symlinked directory be converted
+ * the same way a real filesystem extracts them, e.g. usr-merge layouts where
+ * "/lib64 -> usr/lib64" coexists with entries such as "lib64/libc.so.6".
+ */

I guess the comment is unneeded since the code is already straight-forward.

Quoted 5 lines
lib/rebuild.c +5 -0
+static struct erofs_inode *erofs_rebuild_follow_link(struct erofs_inode *root,
+		struct erofs_inode *pwd, struct erofs_inode *link,
+		unsigned int depth)
+{
+	struct erofs_dentry *d;

lib/rebuild.c struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name) · line 113

Quoted 10 lines
lib/rebuild.c +10 -0
+	struct erofs_inode *base;
+	bool dumb_wh, dumb_opq;
+	char *target;
+
+	if (depth >= EROFS_REBUILD_SYMLINK_DEPTH)
+		return ERR_PTR(-ELOOP);
+	if (!link->i_link)
+		return ERR_PTR(-ENOENT);
+
+	target = strdup(link->i_link);

I guess we could just use an on-disk variable instead?

Quoted 9 lines
lib/rebuild.c +9 -0
+	if (!target)
+		return ERR_PTR(-ENOMEM);
+
+	/* absolute targets are resolved from the tree root */
+	base = target[0] == '/' ? root : pwd;
+	d = __erofs_rebuild_get_dentry(root, base, target, false,
+				       &dumb_wh, &dumb_opq, false, depth + 1);
+	free(target);
+	if (IS_ERR(d))

lib/rebuild.c struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name) · line 132

Quoted 10 lines
lib/rebuild.c +10 -0
+		return ERR_CAST(d);
+	/* target referred to the root or the base directory itself ("/", ".") */
+	if (!d)
+		return base;
+	if (d->type == EROFS_FT_DIR)
+		return d->inode;
+	/* the target is itself a symlink: keep following the chain */
+	if (d->type == EROFS_FT_SYMLINK)
+		return erofs_rebuild_follow_link(root, d->inode->i_parent,
+						 d->inode, depth + 1);

It seems that it could be converted into a tail recursion.

Otherwise it looks good to me.

Thanks,
Gao Xiang

#4 lore raw

PATCH v2 1/1 erofs-utils: lib: follow symlinks on intermediate tar path components

From: Mikhail Malyshev <[email protected]>

mkfs.erofs --tar fails to convert layers that store files underneath a symlinked directory, a common "usr-merge" layout in RHEL/UBI/Fedora based OCI images. For example a base layer contains:

    lib64 -> usr/lib64        (symlink)
    usr/lib64/                (directory)
    lib64/libc.so.6           (regular file, recorded under the symlink)

These paths are not hand-crafted. The glibc RPM ships its dynamic loader and core libraries under /lib64 (and /lib) rather than /usr/lib64, so on a usr-merged base image (where /lib64 is a symlink to usr/lib64) the packaged files are recorded under the symlinked component. In the cdi-operator layer above all 19 glibc objects (ld-linux-x86-64.so.2, libc.so.6, ...) appear only under lib64/, with no usr/lib64/ counterpart, so correct placement depends on resolving the symlink.

erofs_rebuild_get_dentry() walks the path component by component and, when it reaches "lib64", finds an existing dentry that is a symlink rather than a directory. As there are still components left to resolve it bails out with -ENOTDIR, which surfaces as a bogus ~2 TiB image and "Could not format the device", making the whole layer unconvertible although the tar is fine.

Extraction tools resolve such a path by following the symlink to its
target: GNU tar (1.35) and busybox tar (1.37) both write the file to usr/lib64/libc.so.6 and keep lib64 a symlink. (bsdtar/libarchive instead refuses with "Cannot extract through symlink" under its default ARCHIVE_EXTRACT_SECURE_SYMLINKS anti-symlink-attack policy, i.e. an explicit opt-out of normal resolution rather than different semantics.) It is standard POSIX pathname resolution: a symlink in a non-final component is followed. containerd's walking differ, which applies these same layers, does the same -- it resolves each entry's parent with fs.RootPath ("evaluating and bounding any symlink to the root directory").

Do the same during tar rebuild: when an intermediate path component resolves to a symlink, follow it to the target directory (relative targets from the containing directory, absolute targets from the tree root), chasing chained symlinks up to a bounded depth to avoid loops. As the rebuild operates on an in-memory tree with absolute links re-rooted, following cannot escape the image.

---

Changes since v1 (https://lore.kernel.org/linux-erofs/[email protected]/):
- commit message: explain how these tars are generated (glibc RPM under
usr-merge) and summarize tar behaviors (GNU/busybox follow, bsdtar's
secure-symlinks refusal) plus the POSIX / containerd references, per review.
- drop the explanatory block comment; keep the code self-describing.
- resolve the symlink target into an on-stack buffer instead of strdup().
- follow chained symlinks with a loop rather than recursion.
- rebased onto current dev.

 lib/rebuild.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 66 insertions(+), 2 deletions(-)
lib/rebuild.c +66 -2
index ebac1c5..3ac62c6 100644
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <sys/stat.h>
 #include <config.h>
 #if defined(HAVE_SYS_SYSMACROS_H)
@@ -82,8 +83,54 @@ struct erofs_dentry *erofs_d_lookup(struct erofs_inode *dir, const char *name)
 	return NULL;
 }
 
-struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
-		char *path, bool aufs, bool *whout, bool *opq, bool to_head)
+/* Bound symlink chains during path resolution, like the kernel's MAXSYMLINKS. */
+#define EROFS_REBUILD_SYMLINK_DEPTH	40
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
+		struct erofs_inode *pwd, char *path, bool aufs,
+		bool *whout, bool *opq, bool to_head, unsigned int depth);
+
+static struct erofs_inode *erofs_rebuild_follow_link(struct erofs_inode *root,
+		struct erofs_inode *pwd, struct erofs_inode *link,
+		unsigned int depth)
+{
+	char target[PATH_MAX + 1];
+	bool dumb_wh, dumb_opq;
+
+	for (;;) {
+		struct erofs_dentry *d;
+		struct erofs_inode *base;
+
+		if (depth++ >= EROFS_REBUILD_SYMLINK_DEPTH)
+			return ERR_PTR(-ELOOP);
+		if (!link->i_link)
+			return ERR_PTR(-ENOENT);
+		if (strlen(link->i_link) > PATH_MAX)
+			return ERR_PTR(-ENAMETOOLONG);
+		strcpy(target, link->i_link);
+
+		/* absolute targets are resolved from the tree root */
+		base = target[0] == '/' ? root : pwd;
+		d = __erofs_rebuild_get_dentry(root, base, target, false,
+					       &dumb_wh, &dumb_opq, false, depth);
+		if (IS_ERR(d))
+			return ERR_CAST(d);
+		/* target referred to the root or base directory itself ("/", ".") */
+		if (!d)
+			return base;
+		if (d->type == EROFS_FT_DIR)
+			return d->inode;
+		if (d->type != EROFS_FT_SYMLINK)
+			return ERR_PTR(-ENOTDIR);
+		/* the target is itself a symlink: keep following the chain */
+		pwd = d->inode->i_parent;
+		link = d->inode;
+	}
+}
+
+static struct erofs_dentry *__erofs_rebuild_get_dentry(struct erofs_inode *root,
+		struct erofs_inode *pwd, char *path, bool aufs,
+		bool *whout, bool *opq, bool to_head, unsigned int depth)
 {
 	struct erofs_dentry *d = NULL;
 	char *s = path;
@@ -121,6 +168,16 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 			}
 
 			d = erofs_d_lookup(pwd, s);
+			/* follow a symlinked intermediate component like a real fs */
+			if (d && slash && d->type == EROFS_FT_SYMLINK) {
+				pwd = erofs_rebuild_follow_link(root, pwd,
+							d->inode, depth);
+				if (IS_ERR(pwd))
+					return ERR_CAST(pwd);
+				*slash = '/';
+				s = slash + 1;
+				continue;
+			}
 			if (d) {
 				if (d->type != EROFS_FT_DIR) {
 					if (slash)
@@ -152,6 +209,13 @@ struct erofs_dentry *erofs_rebuild_get_dentry(struct erofs_inode *pwd,
 	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, pwd, path, aufs, whout, opq,
+					  to_head, 0);
+}
+
 static int erofs_rebuild_load_chunk_index(struct erofs_sb_info *dst_sb,
 					  struct erofs_inode *inode)
 {
Signature · 1 line
2.43.0