From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH] dir.c: skip .gitignore, etc larger than INT_MAX
Date: Fri, 31 May 2024 08:00:34 -0400 [thread overview]
Message-ID: <20240531120034.GA442032@coredump.intra.peff.net> (raw)
We use add_patterns() to read .gitignore, .git/info/exclude, etc, as
well as other pattern-like files like sparse-checkout. The parser for
these uses an "int" as an index, meaning that files over 2GB will
generally cause signed integer overflow and out-of-bounds access.
This is unlikely to happen in any real files, but we do read .gitignore
files from the tree. A malicious tree could cause an out-of-bounds read
and segfault (we also write NULs over newlines, so in theory it could be
an out-of-bounds write, too, but as we go char-by-char, the first thing
that happens is trying to read a negative 2GB offset).
We could fix the most obvious issue by replacing one "int" with a
"size_t". But there are tons of "int" sprinkled throughout this code for
things like pattern lengths, number of patterns, and so on. Since nobody
would actually want a 2GB .gitignore file, an easy defensive measure is
to just refuse to parse them.
The "int" in question is in add_patterns_from_buffer(), so we could
catch it there. But by putting the checks in its two callers, we can
produce more useful error messages.
Signed-off-by: Jeff King <peff@peff.net>
---
Just something I noticed while working on leaks nearby.
dir.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/dir.c b/dir.c
index f6066cc01d..914060edfd 100644
--- a/dir.c
+++ b/dir.c
@@ -30,6 +30,7 @@
#include "symlinks.h"
#include "trace2.h"
#include "tree.h"
+#include "hex.h"
/*
* Tells read_directory_recursive how a file or directory should be treated.
@@ -1148,6 +1149,12 @@ static int add_patterns(const char *fname, const char *base, int baselen,
}
}
+ if (size > INT_MAX) {
+ warning("ignoring excessively large pattern file: %s", fname);
+ free(buf);
+ return -1;
+ }
+
add_patterns_from_buffer(buf, size, base, baselen, pl);
return 0;
}
@@ -1204,6 +1211,13 @@ int add_patterns_from_blob_to_list(
if (r != 1)
return r;
+ if (size > INT_MAX) {
+ warning("ignoring excessively large pattern blob: %s",
+ oid_to_hex(oid));
+ free(buf);
+ return -1;
+ }
+
add_patterns_from_buffer(buf, size, base, baselen, pl);
return 0;
}
--
2.45.1.727.ge984192922
next reply other threads:[~2024-05-31 12:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-31 12:00 Jeff King [this message]
2024-05-31 15:05 ` [PATCH] dir.c: skip .gitignore, etc larger than INT_MAX Junio C Hamano
2024-06-04 10:39 ` Jeff King
2024-06-04 17:45 ` Junio C Hamano
2024-06-05 8:03 ` Jeff King
2024-06-05 16:23 ` Junio C Hamano
2024-05-31 15:10 ` 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=20240531120034.GA442032@coredump.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 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).