Skip to contentSkip to conversation

attr: fix recursive getfattr failures with low descriptor limits

PATCH New Hetvi Thakar -X (hthakar - E INFOCHIPS PRIVATE LIMITED at Cisco) 1 participant 1 message Nov 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 attr: fix recursive getfattr failures with low descriptor limits

From: Hetvi Thakar <[email protected]>

Under low file descriptor limits, getfattr -R can report "Bad file descriptor" and omit entries from deep directory trees. The 2.6.0 walker closes ancestor directory streams but retains their descriptors and readdir() filename pointers in recursive calls.

Retain an open root directory handle and copy filenames before closing streams. Reopen descendants relative to a valid ancestor, verify their device and inode, and fix descriptor ownership and accounting.

Add a regression test for default, physical and logical traversal at descriptor limits of 32 and 256, and enable it in ptest with its required utilities.

---
 ...traversal-through-anchored-directory.patch | 393 ++++++++++++++++++
 meta/recipes-support/attr/attr/run-ptest      |   8 +-
 meta/recipes-support/attr/attr_2.6.0.bb       |   4 +
 3 files changed, 403 insertions(+), 2 deletions(-)
 create mode 100644 meta/recipes-support/attr/attr/0001-walk_tree-resume-traversal-through-anchored-directory.patch
meta/recipes-support/attr/attr/0001-walk_tree-resume-traversal-through-anchored-directory.patch new file +393 -0
new file mode 100644
index 0000000000..799676f10d
@@ -0,0 +1,393 @@
+From 417b71279fb1f02ed04b8db997395b073c36cf42 Mon Sep 17 00:00:00 2001
+From: Hetvi Thakar <[email protected]>
+Date: Tue, 17 Nov 2026 22:24:17 -0800
+Subject: [PATCH] walk_tree: resume traversal through anchored directory
+ handles
+
+Closing an ancestor directory stream releases both its descriptor and the
+storage returned by readdir(). Recursive frames can consequently retain
+invalid component names and parent descriptors when resuming traversal.
+
+Keep the top-level stream open as an anchor and copy component names before
+closing streams. Reopen closed directories one component at a time relative
+to a surviving ancestor, preserving the original opening flags and checking
+each component's device and inode before continuing. Protect the immediate
+parent while opening and visiting its child.
+
+Transfer descriptor ownership to fdopendir() explicitly, restore the handle
+budget on all close paths, and retry both EMFILE and ENFILE failures. This
+also avoids closing a descriptor again after its stream has been closed.
+
+Add a standalone shell regression test which checks complete recursive xattr
+listings of 74 regular files in 37 directory levels under descriptor limits
+256 and 32, in default, physical and logical traversal modes.
+
+Fixes: 9b37b117c5d5 ("Add the new walk_tree helper")
+Upstream-Status: Pending
+
+Signed-off-by: Hetvi Thakar <[email protected]>
+---
+ libmisc/walk_tree.c | 131 ++++++++++++++++++++++++++++++++++----------
+ test/Makemodule.am  |   6 +-
+ test/walk-tree.sh   |  78 ++++++++++++++++++++++++++
+ 3 files changed, 183 insertions(+), 32 deletions(-)
+ create mode 100755 test/walk-tree.sh
+
+diff --git a/libmisc/walk_tree.c b/libmisc/walk_tree.c
+index 5b1b576..5b145f7 100644
+--- a/libmisc/walk_tree.c
++++ b/libmisc/walk_tree.c
+@@ -34,6 +34,8 @@
+ 
+ struct dir_stack_entry {
+ 	struct dir_stack_entry *parent;
++	const char *name;
++	int open_flags;
+ 	DIR *stream;
+ 	long pos;
+ 	dev_t dev;
+@@ -133,7 +135,74 @@ static char *new_dirname(const char *dirname, const char *pathname)
+ 	return newname;
+ }
+ 
+-static int walk_tree_rec(int dirfd, const char *dirname, const char *pathname,
++static int close_parent(struct walk_tree_args *args,
++			struct dir_stack_entry *parent)
++{
++	struct dir_stack_entry *entry, *oldest = NULL;
++	DIR *stream;
++
++	/* Keep the root as an anchor, and the immediate parent for openat(). */
++	for (entry = parent ? parent->parent : NULL;
++	     entry && entry->parent; entry = entry->parent) {
++		if (entry->stream)
++			oldest = entry;
++	}
++	if (!oldest) {
++		errno = EMFILE;
++		return -1;
++	}
++	oldest->pos = telldir(oldest->stream);
++	if (oldest->pos == -1)
++		return -1;
++	stream = oldest->stream;
++	oldest->stream = NULL;
++	args->num_dir_handles++;
++	return closedir(stream);
++}
++
++static int reopen_dir(struct dir_stack_entry *entry)
++{
++	struct dir_stack_entry *parent = entry->parent;
++	struct stat st;
++	int parent_fd, fd, saved_errno;
++
++	/* The root stream is never closed or reopened by pathname. */
++	if (!parent) {
++		errno = ESTALE;
++		return -1;
++	}
++	if (parent->stream)
++		parent_fd = dirfd(parent->stream);
++	else {
++		parent_fd = reopen_dir(parent);
++		if (parent_fd == -1)
++			return -1;
++	}
++
++	fd = openat(parent_fd, entry->name, entry->open_flags);
++	saved_errno = errno;
++	if (!parent->stream)
++		close(parent_fd);
++	errno = saved_errno;
++	if (fd == -1)
++		return -1;
++
++	if (fstat(fd, &st) == -1)
++		goto fail;
++	if (st.st_dev != entry->dev || st.st_ino != entry->ino) {
++		errno = ESTALE;
++		goto fail;
++	}
++	return fd;
++
++fail:
++	saved_errno = errno;
++	close(fd);
++	errno = saved_errno;
++	return -1;
++}
++
++static int walk_tree_rec(int parent_fd, const char *dirname, const char *pathname,
+ 			 unsigned char dirtype, struct walk_tree_args *args,
+ 			 struct dir_stack_entry *parent_dir)
+ {
+@@ -144,7 +213,7 @@ static int walk_tree_rec(int dirfd, const char *dirname, const char *pathname,
+ 	int follow;
+ 	struct stat st;
+ 	char *mydirname = NULL;
+-	int dir_open_flags;
++	char *myname = NULL;
+ 	int fd = -1;
+ 	struct dirent *entry;
+ 	int err = 0;
+@@ -163,34 +232,27 @@ static int walk_tree_rec(int dirfd, const char *dirname, const char *pathname,
+ 	       !(walk_flags & WALK_TREE_TOPLEVEL)))))
+ 		goto visit_this;
+ 
+-	if (args->num_dir_handles == 0) {
+-		struct dir_stack_entry *top;
+-
+-close_another_parent:
+-		/* Close the topmost parent directory handle still open. */
+-		top = parent_dir;
+-		if (!top || !top->stream) {
+-			errno = ENFILE;
+-			goto fail;
+-		}
+-		while (top->parent && top->parent->stream)
+-			top = top->parent;
+-		top->pos = telldir(top->stream);
+-		closedir(top->stream);
+-		top->stream = NULL;
+-		args->num_dir_handles++;
+-	}
++	/* Closing an ancestor stream also frees its readdir() storage. */
++	myname = strdup(pathname);
++	if (!myname)
++		goto fail;
++	pathname = this_dir.name = myname;
++	this_dir.open_flags = O_RDONLY | O_DIRECTORY | (follow ? 0 : O_NOFOLLOW);
++	if (args->num_dir_handles == 0 && close_parent(args, parent_dir) == -1)
++		goto fail;
+ 
+-	dir_open_flags = O_RDONLY | O_DIRECTORY | (follow ? 0 : O_NOFOLLOW);
+-	fd = openat(dirfd, pathname, dir_open_flags);
++retry_open:
++	fd = openat(parent_fd, pathname, this_dir.open_flags);
+ 	if (fd == -1) {
+ 		if (errno == ENOTDIR || errno == ENOENT || errno == ELOOP)
+ 			goto visit_this;
+-		if (errno != ENFILE)
++		if (errno != EMFILE && errno != ENFILE)
+ 			goto fail;
+ 		/* Ran out of file descriptors. */
+ 		args->num_dir_handles = 0;
+-		goto close_another_parent;
++		if (close_parent(args, parent_dir) == -1)
++			goto fail;
++		goto retry_open;
+ 	}
+ 	args->num_dir_handles--;
+ 	if (dirtype == DT_UNKNOWN && !follow)
+@@ -198,6 +260,8 @@ close_another_parent:
+ 
+ 	if (fstat(fd, &st) == -1)
+ 		goto fail;
++	this_dir.dev = st.st_dev;
++	this_dir.ino = st.st_ino;
+ 	if (walk_flags & WALK_TREE_ONE_FILESYSTEM) {
+ 		if (args->dev == 0)
+ 			args->dev = st.st_dev;
+@@ -208,8 +272,6 @@ close_another_parent:
+ 	if (walk_flags & WALK_TREE_LOGICAL) {
+ 		if (dir_visited(parent_dir, st.st_dev, st.st_ino))
+ 			goto dir_visited;
+-		this_dir.dev = st.st_dev;
+-		this_dir.ino = st.st_ino;
+ 	} else {
+ 		int ret = dir_hash_insert_unique(args, &st);
+ 		if (ret == 1)
+@@ -222,8 +284,9 @@ close_another_parent:
+ dir_visited:
+ 	close(fd);
+ 	fd = -1;
++	args->num_dir_handles++;
+ 	if (dirtype == DT_UNKNOWN) {
+-		if (fstatat(dirfd, pathname, &st, AT_SYMLINK_NOFOLLOW) == -1)
++		if (fstatat(parent_fd, pathname, &st, AT_SYMLINK_NOFOLLOW) == -1)
+ 			goto fail;
+ 		dirtype = IFTODT(st.st_mode);
+ 	}
+@@ -231,7 +294,7 @@ dir_visited:
+ 		goto out;
+ 
+ visit_this:
+-	err += args->func(dirfd, dirname, pathname, dirtype,
++	err += args->func(parent_fd, dirname, pathname, dirtype,
+ 			  walk_flags, args->arg);
+ 
+ 	if (fd == -1)
+@@ -239,6 +302,7 @@ visit_this:
+ 	this_dir.stream = fdopendir(fd);
+ 	if (this_dir.stream == NULL)
+ 		goto fail;
++	fd = -1;
+ 
+ 	mydirname = new_dirname(dirname, pathname);
+ 	if (mydirname == NULL)
+@@ -250,18 +314,19 @@ visit_this:
+ 		    !strcmp(entry->d_name, ".."))
+ 			continue;
+ 
+-		err += walk_tree_rec(fd, mydirname, entry->d_name,
++		err += walk_tree_rec(dirfd(this_dir.stream), mydirname, entry->d_name,
+ 				     entry->d_type, args, &this_dir);
+ 
+ 		if (!this_dir.stream) {
+ 			/* Reopen the directory handle. */
+-			fd = openat(dirfd, pathname, dir_open_flags);
++			fd = reopen_dir(&this_dir);
+ 			if (fd == -1)
+ 				goto fail_depth;
+ 			args->num_dir_handles--;
+ 			this_dir.stream = fdopendir(fd);
+ 			if (!this_dir.stream)
+ 				goto fail_depth;
++			fd = -1;
+ 			seekdir(this_dir.stream, this_dir.pos);
+ 		}
+ 	}
+@@ -269,9 +334,11 @@ visit_this:
+ 
+ 	if (closedir(this_dir.stream) != 0) {
+ 		this_dir.stream = NULL;
++		args->num_dir_handles++;
+ 		goto fail;
+ 	}
+ 	this_dir.stream = NULL;
++	args->num_dir_handles++;
+ 
+ out:
+ 	free(mydirname);
+@@ -284,10 +351,11 @@ out:
+ 		close(fd);
+ 		args->num_dir_handles++;
+ 	}
++	free(myname);
+ 	return err;
+ 
+ fail:
+-	err += args->func(dirfd, dirname, pathname, dirtype,
++	err += args->func(parent_fd, dirname, pathname, dirtype,
+ 			  walk_flags | WALK_TREE_FAILED, args->arg);
+ 	goto out;
+ 
+@@ -318,6 +386,9 @@ int walk_tree(const char *pathname, enum walk_flags walk_flags,
+ 	if (getrlimit(RLIMIT_NOFILE, &rlimit) != 0)
+ 		rlimit.rlim_cur = 0;
+ 	args.num_dir_handles = rlimit.rlim_cur / 4 + 1;
++	/* Reserve streams for the root, an immediate parent, and its child. */
++	if (args.num_dir_handles < 3)
++		args.num_dir_handles = 3;
+ 
+ 	err = walk_tree_rec(AT_FDCWD, "", pathname, DT_UNKNOWN, &args, NULL);
+ 
+diff --git a/test/Makemodule.am b/test/Makemodule.am
+index eb679f1..5342960 100644
+--- a/test/Makemodule.am
++++ b/test/Makemodule.am
+@@ -1,8 +1,9 @@
+-TEST_EXTENSIONS = .run
++TEST_EXTENSIONS = .run .sh
+ 
+ TESTS = \
+ 	test/attr.run \
+-	test/root/getfattr.run
++	test/root/getfattr.run \
++	test/walk-tree.sh
+ 
+ if UNSAFE_RESTORE_WARNINGS
+ TESTS += \
+@@ -20,3 +21,4 @@ AM_TESTS_ENVIRONMENT = \
+ 	export LC_MESSAGES=C; \
+ 	PATH="$(abs_top_builddir):$$PATH";
+ RUN_LOG_COMPILER = $(srcdir)/test/run
++SH_LOG_COMPILER = $(SHELL)
+diff --git a/test/walk-tree.sh b/test/walk-tree.sh
+new file mode 100755
+index 0000000..d579c12
+--- /dev/null
++++ b/test/walk-tree.sh
+@@ -0,0 +1,78 @@
++#!/bin/sh
++# SPDX-License-Identifier: GPL-2.0-or-later
++# Exercise closed-directory reopening without relying on readdir() order.
++
++set -eu
++LC_ALL=C
++export LC_ALL
++
++fail()
++{
++	echo "walk-tree: $*" >&2
++	exit 1
++}
++
++skip()
++{
++	echo "walk-tree: $*" >&2
++	exit 77
++}
++
++# Keep enough handles open in the control case to cover all 37 directories.
++# Probe in children so neither the test process nor its caller loses handles.
++(ulimit -S -n 256) >/dev/null 2>&1 || skip "cannot set a soft descriptor limit of 256"
++(ulimit -S -n 32) >/dev/null 2>&1 || skip "cannot set a soft descriptor limit of 32"
++
++testdir=$(mktemp -d "${TMPDIR:-.}/attr-walk-tree.XXXXXX") || fail "mktemp failed"
++testdir=$(CDPATH= cd "$testdir" && pwd) || fail "cannot resolve test directory"
++trap 'rm -rf "$testdir"' 0
++trap 'exit 1' 1 2 3 15
++cd "$testdir"
++
++# Unsupported filesystems are not traversal failures. Unexpected probe errors
++# remain failures, rather than hiding an unrelated setfattr regression.
++: > probe
++if ! setfattr -n user.walk_tree -v 1 probe 2>probe.err; then
++	if grep -E ': (Operation not supported|Not supported|Function not implemented)$' probe.err >/dev/null; then
++		skip "filesystem does not support user extended attributes"
++	fi
++	cat probe.err >&2
++	fail "setting the probe attribute failed"
++fi
++
++make_tree()
++{
++	mkdir "$1"
++	: > "$1/before"
++	setfattr -n user.walk_tree -v 1 "$1/before"
++	printf '# file: %s\nuser.walk_tree="1"\n\n' "$1/before" >>expected
++	if [ "$2" -lt 36 ]; then
++		make_tree "$1/d" "$(($2 + 1))"
++	fi
++	: > "$1/after"
++	setfattr -n user.walk_tree -v 1 "$1/after"
++	printf '# file: %s\nuser.walk_tree="1"\n\n' "$1/after" >>expected
++}
++
++# A file created on either side of the child directory exercises resumption
++# regardless of whether the filesystem returns the child before other entries.
++make_tree tree 0
++sort expected >expected.sorted
++
++for walk in -R -RP -RL; do
++	for limit in 256 32; do
++		if ! (
++			ulimit -S -n "$limit" || exit 1
++			exec getfattr "$walk" -d -m '^user[.]walk_tree$' tree
++		) >actual 2>actual.err; then
++			cat actual.err >&2
++			fail "listing with $walk failed with descriptor limit $limit"
++		fi
++		if [ -s actual.err ]; then
++			cat actual.err >&2
++			fail "listing with $walk reported errors with descriptor limit $limit"
++		fi
++		sort actual >actual.sorted
++		cmp expected.sorted actual.sorted || fail "incomplete listing with $walk and descriptor limit $limit"
++	done
++done
+-- 
+2.35.6
+
meta/recipes-support/attr/attr/run-ptest +6 -2
index 50970d5b65..7c7c368ab0 100644
@@ -3,8 +3,12 @@
 failed=0
 all=0
 
-for f in *.run; do
-    ./run $f
+for f in *.run *.sh; do
+    [ -f "$f" ] || continue
+    case "$f" in
+        *.run) ./run "$f" ;;
+        *.sh) sh "$f" ;;
+    esac
     case "$?" in
         0)
             echo "PASS: $f"
meta/recipes-support/attr/attr_2.6.0.bb +4 -0
index 4af6c4fac2..36510df4b5 100644
@@ -16,6 +16,7 @@ LIC_FILES_CHKSUM = "file://doc/COPYING;md5=2d0aa14b3fce4694e4f615e30186335f \
 
 SRC_URI = "${SAVANNAH_GNU_MIRROR}/attr/${BP}.tar.gz \
            file://run-ptest \
+           file://0001-walk_tree-resume-traversal-through-anchored-directory.patch \
 "
 
 SRC_URI[sha256sum] = "d42fa374513180bb48cb11a46696f488240e5124ff1e6ad88b0abff706985612"
@@ -47,6 +48,9 @@ do_install_ptest:append:libc-musl() {
 }
 
 RDEPENDS:${PN}-ptest = "attr \
+                        coreutils \
+                        diffutils \
+                        grep \
                         perl \
                         perl-module-cwd \
                         perl-module-file-basename \
Signature · 1 line
2.35.6