Do not DWIM in userpath library under strict mode.
This should force git-daemon administrator's job a bit harder because the exact paths need to be given in the whitelist, but at the same time makes the auditing easier. This moves validate_symref() from refs.c to path.c, because we need to link path.c with git-daemon for its "enter_repo()", but we do not want to link the daemon with the rest of git libraries and its requirements. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 18, 2005 at 14:59 UTC
0870ca7fabe6f25095431280e480859f7c66e8ab
2 files changed
+60
-57
path.c
+60
-17
@@ -91,20 +91,55 @@ char *safe_strncpy(char *dest, const char *src, size_t n)
91
return dest;
92
}
93
94
+int validate_symref(const char *path)
95
+{
96
+ struct stat st;
97
+ char *buf, buffer[256];
98
+ int len, fd;
99
+
100
+ if (lstat(path, &st) < 0)
101
+ return -1;
102
+
103
+ /* Make sure it is a "refs/.." symlink */
104
+ if (S_ISLNK(st.st_mode)) {
105
+ len = readlink(path, buffer, sizeof(buffer)-1);
106
+ if (len >= 5 && !memcmp("refs/", buffer, 5))
107
+ return 0;
108
+ return -1;
109
+ }
110
+
111
+ /*
112
+ * Anything else, just open it and try to see if it is a symbolic ref.
113
+ */
114
+ fd = open(path, O_RDONLY);
115
+ if (fd < 0)
116
+ return -1;
117
+ len = read(fd, buffer, sizeof(buffer)-1);
118
+ close(fd);
119
+
120
+ /*
121
+ * Is it a symbolic ref?
122
+ */
123
+ if (len < 4 || memcmp("ref:", buffer, 4))
124
+ return -1;
125
+ buf = buffer + 4;
126
+ len -= 4;
127
+ while (len && isspace(*buf))
128
+ buf++, len--;
129
+ if (len >= 5 && !memcmp("refs/", buf, 5))
130
+ return 0;
131
+ return -1;
132
+}
133
+
134
static char *current_dir()
135
{
136
return getcwd(pathname, sizeof(pathname));
137
}
138
99
-/* Take a raw path from is_git_repo() and canonicalize it using Linus'
100
- * idea of a blind chdir() and getcwd(). */
101
-static const char *canonical_path(char *path, int strict)
139
+static int user_chdir(char *path)
140
{
141
char *dir = path;
142
105
- if(strict && *dir != '/')
106
- return NULL;
107
-
143
if(*dir == '~') { /* user-relative path */
144
struct passwd *pw;
145
char *slash = strchr(dir, '/');
@@ -125,19 +160,19 @@ static const char *canonical_path(char *path, int strict)
160
161
/* make sure we got something back that we can chdir() to */
162
if(!pw || chdir(pw->pw_dir) < 0)
128
- return NULL;
163
+ return -1;
164
165
if(!slash || !slash[1]) /* no path following username */
131
- return current_dir();
166
+ return 0;
167
168
dir = slash + 1;
169
}
170
171
/* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
172
if(chdir(dir) < 0)
138
- return NULL;
173
+ return -1;
174
140
- return current_dir();
175
+ return 0;
176
}
177
178
char *enter_repo(char *path, int strict)
@@ -145,16 +180,24 @@ char *enter_repo(char *path, int strict)
180
if(!path)
181
return NULL;
182
148
- if(!canonical_path(path, strict)) {
149
- if(strict || !canonical_path(mkpath("%s.git", path), strict))
183
+ if (strict) {
184
+ if((path[0] != '/') || chdir(path) < 0)
185
return NULL;
186
}
187
+ else {
188
+ if (!*path)
189
+ ; /* happy -- no chdir */
190
+ else if (!user_chdir(path))
191
+ ; /* happy -- as given */
192
+ else if (!user_chdir(mkpath("%s.git", path)))
193
+ ; /* happy -- uemacs --> uemacs.git */
194
+ else
195
+ return NULL;
196
+ (void)chdir(".git");
197
+ }
198
153
- /* This is perfectly safe, and people tend to think of the directory
154
- * where they ran git-init-db as their repository, so humour them. */
155
- (void)chdir(".git");
156
-
157
- if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0) {
199
+ if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
200
+ validate_symref("HEAD") == 0) {
201
putenv("GIT_DIR=.");
202
return current_dir();
203
}
refs.c
-40
@@ -10,46 +10,6 @@
10
#define USE_SYMLINK_HEAD 1
11
#endif
12
13
-int validate_symref(const char *path)
14
-{
15
- struct stat st;
16
- char *buf, buffer[256];
17
- int len, fd;
18
-
19
- if (lstat(path, &st) < 0)
20
- return -1;
21
-
22
- /* Make sure it is a "refs/.." symlink */
23
- if (S_ISLNK(st.st_mode)) {
24
- len = readlink(path, buffer, sizeof(buffer)-1);
25
- if (len >= 5 && !memcmp("refs/", buffer, 5))
26
- return 0;
27
- return -1;
28
- }
29
-
30
- /*
31
- * Anything else, just open it and try to see if it is a symbolic ref.
32
- */
33
- fd = open(path, O_RDONLY);
34
- if (fd < 0)
35
- return -1;
36
- len = read(fd, buffer, sizeof(buffer)-1);
37
- close(fd);
38
-
39
- /*
40
- * Is it a symbolic ref?
41
- */
42
- if (len < 4 || memcmp("ref:", buffer, 4))
43
- return -1;
44
- buf = buffer + 4;
45
- len -= 4;
46
- while (len && isspace(*buf))
47
- buf++, len--;
48
- if (len >= 5 && !memcmp("refs/", buf, 5))
49
- return 0;
50
- return -1;
51
-}
52
-
13
const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
14
{
15
int depth = MAXDEPTH, len;