Skip to content

Commit 41284eb

Browse files
dturner-twgitster
authored andcommitted
name-hash: don't reuse cache_entry in dir_entry
Stop reusing cache_entry in dir_entry; doing so causes a use-after-free bug. During merges, we free entries that we no longer need in the destination index. But those entries might have also been stored in the dir_entry cache, and when a later call to add_to_index found them, they would be used after being freed. To prevent this, change dir_entry to store a copy of the name instead of a pointer to a cache_entry. This entails some refactoring of code that expects the cache_entry. Keith McGuigan <[email protected]> diagnosed this bug and wrote the initial patch, but this version does not use any of Keith's code. Helped-by: Keith McGuigan <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: David Turner <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 441c4a4 commit 41284eb

File tree

4 files changed

+35
-60
lines changed

4 files changed

+35
-60
lines changed

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
501501
extern int discard_index(struct index_state *);
502502
extern int unmerged_index(const struct index_state *);
503503
extern int verify_path(const char *path);
504-
extern struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen);
504+
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);
505+
extern void adjust_dirname_case(struct index_state *istate, char *name);
505506
extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
506507
extern int index_name_pos(const struct index_state *, const char *name, int namelen);
507508
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */

dir.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -964,29 +964,15 @@ enum exist_status {
964964
*/
965965
static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
966966
{
967-
const struct cache_entry *ce = cache_dir_exists(dirname, len);
968-
unsigned char endchar;
967+
struct cache_entry *ce;
969968

970-
if (!ce)
971-
return index_nonexistent;
972-
endchar = ce->name[len];
973-
974-
/*
975-
* The cache_entry structure returned will contain this dirname
976-
* and possibly additional path components.
977-
*/
978-
if (endchar == '/')
969+
if (cache_dir_exists(dirname, len))
979970
return index_directory;
980971

981-
/*
982-
* If there are no additional path components, then this cache_entry
983-
* represents a submodule. Submodules, despite being directories,
984-
* are stored in the cache without a closing slash.
985-
*/
986-
if (!endchar && S_ISGITLINK(ce->ce_mode))
972+
ce = cache_file_exists(dirname, len, ignore_case);
973+
if (ce && S_ISGITLINK(ce->ce_mode))
987974
return index_gitdir;
988975

989-
/* This should never be hit, but it exists just in case. */
990976
return index_nonexistent;
991977
}
992978

name-hash.c

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
struct dir_entry {
1212
struct hashmap_entry ent;
1313
struct dir_entry *parent;
14-
struct cache_entry *ce;
1514
int nr;
1615
unsigned int namelen;
16+
char name[FLEX_ARRAY];
1717
};
1818

1919
static int dir_entry_cmp(const struct dir_entry *e1,
2020
const struct dir_entry *e2, const char *name)
2121
{
22-
return e1->namelen != e2->namelen || strncasecmp(e1->ce->name,
23-
name ? name : e2->ce->name, e1->namelen);
22+
return e1->namelen != e2->namelen || strncasecmp(e1->name,
23+
name ? name : e2->name, e1->namelen);
2424
}
2525

2626
static struct dir_entry *find_dir_entry(struct index_state *istate,
@@ -41,14 +41,6 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
4141
* closing slash. Despite submodules being a directory, they never
4242
* reach this point, because they are stored
4343
* in index_state.name_hash (as ordinary cache_entries).
44-
*
45-
* Note that the cache_entry stored with the dir_entry merely
46-
* supplies the name of the directory (up to dir_entry.namelen). We
47-
* track the number of 'active' files in a directory in dir_entry.nr,
48-
* so we can tell if the directory is still relevant, e.g. for git
49-
* status. However, if cache_entries are removed, we cannot pinpoint
50-
* an exact cache_entry that's still active. It is very possible that
51-
* multiple dir_entries point to the same cache_entry.
5244
*/
5345
struct dir_entry *dir;
5446

@@ -63,10 +55,10 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
6355
dir = find_dir_entry(istate, ce->name, namelen);
6456
if (!dir) {
6557
/* not found, create it and add to hash table */
66-
dir = xcalloc(1, sizeof(struct dir_entry));
58+
dir = xcalloc(1, sizeof(struct dir_entry) + namelen + 1);
6759
hashmap_entry_init(dir, memihash(ce->name, namelen));
6860
dir->namelen = namelen;
69-
dir->ce = ce;
61+
strncpy(dir->name, ce->name, namelen);
7062
hashmap_add(&istate->dir_hash, dir);
7163

7264
/* recursively add missing parent directories */
@@ -188,26 +180,36 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
188180
return slow_same_name(name, namelen, ce->name, len);
189181
}
190182

191-
struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen)
183+
int index_dir_exists(struct index_state *istate, const char *name, int namelen)
192184
{
193-
struct cache_entry *ce;
194185
struct dir_entry *dir;
195186

196187
lazy_init_name_hash(istate);
197188
dir = find_dir_entry(istate, name, namelen);
198-
if (dir && dir->nr)
199-
return dir->ce;
189+
return dir && dir->nr;
190+
}
200191

201-
/*
202-
* It might be a submodule. Unlike plain directories, which are stored
203-
* in the dir-hash, submodules are stored in the name-hash, so check
204-
* there, as well.
205-
*/
206-
ce = index_file_exists(istate, name, namelen, 1);
207-
if (ce && S_ISGITLINK(ce->ce_mode))
208-
return ce;
192+
void adjust_dirname_case(struct index_state *istate, char *name)
193+
{
194+
const char *startPtr = name;
195+
const char *ptr = startPtr;
209196

210-
return NULL;
197+
lazy_init_name_hash(istate);
198+
while (*ptr) {
199+
while (*ptr && *ptr != '/')
200+
ptr++;
201+
202+
if (*ptr == '/') {
203+
struct dir_entry *dir;
204+
205+
ptr++;
206+
dir = find_dir_entry(istate, name, ptr - name + 1);
207+
if (dir) {
208+
memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr);
209+
startPtr = ptr;
210+
}
211+
}
212+
}
211213
}
212214

213215
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)

read-cache.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -661,21 +661,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
661661
* entry's directory case.
662662
*/
663663
if (ignore_case) {
664-
const char *startPtr = ce->name;
665-
const char *ptr = startPtr;
666-
while (*ptr) {
667-
while (*ptr && *ptr != '/')
668-
++ptr;
669-
if (*ptr == '/') {
670-
struct cache_entry *foundce;
671-
++ptr;
672-
foundce = index_dir_exists(istate, ce->name, ptr - ce->name - 1);
673-
if (foundce) {
674-
memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr);
675-
startPtr = ptr;
676-
}
677-
}
678-
}
664+
adjust_dirname_case(istate, ce->name);
679665
}
680666

681667
alias = index_file_exists(istate, ce->name, ce_namelen(ce), ignore_case);

0 commit comments

Comments
 (0)