Skip to contentSkip to conversation

git-daemon support for user-relative paths.

PATCH In review Andreas Ericsson 5 participants 12 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 5/5 git-daemon support for user-relative paths.

Dropped a fair amount of reundant code in favour of the library code in path.c

Added option --strict-paths with documentation, with backwards compatibility for whitelist entries with symlinks.

Everything that worked earlier still works insofar as I have remembered testing it.

---

 Documentation/git-daemon.txt       |   16 ++++
 Documentation/pull-fetch-param.txt |    7 +-
 daemon.c                           |  136 ++++++++++++++----------------------
 3 files changed, 72 insertions(+), 87 deletions(-)

applies-to: d1e53de141c477b0b7dd2c6bd5897ea6239a7b20 4c4049426e6202bdb1429f5aeb4e5429df41c015

Documentation/git-daemon.txt +14 -2
index 3783858..972e0e1 100644
@@ -29,9 +29,15 @@ This is ideally suited for read-only upd
 
 OPTIONS
 -------
++--strict-paths::
+	Match paths exactly (i.e. don't allow "/foo/repo" when the real path is
+	"/foo/repo.git" or "/foo/repo/.git") and don't do user-relative paths.
+	git-daemon will refuse to start when this option is enabled and no
+	whitelist is specified.
+
 --export-all::
 	Allow pulling from all directories that look like GIT repositories
-	(have the 'objects' subdirectory and a 'HEAD' file), even if they
+	(have the 'objects' and 'refs' subdirectories), even if they
 	do not have the 'git-daemon-export-ok' file.
 
 --inetd::
@@ -57,9 +63,15 @@ OPTIONS
 --verbose::
 	Log details about the incoming connections and requested files.
 
+<directory>::
+	A directory to add to the whitelist of allowed directories. Unless
+	--strict-paths is specified this will also include subdirectories
+	of each named directory.
+
 Author
 ------
-Written by Linus Torvalds <[email protected]> and YOSHIFUJI Hideaki <[email protected]>
+Written by Linus Torvalds <[email protected]>, YOSHIFUJI Hideaki
+<[email protected]> and the git-list <[email protected]>
 
 Documentation
 --------------
Documentation/pull-fetch-param.txt +4 -3
index a7628aa..6413d52 100644
@@ -9,15 +9,16 @@
 - http://host.xz/path/to/repo.git/
 - https://host.xz/path/to/repo.git/
 - git://host.xz/path/to/repo.git/
+- git://host.xz/~user/path/to/repo.git/
 - ssh://host.xz/path/to/repo.git/
 - ssh://host.xz/~user/path/to/repo.git/
 - ssh://host.xz/~/path/to/repo.git
 ===============================================================
 +
 	SSH Is the default transport protocol and also supports an
-	scp-like syntax.  Both syntaxes support username expansion.
-	The following three are identical to the last three above,
-	respectively:
+	scp-like syntax.  Both syntaxes support username expansion,
+	as does the native git protocol. The following three are
+	identical to the last three above, respectively:
 +
 ===============================================================
 - host.xz:/path/to/repo.git/
daemon.c +54 -82

204 lines not on this page · raw message

---
0.99.9.GIT

#1 lore raw
Quoted 2 lines · [email protected] (Andreas Ericsson) writes:

Everything that worked earlier still works insofar as I have remembered testing it.

Hmph...

daemon.c static int set_dir(const char *dir) · line 142

Quoted 4 lines
daemon.c +0 -0
@@ -152,45 +140,17 @@ static int set_dir(const char *dir)
 	 * We want a readable HEAD, usable "objects" directory, and
 	 * a "git-daemon-export-ok" flag that says that the other side
 	 * is ok with us doing this.

Well, not anymore about HEAD as far as I can tell... Maybe in enter_repo ([PATCH 1/5]) we need to do something like what setup.c::is_toplevel_directory() does?

daemon.c static int set_dir(const char *dir) · line 173

Quoted 4 lines
daemon.c +0 -4
-static int upload(char *dir)
-{
-	/* Try paths in this order */
-	static const char *paths[] = { "%s", "%s/.git", "%s.git", "%s.git/.git", NULL };

I think this list was added relatively recently as a usability measure. Maybe we would want an equivalent in enter_repo()? Under strict-path, I think not doing any DWIM like this is fine, but otherwise I suspect changing this would break existing remotes/origin file people may have. In addition enter_repo() as posted does its own DWIM to chdir to ".git" unconditionally as I pointed out...

Needs a bit more thought, but I think otherwise the basic idea is right.

#2 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 2 lines · [email protected] (Andreas Ericsson) writes:

Everything that worked earlier still works insofar as I have remembered testing it.

Hmph...

:)

I haven't used the daemon without our patches. Everything works as per spec now though.

Quoted 11 lines

daemon.c static int set_dir(const char *dir) · line 142

Quoted 5 lines
daemon.c +0 -0
@@ -152,45 +140,17 @@ static int set_dir(const char *dir)
 	 * We want a readable HEAD, usable "objects" directory, and
 	 * a "git-daemon-export-ok" flag that says that the other side
 	 * is ok with us doing this.

Well, not anymore about HEAD as far as I can tell... Maybe in enter_repo ([PATCH 1/5]) we need to do something like what setup.c::is_toplevel_directory() does?

Umm... Perhaps. I just noticed that it's possible for the .git/objects directory to go missing though.

Quoted 9 lines

daemon.c static int set_dir(const char *dir) · line 173

Quoted 5 lines
daemon.c +0 -4
-static int upload(char *dir)
-{
-	/* Try paths in this order */
-	static const char *paths[] = { "%s", "%s/.git", "%s.git", "%s.git/.git", NULL };

I think this list was added relatively recently as a usability measure. Maybe we would want an equivalent in enter_repo()?

It's already there but in a different format. Adding "if (!strict)" to the previously unconditional 'chdir(".git");' won't change that.

Like I said, I made sure everything that worked before works now too.

Quoted 6 lines

Under strict-path, I think not doing any DWIM like this is fine, but otherwise I suspect changing this would break existing remotes/origin file people may have. In addition enter_repo() as posted does its own DWIM to chdir to ".git" unconditionally as I pointed out...

DWIM? That's an acronym I don't know.

Quoted 3 lines

Needs a bit more thought, but I think otherwise the basic idea is right.

Anything I should change before "take four" ?

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#3 lore raw
Quoted 5 lines · Andreas Ericsson

It's already there but in a different format. Adding "if (!strict)" to the previously unconditional 'chdir(".git");' won't change that.

Like I said, I made sure everything that worked before works now too.

Quoted 8 lines
Quoted 5 lines

Under strict-path, I think not doing any DWIM like this is fine, but otherwise I suspect changing this would break existing remotes/origin file people may have. In addition enter_repo() as posted does its own DWIM to chdir to ".git" unconditionally as I pointed out...

DWIM? That's an acronym I don't know.

DWIM = "Do What I Mean", i.e. program trying to be clever. A (usually) good thing for usability, a very bad thing for security.

In particular, DWIM is bad for security when you have a flow like:

	user input -> security check -> DWIM

... which lets the user subvert the security check by knowing how the DWIM will mangle the input. What's worse, programmers like yourself frequently say "oh, it's okay, though, I know what the DWIM does and it can't break the security checks I do."

Well, then someone comes along and changes either the security checks (e.g. add a blacklist), or the DWIM, or both. Security hole opens.

Therefore, the flow must *ALWAYS* be:

	user input -> DWIM -> security check

Your patch re-introduces the incorrect flow.

-hpa

#4 lore raw
In reply to Andreas Ericsson
Quoted 7 lines · Andreas Ericsson
Quoted 4 lines
Quoted 4 lines
daemon.c +0 -4
-static int upload(char *dir)
-{
-	/* Try paths in this order */
-	static const char *paths[] = { "%s", "%s/.git", "%s.git", "%s.git/.git", NULL };

...

Quoted 7 lines
Quoted 5 lines

Under strict-path, I think not doing any DWIM like this is fine, but otherwise I suspect changing this would break existing remotes/origin file people may have. In addition enter_repo() as posted does its own DWIM to chdir to ".git" unconditionally as I pointed out...

DWIM? That's an acronym I don't know.

"Do what I mean". It lets users say:

git clone git://sample.xz/pub/uemacs uemacs

when the repository on the server side is at any of the following places:

/pub/uemacs

-- a regular naked repository, with subdirectories
/pub/uemacs/refs and /pub/uemacs/objects/, obviously.

/pub/uemacs/.git

-- /pub/uemacs is an ordinary repository with possibly a
working tree; has /pub/uemacs/.git/refs and friends.

/pub/uemacs.git

-- when above two do not exist but this does; a regular
naked repository, with subdirectories /pub/uemacs.git/refs and friends.

/pub/uemacs.git/.git

-- no /pub/uemacs, and /pub/uemacs.git is an ordinary
repository with possibly a working tree; has /pub/uemacs.git/.git/refs and friends.

which is a nice feature, but under --strict-path we need to be careful that we apply whitelist correctly while allowing DWIM.

#5 lore raw
In reply to Andreas Ericsson
Quoted 4 lines · Andreas Ericsson
Quoted 2 lines

I think this list was added relatively recently as a usability measure. Maybe we would want an equivalent in enter_repo()?

It's already there but in a different format.

I noticed that after I asked that question. Thanks.

Quoted 7 lines
Quoted 5 lines

Under strict-path, I think not doing any DWIM like this is fine, but otherwise I suspect changing this would break existing remotes/origin file people may have. In addition enter_repo() as posted does its own DWIM to chdir to ".git" unconditionally as I pointed out...

DWIM? That's an acronym I don't know.

I think you got HPA's message about it and its security implications.

Quoted 4 lines
Quoted 2 lines

Needs a bit more thought, but I think otherwise the basic idea is right.

Anything I should change before "take four" ?

I think it might make sense to inserting something like the attached untested patch in your series, between library and upload-pack. The validation done by path_ok() in git-daemon probabaly needs to lose alternate checks and validate only the path returned by enter_repo(). This would make writing whitelist by git-daemon administrator a bit more cumbersome, but I suspect at the same time would make auditing easier. So the series would become:

[1/6] Library code for user-relative paths, take three. [2/6] Do not DWIM in userpath library under strict mode. [3/6] Server-side support for user-relative paths. [4/6] Client side support for user-relative paths. [5/6] Documentation update for user-relative paths. [6/6] git-daemon support for user-relative paths.

-- >8 -- cut here -- >8 --
Subject: [PATCH] Do not DWIM in userpath library under strict mode.

This should force git-daemon administrator's job a bit harder because the exact paths need to be given in the whitelist, but at the same time makes the auditing easier.

This moves validate_symref() from refs.c to path.c, because we need to link git-daemon with path.c for its "enter_repo()", but we do not want to link the daemon with the rest of git libraries and its requirements.

---

 path.c |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++--------------
 refs.c |   40 ---------------------------------
 2 files changed, 60 insertions(+), 57 deletions(-)

applies-to: 7223b28ede4511977d1e71f45fb2867780027235 bd8770050ee2c8d7aa5b2c3f138bc65d83a974e8

path.c +60 -17
index 5b61709..d635470 100644
@@ -91,20 +91,55 @@ char *safe_strncpy(char *dest, const cha
 	return dest;
 }
 
+int validate_symref(const char *path)
+{
+	struct stat st;
+	char *buf, buffer[256];
+	int len, fd;
+
+	if (lstat(path, &st) < 0)
+		return -1;
+
+	/* Make sure it is a "refs/.." symlink */
+	if (S_ISLNK(st.st_mode)) {
+		len = readlink(path, buffer, sizeof(buffer)-1);
+		if (len >= 5 && !memcmp("refs/", buffer, 5))
+			return 0;
+		return -1;
+	}
+
+	/*
+	 * Anything else, just open it and try to see if it is a symbolic ref.
+	 */
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -1;
+	len = read(fd, buffer, sizeof(buffer)-1);
+	close(fd);
+
+	/*
+	 * Is it a symbolic ref?
+	 */
+	if (len < 4 || memcmp("ref:", buffer, 4))
+		return -1;
+	buf = buffer + 4;
+	len -= 4;
+	while (len && isspace(*buf))
+		buf++, len--;
+	if (len >= 5 && !memcmp("refs/", buf, 5))
+		return 0;
+	return -1;
+}
+
 static char *current_dir()
 {
 	return getcwd(pathname, sizeof(pathname));
 }
 
-/* Take a raw path from is_git_repo() and canonicalize it using Linus'
- * idea of a blind chdir() and getcwd(). */
-static const char *canonical_path(char *path, int strict)
+static int user_chdir(char *path)
 {
 	char *dir = path;
 
-	if(strict && *dir != '/')
-		return NULL;
-
 	if(*dir == '~') {		/* user-relative path */
 		struct passwd *pw;
 		char *slash = strchr(dir, '/');
@@ -125,19 +160,19 @@ static const char *canonical_path(char *
 
 		/* make sure we got something back that we can chdir() to */
 		if(!pw || chdir(pw->pw_dir) < 0)
-			return NULL;
+			return -1;
 
 		if(!slash || !slash[1]) /* no path following username */
-			return current_dir();
+			return 0;
 
 		dir = slash + 1;
 	}
 
 	/* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
 	if(chdir(dir) < 0)
-		return NULL;
+		return -1;
 
-	return current_dir();
+	return 0;
 }
 
 char *enter_repo(char *path, int strict)
@@ -145,16 +180,24 @@ char *enter_repo(char *path, int strict)
 	if(!path)
 		return NULL;
 
-	if(!canonical_path(path, strict)) {
-		if(strict || !canonical_path(mkpath("%s.git", path), strict))
+	if (strict) {
+		if((path[0] != '/') || chdir(path) < 0)
 			return NULL;
 	}
+	else {
+		if (!*path)
+			; /* happy -- no chdir */
+		else if (!user_chdir(path))
+			; /* happy -- as given */
+		else if (!user_chdir(mkpath("%s.git", path)))
+			; /* happy -- uemacs --> uemacs.git */
+		else
+			return NULL;
+		(void)chdir(".git");
+	}
 
-	/* This is perfectly safe, and people tend to think of the directory
-	 * where they ran git-init-db as their repository, so humour them. */
-	(void)chdir(".git");
-
-	if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0) {
+	if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
+	   validate_symref("HEAD") == 0) {
 		putenv("GIT_DIR=.");
 		return current_dir();
 	}
refs.c +0 -40
index f324be5..ac26198 100644
@@ -10,46 +10,6 @@
 #define USE_SYMLINK_HEAD 1
 #endif
 
-int validate_symref(const char *path)
-{
-	struct stat st;
-	char *buf, buffer[256];
-	int len, fd;
-
-	if (lstat(path, &st) < 0)
-		return -1;
-
-	/* Make sure it is a "refs/.." symlink */
-	if (S_ISLNK(st.st_mode)) {
-		len = readlink(path, buffer, sizeof(buffer)-1);
-		if (len >= 5 && !memcmp("refs/", buffer, 5))
-			return 0;
-		return -1;
-	}
-
-	/*
-	 * Anything else, just open it and try to see if it is a symbolic ref.
-	 */
-	fd = open(path, O_RDONLY);
-	if (fd < 0)
-		return -1;
-	len = read(fd, buffer, sizeof(buffer)-1);
-	close(fd);
-
-	/*
-	 * Is it a symbolic ref?
-	 */
-	if (len < 4 || memcmp("ref:", buffer, 4))
-		return -1;
-	buf = buffer + 4;
-	len -= 4;
-	while (len && isspace(*buf))
-		buf++, len--;
-	if (len >= 5 && !memcmp("refs/", buf, 5))
-		return 0;
-	return -1;
-}
-
 const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
 {
 	int depth = MAXDEPTH, len;

---
0.99.9.GIT

#6 lore raw
Quoted 4 lines · Junio C Hamano

I think it might make sense to inserting something like the attached untested patch in your series, between library and upload-pack.

I'll run the clone/fetch/push test-suite again tomorrow, with this applied. It looks good though.

Quoted 4 lines

The validation done by path_ok() in git-daemon
probabaly needs to lose alternate checks and validate only the path returned by enter_repo(). This would make writing whitelist by git-daemon administrator a bit more cumbersome,

Not necessarily. The repositories in the whitelist should be validated (and possibly converted) using the path_ok() function. This will also make it possible to catch typos and permission errors that are just plain annoying for the admins.

In non-strict mode this isn't really a problem so long as all whitelist-paths are absolute and doesn't contain any symlinks, although we could use the chdir() + getcwd() thingie since we don't need the ability to go back to where we started and that's what will be used later when serving the repos.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#7 lore raw
Quoted 2 lines · Andreas Ericsson

I'll run the clone/fetch/push test-suite again tomorrow, with this applied. It looks good though.

Sorry, but there was a thinko in my butchered version of enter_repo(). While allowing only absolute path was good for the version with your daemon.c change, it was not with the current one that runs upload-pack with "." as repo. In either case we _do_ chdir() to it after validating the path, so I am wondering if it is a good idea to keep sending "." as repo when executing upload-pack with this patch as well. This does not make any practical difference, but I think it makes the intent clearer -- "we are already there so do not try going anywhere else".

So I am thinking about applying something like this patch on top of the last part of your patch.

- Do validation only on canonicalized paths;
- Run upload-pack with "." as repo, not full path;
- allow trailing slash under --strict-paths i.e. "git://host/my/repo.git/"

What do you think?

---

daemon.c +9 -12
index ac4c94b..21d8260 100644
@@ -93,22 +93,19 @@ static char *path_ok(char *dir)
 
 	if ( ok_paths && *ok_paths ) {
 		char **pp = NULL;
-		int dirlen = strlen(dir);
 		int pathlen = strlen(path);
 
+		/* The validation is done on the paths after enter_repo
+		 * canonicalization, so whitelist should be written in
+		 * terms of real pathnames (i.e. after ~user is expanded
+		 * and symlinks resolved).
+		 */
 		for ( pp = ok_paths ; *pp ; pp++ ) {
 			int len = strlen(*pp);
-			/* because of symlinks we must match both what the
-			 * user passed and the canonicalized path, otherwise
-			 * the user can send a string matching either a whitelist
-			 * entry or an actual directory exactly and still not
-			 * get through */
 			if (len <= pathlen && !memcmp(*pp, path, len)) {
-				if (path[len] == '\0' || (!strict_paths && path[len] == '/'))
-					return path;
-			}
-			if (len <= dirlen && !memcmp(*pp, dir, len)) {
-				if (dir[len] == '\0' || (!strict_paths && dir[len] == '/'))
+				if (path[len] == '\0' ||
+				    (path[len] == '/' &&
+				     (!strict_paths || path[len+1] == 0)))
 					return path;
 			}
 		}
@@ -160,7 +157,7 @@ static int upload(char *dir)
 	snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
 
 	/* git-upload-pack only ever reads stuff, so this is safe */
-	execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, path, NULL);
+	execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL);
 	return -1;
 }
 
#8 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Andreas Ericsson

I'll run the clone/fetch/push test-suite again tomorrow, with this applied. It looks good though.

Quoted 7 lines

Sorry, but there was a thinko in my butchered version of enter_repo(). While allowing only absolute path was good for the version with your daemon.c change, it was not with the current one that runs upload-pack with "." as repo. In either case we _do_ chdir() to it after validating the path, so I am wondering if it is a good idea to keep sending "." as repo when executing upload-pack with this patch as well.

It might be, and it's good since it prevents the otherwise possible race that occurs when git-upload-pack chdir()'s again.

Quoted 5 lines

This does not
make any practical difference, but I think it makes the intent clearer -- "we are already there so do not try going anywhere else".

So enter_repo allows "." (exactly and without chdir()) and all paths starting with '/' if strict?

Quoted 9 lines

So I am thinking about applying something like this patch on top of the last part of your patch.

- Do validation only on canonicalized paths;
- Run upload-pack with "." as repo, not full path;
- allow trailing slash under --strict-paths i.e. "git://host/my/repo.git/"

What do you think?

Apart from comments and indentation it's more or less exactly what I have in my revised git-daemon patch (although without what you mentioned in your own reply to this mail).

Do you want the revised one from me or will you apply the original with this on top?

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#9 lore raw
Quoted 6 lines · Andreas Ericsson

Apart from comments and indentation it's more or less exactly what I have in my revised git-daemon patch (although without what you mentioned in your own reply to this mail).

Do you want the revised one from me or will you apply the original with this on top?

Well, let's push out what we have so far to "master" and then finish up whatever breakage if any in tree.

#10 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano

...So I am thinking about applying something like this patch on top of the last part of your patch.

- Do validation only on canonicalized paths;
- Run upload-pack with "." as repo, not full path;
- allow trailing slash under --strict-paths i.e. "git://host/my/repo.git/"

Sorry, the last piece was totally unneeded; we are operating on return value from getcwd at this point.

#11 lore raw
In reply to Andreas Ericsson
Quoted 1 line · Andreas Ericsson

DWIM? That's an acronym I don't know.

"Do What I Mean".

It's actually meant to be DMNS ("Do what i Mean, Not what i Say"), but some early AI hackers at MIT shortened it maliciously so that they could claim at least *some* success. :-)

Signature · 5 lines
Matthias Urlichs   |   {M:U} IT Design @ m-u-it.de   |  [email protected]
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
 - -
do you want my self-identities alphabetically, chronologically, or in
random order?  -- Misha