Blame SOURCES/0002-transport-add-a-protocol-whitelist-environment-varia.patch

fdd391
From cfa4e13f09d07f679ffacdddfbe0ef44d1de32d9 Mon Sep 17 00:00:00 2001
fdd391
From: Petr Stodulka <pstodulk@redhat.com>
fdd391
Date: Wed, 28 Oct 2015 15:21:08 +0100
fdd391
Subject: [PATCH 2/5] transport: add a protocol-whitelist environment variable
fdd391
fdd391
If we are cloning an untrusted remote repository into a
fdd391
sandbox, we may also want to fetch remote submodules in
fdd391
order to get the complete view as intended by the other
fdd391
side. However, that opens us up to attacks where a malicious
fdd391
user gets us to clone something they would not otherwise
fdd391
have access to (this is not necessarily a problem by itself,
fdd391
but we may then act on the cloned contents in a way that
fdd391
exposes them to the attacker).
fdd391
fdd391
Ideally such a setup would sandbox git entirely away from
fdd391
high-value items, but this is not always practical or easy
fdd391
to set up (e.g., OS network controls may block multiple
fdd391
protocols, and we would want to enable some but not others).
fdd391
fdd391
We can help this case by providing a way to restrict
fdd391
particular protocols. We use a whitelist in the environment.
fdd391
This is more annoying to set up than a blacklist, but
fdd391
defaults to safety if the set of protocols git supports
fdd391
grows). If no whitelist is specified, we continue to default
fdd391
to allowing all protocols (this is an "unsafe" default, but
fdd391
since the minority of users will want this sandboxing
fdd391
effect, it is the only sensible one).
fdd391
fdd391
A note on the tests: ideally these would all be in a single
fdd391
test file, but the git-daemon and httpd test infrastructure
fdd391
is an all-or-nothing proposition rather than a test-by-test
fdd391
prerequisite. By putting them all together, we would be
fdd391
unable to test the file-local code on machines without
fdd391
apache.
fdd391
---
fdd391
 Documentation/git.txt | 32 ++++++++++++++++++++++++++++++++
fdd391
 connect.c             |  4 ++++
fdd391
 transport-helper.c    |  2 ++
fdd391
 transport.c           | 21 ++++++++++++++++++++-
fdd391
 transport.h           |  7 +++++++
fdd391
 5 files changed, 65 insertions(+), 1 deletion(-)
fdd391
fdd391
diff --git a/Documentation/git.txt b/Documentation/git.txt
fdd391
index 443d88f..179a0e8 100644
fdd391
--- a/Documentation/git.txt
fdd391
+++ b/Documentation/git.txt
fdd391
@@ -847,6 +847,38 @@ GIT_LITERAL_PATHSPECS::
fdd391
 	literal paths to Git (e.g., paths previously given to you by
fdd391
 	`git ls-tree`, `--raw` diff output, etc).
fdd391
 
fdd391
+`GIT_ALLOW_PROTOCOL`::
fdd391
+   If set, provide a colon-separated list of protocols which are
fdd391
+   allowed to be used with fetch/push/clone. This is useful to
fdd391
+   restrict recursive submodule initialization from an untrusted
fdd391
+   repository. Any protocol not mentioned will be disallowed (i.e.,
fdd391
+   this is a whitelist, not a blacklist). If the variable is not
fdd391
+   set at all, all protocols are enabled.  The protocol names
fdd391
+   currently used by git are:
fdd391
+
fdd391
+     - `file`: any local file-based path (including `file://` URLs,
fdd391
+       or local paths)
fdd391
+
fdd391
+     - `git`: the anonymous git protocol over a direct TCP
fdd391
+       connection (or proxy, if configured)
fdd391
+
fdd391
+     - `ssh`: git over ssh (including `host:path` syntax,
fdd391
+       `git+ssh://`, etc).
fdd391
+
fdd391
+     - `rsync`: git over rsync
fdd391
+
fdd391
+     - `http`: git over http, both "smart http" and "dumb http".
fdd391
+       Note that this does _not_ include `https`; if you want both,
fdd391
+       you should specify both as `http:https`.
fdd391
+
fdd391
+     - any external helpers are named by their protocol (e.g., use
fdd391
+       `hg` to allow the `git-remote-hg` helper)
fdd391
++
fdd391
+Note that this controls only git's internal protocol selection.
fdd391
+If libcurl is used (e.g., by the `http` transport), it may
fdd391
+redirect to other protocols. There is not currently any way to
fdd391
+restrict this.
fdd391
+
fdd391
 
fdd391
 Discussion[[Discussion]]
fdd391
 ------------------------
fdd391
diff --git a/connect.c b/connect.c
fdd391
index f57efd0..6d4ea13 100644
fdd391
--- a/connect.c
fdd391
+++ b/connect.c
fdd391
@@ -6,6 +6,7 @@
fdd391
 #include "run-command.h"
fdd391
 #include "remote.h"
fdd391
 #include "url.h"
fdd391
+#include "transport.h"
fdd391
 
fdd391
 static char *server_capabilities;
fdd391
 
fdd391
@@ -587,6 +588,7 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
fdd391
 		 * cannot connect.
fdd391
 		 */
fdd391
 		char *target_host = xstrdup(host);
fdd391
+		transport_check_allowed("git");
fdd391
 		if (git_use_proxy(host))
fdd391
 			conn = git_proxy_connect(fd, host);
fdd391
 		else
fdd391
@@ -623,6 +625,7 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
fdd391
 	if (protocol == PROTO_SSH) {
fdd391
 		const char *ssh = getenv("GIT_SSH");
fdd391
 		int putty = ssh && strcasestr(ssh, "plink");
fdd391
+		transport_check_allowed("ssh");
fdd391
 		if (!ssh) ssh = "ssh";
fdd391
 
fdd391
 		*arg++ = ssh;
fdd391
@@ -639,6 +642,7 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
fdd391
 		/* remove repo-local variables from the environment */
fdd391
 		conn->env = local_repo_env;
fdd391
 		conn->use_shell = 1;
fdd391
+		transport_check_allowed("file");
fdd391
 	}
fdd391
 	*arg++ = cmd.buf;
fdd391
 	*arg = NULL;
fdd391
diff --git a/transport-helper.c b/transport-helper.c
fdd391
index 522d791..be8402a 100644
fdd391
--- a/transport-helper.c
fdd391
+++ b/transport-helper.c
fdd391
@@ -932,6 +932,8 @@ int transport_helper_init(struct transport *transport, const char *name)
fdd391
 	struct helper_data *data = xcalloc(sizeof(*data), 1);
fdd391
 	data->name = name;
fdd391
 
fdd391
+	transport_check_allowed(name);
fdd391
+
fdd391
 	if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
fdd391
 		debug = 1;
fdd391
 
fdd391
diff --git a/transport.c b/transport.c
fdd391
index ba5d8af..733717d 100644
fdd391
--- a/transport.c
fdd391
+++ b/transport.c
fdd391
@@ -894,6 +894,20 @@ static int external_specification_len(const char *url)
fdd391
 	return strchr(url, ':') - url;
fdd391
 }
fdd391
 
fdd391
+void transport_check_allowed(const char *type)
fdd391
+{
fdd391
+	struct string_list allowed = STRING_LIST_INIT_DUP;
fdd391
+	const char *v = getenv("GIT_ALLOW_PROTOCOL");
fdd391
+
fdd391
+	if (!v)
fdd391
+		return;
fdd391
+
fdd391
+	string_list_split(&allowed, v, ':', -1);
fdd391
+	if (!unsorted_string_list_has_string(&allowed, type))
fdd391
+		die("transport '%s' not allowed", type);
fdd391
+	string_list_clear(&allowed, 0);
fdd391
+}
fdd391
+
fdd391
 struct transport *transport_get(struct remote *remote, const char *url)
fdd391
 {
fdd391
 	const char *helper;
fdd391
@@ -925,12 +939,14 @@ struct transport *transport_get(struct remote *remote, const char *url)
fdd391
 	if (helper) {
fdd391
 		transport_helper_init(ret, helper);
fdd391
 	} else if (!prefixcmp(url, "rsync:")) {
fdd391
+		transport_check_allowed("rsync");
fdd391
 		ret->get_refs_list = get_refs_via_rsync;
fdd391
 		ret->fetch = fetch_objs_via_rsync;
fdd391
 		ret->push = rsync_transport_push;
fdd391
 		ret->smart_options = NULL;
fdd391
 	} else if (is_local(url) && is_file(url) && is_bundle(url, 1)) {
fdd391
 		struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
fdd391
+		transport_check_allowed("file");
fdd391
 		ret->data = data;
fdd391
 		ret->get_refs_list = get_refs_from_bundle;
fdd391
 		ret->fetch = fetch_refs_from_bundle;
fdd391
@@ -942,7 +958,10 @@ struct transport *transport_get(struct remote *remote, const char *url)
fdd391
 		|| !prefixcmp(url, "ssh://")
fdd391
 		|| !prefixcmp(url, "git+ssh://")
fdd391
 		|| !prefixcmp(url, "ssh+git://")) {
fdd391
-		/* These are builtin smart transports. */
fdd391
+		/*
fdd391
+		 * These are builtin smart transports; "allowed" transports
fdd391
+		 * will be checked individually in git_connect.
fdd391
+		 */
fdd391
 		struct git_transport_data *data = xcalloc(1, sizeof(*data));
fdd391
 		ret->data = data;
fdd391
 		ret->set_option = NULL;
fdd391
diff --git a/transport.h b/transport.h
fdd391
index fcb1d25..2beda7d 100644
fdd391
--- a/transport.h
fdd391
+++ b/transport.h
fdd391
@@ -113,6 +113,13 @@ struct transport {
fdd391
 /* Returns a transport suitable for the url */
fdd391
 struct transport *transport_get(struct remote *, const char *);
fdd391
 
fdd391
+/*
fdd391
+ * Check whether a transport is allowed by the environment,
fdd391
+ * and die otherwise. type should generally be the URL scheme,
fdd391
+ * as described in Documentation/git.txt
fdd391
+ */
fdd391
+void transport_check_allowed(const char *type);
fdd391
+
fdd391
 /* Transport options which apply to git:// and scp-style URLs */
fdd391
 
fdd391
 /* The program to use on the remote side to send a pack */
fdd391
-- 
fdd391
2.1.0
fdd391