@matheusbsbs / matheus-bagliano / commits / 960deccb26
Fork 0

git-daemon: timeout, eliminate double DWIM

It turns out that not only did git-daemon do DWIM, but git-upload-pack does as well. This is bad; security checks have to be performed *after* canonicalization, not before. Additionally, the current git-daemon can be trivially DoSed by spewing SYNs at the target port. This patch adds a --strict option to git-upload-pack to disable all DWIM, a --timeout option to git-daemon and git-upload-pack, and an --init-timeout option to git-daemon (which is typically set to a much lower value, since the initial request should come immediately from the client.) Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

H. Peter Anvin committed Oct 19, 2005 at 14:27 UTC 960deccb26a5bee6c6cd63d50e8272f540a27b19
2 files changed +59 -7
daemon.c
+19 -2
@@ -13,7 +13,9 @@
13 static int log_syslog;
14 static int verbose;
15
16 -static const char daemon_usage[] = "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all] [directory...]";
16 +static const char daemon_usage[] =
17 +"git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
18 +" [--timeout=n] [--init-timeout=n] [directory...]";
19
20 /* List of acceptable pathname prefixes */
21 static char **ok_paths = NULL;
@@ -21,6 +23,9 @@ static char **ok_paths = NULL;
23 /* If this is set, git-daemon-export-ok is not required */
24 static int export_all_trees = 0;
25
26 +/* Timeout, and initial timeout */
27 +static unsigned int timeout = 0;
28 +static unsigned int init_timeout = 0;
29
30 static void logreport(int priority, const char *err, va_list params)
31 {
@@ -170,6 +175,8 @@ static int upload(char *dir)
175 /* Enough for the longest path above including final null */
176 int buflen = strlen(dir)+10;
177 char *dirbuf = xmalloc(buflen);
178 + /* Timeout as string */
179 + char timeout_buf[64];
180
181 loginfo("Request for '%s'", dir);
182
@@ -190,8 +197,10 @@ static int upload(char *dir)
197 */
198 signal(SIGTERM, SIG_IGN);
199
200 + snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
201 +
202 /* git-upload-pack only ever reads stuff, so this is safe */
194 - execlp("git-upload-pack", "git-upload-pack", ".", NULL);
203 + execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL);
204 return -1;
205 }
206
@@ -200,7 +209,9 @@ static int execute(void)
209 static char line[1000];
210 int len;
211
212 + alarm(init_timeout ? init_timeout : timeout);
213 len = packet_read_line(0, line, sizeof(line));
214 + alarm(0);
215
216 if (len && line[len-1] == '\n')
217 line[--len] = 0;
@@ -598,6 +609,12 @@ int main(int argc, char **argv)
609 export_all_trees = 1;
610 continue;
611 }
612 + if (!strncmp(arg, "--timeout=", 10)) {
613 + timeout = atoi(arg+10);
614 + }
615 + if (!strncmp(arg, "--init-timeout=", 10)) {
616 + init_timeout = atoi(arg+15);
617 + }
618 if (!strcmp(arg, "--")) {
619 ok_paths = &argv[i+1];
620 break;
upload-pack.c
+40 -5
@@ -4,13 +4,19 @@
4 #include "tag.h"
5 #include "object.h"
6
7 -static const char upload_pack_usage[] = "git-upload-pack <dir>";
7 +static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
8
9 #define MAX_HAS (16)
10 #define MAX_NEEDS (256)
11 static int nr_has = 0, nr_needs = 0;
12 static unsigned char has_sha1[MAX_HAS][20];
13 static unsigned char needs_sha1[MAX_NEEDS][20];
14 +static unsigned int timeout = 0;
15 +
16 +static void reset_timeout(void)
17 +{
18 + alarm(timeout);
19 +}
20
21 static int strip(char *line, int len)
22 {
@@ -100,6 +106,7 @@ static int get_common_commits(void)
106
107 for(;;) {
108 len = packet_read_line(0, line, sizeof(line));
109 + reset_timeout();
110
111 if (!len) {
112 packet_write(1, "NAK\n");
@@ -122,6 +129,7 @@ static int get_common_commits(void)
129
130 for (;;) {
131 len = packet_read_line(0, line, sizeof(line));
132 + reset_timeout();
133 if (!len)
134 continue;
135 len = strip(line, len);
@@ -145,6 +153,7 @@ static int receive_needs(void)
153 for (;;) {
154 unsigned char dummy[20], *sha1_buf;
155 len = packet_read_line(0, line, sizeof(line));
156 + reset_timeout();
157 if (!len)
158 return needs;
159
@@ -179,6 +188,7 @@ static int send_ref(const char *refname, const unsigned char *sha1)
188
189 static int upload_pack(void)
190 {
191 + reset_timeout();
192 head_ref(send_ref);
193 for_each_ref(send_ref);
194 packet_flush(1);
@@ -193,18 +203,43 @@ static int upload_pack(void)
203 int main(int argc, char **argv)
204 {
205 const char *dir;
196 - if (argc != 2)
206 + int i;
207 + int strict = 0;
208 +
209 + for (i = 1; i < argc; i++) {
210 + char *arg = argv[i];
211 +
212 + if (arg[0] != '-')
213 + break;
214 + if (!strcmp(arg, "--strict")) {
215 + strict = 1;
216 + continue;
217 + }
218 + if (!strncmp(arg, "--timeout=", 10)) {
219 + timeout = atoi(arg+10);
220 + continue;
221 + }
222 + if (!strcmp(arg, "--")) {
223 + i++;
224 + break;
225 + }
226 + }
227 +
228 + if (i != argc-1)
229 usage(upload_pack_usage);
198 - dir = argv[1];
230 + dir = argv[i];
231
232 /* chdir to the directory. If that fails, try appending ".git" */
233 if (chdir(dir) < 0) {
202 - if (chdir(mkpath("%s.git", dir)) < 0)
234 + if (strict || chdir(mkpath("%s.git", dir)) < 0)
235 die("git-upload-pack unable to chdir to %s", dir);
236 }
205 - chdir(".git");
237 + if (!strict)
238 + chdir(".git");
239 +
240 if (access("objects", X_OK) || access("refs", X_OK))
241 die("git-upload-pack: %s doesn't seem to be a git archive", dir);
242 +
243 putenv("GIT_DIR=.");
244 upload_pack();
245 return 0;