aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2024-05-31 08:00:34 -0400
committerJunio C Hamano <gitster@pobox.com>2024-05-31 15:30:32 -0700
commita2bc523e1ea2ef9b59eb0c26331b6e7d9dc5a812 (patch)
tree7bf62f8a4a7b6570d7a42d74376c3269ed8d0cb8
parent786a3e4b8d754d2b14b1208b98eeb0a554ef19a8 (diff)
downloadgit-a2bc523e1ea2ef9b59eb0c26331b6e7d9dc5a812.tar.gz
dir.c: skip .gitignore, etc larger than INT_MAX
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--dir.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index 20ebe4cba2..6913003164 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.
@@ -1136,6 +1137,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;
}
@@ -1192,6 +1199,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;
}