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:44 UTC
b7080d851630606815a399a162c5b20f2907f9e0
2 files changed
+59
-7
daemon.c
+19
-2
@@ -12,7 +12,9 @@
12
static int log_syslog;
13
static int verbose;
14
15
-static const char daemon_usage[] = "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all] [directory...]";
15
+static const char daemon_usage[] =
16
+"git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n"
17
+" [--timeout=n] [--init-timeout=n] [directory...]";
18
19
/* List of acceptable pathname prefixes */
20
static char **ok_paths = NULL;
@@ -20,6 +22,9 @@ static char **ok_paths = NULL;
22
/* If this is set, git-daemon-export-ok is not required */
23
static int export_all_trees = 0;
24
25
+/* Timeout, and initial timeout */
26
+static unsigned int timeout = 0;
27
+static unsigned int init_timeout = 0;
28
29
static void logreport(int priority, const char *err, va_list params)
30
{
@@ -169,6 +174,8 @@ static int upload(char *dir)
174
/* Enough for the longest path above including final null */
175
int buflen = strlen(dir)+10;
176
char *dirbuf = xmalloc(buflen);
177
+ /* Timeout as string */
178
+ char timeout_buf[64];
179
180
loginfo("Request for '%s'", dir);
181
@@ -189,8 +196,10 @@ static int upload(char *dir)
196
*/
197
signal(SIGTERM, SIG_IGN);
198
199
+ snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
200
+
201
/* git-upload-pack only ever reads stuff, so this is safe */
193
- execlp("git-upload-pack", "git-upload-pack", ".", NULL);
202
+ execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL);
203
return -1;
204
}
205
@@ -199,7 +208,9 @@ static int execute(void)
208
static char line[1000];
209
int len;
210
211
+ alarm(init_timeout ? init_timeout : timeout);
212
len = packet_read_line(0, line, sizeof(line));
213
+ alarm(0);
214
215
if (len && line[len-1] == '\n')
216
line[--len] = 0;
@@ -548,6 +559,12 @@ int main(int argc, char **argv)
559
export_all_trees = 1;
560
continue;
561
}
562
+ if (!strncmp(arg, "--timeout=", 10)) {
563
+ timeout = atoi(arg+10);
564
+ }
565
+ if (!strncmp(arg, "--init-timeout=", 10)) {
566
+ init_timeout = atoi(arg+15);
567
+ }
568
if (!strcmp(arg, "--")) {
569
ok_paths = &argv[i+1];
570
break;
upload-pack.c
+40
-5
@@ -2,13 +2,19 @@
2
#include "refs.h"
3
#include "pkt-line.h"
4
5
-static const char upload_pack_usage[] = "git-upload-pack <dir>";
5
+static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
6
7
#define MAX_HAS (16)
8
#define MAX_NEEDS (256)
9
static int nr_has = 0, nr_needs = 0;
10
static unsigned char has_sha1[MAX_HAS][20];
11
static unsigned char needs_sha1[MAX_NEEDS][20];
12
+static unsigned int timeout = 0;
13
+
14
+static void reset_timeout(void)
15
+{
16
+ alarm(timeout);
17
+}
18
19
static int strip(char *line, int len)
20
{
@@ -98,6 +104,7 @@ static int get_common_commits(void)
104
105
for(;;) {
106
len = packet_read_line(0, line, sizeof(line));
107
+ reset_timeout();
108
109
if (!len) {
110
packet_write(1, "NAK\n");
@@ -120,6 +127,7 @@ static int get_common_commits(void)
127
128
for (;;) {
129
len = packet_read_line(0, line, sizeof(line));
130
+ reset_timeout();
131
if (!len)
132
continue;
133
len = strip(line, len);
@@ -143,6 +151,7 @@ static int receive_needs(void)
151
for (;;) {
152
unsigned char dummy[20], *sha1_buf;
153
len = packet_read_line(0, line, sizeof(line));
154
+ reset_timeout();
155
if (!len)
156
return needs;
157
@@ -171,6 +180,7 @@ static int send_ref(const char *refname, const unsigned char *sha1)
180
181
static int upload_pack(void)
182
{
183
+ reset_timeout();
184
head_ref(send_ref);
185
for_each_ref(send_ref);
186
packet_flush(1);
@@ -185,18 +195,43 @@ static int upload_pack(void)
195
int main(int argc, char **argv)
196
{
197
const char *dir;
188
- if (argc != 2)
198
+ int i;
199
+ int strict = 0;
200
+
201
+ for (i = 1; i < argc; i++) {
202
+ char *arg = argv[i];
203
+
204
+ if (arg[0] != '-')
205
+ break;
206
+ if (!strcmp(arg, "--strict")) {
207
+ strict = 1;
208
+ continue;
209
+ }
210
+ if (!strncmp(arg, "--timeout=", 10)) {
211
+ timeout = atoi(arg+10);
212
+ continue;
213
+ }
214
+ if (!strcmp(arg, "--")) {
215
+ i++;
216
+ break;
217
+ }
218
+ }
219
+
220
+ if (i != argc-1)
221
usage(upload_pack_usage);
190
- dir = argv[1];
222
+ dir = argv[i];
223
224
/* chdir to the directory. If that fails, try appending ".git" */
225
if (chdir(dir) < 0) {
194
- if (chdir(mkpath("%s.git", dir)) < 0)
226
+ if (strict || chdir(mkpath("%s.git", dir)) < 0)
227
die("git-upload-pack unable to chdir to %s", dir);
228
}
197
- chdir(".git");
229
+ if (!strict)
230
+ chdir(".git");
231
+
232
if (access("objects", X_OK) || access("refs", X_OK))
233
die("git-upload-pack: %s doesn't seem to be a git archive", dir);
234
+
235
putenv("GIT_DIR=.");
236
upload_pack();
237
return 0;