Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTaylor Blau <me@ttaylorr.com>2022-10-31 04:04:43 +0300
committerTaylor Blau <me@ttaylorr.com>2022-10-31 04:04:43 +0300
commitc41ec63ef5e68b4e5d2896390948223f5793c4e9 (patch)
tree7959367edf8d1c703f005b3f51e841ff7386537b
parentc7ccd4eae92a551f5237fc04e2997274d543ec5b (diff)
parentf1c0e3946e0bdec16d6440fb7e52edbe78cf12b3 (diff)
Merge branch 'tb/cap-patch-at-1gb'
"git apply" limits its input to a bit less than 1 GiB. * tb/cap-patch-at-1gb: apply: reject patches larger than ~1 GiB
-rw-r--r--apply.c12
-rwxr-xr-xt/t4141-apply-too-large.sh23
2 files changed, 34 insertions, 1 deletions
diff --git a/apply.c b/apply.c
index 6b4dbe0c88d..bc338143134 100644
--- a/apply.c
+++ b/apply.c
@@ -386,9 +386,19 @@ static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
#define SLOP (16)
+/*
+ * apply.c isn't equipped to handle arbitrarily large patches, because
+ * it intermingles `unsigned long` with `int` for the type used to store
+ * buffer lengths.
+ *
+ * Only process patches that are just shy of 1 GiB large in order to
+ * avoid any truncation or overflow issues.
+ */
+#define MAX_APPLY_SIZE (1024UL * 1024 * 1023)
+
static int read_patch_file(struct strbuf *sb, int fd)
{
- if (strbuf_read(sb, fd, 0) < 0)
+ if (strbuf_read(sb, fd, 0) < 0 || sb->len >= MAX_APPLY_SIZE)
return error_errno("git apply: failed to read");
/*
diff --git a/t/t4141-apply-too-large.sh b/t/t4141-apply-too-large.sh
new file mode 100755
index 00000000000..58742d4fc5d
--- /dev/null
+++ b/t/t4141-apply-too-large.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+test_description='git apply with too-large patch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success EXPENSIVE 'git apply rejects patches that are too large' '
+ sz=$((1024 * 1024 * 1023)) &&
+ {
+ cat <<-\EOF &&
+ diff --git a/file b/file
+ new file mode 100644
+ --- /dev/null
+ +++ b/file
+ @@ -0,0 +1 @@
+ EOF
+ test-tool genzeros
+ } | test_copy_bytes $sz | test_must_fail git apply 2>err &&
+ grep "git apply: failed to read" err
+'
+
+test_done