From: Jonathan Nieder <jrnieder@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>,
Dmitry Potapov <dpotapov@gmail.com>,
git@vger.kernel.org
Subject: [PATCH] archive: fix segfault from too long --format parameter
Date: Sun, 7 Feb 2010 01:10:46 -0600 [thread overview]
Message-ID: <20100207070811.GA26338@progeny.tock> (raw)
‘git archive --format=<string of 25 characters or more>’ overflows a
local buffer, producing a segfault here.
The context: in commit 0f4b377 (git-archive: infer output format from
filename when unspecified, 2009-09-14), the cmd_archive wrapper
learned to produce a format argument for the local or remote archive
machinery in a local buffer, but that code was missing a bounds check.
So add the missing check. As a belt-and-suspenders measure, also use
snprintf to make sure the copy afterwards does not overflow.
Cc: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
Cc: Dmitry Potapov <dpotapov@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
I noticed this while reading over the archive code. Thoughts?
builtin-archive.c | 4 +++-
t/t5000-tar-tree.sh | 6 ++++++
2 files changed, 9 insertions(+), 1 deletions(-)
diff --git a/builtin-archive.c b/builtin-archive.c
index 3fb4136..94db00d 100644
--- a/builtin-archive.c
+++ b/builtin-archive.c
@@ -107,7 +107,9 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
}
if (format) {
- sprintf(fmt_opt, "--format=%s", format);
+ if (strlen(format) > sizeof(fmt_opt) - sizeof("--format="))
+ die("git archive: format is too long: %.50s", format);
+ snprintf(fmt_opt, sizeof(fmt_opt), "--format=%s", format);
/*
* We have enough room in argv[] to muck it in place,
* because either --format and/or --output must have
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
index 0037f63..cf114b2 100755
--- a/t/t5000-tar-tree.sh
+++ b/t/t5000-tar-tree.sh
@@ -174,6 +174,12 @@ test_expect_success \
'
test_expect_success \
+ 'git archive --format=<long nonsense string>' \
+ 'format=abacadabra &&
+ format="${format}${format}${format}zip" &&
+ test_must_fail git archive "--format=$format" HEAD'
+
+test_expect_success \
'git archive --format=zip' \
'git archive --format=zip HEAD >d.zip'
--
1.7.0.rc1
next reply other threads:[~2010-02-07 7:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-07 7:10 Jonathan Nieder [this message]
2010-02-07 10:03 ` [PATCH] archive: fix segfault from too long --format parameter René Scharfe
2010-02-07 23:30 ` [PATCH] archive: simplify archive format guessing René Scharfe
2010-02-08 0:45 ` Junio C Hamano
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=20100207070811.GA26338@progeny.tock \
--to=jrnieder@gmail.com \
--cc=dpotapov@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=rene.scharfe@lsrfire.ath.cx \
/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 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 NNTP newsgroup(s).