1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
Backport of:
From a5adaced2e13c135d5d9cc65be9eb95aa3bacedf Mon Sep 17 00:00:00 2001
From: Jeff King <peff@peff.net>
Date: Wed, 16 Sep 2015 13:12:52 -0400
Subject: [PATCH] transport: add a protocol-whitelist environment variable
If we are cloning an untrusted remote repository into a
sandbox, we may also want to fetch remote submodules in
order to get the complete view as intended by the other
side. However, that opens us up to attacks where a malicious
user gets us to clone something they would not otherwise
have access to (this is not necessarily a problem by itself,
but we may then act on the cloned contents in a way that
exposes them to the attacker).
Ideally such a setup would sandbox git entirely away from
high-value items, but this is not always practical or easy
to set up (e.g., OS network controls may block multiple
protocols, and we would want to enable some but not others).
We can help this case by providing a way to restrict
particular protocols. We use a whitelist in the environment.
This is more annoying to set up than a blacklist, but
defaults to safety if the set of protocols git supports
grows). If no whitelist is specified, we continue to default
to allowing all protocols (this is an "unsafe" default, but
since the minority of users will want this sandboxing
effect, it is the only sensible one).
A note on the tests: ideally these would all be in a single
test file, but the git-daemon and httpd test infrastructure
is an all-or-nothing proposition rather than a test-by-test
prerequisite. By putting them all together, we would be
unable to test the file-local code on machines without
apache.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
Documentation/git.txt | 32 ++++++++++++++
connect.c | 5 +++
t/lib-proto-disable.sh | 96 ++++++++++++++++++++++++++++++++++++++++++
t/t5810-proto-disable-local.sh | 14 ++++++
t/t5811-proto-disable-git.sh | 20 +++++++++
t/t5812-proto-disable-http.sh | 20 +++++++++
t/t5813-proto-disable-ssh.sh | 20 +++++++++
t/t5814-proto-disable-ext.sh | 18 ++++++++
transport-helper.c | 2 +
transport.c | 21 ++++++++-
transport.h | 7 +++
11 files changed, 254 insertions(+), 1 deletion(-)
create mode 100644 t/lib-proto-disable.sh
create mode 100755 t/t5810-proto-disable-local.sh
create mode 100755 t/t5811-proto-disable-git.sh
create mode 100755 t/t5812-proto-disable-http.sh
create mode 100755 t/t5813-proto-disable-ssh.sh
create mode 100755 t/t5814-proto-disable-ext.sh
Index: git-1.7.9.5/Documentation/git.txt
===================================================================
--- git-1.7.9.5.orig/Documentation/git.txt 2015-12-14 11:28:56.815667028 -0500
+++ git-1.7.9.5/Documentation/git.txt 2015-12-14 11:28:56.811666996 -0500
@@ -607,6 +607,38 @@
-----------
Please see linkgit:gitglossary[7].
+`GIT_ALLOW_PROTOCOL`::
+ If set, provide a colon-separated list of protocols which are
+ allowed to be used with fetch/push/clone. This is useful to
+ restrict recursive submodule initialization from an untrusted
+ repository. Any protocol not mentioned will be disallowed (i.e.,
+ this is a whitelist, not a blacklist). If the variable is not
+ set at all, all protocols are enabled. The protocol names
+ currently used by git are:
+
+ - `file`: any local file-based path (including `file://` URLs,
+ or local paths)
+
+ - `git`: the anonymous git protocol over a direct TCP
+ connection (or proxy, if configured)
+
+ - `ssh`: git over ssh (including `host:path` syntax,
+ `git+ssh://`, etc).
+
+ - `rsync`: git over rsync
+
+ - `http`: git over http, both "smart http" and "dumb http".
+ Note that this does _not_ include `https`; if you want both,
+ you should specify both as `http:https`.
+
+ - any external helpers are named by their protocol (e.g., use
+ `hg` to allow the `git-remote-hg` helper)
++
+Note that this controls only git's internal protocol selection.
+If libcurl is used (e.g., by the `http` transport), it may
+redirect to other protocols. There is not currently any way to
+restrict this.
+
Environment Variables
---------------------
Index: git-1.7.9.5/connect.c
===================================================================
--- git-1.7.9.5.orig/connect.c 2015-12-14 11:28:56.815667028 -0500
+++ git-1.7.9.5/connect.c 2015-12-14 11:30:16.972311958 -0500
@@ -7,6 +7,7 @@
#include "remote.h"
#include "tcp.h"
#include "url.h"
+#include "string-list.h"
static char *server_capabilities;
@@ -166,6 +167,20 @@
static struct child_process no_fork;
+void connect_transport_check_allowed(const char *type)
+{
+ struct string_list allowed = STRING_LIST_INIT_DUP;
+ const char *v = getenv("GIT_ALLOW_PROTOCOL");
+
+ if (!v)
+ return;
+
+ string_list_split(&allowed, v, ':', -1);
+ if (!unsorted_string_list_has_string(&allowed, type))
+ die("transport '%s' not allowed", type);
+ string_list_clear(&allowed, 0);
+}
+
/*
* This returns a dummy child_process if the transport protocol does not
* need fork(2), or a struct child_process object if it does. Once done,
@@ -264,6 +279,8 @@
port = get_port(host);
if (protocol == PROTO_GIT) {
+ connect_transport_check_allowed("git");
+
/* These underlying connection commands die() if they
* cannot connect.
*/
@@ -305,6 +322,7 @@
const char *ssh = getenv("GIT_SSH");
int putty = ssh && strcasestr(ssh, "plink");
if (!ssh) ssh = "ssh";
+ connect_transport_check_allowed("ssh");
*arg++ = ssh;
if (putty && !strcasestr(ssh, "tortoiseplink"))
@@ -320,6 +338,7 @@
/* remove repo-local variables from the environment */
conn->env = local_repo_env;
conn->use_shell = 1;
+ connect_transport_check_allowed("file");
}
*arg++ = cmd.buf;
*arg = NULL;
Index: git-1.7.9.5/t/lib-proto-disable.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ git-1.7.9.5/t/lib-proto-disable.sh 2015-12-14 11:28:56.815667028 -0500
@@ -0,0 +1,96 @@
+# Test routines for checking protocol disabling.
+
+# test cloning a particular protocol
+# $1 - description of the protocol
+# $2 - machine-readable name of the protocol
+# $3 - the URL to try cloning
+test_proto () {
+ desc=$1
+ proto=$2
+ url=$3
+
+ test_expect_success "clone $1 (enabled)" '
+ rm -rf tmp.git &&
+ (
+ GIT_ALLOW_PROTOCOL=$proto &&
+ export GIT_ALLOW_PROTOCOL &&
+ git clone --bare "$url" tmp.git
+ )
+ '
+
+ test_expect_success "fetch $1 (enabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=$proto &&
+ export GIT_ALLOW_PROTOCOL &&
+ git fetch
+ )
+ '
+
+ test_expect_success "push $1 (enabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=$proto &&
+ export GIT_ALLOW_PROTOCOL &&
+ git push origin HEAD:pushed
+ )
+ '
+
+ test_expect_success "push $1 (disabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git push origin HEAD:pushed
+ )
+ '
+
+ test_expect_success "fetch $1 (disabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git fetch
+ )
+ '
+
+ test_expect_success "clone $1 (disabled)" '
+ rm -rf tmp.git &&
+ (
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git clone --bare "$url" tmp.git
+ )
+ '
+}
+
+# set up an ssh wrapper that will access $host/$repo in the
+# trash directory, and enable it for subsequent tests.
+setup_ssh_wrapper () {
+ test_expect_success 'setup ssh wrapper' '
+ write_script ssh-wrapper <<-\EOF &&
+ echo >&2 "ssh: $*"
+ host=$1; shift
+ cd "$TRASH_DIRECTORY/$host" &&
+ eval "$*"
+ EOF
+ GIT_SSH="$PWD/ssh-wrapper" &&
+ export GIT_SSH &&
+ export TRASH_DIRECTORY
+ '
+}
+
+# set up a wrapper that can be used with remote-ext to
+# access repositories in the "remote" directory of trash-dir,
+# like "ext::fake-remote %S repo.git"
+setup_ext_wrapper () {
+ test_expect_success 'setup ext wrapper' '
+ write_script fake-remote <<-\EOF &&
+ echo >&2 "fake-remote: $*"
+ cd "$TRASH_DIRECTORY/remote" &&
+ eval "$*"
+ EOF
+ PATH=$TRASH_DIRECTORY:$PATH &&
+ export TRASH_DIRECTORY
+ '
+}
Index: git-1.7.9.5/t/t5810-proto-disable-local.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ git-1.7.9.5/t/t5810-proto-disable-local.sh 2015-12-14 11:28:56.815667028 -0500
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+test_description='test disabling of local paths in clone/fetch'
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+
+test_expect_success 'setup repository to clone' '
+ test_commit one
+'
+
+test_proto "file://" file "file://$PWD"
+test_proto "path" file .
+
+test_done
Index: git-1.7.9.5/t/t5812-proto-disable-http.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ git-1.7.9.5/t/t5812-proto-disable-http.sh 2015-12-14 11:28:56.815667028 -0500
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='test disabling of git-over-http in clone/fetch'
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+. "$TEST_DIRECTORY/lib-httpd.sh"
+start_httpd
+
+test_expect_success 'create git-accessible repo' '
+ bare="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ test_commit one &&
+ git --bare init "$bare" &&
+ git push "$bare" HEAD &&
+ (cd "$bare" && git config http.receivepack true)
+'
+
+test_proto "smart http" http "$HTTPD_URL/smart/repo.git"
+
+stop_httpd
+test_done
Index: git-1.7.9.5/t/t5813-proto-disable-ssh.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ git-1.7.9.5/t/t5813-proto-disable-ssh.sh 2015-12-14 11:28:56.815667028 -0500
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='test disabling of git-over-ssh in clone/fetch'
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+
+setup_ssh_wrapper
+
+test_expect_success 'setup repository to clone' '
+ test_commit one &&
+ mkdir remote &&
+ git init --bare remote/repo.git &&
+ git push remote/repo.git HEAD
+'
+
+test_proto "host:path" ssh "remote:repo.git"
+test_proto "ssh://" ssh "ssh://remote/$PWD/remote/repo.git"
+test_proto "git+ssh://" ssh "git+ssh://remote/$PWD/remote/repo.git"
+
+test_done
Index: git-1.7.9.5/t/t5814-proto-disable-ext.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ git-1.7.9.5/t/t5814-proto-disable-ext.sh 2015-12-14 11:28:56.815667028 -0500
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+test_description='test disabling of remote-helper paths in clone/fetch'
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+
+setup_ext_wrapper
+
+test_expect_success 'setup repository to clone' '
+ test_commit one &&
+ mkdir remote &&
+ git init --bare remote/repo.git &&
+ git push remote/repo.git HEAD
+'
+
+test_proto "remote-helper" ext "ext::fake-remote %S repo.git"
+
+test_done
Index: git-1.7.9.5/transport-helper.c
===================================================================
--- git-1.7.9.5.orig/transport-helper.c 2015-12-14 11:28:56.815667028 -0500
+++ git-1.7.9.5/transport-helper.c 2015-12-14 11:28:56.815667028 -0500
@@ -878,6 +878,8 @@
struct helper_data *data = xcalloc(sizeof(*data), 1);
data->name = name;
+ transport_check_allowed(name);
+
if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
debug = 1;
Index: git-1.7.9.5/transport.c
===================================================================
--- git-1.7.9.5.orig/transport.c 2015-12-14 11:28:56.815667028 -0500
+++ git-1.7.9.5/transport.c 2015-12-14 11:28:56.815667028 -0500
@@ -11,6 +11,7 @@
#include "branch.h"
#include "url.h"
#include "submodule.h"
+#include "string-list.h"
/* rsync support */
@@ -884,6 +885,20 @@
return strchr(url, ':') - url;
}
+void transport_check_allowed(const char *type)
+{
+ struct string_list allowed = STRING_LIST_INIT_DUP;
+ const char *v = getenv("GIT_ALLOW_PROTOCOL");
+
+ if (!v)
+ return;
+
+ string_list_split(&allowed, v, ':', -1);
+ if (!unsorted_string_list_has_string(&allowed, type))
+ die("transport '%s' not allowed", type);
+ string_list_clear(&allowed, 0);
+}
+
struct transport *transport_get(struct remote *remote, const char *url)
{
const char *helper;
@@ -915,12 +930,14 @@
if (helper) {
transport_helper_init(ret, helper);
} else if (!prefixcmp(url, "rsync:")) {
+ transport_check_allowed("rsync");
ret->get_refs_list = get_refs_via_rsync;
ret->fetch = fetch_objs_via_rsync;
ret->push = rsync_transport_push;
ret->smart_options = NULL;
} else if (is_local(url) && is_file(url) && is_bundle(url, 1)) {
struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
+ transport_check_allowed("file");
ret->data = data;
ret->get_refs_list = get_refs_from_bundle;
ret->fetch = fetch_refs_from_bundle;
@@ -932,7 +949,10 @@
|| !prefixcmp(url, "ssh://")
|| !prefixcmp(url, "git+ssh://")
|| !prefixcmp(url, "ssh+git://")) {
- /* These are builtin smart transports. */
+ /*
+ * These are builtin smart transports; "allowed" transports
+ * will be checked individually in git_connect.
+ */
struct git_transport_data *data = xcalloc(1, sizeof(*data));
ret->data = data;
ret->set_option = NULL;
Index: git-1.7.9.5/transport.h
===================================================================
--- git-1.7.9.5.orig/transport.h 2015-12-14 11:28:56.815667028 -0500
+++ git-1.7.9.5/transport.h 2015-12-14 11:28:56.815667028 -0500
@@ -109,6 +109,13 @@
/* Returns a transport suitable for the url */
struct transport *transport_get(struct remote *, const char *);
+/*
+ * Check whether a transport is allowed by the environment,
+ * and die otherwise. type should generally be the URL scheme,
+ * as described in Documentation/git.txt
+ */
+void transport_check_allowed(const char *type);
+
/* Transport options which apply to git:// and scp-style URLs */
/* The program to use on the remote side to send a pack */
|