Git development
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
	Patrick Steinhardt <ps@pks.im>,  Elijah Newren <newren@gmail.com>,
	dmnk+artist@google.com, Dominik Maier <dmnk@google.com>
Subject: [PATCH] attr: do not traverse leading directory symlinks
Date: Thu, 19 Nov 2026 20:23:02 +0100	[thread overview]
Message-ID: <20261119192302.2681052-1-dmnk@google.com> (raw)

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(-)

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


                 reply	other threads:[~2026-11-19 19:23 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=20261119192302.2681052-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=dmnk+artist@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    --cc=peff@peff.net \
    --cc=ps@pks.im \
    /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