Skip to content

Commit bffc762

Browse files
ttaylorrgitster
authored andcommitted
dir-iterator: prevent top-level symlinks without FOLLOW_SYMLINKS
When using the dir_iterator API, we first stat(2) the base path, and then use that as a starting point to enumerate the directory's contents. If the directory contains symbolic links, we will immediately die() upon encountering them without the `FOLLOW_SYMLINKS` flag. The same is not true when resolving the top-level directory, though. As explained in a previous commit, this oversight in 6f054f9 (builtin/clone.c: disallow `--local` clones with symlinks, 2022-07-28) can be used as an attack vector to include arbitrary files on a victim's filesystem from outside of the repository. Prevent resolving top-level symlinks unless the FOLLOW_SYMLINKS flag is given, which will cause clones of a repository with a symlink'd "$GIT_DIR/objects" directory to fail. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cf8f6ce commit bffc762

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

dir-iterator.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
203203
{
204204
struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
205205
struct dir_iterator *dir_iterator = &iter->base;
206-
int saved_errno;
206+
int saved_errno, err;
207207

208208
strbuf_init(&iter->base.path, PATH_MAX);
209209
strbuf_addstr(&iter->base.path, path);
@@ -213,10 +213,15 @@ struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
213213
iter->flags = flags;
214214

215215
/*
216-
* Note: stat already checks for NULL or empty strings and
217-
* inexistent paths.
216+
* Note: stat/lstat already checks for NULL or empty strings and
217+
* nonexistent paths.
218218
*/
219-
if (stat(iter->base.path.buf, &iter->base.st) < 0) {
219+
if (iter->flags & DIR_ITERATOR_FOLLOW_SYMLINKS)
220+
err = stat(iter->base.path.buf, &iter->base.st);
221+
else
222+
err = lstat(iter->base.path.buf, &iter->base.st);
223+
224+
if (err < 0) {
220225
saved_errno = errno;
221226
goto error_out;
222227
}

dir-iterator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
* not the symlinks themselves, which is the default behavior. Broken
6262
* symlinks are ignored.
6363
*
64+
* Note: setting DIR_ITERATOR_FOLLOW_SYMLINKS affects resolving the
65+
* starting path as well (e.g., attempting to iterate starting at a
66+
* symbolic link pointing to a directory without FOLLOW_SYMLINKS will
67+
* result in an error).
68+
*
6469
* Warning: circular symlinks are also followed when
6570
* DIR_ITERATOR_FOLLOW_SYMLINKS is set. The iteration may end up with
6671
* an ELOOP if they happen and DIR_ITERATOR_PEDANTIC is set.

t/t0066-dir-iterator.sh

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ test_expect_success SYMLINKS 'setup dirs with symlinks' '
109109
mkdir -p dir5/a/c &&
110110
ln -s ../c dir5/a/b/d &&
111111
ln -s ../ dir5/a/b/e &&
112-
ln -s ../../ dir5/a/b/f
112+
ln -s ../../ dir5/a/b/f &&
113+
114+
ln -s dir4 dir6
113115
'
114116

115117
test_expect_success SYMLINKS 'dir-iterator should not follow symlinks by default' '
@@ -145,4 +147,27 @@ test_expect_success SYMLINKS 'dir-iterator should follow symlinks w/ follow flag
145147
test_cmp expected-follow-sorted-output actual-follow-sorted-output
146148
'
147149

150+
test_expect_success SYMLINKS 'dir-iterator does not resolve top-level symlinks' '
151+
test_must_fail test-tool dir-iterator ./dir6 >out &&
152+
153+
grep "ENOTDIR" out
154+
'
155+
156+
test_expect_success SYMLINKS 'dir-iterator resolves top-level symlinks w/ follow flag' '
157+
cat >expected-follow-sorted-output <<-EOF &&
158+
[d] (a) [a] ./dir6/a
159+
[d] (a/f) [f] ./dir6/a/f
160+
[d] (a/f/c) [c] ./dir6/a/f/c
161+
[d] (b) [b] ./dir6/b
162+
[d] (b/c) [c] ./dir6/b/c
163+
[f] (a/d) [d] ./dir6/a/d
164+
[f] (a/e) [e] ./dir6/a/e
165+
EOF
166+
167+
test-tool dir-iterator --follow-symlinks ./dir6 >out &&
168+
sort out >actual-follow-sorted-output &&
169+
170+
test_cmp expected-follow-sorted-output actual-follow-sorted-output
171+
'
172+
148173
test_done

t/t5604-clone-reference.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,20 @@ test_expect_success SYMLINKS 'clone repo with symlinked or unknown files at obje
341341
test_must_be_empty T--shared.objects-symlinks.raw
342342
'
343343

344+
test_expect_success SYMLINKS 'clone repo with symlinked objects directory' '
345+
test_when_finished "rm -fr sensitive malicious" &&
346+
347+
mkdir -p sensitive &&
348+
echo "secret" >sensitive/file &&
349+
350+
git init malicious &&
351+
rm -fr malicious/.git/objects &&
352+
ln -s "$(pwd)/sensitive" ./malicious/.git/objects &&
353+
354+
test_must_fail git clone --local malicious clone 2>err &&
355+
356+
test_path_is_missing clone &&
357+
grep "failed to start iterator over" err
358+
'
359+
344360
test_done

0 commit comments

Comments
 (0)