4b749c
From 837a69dc6ff77d8c93e73a64c067fe60530e4f1b Mon Sep 17 00:00:00 2001
4b749c
From: Mateusz Kwapich <mitrandir@fb.com>
4b749c
Date: Sun, 20 Mar 2016 21:52:21 -0700
4b749c
Subject: [PATCH 1/6] subrepo: set GIT_ALLOW_PROTOCOL to limit git clone
4b749c
 protocols (SEC)
4b749c
4b749c
CVE-2016-3068 (1/1)
4b749c
4b749c
Git's git-remote-ext remote helper provides an ext:: URL scheme that
4b749c
allows running arbitrary shell commands. This feature allows
4b749c
implementing simple git smart transports with a single shell shell
4b749c
command. However, git submodules could clone arbitrary URLs specified
4b749c
in the .gitmodules file. This was reported as CVE-2015-7545 and fixed
4b749c
in git v2.6.1.
4b749c
4b749c
However, if a user directly clones a malicious ext URL, the git client
4b749c
will still run arbitrary shell commands.
4b749c
4b749c
Mercurial is similarly effected. Mercurial allows specifying git
4b749c
repositories as subrepositories. Git ext:: URLs can be specified as
4b749c
Mercurial subrepositories allowing arbitrary shell commands to be run
4b749c
on `hg clone ...`.
4b749c
4b749c
The Mercurial community would like to thank Blake Burkhart for
4b749c
reporting this issue. The description of the issue is copied from
4b749c
Blake's report.
4b749c
4b749c
This commit changes submodules to pass the GIT_ALLOW_PROTOCOL env
4b749c
variable to git commands  with the same list of allowed protocols that
4b749c
git submodule is using.
4b749c
4b749c
When the GIT_ALLOW_PROTOCOL env variable is already set, we just pass it
4b749c
to git without modifications.
4b749c
---
4b749c
 mercurial/subrepo.py     |  5 +++++
4b749c
 tests/test-subrepo-git.t | 34 ++++++++++++++++++++++++++++++++++
4b749c
 2 files changed, 39 insertions(+)
4b749c
4b749c
diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
4b749c
index 3747377..7286f06 100644
4b749c
--- a/mercurial/subrepo.py
4b749c
+++ b/mercurial/subrepo.py
4b749c
@@ -1060,6 +1060,11 @@ class gitsubrepo(abstractsubrepo):
4b749c
         are not supported and very probably fail.
4b749c
         """
4b749c
         self._ui.debug('%s: git %s\n' % (self._relpath, ' '.join(commands)))
4b749c
+        if env is None:
4b749c
+            env = os.environ.copy()
4b749c
+        # fix for Git CVE-2015-7545
4b749c
+        if 'GIT_ALLOW_PROTOCOL' not in env:
4b749c
+            env['GIT_ALLOW_PROTOCOL'] = 'file:git:http:https:ssh'
4b749c
         # unless ui.quiet is set, print git's stderr,
4b749c
         # which is mostly progress and useful info
4b749c
         errpipe = None
4b749c
diff --git a/tests/test-subrepo-git.t b/tests/test-subrepo-git.t
4b749c
index 9361193..24cb6a2 100644
4b749c
--- a/tests/test-subrepo-git.t
4b749c
+++ b/tests/test-subrepo-git.t
4b749c
@@ -558,3 +558,37 @@ traceback
4b749c
 #endif
4b749c
 
4b749c
   $ cd ..
4b749c
+
4b749c
+test for Git CVE-2016-3068
4b749c
+  $ hg init malicious-subrepository
4b749c
+  $ cd malicious-subrepository
4b749c
+  $ echo "s = [git]ext::sh -c echo% pwned% >&2" > .hgsub
4b749c
+  $ git init s
4b749c
+  Initialized empty Git repository in $TESTTMP/tc/malicious-subrepository/s/.git/
4b749c
+  $ cd s
4b749c
+  $ git commit --allow-empty -m 'empty'
4b749c
+  [master (root-commit) 153f934] empty
4b749c
+  $ cd ..
4b749c
+  $ hg add .hgsub
4b749c
+  $ hg commit -m "add subrepo"
4b749c
+  $ cd ..
4b749c
+  $ env -u GIT_ALLOW_PROTOCOL hg clone malicious-subrepository malicious-subrepository-protected
4b749c
+  Cloning into '$TESTTMP/tc/malicious-subrepository-protected/s'...
4b749c
+  fatal: transport 'ext' not allowed
4b749c
+  updating to branch default
4b749c
+  cloning subrepo s from ext::sh -c echo% pwned% >&2
4b749c
+  abort: git clone error 128 in s (in subrepo s)
4b749c
+  [255]
4b749c
+
4b749c
+whitelisting of ext should be respected (that's the git submodule behaviour)
4b749c
+  $ env GIT_ALLOW_PROTOCOL=ext hg clone malicious-subrepository malicious-subrepository-clone-allowed
4b749c
+  Cloning into '$TESTTMP/tc/malicious-subrepository-clone-allowed/s'...
4b749c
+  pwned
4b749c
+  fatal: Could not read from remote repository.
4b749c
+  
4b749c
+  Please make sure you have the correct access rights
4b749c
+  and the repository exists.
4b749c
+  updating to branch default
4b749c
+  cloning subrepo s from ext::sh -c echo% pwned% >&2
4b749c
+  abort: git clone error 128 in s (in subrepo s)
4b749c
+  [255]
4b749c
-- 
4b749c
2.4.11
4b749c