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