Skip to contentSkip to conversation

link_alt_odb_entry: fix read over array bounds reported by valgrind

PATCH Superseded Heiko Voigt 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 link_alt_odb_entry: fix read over array bounds reported by valgrind

pfxlen can be longer than the path in objdir when relative_base contains the path to gits object directory.

---
 sha1_file.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sha1_file.c +3 -2
index 4ccaf7a..631d0dd 100644
@@ -251,7 +251,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 	const char *objdir = get_object_directory();
 	struct alternate_object_database *ent;
 	struct alternate_object_database *alt;
-	int pfxlen, entlen;
+	int pfxlen, entlen, objdirlen;
 	struct strbuf pathbuf = STRBUF_INIT;
 
 	if (!is_absolute_path(entry) && relative_base) {
@@ -298,7 +298,8 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 			return -1;
 		}
 	}
-	if (!memcmp(ent->base, objdir, pfxlen)) {
+	objdirlen = strlen(objdir);
+	if (!memcmp(ent->base, objdir, pfxlen > objdirlen ? objdirlen : pfxlen)) {
 		free(ent);
 		return -1;
 	}
Signature · 1 line
1.7.12.rc0.23.gc9a5ac4
#1 lore raw
Quoted 2 lines · Heiko Voigt

pfxlen can be longer than the path in objdir when relative_base contains the path to gits object directory.

s/gits/????/ perhaps "Git's", but I am not sure.

Quoted 19 lines
---
 sha1_file.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sha1_file.c +1 -1
index 4ccaf7a..631d0dd 100644
@@ -251,7 +251,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 	const char *objdir = get_object_directory();
 	struct alternate_object_database *ent;
 	struct alternate_object_database *alt;
-	int pfxlen, entlen;
+	int pfxlen, entlen, objdirlen;
 	struct strbuf pathbuf = STRBUF_INIT;
 
 	if (!is_absolute_path(entry) && relative_base) {

sha1_file.c static int link_alt_odb_entry(const char * entry, int len, const char * relative · line 302

Quoted 6 lines
sha1_file.c +2 -1
@@ -298,7 +298,8 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 			return -1;
 		}
 	}
-	if (!memcmp(ent->base, objdir, pfxlen)) {
+	objdirlen = strlen(objdir);
+	if (!memcmp(ent->base, objdir, pfxlen > objdirlen ? objdirlen : pfxlen)) {

The new code tells us to compare up to the shorter length between objdir (i.e. path/to/.git/objects) and the given alternate object directory (i.e. alt/path/to/.git/objects), but is that really what we want? What happens if the given alternate object directory were "path/to/.git/objects-not-quite", with objdir "path/to/.git/objects"?

They are not the same directory, and this check is about avoiding "the common mistake of listing ... object directory itself", no?

Quoted 3 lines
sha1_file.c +0 -0
 		free(ent);
 		return -1;
 	}
#2 lore raw
Quoted 29 lines · Junio C Hamano
Quoted 2 lines · Heiko Voigt

pfxlen can be longer than the path in objdir when relative_base contains the path to gits object directory.

s/gits/????/ perhaps "Git's", but I am not sure.

Quoted 9 lines
---
 sha1_file.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
sha1_file.c +0 -0
index 4ccaf7a..631d0dd 100644

sha1_file.c

Quoted 6 lines
sha1_file.c +2 -1
 			return -1;
 		}
 	}
-	if (!memcmp(ent->base, objdir, pfxlen)) {
+	objdirlen = strlen(objdir);
+	if (!memcmp(ent->base, objdir, pfxlen > objdirlen ? objdirlen : pfxlen)) {

The new code tells us to compare up to the shorter length between objdir (i.e. path/to/.git/objects) and the given alternate object directory (i.e. alt/path/to/.git/objects), but is that really what we want? What happens if the given alternate object directory were "path/to/.git/objects-not-quite", with objdir "path/to/.git/objects"?

Quoted 6 lines

They are not the same directory, and this check is about avoiding "the common mistake of listing ... object directory itself", no?

Quoted 3 lines
sha1_file.c +0 -0
 		free(ent);
 		return -1;
 	}

In other words, wouldn't this be sufficient? We NUL terminate ent->base[pfxlen] when we prepare that buffer with

LEADING PATH\0XX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\0

in preparation for these "duplicate check" step, and then we turn the NUL at ent->base[pfxlen] to '/' before leaving the function to make it

LEADING PATH/XX/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\0

so that we can fill XX when probing for loose objects.

 sha1_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sha1_file.c +1 -1
index 4f06a0e..a1f3bee 100644
@@ -298,7 +298,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 			return -1;
 		}
 	}
-	if (!memcmp(ent->base, objdir, pfxlen)) {
+	if (!strcmp(ent->base, objdir)) {
 		free(ent);
 		return -1;
 	}
#3 lore raw

PATCH v2 1/1 link_alt_odb_entry: fix read over array bounds reported by valgrind

In reply to Junio C Hamano

pfxlen can be longer than the path in objdir when relative_base contains the path to Git's object directory.

---

Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Heiko Voigt

pfxlen can be longer than the path in objdir when relative_base contains the path to gits object directory.

s/gits/????/ perhaps "Git's", but I am not sure.

Git's is correct.

Quoted 14 lines

Review static int link_alt_odb_entry(const char * entry, int len, const char * relative · line 302

Quoted 7 lines
(quoted diff) +2 -1
@@ -298,7 +298,8 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 			return -1;
 		}
 	}
-	if (!memcmp(ent->base, objdir, pfxlen)) {
+	objdirlen = strlen(objdir);
+	if (!memcmp(ent->base, objdir, pfxlen > objdirlen ? objdirlen : pfxlen)) {

The new code tells us to compare up to the shorter length between objdir (i.e. path/to/.git/objects) and the given alternate object directory (i.e. alt/path/to/.git/objects), but is that really what we want? What happens if the given alternate object directory were "path/to/.git/objects-not-quite", with objdir "path/to/.git/objects"?

Quoted 2 lines

They are not the same directory, and this check is about avoiding "the common mistake of listing ... object directory itself", no?

You are right. strcmp is the correct solution for this. I tried your change and it also fixes the error valgrind reported.

 sha1_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sha1_file.c +1 -1
index 4ccaf7a..af5cfbd 100644
@@ -298,7 +298,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 			return -1;
 		}
 	}
-	if (!memcmp(ent->base, objdir, pfxlen)) {
+	if (!strcmp(ent->base, objdir)) {
 		free(ent);
 		return -1;
 	}
Signature · 1 line
1.7.12.rc0.24.g3fa2a49