Skip to content

Commit 4412a04

Browse files
committed
init.templateDir: consider this config setting protected
The ability to configuring the template directory is a delicate feature: It allows defining hooks that will be run e.g. during a `git clone` operation, such as the `post-checkout` hook. As such, it is of utmost importance that Git would not allow that config setting to be changed during a `git clone` by mistake, allowing an attacker a chance for a Remote Code Execution, allowing attackers to run arbitrary code on unsuspecting users' machines. As a defense-in-depth measure, to prevent minor vulnerabilities in the `git clone` code from ballooning into higher-serverity attack vectors, let's make this a protected setting just like `safe.directory` and friends, i.e. ignore any `init.templateDir` entries from any local config. Note: This does not change the behavior of any recursive clone (modulo bugs), as the local repository config is not even supposed to be written while cloning the superproject, except in one scenario: If a config template is configured that sets the template directory. This might be done because `git clone --recurse-submodules --template=<directory>` does not pass that template directory on to the submodules' initialization. Another scenario where this commit changes behavior is where repositories are _not_ cloned recursively, and then some (intentional, benign) automation configures the template directory to be used before initializing the submodules. So the caveat is that this could theoretically break existing processes. In both scenarios, there is a way out, though: configuring the template directory via the environment variable `GIT_TEMPLATE_DIR`. This change in behavior is a trade-off between security and backwards-compatibility that is struck in favor of security. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8db1e87 commit 4412a04

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

setup.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,22 +1726,45 @@ int daemonize(void)
17261726
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
17271727
#endif
17281728

1729+
struct template_dir_cb_data {
1730+
char *path;
1731+
int initialized;
1732+
};
1733+
1734+
static int template_dir_cb(const char *key, const char *value, void *d)
1735+
{
1736+
struct template_dir_cb_data *data = d;
1737+
1738+
if (strcmp(key, "init.templatedir"))
1739+
return 0;
1740+
1741+
if (!value) {
1742+
data->path = NULL;
1743+
} else {
1744+
char *path = NULL;
1745+
1746+
FREE_AND_NULL(data->path);
1747+
if (!git_config_pathname((const char **)&path, key, value))
1748+
data->path = path ? path : xstrdup(value);
1749+
}
1750+
1751+
return 0;
1752+
}
1753+
17291754
const char *get_template_dir(const char *option_template)
17301755
{
17311756
const char *template_dir = option_template;
17321757

17331758
if (!template_dir)
17341759
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
17351760
if (!template_dir) {
1736-
static const char *init_template_dir;
1737-
static int initialized;
1761+
static struct template_dir_cb_data data;
17381762

1739-
if (!initialized) {
1740-
git_config_get_pathname("init.templatedir",
1741-
&init_template_dir);
1742-
initialized = 1;
1763+
if (!data.initialized) {
1764+
git_protected_config(template_dir_cb, &data);
1765+
data.initialized = 1;
17431766
}
1744-
template_dir = init_template_dir;
1767+
template_dir = data.path;
17451768
}
17461769
if (!template_dir) {
17471770
static char *dir;

t/t7400-submodule-basic.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,4 +1436,35 @@ test_expect_success 'recursive clone respects -q' '
14361436
test_must_be_empty actual
14371437
'
14381438

1439+
test_expect_success '`submodule init` and `init.templateDir`' '
1440+
mkdir -p tmpl/hooks &&
1441+
write_script tmpl/hooks/post-checkout <<-EOF &&
1442+
echo HOOK-RUN >&2
1443+
echo I was here >hook.run
1444+
exit 1
1445+
EOF
1446+
1447+
test_config init.templateDir "$(pwd)/tmpl" &&
1448+
test_when_finished \
1449+
"git config --global --unset init.templateDir || true" &&
1450+
(
1451+
sane_unset GIT_TEMPLATE_DIR &&
1452+
NO_SET_GIT_TEMPLATE_DIR=t &&
1453+
export NO_SET_GIT_TEMPLATE_DIR &&
1454+
1455+
git config --global init.templateDir "$(pwd)/tmpl" &&
1456+
test_must_fail git submodule \
1457+
add "$submodurl" sub-global 2>err &&
1458+
git config --global --unset init.templateDir &&
1459+
grep HOOK-RUN err &&
1460+
test_path_is_file sub-global/hook.run &&
1461+
1462+
git config init.templateDir "$(pwd)/tmpl" &&
1463+
git submodule add "$submodurl" sub-local 2>err &&
1464+
git config --unset init.templateDir &&
1465+
! grep HOOK-RUN err &&
1466+
test_path_is_missing sub-local/hook.run
1467+
)
1468+
'
1469+
14391470
test_done

0 commit comments

Comments
 (0)