{"thread":{"id":"7126","subject":"[PATCH 2/9] Fix mmap leak caused by reading bad indexes.","startedAt":"2007-03-07T01:44:11Z","lastAt":"2007-03-07T01:44:11Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"36516","messageId":"20070307014411.GC26482@spearce.org","threadId":"7126","inReplyTo":null,"subject":"[PATCH 2/9] Fix mmap leak caused by reading bad indexes.","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-07T01:44:11Z","receivedAt":"2007-03-07T01:44:11Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"If an index is corrupt, or is simply too new for us to understand,\nwe were leaking the mmap that held the entire content of the index.\nThis could be a considerable size on large projects, given that\nthe index is at least 24 bytes * nr_objects.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n sha1_file.c |   25 ++++++++++++++++---------\n 1 files changed, 16 insertions(+), 9 deletions(-)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex c13ef66..cfce7ac 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -430,8 +430,9 @@ void pack_report()\n \t\tpack_mapped, peak_pack_mapped);\n }\n \n-static int check_packed_git_idx(const char *path, unsigned long *idx_size_,\n-\t\t\t\tvoid **idx_map_)\n+static int check_packed_git_idx(const char *path,\n+\tunsigned long *idx_size_,\n+\tvoid **idx_map_)\n {\n \tvoid *idx_map;\n \tuint32_t *index;\n@@ -446,6 +447,10 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,\n \t\treturn -1;\n \t}\n \tidx_size = st.st_size;\n+\tif (idx_size < 4 * 256 + 20 + 20) {\n+\t\tclose(fd);\n+\t\treturn error(\"index file %s is too small\", path);\n+\t}\n \tidx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tclose(fd);\n \n@@ -453,25 +458,25 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,\n \t*idx_map_ = idx_map;\n \t*idx_size_ = idx_size;\n \n-\t/* check index map */\n-\tif (idx_size < 4*256 + 20 + 20)\n-\t\treturn error(\"index file %s is too small\", path);\n-\n \t/* a future index format would start with this, as older git\n \t * binaries would fail the non-monotonic index check below.\n \t * give a nicer warning to the user if we can.\n \t */\n-\tif (index[0] == htonl(PACK_IDX_SIGNATURE))\n+\tif (index[0] == htonl(PACK_IDX_SIGNATURE)) {\n+\t\tmunmap(idx_map, idx_size);\n \t\treturn error(\"index file %s is a newer version\"\n \t\t\t\" and is not supported by this binary\"\n \t\t\t\" (try upgrading GIT to a newer version)\",\n \t\t\tpath);\n+\t}\n \n \tnr = 0;\n \tfor (i = 0; i < 256; i++) {\n \t\tunsigned int n = ntohl(index[i]);\n-\t\tif (n < nr)\n+\t\tif (n < nr) {\n+\t\t\tmunmap(idx_map, idx_size);\n \t\t\treturn error(\"non-monotonic index %s\", path);\n+\t\t}\n \t\tnr = n;\n \t}\n \n@@ -482,8 +487,10 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,\n \t *  - 20-byte SHA1 of the packfile\n \t *  - 20-byte SHA1 file checksum\n \t */\n-\tif (idx_size != 4*256 + nr * 24 + 20 + 20)\n+\tif (idx_size != 4*256 + nr * 24 + 20 + 20) {\n+\t\tmunmap(idx_map, idx_size);\n \t\treturn error(\"wrong index file size in %s\", path);\n+\t}\n \n \treturn 0;\n }\n-- \n1.5.0.3.863.gf0989\n"}]}