threads / patch / 66886

patchattr: do not traverse leading directory symlinks

Subject: [PATCH] attr: do not traverse leading directory symlinks

## tl;dr

One message between Nov 19, 2026 and Nov 19, 2026. Diffs are folded; open one to read it.

replies: 0people: 1as markdown or json

Dominik Maier· Nov 19, 2026, 19:23 UTC · lore

Commit 2ef579e261 (attr: do not respect symlinks for in-tree .gitattributes, 2021-02-16) taught read_attr_from_file() to open in-tree .gitattributes files with open_nofollow() so worktree lookups match index lookups and do not follow symlinks outside the repository.

open_nofollow() only checks O_NOFOLLOW on the final ".gitattributes" component. When an explicit path such as "symdir/file" is passed to "git check-attr" and "symdir" is a directory symlink, prepare_attr_stack() still traverses "symdir" to read "symdir/.gitattributes", and builtin_object_mode_attr() calls lstat("symdir/file") across the link.

Check has_symlink_leading_path() before opening READ_ATTR_NOFOLLOW files in read_attr_from_file() and before stating paths in builtin_object_mode_attr().

Signed-off-by: Dominik Maier <dmnk@google.com>
---
 attr.c                | 10 ++++++++--
 t/t0003-attributes.sh | 10 ++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)
Show changes to 2 files +18 −2

attr.c, t/t0003-attributes.sh

diff --git a/attr.c b/attr.c
index 0e63f1b6de..50e25a3ee6 100644
--- a/attr.c
+++ b/attr.c
@@ -24,6 +24,7 @@
 #include "revision.h"
 #include "odb.h"
 #include "setup.h"
+#include "symlinks.h"
 #include "thread-utils.h"
 #include "tree-walk.h"
 #include "object-name.h"
@@ -699,10 +700,13 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
 	int lineno = 0;
 	struct stat st;
 
-	if (flags & READ_ATTR_NOFOLLOW)
+	if (flags & READ_ATTR_NOFOLLOW) {
+		if (has_symlink_leading_path(path, strlen(path)))
+			return NULL;
 		fd = open_nofollow(path, O_RDONLY);
-	else
+	} else {
 		fd = open(path, O_RDONLY);
+	}
 
 	if (fd < 0) {
 		warn_on_fopen_errors(path);
@@ -1274,6 +1278,8 @@ static const char *builtin_object_mode_attr(struct index_state *istate, const ch
 	if (direction == GIT_ATTR_CHECKIN) {
 		struct object_id oid;
 		struct stat st;
+		if (has_symlink_leading_path(path, strlen(path)))
+			return ATTR__UNSET;
 		if (lstat(path, &st))
 			die_errno(_("unable to stat '%s'"), path);
 		mode = canon_mode(st.st_mode);
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index 582e207aa1..c252e7ed55 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -533,6 +533,16 @@ test_expect_success SYMLINKS 'symlinks not respected in-tree' '
 	test_grep "unable to access.*gitattributes" err
 '
 
+test_expect_success SYMLINKS 'leading directory symlinks not traversed in-tree' '
+	test_when_finished "rm -rf ext_dir symdir" &&
+	mkdir ext_dir &&
+	echo "* test" >ext_dir/.gitattributes &&
+	>ext_dir/file &&
+	ln -s ext_dir symdir &&
+	attr_check_basic symdir/file unspecified &&
+	attr_check_object_mode symdir/file unspecified
+'
+
 test_expect_success 'large attributes line ignored in tree' '
 	test_when_finished "rm .gitattributes" &&
 	printf "path %02043d" 1 >.gitattributes &&
-- 
2.56.1.750.gf70dc682cf-goog

← back to recent threads