]> git.ipfire.org Git - thirdparty/git.git/commit - apply.c
apply: fix writing behind newly created symbolic links
authorPatrick Steinhardt <ps@pks.im>
Thu, 2 Feb 2023 10:54:34 +0000 (11:54 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 3 Feb 2023 22:41:31 +0000 (14:41 -0800)
commitfade728df1221598f42d391cf377e9e84a32053f
treeb07bdf88b2db5ddf84cb612cc42d93a2a24cfb78
parent0227130244c007870c106fc613903d078730e45c
apply: fix writing behind newly created symbolic links

When writing files git-apply(1) initially makes sure that none of the
files it is about to create are behind a symlink:

```
 $ git init repo
 Initialized empty Git repository in /tmp/repo/.git/
 $ cd repo/
 $ ln -s dir symlink
 $ git apply - <<EOF
 diff --git a/symlink/file b/symlink/file
 new file mode 100644
 index 0000000..e69de29
 EOF
 error: affected file 'symlink/file' is beyond a symbolic link
```

This safety mechanism is crucial to ensure that we don't write outside
of the repository's working directory. It can be fooled though when the
patch that is being applied creates the symbolic link in the first
place, which can lead to writing files in arbitrary locations.

Fix this by checking whether the path we're about to create is
beyond a symlink or not. Tightening these checks like this should be
fine as we already have these precautions in Git as explained
above. Ideally, we should update the check we do up-front before
starting to reflect the computed changes to the working tree so that
we catch this case as well, but as part of embargoed security work,
adding an equivalent check just before we try to write out a file
should serve us well as a reasonable first step.

Digging back into history shows that this vulnerability has existed
since at least Git v2.9.0. As Git v2.8.0 and older don't build on my
system anymore I cannot tell whether older versions are affected, as
well.

Reported-by: Joern Schneeweisz <jschneeweisz@gitlab.com>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
apply.c
t/t4115-apply-symlink.sh