Linux-EROFS Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gao Xiang <hsiangkao@linux.alibaba.com>
To: linux-erofs@lists.ozlabs.org
Cc: Gao Xiang <hsiangkao@linux.alibaba.com>,
	Tristan <TristanInSec@gmail.com>
Subject: [PATCH] erofs-utils: dump: fix stack-overflow due to directory loops
Date: Mon, 29 Jun 2026 15:25:07 +0800	[thread overview]
Message-ID: <20260629072507.2375923-1-hsiangkao@linux.alibaba.com> (raw)

This mirrors the solution in commit f3728a162d22 ("erofs-utils: dump:
fix stack-overflow due to directory loops").

Reported-by: Tristan <TristanInSec@gmail.com>
Fixes: 5a9ac8e057cf ("erofs-utils: dump: convert readdir to use erofs_iterate_dir()")
Closes: https://lore.kernel.org/r/CAA1XrhPMekMqAnRkC-jV9rTsO4LHjzh=kxn6zQKMgBrqfrnp8A@mail.gmail.com/6-dump-erofs-recursion.txt
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
---
 dump/main.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/dump/main.c b/dump/main.c
index 6c7258a5db40..9eebcb8a3e3f 100644
--- a/dump/main.c
+++ b/dump/main.c
@@ -17,7 +17,13 @@
 #include "../lib/liberofs_private.h"
 #include "../lib/liberofs_uuid.h"
 
+struct erofsdump_dirstack {
+       erofs_nid_t dirs[PATH_MAX];
+       int top;
+};
+
 struct erofsdump_cfg {
+	struct erofsdump_dirstack dirstack;
 	unsigned int totalshow;
 	bool show_inode;
 	bool show_extent;
@@ -359,7 +365,6 @@ static int erofsdump_readdir(struct erofs_dir_context *ctx)
 		update_file_size_statistics(occupied_size, false);
 	}
 
-	/* XXXX: the dir depth should be restricted in order to avoid loops */
 	if (S_ISDIR(vi.i_mode)) {
 		struct erofs_dir_context nctx = {
 			.flags = ctx->dir ? EROFS_READDIR_VALID_PNID : 0,
@@ -367,8 +372,18 @@ static int erofsdump_readdir(struct erofs_dir_context *ctx)
 			.dir = &vi,
 			.cb = erofsdump_dirent_iter,
 		};
-
-		return erofs_iterate_dir(&nctx, false);
+		int i, ret;
+
+		/* XXX: support the deeper cases later */
+		if (dumpcfg.dirstack.top >= ARRAY_SIZE(dumpcfg.dirstack.dirs))
+			return -ENAMETOOLONG;
+		for (i = 0; i < dumpcfg.dirstack.top; ++i)
+			if (vi.nid == dumpcfg.dirstack.dirs[i])
+				return -ELOOP;
+		dumpcfg.dirstack.dirs[dumpcfg.dirstack.top++] = nctx.pnid;
+		ret = erofs_iterate_dir(&nctx, false);
+		--dumpcfg.dirstack.top;
+		return ret;
 	}
 	return 0;
 }
-- 
2.43.5



                 reply	other threads:[~2026-06-29  7:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260629072507.2375923-1-hsiangkao@linux.alibaba.com \
    --to=hsiangkao@linux.alibaba.com \
    --cc=TristanInSec@gmail.com \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox