Support a modicum of path validation, and allow an export all trees option.
H. Peter Anvin committed
Sep 26, 2005 at 19:10 UTC
4ae95682694a1cd05ee2029fe241ad90d43c8c0e
1 file changed
+67
-5
daemon.c
+67
-5
@@ -12,7 +12,13 @@
12
static int log_syslog;
13
static int verbose;
14
15
-static const char daemon_usage[] = "git-daemon [--verbose] [--syslog] [--inetd | --port=n]";
15
+static const char daemon_usage[] = "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all] [directory...]";
16
+
17
+/* List of acceptable pathname prefixes */
18
+static char **ok_paths = NULL;
19
+
20
+/* If this is set, git-daemon-export-ok is not required */
21
+static int export_all_trees = 0;
22
23
24
static void logreport(int priority, const char *err, va_list params)
@@ -69,15 +75,61 @@ void loginfo(const char *err, ...)
75
va_end(params);
76
}
77
78
+static int path_ok(const char *dir)
79
+{
80
+ const char *p = dir;
81
+ char **pp;
82
+ int sl = 1, ndot = 0;
83
+
84
+ for (;;) {
85
+ if ( *p == '.' ) {
86
+ ndot++;
87
+ } else if ( *p == '/' || *p == '\0' ) {
88
+ if ( sl && ndot > 0 && ndot < 3 )
89
+ return 0; /* . or .. in path */
90
+ sl = 1;
91
+ if ( *p == '\0' )
92
+ break; /* End of string and all is good */
93
+ } else {
94
+ sl = ndot = 0;
95
+ }
96
+ p++;
97
+ }
98
+
99
+ if ( ok_paths && *ok_paths ) {
100
+ int ok = 0;
101
+ int dirlen = strlen(dir); /* read_packet_line can return embedded \0 */
102
+
103
+ for ( pp = ok_paths ; *pp ; pp++ ) {
104
+ int len = strlen(*pp);
105
+ if ( len <= dirlen &&
106
+ !strncmp(*pp, dir, len) &&
107
+ (dir[len] == '/' || dir[len] == '\0') ) {
108
+ ok = 1;
109
+ break;
110
+ }
111
+ }
112
+
113
+ if ( !ok )
114
+ return 0; /* Path not in whitelist */
115
+ }
116
+
117
+ return 1; /* Path acceptable */
118
+}
119
120
static int upload(char *dir, int dirlen)
121
{
122
loginfo("Request for '%s'", dir);
123
+
124
+ if (!path_ok(dir)) {
125
+ logerror("Forbidden directory: %s\n", dir);
126
+ return -1;
127
+ }
128
+
129
if (chdir(dir) < 0) {
130
logerror("Cannot chdir('%s'): %s", dir, strerror(errno));
131
return -1;
132
}
80
- chdir(".git");
133
134
/*
135
* Security on the cheap.
@@ -86,10 +138,10 @@ static int upload(char *dir, int dirlen)
138
* a "git-daemon-export-ok" flag that says that the other side
139
* is ok with us doing this.
140
*/
89
- if (access("git-daemon-export-ok", F_OK) ||
141
+ if ((!export_all_trees && access("git-daemon-export-ok", F_OK)) ||
142
access("objects/00", X_OK) ||
143
access("HEAD", R_OK)) {
92
- logerror("Not a valid gitd-enabled repository: '%s'", dir);
144
+ logerror("Not a valid git-daemon-enabled repository: '%s'", dir);
145
return -1;
146
}
147
@@ -441,7 +493,6 @@ int main(int argc, char **argv)
493
continue;
494
}
495
}
444
-
496
if (!strcmp(arg, "--inetd")) {
497
inetd_mode = 1;
498
continue;
@@ -455,6 +506,17 @@ int main(int argc, char **argv)
506
openlog("git-daemon", 0, LOG_DAEMON);
507
continue;
508
}
509
+ if (!strcmp(arg, "--export-all")) {
510
+ export_all_trees = 1;
511
+ continue;
512
+ }
513
+ if (!strcmp(arg, "--")) {
514
+ ok_paths = &argv[i+1];
515
+ break;
516
+ } else if (arg[0] != '-') {
517
+ ok_paths = &argv[i];
518
+ break;
519
+ }
520
521
usage(daemon_usage);
522
}