attr: do not traverse leading directory symlinks
PATCH 1/1 attr: do not traverse leading directory symlinks
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().
--- attr.c | 10 ++++++++-- t/t0003-attributes.sh | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-)
attr.c +8 -2
@@ -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 chif (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);
t/t0003-attributes.sh +10 -0
@@ -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 &&
Signature · 1 line
2.56.1.750.gf70dc682cf-goog