Skip to content

Commit 6d86841

Browse files
committed
mingw: fix quoting of arguments
We need to be careful to follow proper quoting rules. For example, if an argument contains spaces, we have to quote them. Double-quotes need to be escaped. Backslashes need to be escaped, but only if they are followed by a double-quote character. We need to be _extra_ careful to consider the case where an argument ends in a backslash _and_ needs to be quoted: in this case, we append a double-quote character, i.e. the backslash now has to be escaped! The current code, however, fails to recognize that, and therefore can turn an argument that ends in a single backslash into a quoted argument that now ends in an escaped double-quote character. This allows subsequent command-line parameters to be split and part of them being mistaken for command-line options, e.g. through a maliciously-crafted submodule URL during a recursive clone. Technically, we would not need to quote _all_ arguments which end in a backslash _unless_ the argument needs to be quoted anyway. For example, `test\` would not need to be quoted, while `test \` would need to be. To keep the code simple, however, and therefore easier to reason about and ensure its correctness, we now _always_ quote an argument that ends in a backslash. This addresses CVE-2019-1350. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a8dee3c commit 6d86841

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

compat/mingw.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ static const char *quote_arg(const char *arg)
872872
p++;
873873
len++;
874874
}
875-
if (*p == '"')
875+
if (*p == '"' || !*p)
876876
n += count*2 + 1;
877877
continue;
878878
}
@@ -894,16 +894,19 @@ static const char *quote_arg(const char *arg)
894894
count++;
895895
*d++ = *arg++;
896896
}
897-
if (*arg == '"') {
897+
if (*arg == '"' || !*arg) {
898898
while (count-- > 0)
899899
*d++ = '\\';
900+
/* don't escape the surrounding end quote */
901+
if (!*arg)
902+
break;
900903
*d++ = '\\';
901904
}
902905
}
903906
*d++ = *arg++;
904907
}
905908
*d++ = '"';
906-
*d++ = 0;
909+
*d++ = '\0';
907910
return q;
908911
}
909912

t/t7416-submodule-dash-url.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,18 @@ test_expect_success 'clone rejects unprotected dash' '
3131
test_i18ngrep ignoring err
3232
'
3333

34+
test_expect_success 'trailing backslash is handled correctly' '
35+
git init testmodule &&
36+
test_commit -C testmodule c &&
37+
git submodule add ./testmodule &&
38+
: ensure that the name ends in a double backslash &&
39+
sed -e "s|\\(submodule \"testmodule\\)\"|\\1\\\\\\\\\"|" \
40+
-e "s|url = .*|url = \" --should-not-be-an-option\"|" \
41+
<.gitmodules >.new &&
42+
mv .new .gitmodules &&
43+
git commit -am "Add testmodule" &&
44+
test_must_fail git clone --verbose --recurse-submodules . dolly 2>err &&
45+
test_i18ngrep ! "unknown option" err
46+
'
47+
3448
test_done

0 commit comments

Comments
 (0)