@matheusbsbs / matheus-bagliano / commits / 3e04c62daa
Fork 0

revised^2: git-daemon extra paranoia, and path DWIM

This patch adds some extra paranoia to the git-daemon filename test. In particular, it now rejects pathnames containing //; it also adds a redundant test for pathname absoluteness (belts and suspenders.) A single / at the end of the path is still permitted, however, and the .git and /.git append DWIM stuff is now handled in an integrated manner, which means the resulting path will always be subjected to pathname checks. Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

H. Peter Anvin committed Oct 18, 2005 at 18:26 UTC 3e04c62daab0a8481f907c30414ed246f284a1d9
1 file changed +57 -21
daemon.c
+57 -21
@@ -80,17 +80,30 @@ static int path_ok(const char *dir)
80 {
81 const char *p = dir;
82 char **pp;
83 - int sl = 1, ndot = 0;
83 + int sl, ndot;
84 +
85 + /* The pathname here should be an absolute path. */
86 + if ( *p++ != '/' )
87 + return 0;
88 +
89 + sl = 1; ndot = 0;
90
91 for (;;) {
92 if ( *p == '.' ) {
93 ndot++;
88 - } else if ( *p == '/' || *p == '\0' ) {
94 + } else if ( *p == '\0' ) {
95 + /* Reject "." and ".." at the end of the path */
96 if ( sl && ndot > 0 && ndot < 3 )
90 - return 0; /* . or .. in path */
97 + return 0;
98 +
99 + /* Otherwise OK */
100 + break;
101 + } else if ( *p == '/' ) {
102 + /* Refuse "", "." or ".." */
103 + if ( sl && ndot < 3 )
104 + return 0;
105 sl = 1;
92 - if ( *p == '\0' )
93 - break; /* End of string and all is good */
106 + ndot = 0;
107 } else {
108 sl = ndot = 0;
109 }
@@ -99,7 +112,7 @@ static int path_ok(const char *dir)
112
113 if ( ok_paths && *ok_paths ) {
114 int ok = 0;
102 - int dirlen = strlen(dir); /* read_packet_line can return embedded \0 */
115 + int dirlen = strlen(dir);
116
117 for ( pp = ok_paths ; *pp ; pp++ ) {
118 int len = strlen(*pp);
@@ -118,22 +131,16 @@ static int path_ok(const char *dir)
131 return 1; /* Path acceptable */
132 }
133
121 -static int upload(char *dir, int dirlen)
134 +static int set_dir(const char *dir)
135 {
123 - loginfo("Request for '%s'", dir);
124 -
136 if (!path_ok(dir)) {
126 - logerror("Forbidden directory: %s\n", dir);
137 + errno = EACCES;
138 return -1;
139 }
140
130 - if (chdir(dir) < 0) {
131 - logerror("Cannot chdir('%s'): %s", dir, strerror(errno));
141 + if ( chdir(dir) )
142 return -1;
133 - }
134 -
135 - chdir(".git");
136 -
143 +
144 /*
145 * Security on the cheap.
146 *
@@ -141,10 +148,39 @@ static int upload(char *dir, int dirlen)
148 * a "git-daemon-export-ok" flag that says that the other side
149 * is ok with us doing this.
150 */
144 - if ((!export_all_trees && access("git-daemon-export-ok", F_OK)) ||
145 - access("objects/", X_OK) ||
146 - access("HEAD", R_OK)) {
147 - logerror("Not a valid git-daemon-enabled repository: '%s'", dir);
151 + if (!export_all_trees && access("git-daemon-export-ok", F_OK)) {
152 + errno = EACCES;
153 + return -1;
154 + }
155 +
156 + if (access("objects/", X_OK) || access("HEAD", R_OK)) {
157 + errno = EINVAL;
158 + return -1;
159 + }
160 +
161 + /* If all this passed, we're OK */
162 + return 0;
163 +}
164 +
165 +static int upload(char *dir)
166 +{
167 + /* Try paths in this order */
168 + static const char *paths[] = { "%s", "%s/.git", "%s.git", "%s.git/.git", NULL };
169 + const char **pp;
170 + /* Enough for the longest path above including final null */
171 + int buflen = strlen(dir)+10;
172 + char *dirbuf = xmalloc(buflen);
173 +
174 + loginfo("Request for '%s'", dir);
175 +
176 + for ( pp = paths ; *pp ; pp++ ) {
177 + snprintf(dirbuf, buflen, *pp, dir);
178 + if ( !set_dir(dirbuf) )
179 + break;
180 + }
181 +
182 + if ( !*pp ) {
183 + logerror("Cannot set directory '%s': %s", dir, strerror(errno));
184 return -1;
185 }
186
@@ -170,7 +206,7 @@ static int execute(void)
206 line[--len] = 0;
207
208 if (!strncmp("git-upload-pack /", line, 17))
173 - return upload(line + 16, len - 16);
209 + return upload(line+16);
210
211 logerror("Protocol error: '%s'", line);
212 return -1;