diff mbox series

[17/32] libfrog: detect looping paths when walking directory parent pointers

Message ID 170405006328.1804688.14864529372800053315.stgit@frogsfrogsfrogs (mailing list archive)
State New
Headers show
Series [01/32] xfs: add parent pointer support to attribute code | expand

Commit Message

Darrick J. Wong Dec. 31, 2023, 11:09 p.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

Detect loops when we're walking directory parent pointers so that we
don't loop infinitely.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
 libfrog/getparents.c |    3 +++
 libfrog/paths.c      |   16 ++++++++++++++++
 libfrog/paths.h      |    2 ++
 3 files changed, 21 insertions(+)
diff mbox series

Patch

diff --git a/libfrog/getparents.c b/libfrog/getparents.c
index 016fe3f026d..fa4e4a1c9c0 100644
--- a/libfrog/getparents.c
+++ b/libfrog/getparents.c
@@ -148,6 +148,9 @@  handle_walk_ppath_rec(
 	if (rec->p_flags & PARENT_IS_ROOT)
 		return wpi->fn(wpi->mntpt, wpi->path, wpi->arg);
 
+	if (path_will_loop(wpi->path, rec->p_ino))
+		return 0;
+
 	ret = path_component_change(wpli->pc, rec->p_name,
 				strlen((char *)rec->p_name), rec->p_ino);
 	if (ret)
diff --git a/libfrog/paths.c b/libfrog/paths.c
index b3c5236990e..9ba2a2f313b 100644
--- a/libfrog/paths.c
+++ b/libfrog/paths.c
@@ -734,3 +734,19 @@  path_walk_components(
 
 	return 0;
 }
+
+/* Will this path contain a loop if we add this inode? */
+bool
+path_will_loop(
+	const struct path_list	*path_list,
+	uint64_t		ino)
+{
+	struct path_component	*pc;
+
+	list_for_each_entry(pc, &path_list->p_head, pc_list) {
+		if (pc->pc_ino == ino)
+			return true;
+	}
+
+	return false;
+}
diff --git a/libfrog/paths.h b/libfrog/paths.h
index 6be74c42b07..895171aa342 100644
--- a/libfrog/paths.h
+++ b/libfrog/paths.h
@@ -83,4 +83,6 @@  typedef int (*path_walk_fn_t)(const char *name, uint64_t ino, void *arg);
 int path_walk_components(const struct path_list *path, path_walk_fn_t fn,
 		void *arg);
 
+bool path_will_loop(const struct path_list *path, uint64_t ino);
+
 #endif	/* __LIBFROG_PATH_H__ */