git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Erik Elfström" <erik.elfstrom@gmail.com>
To: git@vger.kernel.org, gitster@pobox.com
Cc: "Erik Elfström" <erik.elfstrom@gmail.com>
Subject: [PATCH v5 2/5] setup: sanity check file size in read_gitfile_gently
Date: Sun, 26 Apr 2015 08:49:42 +0200	[thread overview]
Message-ID: <1430030985-14499-3-git-send-email-erik.elfstrom@gmail.com> (raw)
In-Reply-To: <1430030985-14499-1-git-send-email-erik.elfstrom@gmail.com>

read_gitfile_gently will allocate a buffer to fit the entire file that
should be read. Add a sanity check of the file size before opening to
avoid allocating a potentially huge amount of memory if we come across
a large file that someone happened to name ".git". The limit is set to
a sufficiently unreasonable size that should never be exceeded by a
genuine .git file.

Signed-off-by: Erik Elfström <erik.elfstrom@gmail.com>
---
 cache.h | 1 +
 setup.c | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/cache.h b/cache.h
index 868e4d3..c9f1f8e 100644
--- a/cache.h
+++ b/cache.h
@@ -439,6 +439,7 @@ extern const char *get_git_work_tree(void);
 #define READ_GITFILE_ERR_INVALID_FORMAT 5
 #define READ_GITFILE_ERR_NO_PATH 6
 #define READ_GITFILE_ERR_NOT_A_REPO 7
+#define READ_GITFILE_ERR_TOO_LARGE 8
 extern const char *read_gitfile_gently(const char *path, int *return_error_code);
 #define read_gitfile(path) read_gitfile_gently((path), NULL)
 extern const char *resolve_gitdir(const char *suspect);
diff --git a/setup.c b/setup.c
index c4538ca..792c37b 100644
--- a/setup.c
+++ b/setup.c
@@ -364,6 +364,10 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
 		error_code = READ_GITFILE_ERR_OPEN_FAILED;
 		goto cleanup_return;
 	}
+	if (st.st_size > PATH_MAX * 4) {
+		error_code = READ_GITFILE_ERR_TOO_LARGE;
+		goto cleanup_return;
+	}
 	buf = xmalloc(st.st_size + 1);
 	len = read_in_full(fd, buf, st.st_size);
 	close(fd);
@@ -418,6 +422,8 @@ cleanup_return:
 			return NULL;
 		case READ_GITFILE_ERR_OPEN_FAILED:
 			die_errno("Error opening '%s'", path);
+		case READ_GITFILE_ERR_TOO_LARGE:
+			die("Too large to be a .git file: '%s'", path);
 		case READ_GITFILE_ERR_READ_FAILED:
 			die("Error reading %s", path);
 		case READ_GITFILE_ERR_INVALID_FORMAT:
-- 
2.4.0.rc3.8.gbb31afb

  parent reply	other threads:[~2015-04-26  6:50 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-25  9:06 [PATCH v4 0/5] Improving performance of git clean Erik Elfström
2015-04-25  9:06 ` [PATCH v4 1/5] setup: add gentle version of read_gitfile Erik Elfström
2015-04-25 16:51   ` Junio C Hamano
2015-04-25 16:54     ` Junio C Hamano
2015-04-25  9:06 ` [PATCH v4 2/5] setup: sanity check file size in read_gitfile_gently Erik Elfström
2015-04-25 16:47   ` Junio C Hamano
2015-04-25 17:59     ` Erik Elfström
2015-04-26  4:29       ` Junio C Hamano
2015-04-26  6:49         ` [PATCH v5 0/5] Improving performance of git clean Erik Elfström
2015-04-26  6:49           ` [PATCH v5 1/5] setup: add gentle version of read_gitfile Erik Elfström
2015-04-28  6:17             ` Jeff King
2015-04-28 20:07               ` erik elfström
2015-04-28 20:19                 ` Jeff King
2015-04-28 20:34                   ` Jonathan Nieder
2015-04-28 20:36                     ` Jeff King
2015-04-28 20:42                       ` Jonathan Nieder
2015-04-28 20:48                         ` Jeff King
2015-04-28 21:06                           ` Jonathan Nieder
2015-04-28 23:34                           ` Junio C Hamano
2015-04-29 23:47             ` Stefan Beller
2015-04-30  1:35               ` Junio C Hamano
2015-04-26  6:49           ` Erik Elfström [this message]
2015-04-28  6:02             ` [PATCH v5 2/5] setup: sanity check file size in read_gitfile_gently Jeff King
2015-04-28  7:21               ` Windows path limites, was " Johannes Schindelin
2015-04-28 15:33                 ` Doug Kelly
2015-04-28 16:20                   ` Windows path limits, " Johannes Schindelin
2015-04-28 19:28               ` erik elfström
2015-04-29 15:42             ` Junio C Hamano
2015-04-26  6:49           ` [PATCH v5 3/5] t7300: add tests to document behavior of clean and nested git Erik Elfström
2015-04-26  6:49           ` [PATCH v5 4/5] p7300: add performance tests for clean Erik Elfström
2015-04-28  6:33             ` Jeff King
2015-04-28 19:36               ` erik elfström
2015-04-26  6:49           ` [PATCH v5 5/5] clean: improve performance when removing lots of directories Erik Elfström
2015-04-28  6:24             ` Jeff King
2015-04-28 20:31               ` erik elfström
2015-04-25  9:06 ` [PATCH v4 3/5] t7300: add tests to document behavior of clean and nested git Erik Elfström
2015-04-25  9:06 ` [PATCH v4 4/5] p7300: add performance tests for clean Erik Elfström
2015-04-25  9:06 ` [PATCH v4 5/5] clean: improve performance when removing lots of directories Erik Elfström

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1430030985-14499-3-git-send-email-erik.elfstrom@gmail.com \
    --to=erik.elfstrom@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).