index: Check for valid paths before creating an index entry
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
diff --git a/src/index.c b/src/index.c
index d3bc081..644f0c8 100644
--- a/src/index.c
+++ b/src/index.c
@@ -762,19 +762,96 @@ void git_index_entry__init_from_stat(
entry->file_size = st->st_size;
}
-static git_index_entry *index_entry_alloc(const char *path)
+/*
+ * We fundamentally don't like some paths: we don't want
+ * dot or dot-dot anywhere, and for obvious reasons don't
+ * want to recurse into ".git" either.
+ *
+ * Also, we don't want double slashes or slashes at the
+ * end that can make pathnames ambiguous.
+ */
+static int verify_dotfile(const char *rest)
+{
+ /*
+ * The first character was '.', but that
+ * has already been discarded, we now test
+ * the rest.
+ */
+
+ /* "." is not allowed */
+ if (*rest == '\0' || *rest == '/')
+ return -1;
+
+ switch (*rest) {
+ /*
+ * ".git" followed by NUL or slash is bad. This
+ * shares the path end test with the ".." case.
+ */
+ case 'g':
+ case 'G':
+ if (rest[1] != 'i' && rest[1] != 'I')
+ break;
+ if (rest[2] != 't' && rest[2] != 'T')
+ break;
+ rest += 2;
+ /* fallthrough */
+ case '.':
+ if (rest[1] == '\0' || rest[1] == '/')
+ return -1;
+ }
+ return 0;
+}
+
+static int verify_component(char c, const char *rest)
+{
+ if ((c == '.' && verify_dotfile(rest)) < 0 || c == '/' || c == '\0') {
+ giterr_set(GITERR_INDEX, "Invalid path component in index: '%c%s'", c, rest);
+ return -1;
+ }
+ return 0;
+}
+
+static int verify_path(const char *path)
+{
+ char c;
+
+ /* TODO: should we check this? */
+ /*
+ if (has_dos_drive_prefix(path))
+ return -1;
+ */
+
+ c = *path++;
+ if (verify_component(c, path) < 0)
+ return -1;
+
+ while ((c = *path++) != '\0') {
+ if (c == '/') {
+ c = *path++;
+ if (verify_component(c, path) < 0)
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static int index_entry_create(git_index_entry **out, const char *path)
{
size_t pathlen = strlen(path);
- struct entry_internal *entry =
- git__calloc(sizeof(struct entry_internal) + pathlen + 1, 1);
- if (!entry)
- return NULL;
+ struct entry_internal *entry;
+
+ if (verify_path(path) < 0)
+ return -1;
+
+ entry = git__calloc(sizeof(struct entry_internal) + pathlen + 1, 1);
+ GITERR_CHECK_ALLOC(entry);
entry->pathlen = pathlen;
memcpy(entry->path, path, pathlen);
entry->entry.path = entry->path;
- return (git_index_entry *)entry;
+ *out = (git_index_entry *)entry;
+ return 0;
}
static int index_entry_init(
@@ -790,14 +867,17 @@ static int index_entry_init(
"Could not initialize index entry. "
"Index is not backed up by an existing repository.");
+ if (index_entry_create(&entry, rel_path) < 0)
+ return -1;
+
/* write the blob to disk and get the oid and stat info */
error = git_blob__create_from_paths(
&oid, &st, INDEX_OWNER(index), NULL, rel_path, 0, true);
- if (error < 0)
- return error;
- entry = index_entry_alloc(rel_path);
- GITERR_CHECK_ALLOC(entry);
+ if (error < 0) {
+ index_entry_free(entry);
+ return error;
+ }
entry->id = oid;
git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
@@ -862,11 +942,11 @@ static int index_entry_dup(git_index_entry **out, const git_index_entry *src)
return 0;
}
- *out = entry = index_entry_alloc(src->path);
- GITERR_CHECK_ALLOC(entry);
+ if (index_entry_create(&entry, src->path) < 0)
+ return -1;
index_entry_cpy(entry, src);
-
+ *out = entry;
return 0;
}
@@ -2316,8 +2396,8 @@ static int read_tree_cb(
if (git_buf_joinpath(&path, root, tentry->filename) < 0)
return -1;
- entry = index_entry_alloc(path.ptr);
- GITERR_CHECK_ALLOC(entry);
+ if (index_entry_create(&entry, path.ptr) < 0)
+ return -1;
entry->mode = tentry->attr;
entry->id = tentry->oid;
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 7d544e8..0464e73 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -309,31 +309,116 @@ void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void)
git_repository_free(bare_repo);
}
+static void add_invalid_filename(git_repository *repo, const char *fn)
+{
+ git_index *index;
+ git_buf path = GIT_BUF_INIT;
+
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_assert(git_index_entrycount(index) == 0);
+
+ git_buf_joinpath(&path, "./invalid", fn);
+
+ cl_git_mkfile(path.ptr, NULL);
+ cl_git_fail(git_index_add_bypath(index, fn));
+ cl_must_pass(p_unlink(path.ptr));
+
+ cl_assert(git_index_entrycount(index) == 0);
+
+ git_index_free(index);
+}
+
/* Test that writing an invalid filename fails */
-void test_index_tests__write_invalid_filename(void)
+void test_index_tests__add_invalid_filename(void)
{
git_repository *repo;
+
+ p_mkdir("invalid", 0700);
+
+ cl_git_pass(git_repository_init(&repo, "./invalid", 0));
+ cl_must_pass(p_mkdir("./invalid/subdir", 0777));
+
+ add_invalid_filename(repo, ".git/hello");
+ add_invalid_filename(repo, ".GIT/hello");
+ add_invalid_filename(repo, ".GiT/hello");
+ add_invalid_filename(repo, "./.git/hello");
+ add_invalid_filename(repo, "./foo");
+ add_invalid_filename(repo, "./bar");
+ add_invalid_filename(repo, "subdir/../bar");
+
+ git_repository_free(repo);
+
+ cl_fixture_cleanup("invalid");
+}
+
+static void replace_char(char *str, char in, char out)
+{
+ char *c = str;
+
+ while (*c++)
+ if (*c == in)
+ *c = out;
+}
+
+static void write_invalid_filename(git_repository *repo, const char *fn_orig)
+{
git_index *index;
git_oid expected;
+ const git_index_entry *entry;
+ git_buf path = GIT_BUF_INIT;
+ char *fn;
- p_mkdir("read_tree", 0700);
-
- cl_git_pass(git_repository_init(&repo, "./read_tree", 0));
cl_git_pass(git_repository_index(&index, repo));
-
cl_assert(git_index_entrycount(index) == 0);
- cl_git_mkfile("./read_tree/.git/hello", NULL);
+ /*
+ * Sneak a valid path into the index, we'll update it
+ * to an invalid path when we try to write the index.
+ */
+ fn = git__strdup(fn_orig);
+ replace_char(fn, '/', '_');
+
+ git_buf_joinpath(&path, "./invalid", fn);
+
+ cl_git_mkfile(path.ptr, NULL);
+
+ cl_git_pass(git_index_add_bypath(index, fn));
+
+ cl_assert(entry = git_index_get_bypath(index, fn, 0));
- cl_git_pass(git_index_add_bypath(index, ".git/hello"));
+ /* kids, don't try this at home */
+ replace_char((char *)entry->path, '_', '/');
/* write-tree */
cl_git_fail(git_index_write_tree(&expected, index));
+ p_unlink(path.ptr);
+
+ cl_git_pass(git_index_remove_all(index, NULL, NULL, NULL));
git_index_free(index);
+ git__free(fn);
+}
+
+/* Test that writing an invalid filename fails */
+void test_index_tests__write_invalid_filename(void)
+{
+ git_repository *repo;
+
+ p_mkdir("invalid", 0700);
+
+ cl_git_pass(git_repository_init(&repo, "./invalid", 0));
+
+ write_invalid_filename(repo, ".git/hello");
+ write_invalid_filename(repo, ".GIT/hello");
+ write_invalid_filename(repo, ".GiT/hello");
+ write_invalid_filename(repo, "./.git/hello");
+ write_invalid_filename(repo, "./foo");
+ write_invalid_filename(repo, "./bar");
+ write_invalid_filename(repo, "foo/../bar");
+
git_repository_free(repo);
- cl_fixture_cleanup("read_tree");
+ cl_fixture_cleanup("invalid");
}
void test_index_tests__remove_entry(void)