Skip to content

Commit a2bc523

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 786a3e4 commit a2bc523

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

dir.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "symlinks.h"
3131
#include "trace2.h"
3232
#include "tree.h"
33+
#include "hex.h"
3334

3435
/*
3536
* 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,
11361137
}
11371138
}
11381139

1140+
if (size > INT_MAX) {
1141+
warning("ignoring excessively large pattern file: %s", fname);
1142+
free(buf);
1143+
return -1;
1144+
}
1145+
11391146
add_patterns_from_buffer(buf, size, base, baselen, pl);
11401147
return 0;
11411148
}
@@ -1192,6 +1199,13 @@ int add_patterns_from_blob_to_list(
11921199
if (r != 1)
11931200
return r;
11941201

1202+
if (size > INT_MAX) {
1203+
warning("ignoring excessively large pattern blob: %s",
1204+
oid_to_hex(oid));
1205+
free(buf);
1206+
return -1;
1207+
}
1208+
11951209
add_patterns_from_buffer(buf, size, base, baselen, pl);
11961210
return 0;
11971211
}

0 commit comments

Comments
 (0)