All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 2/5] fsck: report integer overflow in author timestamps
Date: Mon, 24 Feb 2014 02:39:04 -0500	[thread overview]
Message-ID: <20140224073904.GB9969@sigill.intra.peff.net> (raw)
In-Reply-To: <20140224073348.GA20221@sigill.intra.peff.net>

When we check commit objects, we complain if commit->date is
ULONG_MAX, which is an indication that we saw integer
overflow when parsing it. However, we do not do any check at
all for author lines, which also contain a timestamp.

Let's actually check the timestamps on each ident line
with strtoul. This catches both author and committer lines,
and we can get rid of the now-redundant commit->date check.

Note that like the existing check, we compare only against
ULONG_MAX. Now that we are calling strtoul at the site of
the check, we could be slightly more careful and also check
that errno is set to ERANGE. However, this will make further
refactoring in future patches a little harder, and it
doesn't really matter in practice.

For 32-bit systems, one would have to create a commit at the
exact wrong second in 2038. But by the time we get close to
that, all systems will hopefully have moved to 64-bit (and
if they haven't, they have a real problem one second later).

For 64-bit systems, by the time we get close to ULONG_MAX,
all systems will hopefully have been consumed in the fiery
wrath of our expanding Sun.

Signed-off-by: Jeff King <peff@peff.net>
---
Note that tags don't get checked here, because we do not feed their
ident lines to fsck_ident at all. This is still a step forward, though,
as if we ever teach them to check ident lines, they'll get this new
check automatically.

 fsck.c          | 12 ++++++------
 t/t1450-fsck.sh | 14 ++++++++++++++
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/fsck.c b/fsck.c
index 99c0497..760e072 100644
--- a/fsck.c
+++ b/fsck.c
@@ -245,6 +245,8 @@ static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
 
 static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
 {
+	char *end;
+
 	if (**ident == '<')
 		return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
 	*ident += strcspn(*ident, "<>\n");
@@ -264,10 +266,11 @@ static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)
 	(*ident)++;
 	if (**ident == '0' && (*ident)[1] != ' ')
 		return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
-	*ident += strspn(*ident, "0123456789");
-	if (**ident != ' ')
+	if (strtoul(*ident, &end, 10) == ULONG_MAX)
+		return error_func(obj, FSCK_ERROR, "invalid author/committer line - date causes integer overflow");
+	if (end == *ident || *end != ' ')
 		return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
-	(*ident)++;
+	*ident = end + 1;
 	if ((**ident != '+' && **ident != '-') ||
 	    !isdigit((*ident)[1]) ||
 	    !isdigit((*ident)[2]) ||
@@ -287,9 +290,6 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)
 	int parents = 0;
 	int err;
 
-	if (commit->date == ULONG_MAX)
-		return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
-
 	if (memcmp(buffer, "tree ", 5))
 		return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
 	if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh
index d730734..8c739c9 100755
--- a/t/t1450-fsck.sh
+++ b/t/t1450-fsck.sh
@@ -142,6 +142,20 @@ test_expect_success '> in name is reported' '
 	grep "error in commit $new" out
 '
 
+# date is 2^64 + 1
+test_expect_success 'integer overflow in timestamps is reported' '
+	git cat-file commit HEAD >basis &&
+	sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
+		<basis >bad-timestamp &&
+	new=$(git hash-object -t commit -w --stdin <bad-timestamp) &&
+	test_when_finished "remove_object $new" &&
+	git update-ref refs/heads/bogus "$new" &&
+	test_when_finished "git update-ref -d refs/heads/bogus" &&
+	git fsck 2>out &&
+	cat out &&
+	grep "error in commit $new.*integer overflow" out
+'
+
 test_expect_success 'tag pointing to nonexistent' '
 	cat >invalid-tag <<-\EOF &&
 	object ffffffffffffffffffffffffffffffffffffffff
-- 
1.8.5.2.500.g8060133

  parent reply	other threads:[~2014-02-24  7:39 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-24  7:33 [PATCH 0/5] handle bogus commit dates Jeff King
2014-02-24  7:36 ` [PATCH 1/5] t4212: test bogus timestamps with git-log Jeff King
2014-02-24  7:39 ` Jeff King [this message]
2014-02-24  7:39 ` [PATCH 3/5] date: check date overflow against time_t Jeff King
2014-02-24  7:46 ` [PATCH 4/5] log: handle integer overflow in timestamps Jeff King
2014-02-24 19:50   ` Junio C Hamano
2014-02-24 19:58     ` Jeff King
2014-02-24 20:21       ` Junio C Hamano
2014-02-24 20:37         ` Jeff King
2014-02-24 21:01           ` Junio C Hamano
2014-02-24  7:49 ` [PATCH 5/5] log: do not segfault on gmtime errors Jeff King
2014-03-22  9:32   ` René Scharfe
2014-03-24 21:33     ` Jeff King
2014-03-24 22:03       ` René Scharfe
2014-03-24 22:11         ` Jeff King
2014-03-26 11:05   ` Charles Bailey
2014-03-26 18:21     ` Jeff King
2014-03-26 18:51       ` [PATCH] t4212: handle systems with post-apocalyptic gmtime Jeff King
2014-03-26 19:18         ` Junio C Hamano
2014-03-26 19:25           ` Jeff King
2014-03-26 19:33             ` Jeff King
2014-03-26 19:40               ` Jeff King
2014-03-26 20:36                 ` Charles Bailey
2014-03-26 20:38                   ` Jeff King
2014-03-26 20:41                     ` Charles Bailey
2014-03-26 21:22               ` Charles Bailey
2014-03-26 21:57                 ` Jeff King
2014-03-26 22:46                   ` Charles Bailey
2014-03-27 22:48                     ` Jeff King
2014-03-28 16:41                       ` Junio C Hamano
2014-03-28 18:47                         ` Jeff King
2014-03-28 19:02                           ` Junio C Hamano
2014-03-28 19:05                             ` Jeff King
2014-03-28 19:30                               ` Junio C Hamano
2014-04-01  7:38                                 ` Jeff King
2014-04-01  7:42                                   ` [PATCH 1/2] date: recognize bogus FreeBSD gmtime output Jeff King
2014-04-01 17:42                                     ` René Scharfe
2014-04-01 19:08                                       ` Junio C Hamano
2014-04-01 21:17                                         ` René Scharfe
2014-04-01 21:28                                           ` Jeff King
2014-04-01  7:43                                   ` [PATCH 2/2] t4212: loosen far-in-future test for AIX Jeff King
2014-04-01  7:45                                   ` [PATCH 2alt/2] work around unreliable gmtime errors on AIX Jeff King
2014-04-01 19:07                                   ` [PATCH] t4212: handle systems with post-apocalyptic gmtime Junio C Hamano
2014-04-01 19:46                                     ` Jeff King
2014-03-26 18:58       ` [PATCH 5/5] log: do not segfault on gmtime errors Junio C Hamano
2014-03-26 19:01         ` Jeff King
2014-03-26 21:01           ` Junio C Hamano
2014-03-26 21:09             ` Jeff King

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

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

  git send-email \
    --in-reply-to=20140224073904.GB9969@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.