* [PATCH v3 1/5] Documentation: define protected configuration
2022-05-27 21:09 ` [PATCH v3 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
@ 2022-05-27 21:09 ` Glen Choo via GitGitGadget
2022-05-27 23:29 ` Junio C Hamano
2022-05-27 21:09 ` [PATCH v3 2/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
` (4 subsequent siblings)
5 siblings, 1 reply; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-05-27 21:09 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
For security reasons, some config variables are only trusted when they
are specified in so-called 'protected configuration' [1]. A future
commit will introduce another such config variable, so this is a good
time to standardize the documentation and implementation of 'protected
configuration'.
Define 'protected configuration' as global and system-level config, and
mark `safe.directory` 'Protected config only'. In a future commit,
protected configuration will also include "-c".
The following variables are intentionally not marked 'Protected config
only':
- `uploadpack.packObjectsHook` has the same security concerns as
`safe.directory`, but due to a different implementation, it also
respects the "-c" option.
When protected configuration includes "-c", `upload.packObjectsHook`
will be marked 'Protected config only'.
- `trace2.*` happens to read the same config as `safe.directory` because
they share an implementation. However, this is not for security
reasons; it is because we want to start tracing so early that
repository-level config and "-c" are not available [2].
This requirement is unique to `trace2.*`, so it does not makes sense
for protected configuration to be subject to the same constraints.
[1] For example,
https://lore.kernel.org/git/6af83767-576b-75c4-c778-0284344a8fe7@github.com/
[2] https://lore.kernel.org/git/a0c89d0d-669e-bf56-25d2-cbb09b012e70@jeffhostetler.com/
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config.txt | 6 ++++++
Documentation/config/safe.txt | 19 ++++++++-----------
Documentation/glossary-content.txt | 18 ++++++++++++++++++
3 files changed, 32 insertions(+), 11 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index e284b042f22..07832de1a6c 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -369,6 +369,12 @@ inventing new variables for use in your own tool, make sure their
names do not conflict with those that are used by Git itself and
other popular tools, and describe them in your documentation.
+Variables marked with '(Protected config only)' are only respected when
+they are specified in protected configuration. This includes global and
+system-level config, and excludes repository config, the command line
+option `-c`, and environment variables. For more details, see the
+'protected configuration' entry in linkgit:gitglossary[7].
+
include::config/advice.txt[]
include::config/core.txt[]
diff --git a/Documentation/config/safe.txt b/Documentation/config/safe.txt
index ae0e2e3bdb4..c1caec460e8 100644
--- a/Documentation/config/safe.txt
+++ b/Documentation/config/safe.txt
@@ -1,21 +1,18 @@
safe.directory::
- These config entries specify Git-tracked directories that are
- considered safe even if they are owned by someone other than the
- current user. By default, Git will refuse to even parse a Git
- config of a repository owned by someone else, let alone run its
- hooks, and this config setting allows users to specify exceptions,
- e.g. for intentionally shared repositories (see the `--shared`
- option in linkgit:git-init[1]).
+ '(Protected config only) ' These config entries specify
+ Git-tracked directories that are considered safe even if they
+ are owned by someone other than the current user. By default,
+ Git will refuse to even parse a Git config of a repository owned
+ by someone else, let alone run its hooks, and this config
+ setting allows users to specify exceptions, e.g. for
+ intentionally shared repositories (see the `--shared` option in
+ linkgit:git-init[1]).
+
This is a multi-valued setting, i.e. you can add more than one directory
via `git config --add`. To reset the list of safe directories (e.g. to
override any such directories specified in the system config), add a
`safe.directory` entry with an empty value.
+
-This config setting is only respected when specified in a system or global
-config, not when it is specified in a repository config, via the command
-line option `-c safe.directory=<path>`, or in environment variables.
-+
The value of this setting is interpolated, i.e. `~/<path>` expands to a
path relative to the home directory and `%(prefix)/<path>` expands to a
path relative to Git's (runtime) prefix.
diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt
index aa2f41f5e70..a669983abd6 100644
--- a/Documentation/glossary-content.txt
+++ b/Documentation/glossary-content.txt
@@ -483,6 +483,24 @@ exclude;;
head ref. If the remote <<def_head,head>> is not an
ancestor to the local head, the push fails.
+[[def_protected_config]]protected configuration::
+ Protected configuration is configuration that Git considers more
+ trustworthy because it is unlikely to be tampered with by an
+ attacker. For security reasons, some configuration variables are
+ only respected when they are defined in protected configuration.
++
+Protected configuration includes:
++
+- system-level config, e.g. `/etc/git/config`
+- global config, e.g. `$XDG_CONFIG_HOME/git/config` and
+ `$HOME/.gitconfig`
++
+Protected configuration excludes:
++
+- repository config, e.g. `$GIT_DIR/config` and
+ `$GIT_DIR/config.worktree`
+- the command line option `-c` and its equivalent environment variables
+
[[def_reachable]]reachable::
All of the ancestors of a given <<def_commit,commit>> are said to be
"reachable" from that commit. More
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 1/5] Documentation: define protected configuration
2022-05-27 21:09 ` [PATCH v3 1/5] Documentation: define protected configuration Glen Choo via GitGitGadget
@ 2022-05-27 23:29 ` Junio C Hamano
2022-06-02 12:42 ` Derrick Stolee
2022-06-03 15:57 ` Glen Choo
0 siblings, 2 replies; 67+ messages in thread
From: Junio C Hamano @ 2022-05-27 23:29 UTC (permalink / raw)
To: Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee,
Emily Shaffer, Glen Choo
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> safe.directory::
> - These config entries specify Git-tracked directories that are
> - considered safe even if they are owned by someone other than the
> - current user. By default, Git will refuse to even parse a Git
> - config of a repository owned by someone else, let alone run its
> - hooks, and this config setting allows users to specify exceptions,
> - e.g. for intentionally shared repositories (see the `--shared`
> - option in linkgit:git-init[1]).
> + '(Protected config only) ' These config entries specify
What's the SP in "only) '" doing?
> diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt
> index aa2f41f5e70..a669983abd6 100644
> --- a/Documentation/glossary-content.txt
> +++ b/Documentation/glossary-content.txt
> @@ -483,6 +483,24 @@ exclude;;
> head ref. If the remote <<def_head,head>> is not an
> ancestor to the local head, the push fails.
>
> +[[def_protected_config]]protected configuration::
> + Protected configuration is configuration that Git considers more
> + trustworthy because it is unlikely to be tampered with by an
> + attacker. For security reasons, some configuration variables are
> + only respected when they are defined in protected configuration.
> ++
> +Protected configuration includes:
> ++
> +- system-level config, e.g. `/etc/git/config`
> +- global config, e.g. `$XDG_CONFIG_HOME/git/config` and
> + `$HOME/.gitconfig`
> +Protected configuration excludes:
> ++
> +- repository config, e.g. `$GIT_DIR/config` and
> + `$GIT_DIR/config.worktree`
> +- the command line option `-c` and its equivalent environment variables
The description is a bit unclear what "protected configuration"
refers.
If it is the scopes (as in "git config --show-scope") Git can trust
more, in other words, a statement like this
safe.directory is honored only when it comes from a protected
configuration.
is what you want to make easier to write by introducing a new
phrase, perhaps use the word "scope" for more consistency? E.g.
Only safe.directory that is defined in a trusted scope is
honored.
I dunno.
It would make sense to give a rationale behind the seemingly
arbitrary choice of what is and what is not "protected". Not
necessarily in the glossary, but in the proposed log message of the
commit that makes the decision. The rationale must help readers to
be able to answer the following questions.
- The system level is "protected" because? Is it because we do not
even try to protect ourselves from those who can write anywhere
in /etc/ or other system directories?
- The per-user config is "protected" because? Is it because our
primary interest in "protection" is to protect individual users
from landmines laid in the filesystem by other users, and those
who can already write into $HOME are not we try to guard against?
- The per-repo config is not "protected" (i.e. "trusted"), because?
If we are not honoring a configuration in the repository, why are
we working in that repository in the first place?
- The per invocation config is not "protected" (i.e. "trusted"),
because? If we cannot trusting our own command line, what
prevents an attacker from mucking with our command line to say
"sudo whatever" using the same attack vector?
Thanks.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 1/5] Documentation: define protected configuration
2022-05-27 23:29 ` Junio C Hamano
@ 2022-06-02 12:42 ` Derrick Stolee
2022-06-02 16:53 ` Junio C Hamano
2022-06-03 15:57 ` Glen Choo
1 sibling, 1 reply; 67+ messages in thread
From: Derrick Stolee @ 2022-06-02 12:42 UTC (permalink / raw)
To: Junio C Hamano, Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Emily Shaffer, Glen Choo
On 5/27/2022 7:29 PM, Junio C Hamano wrote:
> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> +[[def_protected_config]]protected configuration::
>> + Protected configuration is configuration that Git considers more
>> + trustworthy because it is unlikely to be tampered with by an
>> + attacker. For security reasons, some configuration variables are
>> + only respected when they are defined in protected configuration.
>> ++
>> +Protected configuration includes:
>> ++
>> +- system-level config, e.g. `/etc/git/config`
>> +- global config, e.g. `$XDG_CONFIG_HOME/git/config` and
>> + `$HOME/.gitconfig`
>> +Protected configuration excludes:
>> ++
>> +- repository config, e.g. `$GIT_DIR/config` and
>> + `$GIT_DIR/config.worktree`
>> +- the command line option `-c` and its equivalent environment variables
>
> The description is a bit unclear what "protected configuration"
> refers.
>
> If it is the scopes (as in "git config --show-scope") Git can trust
> more, in other words, a statement like this
>
> safe.directory is honored only when it comes from a protected
> configuration.
>
> is what you want to make easier to write by introducing a new
> phrase, perhaps use the word "scope" for more consistency? E.g.
>
> Only safe.directory that is defined in a trusted scope is
> honored.
>
> I dunno.
>
> It would make sense to give a rationale behind the seemingly
> arbitrary choice of what is and what is not "protected". Not
> necessarily in the glossary, but in the proposed log message of the
> commit that makes the decision. The rationale must help readers to
> be able to answer the following questions.
>
> - The system level is "protected" because? Is it because we do not
> even try to protect ourselves from those who can write anywhere
> in /etc/ or other system directories?
>
> - The per-user config is "protected" because? Is it because our
> primary interest in "protection" is to protect individual users
> from landmines laid in the filesystem by other users, and those
> who can already write into $HOME are not we try to guard against?
I think the answers to these two questions is "yes", so they can
be turned into an affirmative sentence:
We do not event try to protect ourselves from those who can
write anywhere...
> - The per-repo config is not "protected" (i.e. "trusted"), because?
> If we are not honoring a configuration in the repository, why are
> we working in that repository in the first place?
This requires an example:
Some workflows use repositories stored in shared directories,
which are writable by multiple unprivileged users.
> - The per invocation config is not "protected" (i.e. "trusted"),
> because? If we cannot trusting our own command line, what
> prevents an attacker from mucking with our command line to say
> "sudo whatever" using the same attack vector?
With this argument, I agree that -c config can be considered
protected. At the very least, it is visible to the user when they
are running a command. This would unify our expectations with
uploadPack.packObjectsHook, too.
Thanks,
-Stolee
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 1/5] Documentation: define protected configuration
2022-06-02 12:42 ` Derrick Stolee
@ 2022-06-02 16:53 ` Junio C Hamano
2022-06-02 17:39 ` Glen Choo
0 siblings, 1 reply; 67+ messages in thread
From: Junio C Hamano @ 2022-06-02 16:53 UTC (permalink / raw)
To: Derrick Stolee
Cc: Glen Choo via GitGitGadget, git, Taylor Blau, brian m. carlson,
Emily Shaffer, Glen Choo
Derrick Stolee <derrickstolee@github.com> writes:
>> It would make sense to give a rationale behind the seemingly
>> arbitrary choice of what is and what is not "protected". Not
>> necessarily in the glossary, but in the proposed log message of the
>> commit that makes the decision. The rationale must help readers to
>> be able to answer the following questions.
>>
>> - The system level is "protected" because? Is it because we do not
>> even try to protect ourselves from those who can write anywhere
>> in /etc/ or other system directories?
>>
>> - The per-user config is "protected" because? Is it because our
>> primary interest in "protection" is to protect individual users
>> from landmines laid in the filesystem by other users, and those
>> who can already write into $HOME are not we try to guard against?
>
> I think the answers to these two questions is "yes", so they can
> be turned into an affirmative sentence:
>
> We do not event try to protect ourselves from those who can
> write anywhere...
s/event/even/.
>
>> - The per-repo config is not "protected" (i.e. "trusted"), because?
>> If we are not honoring a configuration in the repository, why are
>> we working in that repository in the first place?
>
> This requires an example:
>
> Some workflows use repositories stored in shared directories,
> which are writable by multiple unprivileged users.
Hmph, "... and we do not trust these colleagues"? It might be true,
but sounds a bit weak rationale, at least to me. A natural reaction
coming form a devil's advocate naïve me would be "well, then I would
not be directly interacting with such a repository; I'd work in a
clone of it of my own, and pull and push as needed".
Isn't the reason more like "users may go spelunking random places in
the filesystem, with PS1 settings and the like that causes some
"git" command invoked automatically in their current directory, and
we want to protect these users from getting harmed by a random
repository with hostile contents in their configuration and hooks
without even realizing they have wandered into such a repository"?
>> - The per invocation config is not "protected" (i.e. "trusted"),
>> because? If we cannot trusting our own command line, what
>> prevents an attacker from mucking with our command line to say
>> "sudo whatever" using the same attack vector?
>
> With this argument, I agree that -c config can be considered
> protected. At the very least, it is visible to the user when they
> are running a command. This would unify our expectations with
> uploadPack.packObjectsHook, too.
Yup, that matches my understanding.
In any case, I'd prefer to see not just the definition but the
reasoning behind the decision that made some "protected" while
leaving others not-"protected" clearly documented to help users.
Thanks.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 1/5] Documentation: define protected configuration
2022-06-02 16:53 ` Junio C Hamano
@ 2022-06-02 17:39 ` Glen Choo
0 siblings, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-02 17:39 UTC (permalink / raw)
To: Junio C Hamano, Derrick Stolee
Cc: Glen Choo via GitGitGadget, git, Taylor Blau, brian m. carlson,
Emily Shaffer
Junio C Hamano <gitster@pobox.com> writes:
> Derrick Stolee <derrickstolee@github.com> writes:
>>> - The per-repo config is not "protected" (i.e. "trusted"), because?
>>> If we are not honoring a configuration in the repository, why are
>>> we working in that repository in the first place?
>>
>> This requires an example:
>>
>> Some workflows use repositories stored in shared directories,
>> which are writable by multiple unprivileged users.
>
> Isn't the reason more like "users may go spelunking random places in
> the filesystem, with PS1 settings and the like that causes some
> "git" command invoked automatically in their current directory, and
> we want to protect these users from getting harmed by a random
> repository with hostile contents in their configuration and hooks
> without even realizing they have wandered into such a repository"?
Hm, this is my understanding as well, i.e. `safe.directory` is meant to
protect you from shared repositories that you didn't expect, but it lets
you trust the shared repositories that you need (and there is no
protection once you decide to trust the repo).
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 1/5] Documentation: define protected configuration
2022-05-27 23:29 ` Junio C Hamano
2022-06-02 12:42 ` Derrick Stolee
@ 2022-06-03 15:57 ` Glen Choo
1 sibling, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-03 15:57 UTC (permalink / raw)
To: Junio C Hamano, Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee, Emily Shaffer
Junio C Hamano <gitster@pobox.com> writes:
> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> safe.directory::
>> - These config entries specify Git-tracked directories that are
>> - considered safe even if they are owned by someone other than the
>> - current user. By default, Git will refuse to even parse a Git
>> - config of a repository owned by someone else, let alone run its
>> - hooks, and this config setting allows users to specify exceptions,
>> - e.g. for intentionally shared repositories (see the `--shared`
>> - option in linkgit:git-init[1]).
>> + '(Protected config only) ' These config entries specify
>
> What's the SP in "only) '" doing?
Silly typo. Thanks for the catch :)
>> diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt
>> index aa2f41f5e70..a669983abd6 100644
>> --- a/Documentation/glossary-content.txt
>> +++ b/Documentation/glossary-content.txt
>> @@ -483,6 +483,24 @@ exclude;;
>> head ref. If the remote <<def_head,head>> is not an
>> ancestor to the local head, the push fails.
>>
>> +[[def_protected_config]]protected configuration::
>> + Protected configuration is configuration that Git considers more
>> + trustworthy because it is unlikely to be tampered with by an
>> + attacker. For security reasons, some configuration variables are
>> + only respected when they are defined in protected configuration.
>> ++
>> +Protected configuration includes:
>> ++
>> +- system-level config, e.g. `/etc/git/config`
>> +- global config, e.g. `$XDG_CONFIG_HOME/git/config` and
>> + `$HOME/.gitconfig`
>> +Protected configuration excludes:
>> ++
>> +- repository config, e.g. `$GIT_DIR/config` and
>> + `$GIT_DIR/config.worktree`
>> +- the command line option `-c` and its equivalent environment variables
>
> The description is a bit unclear what "protected configuration"
> refers.
>
> If it is the scopes (as in "git config --show-scope") Git can trust
> more, in other words, a statement like this
>
> safe.directory is honored only when it comes from a protected
> configuration.
>
> is what you want to make easier to write by introducing a new
> phrase, perhaps use the word "scope" for more consistency? E.g.
>
> Only safe.directory that is defined in a trusted scope is
> honored.
Good point. I think using scope would be a lot clearer, and maybe I
will consider s/protected configuration/protected scope. I'm hesitant to
call the scope "trusted", because I don't want to insinuate that
repository config is "untrusted" since we _do_ trust it in most cases.
I don't think Documentation/git-config.txt has adequately defined what a
'scope' is though, even though scopes have been with us since 9acc591111
(config: add a notion of "scope", 2016-05-18). The best I could find is
"--show-scope", introduced in 145d59f482 (config: add '--show-scope' to
print the scope of a config value, 2020-02-10), which mentions scopes
but doesn't link the idea back to the specific files or CLI options
("--system", "--global", etc).
So I'll see if I can improve the docs around scopes since that will help
the language in this patch.
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v3 2/5] config: read protected config with `git_protected_config()`
2022-05-27 21:09 ` [PATCH v3 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
2022-05-27 21:09 ` [PATCH v3 1/5] Documentation: define protected configuration Glen Choo via GitGitGadget
@ 2022-05-27 21:09 ` Glen Choo via GitGitGadget
2022-05-28 0:28 ` Junio C Hamano
2022-06-02 12:56 ` Derrick Stolee
2022-05-27 21:09 ` [PATCH v3 3/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
` (3 subsequent siblings)
5 siblings, 2 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-05-27 21:09 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
Protected config is read using `read_very_early_config()`, which has
several downsides:
- Every call to `read_very_early_config()` parses global and
system-level config files anew, but this can be optimized by just
parsing them once [1].
- Protected variables should respect "-c" because we can reasonably
assume that it comes from the user. But, `read_very_early_config()`
can't use "-c" because it is called so early that it does not have
access to command line arguments.
Introduce `git_protected_config()`, which reads protected config and
caches the values in `the_repository.protected_config`. Then, refactor
`safe.directory` to use `git_protected_config()`.
This implementation can still be improved, however:
- `git_protected_config()` iterates through every variable in
`the_repository.protected_config`, which may still be too expensive to
be called in every "git" invocation. There exist constant time lookup
functions for non-protected config (repo_config_get_*()), but for
simplicity, this commit does not implement similar functions for
protected config.
- Protected config is stored in `the_repository` so that we don't need
to statically allocate it. But this might be confusing since protected
config ignores repository config by definition.
[1] While `git_protected_config()` should save on file I/O, I wasn't
able to measure a meaningful difference between that and
`read_very_early_config()` on my machine (which has an SSD).
Signed-off-by: Glen Choo <chooglen@google.com>
---
config.c | 35 +++++++++++++++++++++++++++++++++++
config.h | 8 ++++++++
repository.c | 5 +++++
repository.h | 8 ++++++++
setup.c | 2 +-
5 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/config.c b/config.c
index fa471dbdb89..c30bb7c5d09 100644
--- a/config.c
+++ b/config.c
@@ -2614,6 +2614,41 @@ int repo_config_get_pathname(struct repository *repo,
return ret;
}
+/* Read protected config into the_repository->protected_config. */
+static void read_protected_config(void)
+{
+ char *xdg_config = NULL, *user_config = NULL, *system_config = NULL;
+
+ CALLOC_ARRAY(the_repository->protected_config, 1);
+ git_configset_init(the_repository->protected_config);
+
+ system_config = git_system_config();
+ git_global_config(&user_config, &xdg_config);
+
+ git_configset_add_file(the_repository->protected_config, system_config);
+ git_configset_add_file(the_repository->protected_config, xdg_config);
+ git_configset_add_file(the_repository->protected_config, user_config);
+
+ free(system_config);
+ free(xdg_config);
+ free(user_config);
+}
+
+/* Ensure that the_repository->protected_config has been initialized. */
+static void git_protected_config_check_init(void)
+{
+ if (the_repository->protected_config &&
+ the_repository->protected_config->hash_initialized)
+ return;
+ read_protected_config();
+}
+
+void git_protected_config(config_fn_t fn, void *data)
+{
+ git_protected_config_check_init();
+ configset_iter(the_repository->protected_config, fn, data);
+}
+
/* Functions used historically to read configuration from 'the_repository' */
void git_config(config_fn_t fn, void *data)
{
diff --git a/config.h b/config.h
index 7654f61c634..411965f52b5 100644
--- a/config.h
+++ b/config.h
@@ -505,6 +505,14 @@ int repo_config_get_maybe_bool(struct repository *repo,
int repo_config_get_pathname(struct repository *repo,
const char *key, const char **dest);
+/*
+ * Functions for reading protected config. By definition, protected
+ * config ignores repository config, so it is unnecessary to read
+ * protected config from any `struct repository` other than
+ * the_repository.
+ */
+void git_protected_config(config_fn_t fn, void *data);
+
/**
* Querying For Specific Variables
* -------------------------------
diff --git a/repository.c b/repository.c
index 5d166b692c8..ec319a5e09a 100644
--- a/repository.c
+++ b/repository.c
@@ -295,6 +295,11 @@ void repo_clear(struct repository *repo)
FREE_AND_NULL(repo->remote_state);
}
+ if (repo->protected_config) {
+ git_configset_clear(repo->protected_config);
+ FREE_AND_NULL(repo->protected_config);
+ }
+
repo_clear_path_cache(&repo->cached_paths);
}
diff --git a/repository.h b/repository.h
index 6cc661e5a43..24251aac553 100644
--- a/repository.h
+++ b/repository.h
@@ -126,6 +126,14 @@ struct repository {
struct repo_settings settings;
+ /*
+ * Config that comes from trusted sources, namely
+ * - system config files (e.g. /etc/gitconfig)
+ * - global config files (e.g. $HOME/.gitconfig,
+ * $XDG_CONFIG_HOME/git)
+ */
+ struct config_set *protected_config;
+
/* Subsystems */
/*
* Repository's config which contains key-value pairs from the usual
diff --git a/setup.c b/setup.c
index f818dd858c6..847d47f9195 100644
--- a/setup.c
+++ b/setup.c
@@ -1128,7 +1128,7 @@ static int ensure_valid_ownership(const char *path)
is_path_owned_by_current_user(path))
return 1;
- read_very_early_config(safe_directory_cb, &data);
+ git_protected_config(safe_directory_cb, &data);
return data.is_safe;
}
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 2/5] config: read protected config with `git_protected_config()`
2022-05-27 21:09 ` [PATCH v3 2/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
@ 2022-05-28 0:28 ` Junio C Hamano
2022-05-31 17:43 ` Glen Choo
2022-06-02 12:56 ` Derrick Stolee
1 sibling, 1 reply; 67+ messages in thread
From: Junio C Hamano @ 2022-05-28 0:28 UTC (permalink / raw)
To: Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee,
Emily Shaffer, Glen Choo
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
>
> Protected config is read using `read_very_early_config()`, which has
> several downsides:
>
> - Every call to `read_very_early_config()` parses global and
> system-level config files anew, but this can be optimized by just
> parsing them once [1].
> - Protected variables should respect "-c" because we can reasonably
> assume that it comes from the user. But, `read_very_early_config()`
> can't use "-c" because it is called so early that it does not have
> access to command line arguments.
Now we are talking about protected "variable". Is that a synonym
for "config", or are there some distinctions between them?
> - Protected config is stored in `the_repository` so that we don't need
> to statically allocate it. But this might be confusing since protected
> config ignores repository config by definition.
Yes, it indeed is. Is it because we were over-eager when we
introduced the "struct repository *repo" parameter to many functions
and the configuration system wants you to have some repository, even
when you know you are not reading from any repository?
I am wondering if it is a cleaner solution *not* to hang the
protected config as a configset in the_repository, but keep the
configset as a separate global variable, perhaps static to config.c
and is meant to be only accessed via git_protected_config() and the
like.
> @@ -295,6 +295,11 @@ void repo_clear(struct repository *repo)
> FREE_AND_NULL(repo->remote_state);
> }
>
> + if (repo->protected_config) {
> + git_configset_clear(repo->protected_config);
> + FREE_AND_NULL(repo->protected_config);
> + }
> +
This becomes necessary only because each repository instance has
protected_config, even though we need only one instance, no matter
how many repositories we are accessing in this single invocation of
Git, no?
How should "git config -l" interact with "protected config" and
"protected variables", by the way? Should a user be able to tell
which ones are coming from protected scope? Should we gain, next to
--global, --system, etc., --protected option to list only the
protected config/variable?
This is another thing that I find iffy on terminology. Should a
random variable, like user.name, be a "protected config", if it is
found in $HOME/.gitconfig? If it comes from there, surely we can
trust its value, but unlike things like safe.directory, there is no
code that wants to enforce that we pay attention only to user.name
that came from trusted scopes. Should such a variable be called
"protected variable"?
Thanks.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 2/5] config: read protected config with `git_protected_config()`
2022-05-28 0:28 ` Junio C Hamano
@ 2022-05-31 17:43 ` Glen Choo
2022-06-01 15:58 ` Junio C Hamano
0 siblings, 1 reply; 67+ messages in thread
From: Glen Choo @ 2022-05-31 17:43 UTC (permalink / raw)
To: Junio C Hamano, Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee, Emily Shaffer
Junio C Hamano <gitster@pobox.com> writes:
>> Protected config is read using `read_very_early_config()`, which has
>> several downsides:
>>
>> - Every call to `read_very_early_config()` parses global and
>> system-level config files anew, but this can be optimized by just
>> parsing them once [1].
>> - Protected variables should respect "-c" because we can reasonably
>> assume that it comes from the user. But, `read_very_early_config()`
>> can't use "-c" because it is called so early that it does not have
>> access to command line arguments.
>
> Now we are talking about protected "variable". Is that a synonym
> for "config", or are there some distinctions between them?
Sorry, that's an old term I was toying with (this somehow snuck through
my proofreading). I just meant "variable that is only read from
protected config", aka a "protected config only variable".
A goal in this version was to introduce as little jargon as possible, so
- "protected config" refers to the set of config sources, and
- "protected config only" refers to config variables/settings that are
only read from protected config.
>> - Protected config is stored in `the_repository` so that we don't need
>> to statically allocate it. But this might be confusing since protected
>> config ignores repository config by definition.
>
> Yes, it indeed is. Is it because we were over-eager when we
> introduced the "struct repository *repo" parameter to many functions
> and the configuration system wants you to have some repository, even
> when you know you are not reading from any repository?
Ah no, I was just trying to avoid yet-another global variable (since
IIRC we want to move towards a more lib-like Git), and the_repository
was a convenient global variable to (ab)use.
> I am wondering if it is a cleaner solution *not* to hang the
> protected config as a configset in the_repository, but keep the
> configset as a separate global variable, perhaps static to config.c
> and is meant to be only accessed via git_protected_config() and the
> like.
I think your suggestion to use a global variable is better, as much as I
want to avoid another global variable. Protected config would affect any
repositories that we work with in-core, so using a global sounds ok.
environment.c might be a better place since we already make a concerted
effort to put global config variables there instead of config.c.
As an aside, I wonder how we could get rid of all of the globals in
environment.c in the long term. Maybe we would have yet-another all
encompassing global, the_environment, and then figure out which
variables belong to the repository and which belong to the environment.
>> @@ -295,6 +295,11 @@ void repo_clear(struct repository *repo)
>> FREE_AND_NULL(repo->remote_state);
>> }
>>
>> + if (repo->protected_config) {
>> + git_configset_clear(repo->protected_config);
>> + FREE_AND_NULL(repo->protected_config);
>> + }
>> +
>
> This becomes necessary only because each repository instance has
> protected_config, even though we need only one instance, no matter
> how many repositories we are accessing in this single invocation of
> Git, no?
Yes.
> How should "git config -l" interact with "protected config" and
> "protected variables", by the way? Should a user be able to tell
> which ones are coming from protected scope? Should we gain, next to
> --global, --system, etc., --protected option to list only the
> protected config/variable?
I'll have to think about this some more. My initial thoughts are that we
should do this if we formalize 'protected' as a scope-like concept, but
I don't see the lack of "--protected" as a significant hindrance to
users because they can use "--global" and "--system" (albeit in two
invocations instead of one).
> This is another thing that I find iffy on terminology. Should a
> random variable, like user.name, be a "protected config", if it is
> found in $HOME/.gitconfig? If it comes from there, surely we can
> trust its value, but unlike things like safe.directory, there is no
> code that wants to enforce that we pay attention only to user.name
> that came from trusted scopes. Should such a variable be called
> "protected variable"?
Ah.. I think it would be best to pretend that the "Protected variable"
typo never happened. That term was destined to be confusing and
meaningless.
Instead, we can use "protected config" to refer to the config and
"protected config only" to refer to variables. Since "protected config"
is defined as (global + system + CLI) config, then yes, we would say
that it is "protected config". But since we do not enforce that
"user.name" _must_ come from only protected config, it is not "protected
config only".
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 2/5] config: read protected config with `git_protected_config()`
2022-05-31 17:43 ` Glen Choo
@ 2022-06-01 15:58 ` Junio C Hamano
0 siblings, 0 replies; 67+ messages in thread
From: Junio C Hamano @ 2022-06-01 15:58 UTC (permalink / raw)
To: Glen Choo
Cc: Glen Choo via GitGitGadget, git, Taylor Blau, brian m. carlson,
Derrick Stolee, Emily Shaffer
Glen Choo <chooglen@google.com> writes:
> A goal in this version was to introduce as little jargon as possible, so
> - "protected config" refers to the set of config sources, and
> - "protected config only" refers to config variables/settings that are
> only read from protected config.
OK. Let's have such a clear pair of definitions somewhere in the
doc or at least in a proposed log message.
>
>>> - Protected config is stored in `the_repository` so that we don't need
>>> to statically allocate it. But this might be confusing since protected
>>> config ignores repository config by definition.
>>
>> Yes, it indeed is. Is it because we were over-eager when we
>> introduced the "struct repository *repo" parameter to many functions
>> and the configuration system wants you to have some repository, even
>> when you know you are not reading from any repository?
>
> Ah no, I was just trying to avoid yet-another global variable (since
> IIRC we want to move towards a more lib-like Git), and the_repository
> was a convenient global variable to (ab)use.
If this does not have to be known only inside config.c, until we
introduce a more global bag of things, which may have the current
the_repository as one of its components, I do not think it hurts to
have a file-scope static there. Then, perhaps git_configset_get*()
helper functions can recognize cs==NULL as a sign that the caller
wants to grab from the "protected config", or something? If we do
not want to expose the underying global variable to the public, that
is.
> As an aside, I wonder how we could get rid of all of the globals in
> environment.c in the long term. Maybe we would have yet-another all
> encompassing global, the_environment, and then figure out which
> variables belong to the repository and which belong to the environment.
I think we are on the same page, we'd probably need something called
the_world ;-)
> Instead, we can use "protected config" to refer to the config and
> "protected config only" to refer to variables. Since "protected config"
> is defined as (global + system + CLI) config, then yes, we would say
> that it is "protected config". But since we do not enforce that
> "user.name" _must_ come from only protected config, it is not "protected
> config only".
Very clear. Thanks.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 2/5] config: read protected config with `git_protected_config()`
2022-05-27 21:09 ` [PATCH v3 2/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
2022-05-28 0:28 ` Junio C Hamano
@ 2022-06-02 12:56 ` Derrick Stolee
1 sibling, 0 replies; 67+ messages in thread
From: Derrick Stolee @ 2022-06-02 12:56 UTC (permalink / raw)
To: Glen Choo via GitGitGadget, git
Cc: Taylor Blau, brian m. carlson, Junio C Hamano, Emily Shaffer, Glen Choo
On 5/27/2022 5:09 PM, Glen Choo via GitGitGadget wrote:
> From: Glen Choo <chooglen@google.com>
>
> Protected config is read using `read_very_early_config()`, which has
> several downsides:
>
> - Every call to `read_very_early_config()` parses global and
> system-level config files anew, but this can be optimized by just
> parsing them once [1].
> - Protected variables should respect "-c" because we can reasonably
> assume that it comes from the user. But, `read_very_early_config()`
> can't use "-c" because it is called so early that it does not have
> access to command line arguments.
>
> Introduce `git_protected_config()`, which reads protected config and
> caches the values in `the_repository.protected_config`. Then, refactor
> `safe.directory` to use `git_protected_config()`.
>
> This implementation can still be improved, however:
>
> - `git_protected_config()` iterates through every variable in
> `the_repository.protected_config`, which may still be too expensive to
> be called in every "git" invocation. There exist constant time lookup
> functions for non-protected config (repo_config_get_*()), but for
> simplicity, this commit does not implement similar functions for
> protected config.
I originally thought that we should jump to that "right" solution, but
the existing logic in ensure_valid_ownership() uses the iterator method,
mostly because it uses a multi-valued string. There are helpers that
allow iterating over a specific multi-valued key, but there is no reason
to complicate the current patch with that amount of refactoring. That
can be handled as a completely separate topic.
> - Protected config is stored in `the_repository` so that we don't need
> to statically allocate it. But this might be confusing since protected
> config ignores repository config by definition.
I agree with Junio's suggestion of keeping this as a static global in
config.c, accessible only by the public methods from config.h. A future
where we have "the_world" might be nice for inventory on all these
globals. Definitely not something to hold up this series.
> +/* Read protected config into the_repository->protected_config. */
> +static void read_protected_config(void)
> +{
> + char *xdg_config = NULL, *user_config = NULL, *system_config = NULL;
> +
> + CALLOC_ARRAY(the_repository->protected_config, 1);
> + git_configset_init(the_repository->protected_config);
> +
> + system_config = git_system_config();
> + git_global_config(&user_config, &xdg_config);
> +
> + git_configset_add_file(the_repository->protected_config, system_config);
> + git_configset_add_file(the_repository->protected_config, xdg_config);
> + git_configset_add_file(the_repository->protected_config, user_config);
> +
> + free(system_config);
> + free(xdg_config);
> + free(user_config);
> +}
This loads the config from three files, including the xdg_config, which
I wasn't thinking about before.
This implementation does not use the -c config yet, which you listed as
a downside of read_very_early_config(). I see that you include that in
your patch 4, but the commit message for this patch could list that as a
step that will be handled by a later change.
(You could also do that as patch 3 and add a test near the existing
safe.directory tests instead of waiting for discovery.bare.)
> +
> +/* Ensure that the_repository->protected_config has been initialized. */
> +static void git_protected_config_check_init(void)
> +{
> + if (the_repository->protected_config &&
> + the_repository->protected_config->hash_initialized)
> + return;
> + read_protected_config();
> +}
> +
> +void git_protected_config(config_fn_t fn, void *data)
> +{
> + git_protected_config_check_init();
> + configset_iter(the_repository->protected_config, fn, data);
> +}
These two methods are clearly correct.
..._check_init() is an OK name. I've seen us use "prepare_...()" in
other areas as a way of making sure that we have the proper state
(see prepare_packed_git() and the like), so maybe a rename here to
match would be worthwhile. Feel free to ignore.
> + if (repo->protected_config) {
> + git_configset_clear(repo->protected_config);
> + FREE_AND_NULL(repo->protected_config);
> + }
This will have no equivalent when protected_config is left as a
static global, but that is fine. It only goes out of scope with
the end of the process, anyway.
> @@ -1128,7 +1128,7 @@ static int ensure_valid_ownership(const char *path)
> is_path_owned_by_current_user(path))
> return 1;
>
> - read_very_early_config(safe_directory_cb, &data);
> + git_protected_config(safe_directory_cb, &data);
Nice to have a very simple conversion here.
Thanks,
-Stolee
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v3 3/5] setup.c: create `discovery.bare`
2022-05-27 21:09 ` [PATCH v3 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
2022-05-27 21:09 ` [PATCH v3 1/5] Documentation: define protected configuration Glen Choo via GitGitGadget
2022-05-27 21:09 ` [PATCH v3 2/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
@ 2022-05-27 21:09 ` Glen Choo via GitGitGadget
2022-05-28 0:59 ` Junio C Hamano
2022-06-02 13:11 ` Derrick Stolee
2022-05-27 21:09 ` [PATCH v3 4/5] config: include "-c" in protected config Glen Choo via GitGitGadget
` (2 subsequent siblings)
5 siblings, 2 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-05-27 21:09 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
There is a known social engineering attack that takes advantage of the
fact that a working tree can include an entire bare repository,
including a config file. A user could run a Git command inside the bare
repository thinking that the config file of the 'outer' repository would
be used, but in reality, the bare repository's config file (which is
attacker-controlled) is used, which may result in arbitrary code
execution. See [1] for a fuller description and deeper discussion.
A simple mitigation is to forbid bare repositories unless specified via
`--git-dir` or `GIT_DIR`. In environments that don't use bare
repositories, this would be minimally disruptive.
Create a config variable, `discovery.bare`, that tells Git whether or
not to die() when it discovers a bare repository. This only affects
repository discovery, thus it has no effect if discovery was not
done (e.g. `--git-dir` was passed).
This config is an enum of:
- "always": always allow bare repositories (this is the default)
- "never": never allow bare repositories
If we want to protect users from such attacks by default, neither value
will suffice - "always" provides no protection, but "never" is
impractical for bare repository users. A more usable default would be to
allow only non-embedded bare repositories ([2] contains one such
proposal), but detecting if a repository is embedded is potentially
non-trivial, so this work is not implemented in this series.
[1]: https://lore.kernel.org/git/kl6lsfqpygsj.fsf@chooglen-macbookpro.roam.corp.google.com
[2]: https://lore.kernel.org/git/5b969c5e-e802-c447-ad25-6acc0b784582@github.com
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config.txt | 2 +
Documentation/config/discovery.txt | 19 +++++++++
setup.c | 66 +++++++++++++++++++++++++++++-
t/t0035-discovery-bare.sh | 64 +++++++++++++++++++++++++++++
4 files changed, 150 insertions(+), 1 deletion(-)
create mode 100644 Documentation/config/discovery.txt
create mode 100755 t/t0035-discovery-bare.sh
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 07832de1a6c..34133288d75 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -415,6 +415,8 @@ include::config/diff.txt[]
include::config/difftool.txt[]
+include::config/discovery.txt[]
+
include::config/extensions.txt[]
include::config/fastimport.txt[]
diff --git a/Documentation/config/discovery.txt b/Documentation/config/discovery.txt
new file mode 100644
index 00000000000..fbe93597e7c
--- /dev/null
+++ b/Documentation/config/discovery.txt
@@ -0,0 +1,19 @@
+discovery.bare::
+ '(Protected config only)' Specifies whether Git will work with a
+ bare repository that it found during repository discovery. This
+ has no effect if the repository is specified directly via the
+ --git-dir command-line option or the GIT_DIR environment
+ variable (see linkgit:git[1]).
++
+The currently supported values are:
++
+* `always`: Git always works with bare repositories
+* `never`: Git never works with bare repositories
++
+This defaults to `always`, but this default may change in the future.
++
+If you do not use bare repositories in your workflow, then it may be
+beneficial to set `discovery.bare` to `never` in your global config.
+This will protect you from attacks that involve cloning a repository
+that contains a bare repository and running a Git command within that
+directory.
diff --git a/setup.c b/setup.c
index 847d47f9195..6686743ab7d 100644
--- a/setup.c
+++ b/setup.c
@@ -10,6 +10,13 @@
static int inside_git_dir = -1;
static int inside_work_tree = -1;
static int work_tree_config_is_bogus;
+enum discovery_bare_config {
+ DISCOVERY_BARE_UNKNOWN = -1,
+ DISCOVERY_BARE_NEVER = 0,
+ DISCOVERY_BARE_ALWAYS,
+};
+static enum discovery_bare_config discovery_bare_config =
+ DISCOVERY_BARE_UNKNOWN;
static struct startup_info the_startup_info;
struct startup_info *startup_info = &the_startup_info;
@@ -1133,6 +1140,52 @@ static int ensure_valid_ownership(const char *path)
return data.is_safe;
}
+static int discovery_bare_cb(const char *key, const char *value, void *d)
+{
+ if (strcmp(key, "discovery.bare"))
+ return 0;
+
+ if (!strcmp(value, "never")) {
+ discovery_bare_config = DISCOVERY_BARE_NEVER;
+ return 0;
+ }
+ if (!strcmp(value, "always")) {
+ discovery_bare_config = DISCOVERY_BARE_ALWAYS;
+ return 0;
+ }
+ return -1;
+}
+
+static int check_bare_repo_allowed(void)
+{
+ if (discovery_bare_config == DISCOVERY_BARE_UNKNOWN) {
+ discovery_bare_config = DISCOVERY_BARE_ALWAYS;
+ git_protected_config(discovery_bare_cb, NULL);
+ }
+ switch (discovery_bare_config) {
+ case DISCOVERY_BARE_NEVER:
+ return 0;
+ case DISCOVERY_BARE_ALWAYS:
+ return 1;
+ case DISCOVERY_BARE_UNKNOWN:
+ BUG("invalid discovery_bare_config %d", discovery_bare_config);
+ }
+ return 0;
+}
+
+static const char *discovery_bare_config_to_string(void)
+{
+ switch (discovery_bare_config) {
+ case DISCOVERY_BARE_NEVER:
+ return "never";
+ case DISCOVERY_BARE_ALWAYS:
+ return "always";
+ case DISCOVERY_BARE_UNKNOWN:
+ BUG("invalid discovery_bare_config %d", discovery_bare_config);
+ }
+ return NULL;
+}
+
enum discovery_result {
GIT_DIR_NONE = 0,
GIT_DIR_EXPLICIT,
@@ -1142,7 +1195,8 @@ enum discovery_result {
GIT_DIR_HIT_CEILING = -1,
GIT_DIR_HIT_MOUNT_POINT = -2,
GIT_DIR_INVALID_GITFILE = -3,
- GIT_DIR_INVALID_OWNERSHIP = -4
+ GIT_DIR_INVALID_OWNERSHIP = -4,
+ GIT_DIR_DISALLOWED_BARE = -5,
};
/*
@@ -1239,6 +1293,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
}
if (is_git_directory(dir->buf)) {
+ if (!check_bare_repo_allowed())
+ return GIT_DIR_DISALLOWED_BARE;
if (!ensure_valid_ownership(dir->buf))
return GIT_DIR_INVALID_OWNERSHIP;
strbuf_addstr(gitdir, ".");
@@ -1385,6 +1441,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
}
*nongit_ok = 1;
break;
+ case GIT_DIR_DISALLOWED_BARE:
+ if (!nongit_ok) {
+ die(_("cannot use bare repository '%s' (discovery.bare is '%s')"),
+ dir.buf,
+ discovery_bare_config_to_string());
+ }
+ *nongit_ok = 1;
+ break;
case GIT_DIR_NONE:
/*
* As a safeguard against setup_git_directory_gently_1 returning
diff --git a/t/t0035-discovery-bare.sh b/t/t0035-discovery-bare.sh
new file mode 100755
index 00000000000..94c2f76d774
--- /dev/null
+++ b/t/t0035-discovery-bare.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+test_description='verify discovery.bare checks'
+
+. ./test-lib.sh
+
+pwd="$(pwd)"
+
+expect_rejected () {
+ test_must_fail git rev-parse --git-dir 2>err &&
+ grep "discovery.bare" err
+}
+
+test_expect_success 'setup bare repo in worktree' '
+ git init outer-repo &&
+ git init --bare outer-repo/bare-repo
+'
+
+test_expect_success 'discovery.bare unset' '
+ (
+ cd outer-repo/bare-repo &&
+ git rev-parse --git-dir
+ )
+'
+
+test_expect_success 'discovery.bare=always' '
+ git config --global discovery.bare always &&
+ (
+ cd outer-repo/bare-repo &&
+ git rev-parse --git-dir
+ )
+'
+
+test_expect_success 'discovery.bare=never' '
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_rejected
+ )
+'
+
+test_expect_success 'discovery.bare in the repository' '
+ (
+ cd outer-repo/bare-repo &&
+ # Temporarily set discovery.bare=always, otherwise git
+ # config fails with "fatal: not in a git directory"
+ # (like safe.directory)
+ git config --global discovery.bare always &&
+ git config discovery.bare always &&
+ git config --global discovery.bare never &&
+ expect_rejected
+ )
+'
+
+test_expect_success 'discovery.bare on the command line' '
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
+ test_must_fail git -c discovery.bare=always rev-parse --git-dir 2>err &&
+ grep "discovery.bare" err
+ )
+'
+
+test_done
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 3/5] setup.c: create `discovery.bare`
2022-05-27 21:09 ` [PATCH v3 3/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
@ 2022-05-28 0:59 ` Junio C Hamano
2022-06-02 13:11 ` Derrick Stolee
1 sibling, 0 replies; 67+ messages in thread
From: Junio C Hamano @ 2022-05-28 0:59 UTC (permalink / raw)
To: Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee,
Emily Shaffer, Glen Choo
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> +enum discovery_bare_config {
> + DISCOVERY_BARE_UNKNOWN = -1,
> + DISCOVERY_BARE_NEVER = 0,
> + DISCOVERY_BARE_ALWAYS,
> +};
> +static enum discovery_bare_config discovery_bare_config =
> + DISCOVERY_BARE_UNKNOWN;
Can discovery_bare come from anywhere other than config?
I am wondering if both the variable and the type should be called
"discovery_bare_allowed" instead. That it comes from the config is
not the more important part. That it determines if it is allowed
is.
> +static int check_bare_repo_allowed(void)
> +{
> + if (discovery_bare_config == DISCOVERY_BARE_UNKNOWN) {
> + discovery_bare_config = DISCOVERY_BARE_ALWAYS;
> + git_protected_config(discovery_bare_cb, NULL);
> + }
OK, so the thing is initialized to "unknown", and the first time we
want to use the value of it, we read from the file (or default to
"always"). Makes sense.
And then ...
> + switch (discovery_bare_config) {
> + case DISCOVERY_BARE_NEVER:
> + return 0;
> + case DISCOVERY_BARE_ALWAYS:
> + return 1;
> + case DISCOVERY_BARE_UNKNOWN:
> + BUG("invalid discovery_bare_config %d", discovery_bare_config);
... this is being defensive; we know discovery_bare_cb() won't give
UNKNOWN, but we want to make sure.
> + }
> + return 0;
> +}
> +
> +static const char *discovery_bare_config_to_string(void)
> +{
But this one feels strangely asymmetrical, as there is no inherent
reason why one must be called before the other. I would expect it
to either
* take a parameter of type "enum discovery_bare" and return
"never", "always", or "unset", without calling any BUG().
or
* have the same "we lazily figure out the discovery_bare_config
variable on demand" logic.
As both of these functions are file-scope static, we can live with
it, though.
> + switch (discovery_bare_config) {
> + case DISCOVERY_BARE_NEVER:
> + return "never";
> + case DISCOVERY_BARE_ALWAYS:
> + return "always";
> + case DISCOVERY_BARE_UNKNOWN:
> + BUG("invalid discovery_bare_config %d", discovery_bare_config);
> + }
> + return NULL;
> +}
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 3/5] setup.c: create `discovery.bare`
2022-05-27 21:09 ` [PATCH v3 3/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
2022-05-28 0:59 ` Junio C Hamano
@ 2022-06-02 13:11 ` Derrick Stolee
1 sibling, 0 replies; 67+ messages in thread
From: Derrick Stolee @ 2022-06-02 13:11 UTC (permalink / raw)
To: Glen Choo via GitGitGadget, git
Cc: Taylor Blau, brian m. carlson, Junio C Hamano, Emily Shaffer, Glen Choo
On 5/27/2022 5:09 PM, Glen Choo via GitGitGadget wrote:
> +enum discovery_bare_config {
> + DISCOVERY_BARE_UNKNOWN = -1,
> + DISCOVERY_BARE_NEVER = 0,
> + DISCOVERY_BARE_ALWAYS,
> +};
> +static enum discovery_bare_config discovery_bare_config =
> + DISCOVERY_BARE_UNKNOWN;
Using this static global is fine, I think.
> +static int discovery_bare_cb(const char *key, const char *value, void *d)
> +{
> + if (strcmp(key, "discovery.bare"))
> + return 0;
> +
> + if (!strcmp(value, "never")) {
> + discovery_bare_config = DISCOVERY_BARE_NEVER;
> + return 0;
> + }
> + if (!strcmp(value, "always")) {
> + discovery_bare_config = DISCOVERY_BARE_ALWAYS;
> + return 0;
> + }
> + return -1;
> +}
However, I do think that this _cb method could benefit from interpreting
the 'd' pointer as a 'enum discovery_bare_config *' and assigning the
value at the pointer. We can then pass the global to the
git_protected_config() call below.
This is probably over-defensive future-proofing, but this kind of change
would be necessary if we ever wanted to return the enum instead of
simply an integer, as below:
> +
> +static int check_bare_repo_allowed(void)
> +{
> + if (discovery_bare_config == DISCOVERY_BARE_UNKNOWN) {
> + discovery_bare_config = DISCOVERY_BARE_ALWAYS;
> + git_protected_config(discovery_bare_cb, NULL);
> + }
> + switch (discovery_bare_config) {
> + case DISCOVERY_BARE_NEVER:
> + return 0;
> + case DISCOVERY_BARE_ALWAYS:
> + return 1;
> + case DISCOVERY_BARE_UNKNOWN:
> + BUG("invalid discovery_bare_config %d", discovery_bare_config);
> + }
> + return 0;
> +}
With the recommended change to the _cb method, we could rewrite this as
static enum discovery_bare_config get_discovery_bare(void)
{
enum discovery_bare_config result = DISCOVERY_BARE_ALWAYS;
git_protected_config(discovery_bare_cb, &result);
return result;
}
With this, we can drop the UNKNOWN and let the caller treat the response
as a simple boolean.
I think this is simpler overall, but also makes it easier to extend in the
future to have "discovery.bare=non-embedded" by adding a new mode and
adjusting the consumer in setup_git_directory_gently_1() to use a switch()
on the resurned enum.
> +
> +static const char *discovery_bare_config_to_string(void)
> +{
> + switch (discovery_bare_config) {
> + case DISCOVERY_BARE_NEVER:
> + return "never";
> + case DISCOVERY_BARE_ALWAYS:
> + return "always";
> + case DISCOVERY_BARE_UNKNOWN:
> + BUG("invalid discovery_bare_config %d", discovery_bare_config);
This case should be a "default:" in case somehow an arbitrary integer
value was placed in the variable. This could also take an enum as a
parameter, to avoid being coupled to the global.
> +++ b/t/t0035-discovery-bare.sh
> @@ -0,0 +1,64 @@
> +#!/bin/sh
> +
> +test_description='verify discovery.bare checks'
> +
> +. ./test-lib.sh
> +
> +pwd="$(pwd)"
> +
> +expect_rejected () {
> + test_must_fail git rev-parse --git-dir 2>err &&
> + grep "discovery.bare" err
> +}
Should we make a simple "expect_accepted" helper in case we ever
want to replace the "git rev-parse --git-dir" with anything else?
> +
> +test_expect_success 'setup bare repo in worktree' '
> + git init outer-repo &&
> + git init --bare outer-repo/bare-repo
> +'
> +
> +test_expect_success 'discovery.bare unset' '
> + (
> + cd outer-repo/bare-repo &&
> + git rev-parse --git-dir
> + )
> +'
> +
> +test_expect_success 'discovery.bare=always' '
> + git config --global discovery.bare always &&
> + (
> + cd outer-repo/bare-repo &&
> + git rev-parse --git-dir
> + )
> +'
> +
> +test_expect_success 'discovery.bare=never' '
> + git config --global discovery.bare never &&
> + (
> + cd outer-repo/bare-repo &&
> + expect_rejected
> + )
> +'
> +
> +test_expect_success 'discovery.bare in the repository' '
> + (
> + cd outer-repo/bare-repo &&
> + # Temporarily set discovery.bare=always, otherwise git
> + # config fails with "fatal: not in a git directory"
> + # (like safe.directory)
> + git config --global discovery.bare always &&
> + git config discovery.bare always &&
> + git config --global discovery.bare never &&
> + expect_rejected
> + )
> +'
> +
> +test_expect_success 'discovery.bare on the command line' '
> + git config --global discovery.bare never &&> + (
> + cd outer-repo/bare-repo &&
> + test_must_fail git -c discovery.bare=always rev-parse --git-dir 2>err &&
> + grep "discovery.bare" err
> + )
Ok, at the current place in the series, this test_must_fail matches
expectation. If you reorder to have this patch after your current patch 4,
then we can write this test immediately as a successful case.
We could also reuse some information from the expect_rejected helper by
adding this:
expect_rejected () {
test_must_fail git $* rev-parse --git-dir 2>err &&
grep "discovery.bare" err
}
Then you can test the -c options in the tests as
expect_rejected -c discovery.bare=always
Thanks,
-Stolee
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v3 4/5] config: include "-c" in protected config
2022-05-27 21:09 ` [PATCH v3 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (2 preceding siblings ...)
2022-05-27 21:09 ` [PATCH v3 3/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
@ 2022-05-27 21:09 ` Glen Choo via GitGitGadget
2022-06-02 13:15 ` Derrick Stolee
2022-05-27 21:09 ` [PATCH v3 5/5] upload-pack: make uploadpack.packObjectsHook protected Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
5 siblings, 1 reply; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-05-27 21:09 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
Protected config should include the command line (aka "-c") because we
can be quite certain that this config is specified by the user.
Introduce a function, `git_configset_add_parameters()`, that adds "-c"
config to a config_set, and use it to add "-c" to protected config.
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config.txt | 6 +++---
Documentation/glossary-content.txt | 2 +-
config.c | 6 ++++++
config.h | 9 +++++++++
t/t0033-safe-directory.sh | 24 ++++++++++--------------
t/t0035-discovery-bare.sh | 3 +--
6 files changed, 30 insertions(+), 20 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 34133288d75..f40a3e297ce 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -370,9 +370,9 @@ names do not conflict with those that are used by Git itself and
other popular tools, and describe them in your documentation.
Variables marked with '(Protected config only)' are only respected when
-they are specified in protected configuration. This includes global and
-system-level config, and excludes repository config, the command line
-option `-c`, and environment variables. For more details, see the
+they are specified in protected configuration. This includes global,
+system-level config, the command line option `-c`, and environment
+variables, and excludes repository config. For more details, see the
'protected configuration' entry in linkgit:gitglossary[7].
include::config/advice.txt[]
diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt
index a669983abd6..4190c410a00 100644
--- a/Documentation/glossary-content.txt
+++ b/Documentation/glossary-content.txt
@@ -494,12 +494,12 @@ Protected configuration includes:
- system-level config, e.g. `/etc/git/config`
- global config, e.g. `$XDG_CONFIG_HOME/git/config` and
`$HOME/.gitconfig`
+- the command line option `-c` and its equivalent environment variables
+
Protected configuration excludes:
+
- repository config, e.g. `$GIT_DIR/config` and
`$GIT_DIR/config.worktree`
-- the command line option `-c` and its equivalent environment variables
[[def_reachable]]reachable::
All of the ancestors of a given <<def_commit,commit>> are said to be
diff --git a/config.c b/config.c
index c30bb7c5d09..22192ca1d63 100644
--- a/config.c
+++ b/config.c
@@ -2373,6 +2373,11 @@ int git_configset_add_file(struct config_set *cs, const char *filename)
return git_config_from_file(config_set_callback, filename, cs);
}
+int git_configset_add_parameters(struct config_set *cs)
+{
+ return git_config_from_parameters(config_set_callback, cs);
+}
+
int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
{
const struct string_list *values = NULL;
@@ -2628,6 +2633,7 @@ static void read_protected_config(void)
git_configset_add_file(the_repository->protected_config, system_config);
git_configset_add_file(the_repository->protected_config, xdg_config);
git_configset_add_file(the_repository->protected_config, user_config);
+ git_configset_add_parameters(the_repository->protected_config);
free(system_config);
free(xdg_config);
diff --git a/config.h b/config.h
index 411965f52b5..e3ff1fcf683 100644
--- a/config.h
+++ b/config.h
@@ -446,6 +446,15 @@ void git_configset_init(struct config_set *cs);
*/
int git_configset_add_file(struct config_set *cs, const char *filename);
+/**
+ * Parses command line options and environment variables, and adds the
+ * variable-value pairs to the `config_set`. Returns 0 on success, or -1
+ * if there is an error in parsing. The caller decides whether to free
+ * the incomplete configset or continue using it when the function
+ * returns -1.
+ */
+int git_configset_add_parameters(struct config_set *cs);
+
/**
* Finds and returns the value list, sorted in order of increasing priority
* for the configuration variable `key` and config set `cs`. When the
diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh
index 238b25f91a3..5a1cd0d0947 100755
--- a/t/t0033-safe-directory.sh
+++ b/t/t0033-safe-directory.sh
@@ -16,24 +16,20 @@ test_expect_success 'safe.directory is not set' '
expect_rejected_dir
'
-test_expect_success 'ignoring safe.directory on the command line' '
- test_must_fail git -c safe.directory="$(pwd)" status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory on the command line' '
+ git -c safe.directory="$(pwd)" status
'
-test_expect_success 'ignoring safe.directory in the environment' '
- test_must_fail env GIT_CONFIG_COUNT=1 \
- GIT_CONFIG_KEY_0="safe.directory" \
- GIT_CONFIG_VALUE_0="$(pwd)" \
- git status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory in the environment' '
+ env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0="safe.directory" \
+ GIT_CONFIG_VALUE_0="$(pwd)" \
+ git status
'
-test_expect_success 'ignoring safe.directory in GIT_CONFIG_PARAMETERS' '
- test_must_fail env \
- GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
- git status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory in GIT_CONFIG_PARAMETERS' '
+ env GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
+ git status
'
test_expect_success 'ignoring safe.directory in repo config' '
diff --git a/t/t0035-discovery-bare.sh b/t/t0035-discovery-bare.sh
index 94c2f76d774..0d5983df307 100755
--- a/t/t0035-discovery-bare.sh
+++ b/t/t0035-discovery-bare.sh
@@ -56,8 +56,7 @@ test_expect_success 'discovery.bare on the command line' '
git config --global discovery.bare never &&
(
cd outer-repo/bare-repo &&
- test_must_fail git -c discovery.bare=always rev-parse --git-dir 2>err &&
- grep "discovery.bare" err
+ git -c discovery.bare=always rev-parse --git-dir
)
'
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 4/5] config: include "-c" in protected config
2022-05-27 21:09 ` [PATCH v3 4/5] config: include "-c" in protected config Glen Choo via GitGitGadget
@ 2022-06-02 13:15 ` Derrick Stolee
0 siblings, 0 replies; 67+ messages in thread
From: Derrick Stolee @ 2022-06-02 13:15 UTC (permalink / raw)
To: Glen Choo via GitGitGadget, git
Cc: Taylor Blau, brian m. carlson, Junio C Hamano, Emily Shaffer, Glen Choo
On 5/27/2022 5:09 PM, Glen Choo via GitGitGadget wrote:
> +int git_configset_add_parameters(struct config_set *cs)
> +{
> + return git_config_from_parameters(config_set_callback, cs);
> +}
> +
This one-line method could be inlined into the read_protected_config()
method:
> @@ -2628,6 +2633,7 @@ static void read_protected_config(void)
> git_configset_add_file(the_repository->protected_config, system_config);
> git_configset_add_file(the_repository->protected_config, xdg_config);
> git_configset_add_file(the_repository->protected_config, user_config);
> + git_configset_add_parameters(the_repository->protected_config);
git_config_from_parameters(config_set_callback, the_repository->protected_config);
...would be the way to inline it.
> +/**
> + * Parses command line options and environment variables, and adds the
> + * variable-value pairs to the `config_set`. Returns 0 on success, or -1
> + * if there is an error in parsing. The caller decides whether to free
> + * the incomplete configset or continue using it when the function
> + * returns -1.
> + */
> +int git_configset_add_parameters(struct config_set *cs);
You do make it public here. I wonder if we can think of other consumers
of this method that justify the addition to the API.
But this is also a nitpick. I don't feel strongly one way or another. The
code definitely works as-is.
Thanks,
-Stolee
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v3 5/5] upload-pack: make uploadpack.packObjectsHook protected
2022-05-27 21:09 ` [PATCH v3 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (3 preceding siblings ...)
2022-05-27 21:09 ` [PATCH v3 4/5] config: include "-c" in protected config Glen Choo via GitGitGadget
@ 2022-05-27 21:09 ` Glen Choo via GitGitGadget
2022-06-02 13:18 ` Derrick Stolee
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
5 siblings, 1 reply; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-05-27 21:09 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
Now that protected config includes "-c", "uploadpack.packObjectsHook"
behaves identically to a 'Protected config only' variable. Refactor it
to use git_protected_config() and mark it 'Protected config only'.
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config/uploadpack.txt | 22 +++++++++-------------
upload-pack.c | 17 +++++++++++------
2 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/Documentation/config/uploadpack.txt b/Documentation/config/uploadpack.txt
index 32fad5bbe81..57e5e021323 100644
--- a/Documentation/config/uploadpack.txt
+++ b/Documentation/config/uploadpack.txt
@@ -39,19 +39,15 @@ uploadpack.keepAlive::
disables keepalive packets entirely. The default is 5 seconds.
uploadpack.packObjectsHook::
- If this option is set, when `upload-pack` would run
- `git pack-objects` to create a packfile for a client, it will
- run this shell command instead. The `pack-objects` command and
- arguments it _would_ have run (including the `git pack-objects`
- at the beginning) are appended to the shell command. The stdin
- and stdout of the hook are treated as if `pack-objects` itself
- was run. I.e., `upload-pack` will feed input intended for
- `pack-objects` to the hook, and expects a completed packfile on
- stdout.
-+
-Note that this configuration variable is ignored if it is seen in the
-repository-level config (this is a safety measure against fetching from
-untrusted repositories).
+ '(Protected config only)' If this option is set, when
+ `upload-pack` would run `git pack-objects` to create a packfile
+ for a client, it will run this shell command instead. The
+ `pack-objects` command and arguments it _would_ have run
+ (including the `git pack-objects` at the beginning) are appended
+ to the shell command. The stdin and stdout of the hook are
+ treated as if `pack-objects` itself was run. I.e., `upload-pack`
+ will feed input intended for `pack-objects` to the hook, and
+ expects a completed packfile on stdout.
uploadpack.allowFilter::
If this option is set, `upload-pack` will support partial
diff --git a/upload-pack.c b/upload-pack.c
index 3a851b36066..2a39391369d 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1321,18 +1321,21 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
data->advertise_sid = git_config_bool(var, value);
}
- if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
- current_config_scope() != CONFIG_SCOPE_WORKTREE) {
- if (!strcmp("uploadpack.packobjectshook", var))
- return git_config_string(&data->pack_objects_hook, var, value);
- }
-
if (parse_object_filter_config(var, value, data) < 0)
return -1;
return parse_hide_refs_config(var, value, "uploadpack");
}
+static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+{
+ struct upload_pack_data *data = cb_data;
+
+ if (!strcmp("uploadpack.packobjectshook", var))
+ return git_config_string(&data->pack_objects_hook, var, value);
+ return 0;
+}
+
void upload_pack(const int advertise_refs, const int stateless_rpc,
const int timeout)
{
@@ -1342,6 +1345,7 @@ void upload_pack(const int advertise_refs, const int stateless_rpc,
upload_pack_data_init(&data);
git_config(upload_pack_config, &data);
+ git_protected_config(upload_pack_protected_config, &data);
data.stateless_rpc = stateless_rpc;
data.timeout = timeout;
@@ -1697,6 +1701,7 @@ int upload_pack_v2(struct repository *r, struct packet_reader *request)
data.use_sideband = LARGE_PACKET_MAX;
git_config(upload_pack_config, &data);
+ git_protected_config(upload_pack_protected_config, &data);
while (state != FETCH_DONE) {
switch (state) {
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v3 5/5] upload-pack: make uploadpack.packObjectsHook protected
2022-05-27 21:09 ` [PATCH v3 5/5] upload-pack: make uploadpack.packObjectsHook protected Glen Choo via GitGitGadget
@ 2022-06-02 13:18 ` Derrick Stolee
0 siblings, 0 replies; 67+ messages in thread
From: Derrick Stolee @ 2022-06-02 13:18 UTC (permalink / raw)
To: Glen Choo via GitGitGadget, git
Cc: Taylor Blau, brian m. carlson, Junio C Hamano, Emily Shaffer, Glen Choo
On 5/27/2022 5:09 PM, Glen Choo via GitGitGadget wrote:
> From: Glen Choo <chooglen@google.com>
>
> Now that protected config includes "-c", "uploadpack.packObjectsHook"
> behaves identically to a 'Protected config only' variable. Refactor it
> to use git_protected_config() and mark it 'Protected config only'.
I'm really glad to see this simplification at the end of your series.
> @@ -1321,18 +1321,21 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
> data->advertise_sid = git_config_bool(var, value);
> }
>
> - if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
> - current_config_scope() != CONFIG_SCOPE_WORKTREE) {
> - if (!strcmp("uploadpack.packobjectshook", var))
> - return git_config_string(&data->pack_objects_hook, var, value);
> - }
> -
...
> +static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
> +{
> + struct upload_pack_data *data = cb_data;
> +
> + if (!strcmp("uploadpack.packobjectshook", var))
> + return git_config_string(&data->pack_objects_hook, var, value);
> + return 0;
> +}
> +
This is much cleaner.
> @@ -1342,6 +1345,7 @@ void upload_pack(const int advertise_refs, const int stateless_rpc,
> upload_pack_data_init(&data);
>
> git_config(upload_pack_config, &data);
> + git_protected_config(upload_pack_protected_config, &data);
>
> data.stateless_rpc = stateless_rpc;
> data.timeout = timeout;
> @@ -1697,6 +1701,7 @@ int upload_pack_v2(struct repository *r, struct packet_reader *request)
> data.use_sideband = LARGE_PACKET_MAX;
>
> git_config(upload_pack_config, &data);
> + git_protected_config(upload_pack_protected_config, &data);
It's unfortunate that there are two places that need this change.
Is it worth adding a static helper that executes these?
static void get_upload_pack_config(void *data)
{
git_config(upload_pack_config, data);
git_protected_config(upload_pack_protected_config, data);
}
Thanks,
-Stolee
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v4 0/5] config: introduce discovery.bare and protected config
2022-05-27 21:09 ` [PATCH v3 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (4 preceding siblings ...)
2022-05-27 21:09 ` [PATCH v3 5/5] upload-pack: make uploadpack.packObjectsHook protected Glen Choo via GitGitGadget
@ 2022-06-07 20:57 ` Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 1/5] Documentation/git-config.txt: add SCOPES section Glen Choo via GitGitGadget
` (7 more replies)
5 siblings, 8 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-07 20:57 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo
Thanks again for the kind feedback, everyone :)
The motivation has remained the same as the last round; you can find it in
the "Description" section in the previous cover letter [1].
This round doesn't introduce any major code changes. The most notable
changes are:
* I've reorganized the patches so that protected config includes "-c" from
the beginning (3/5) (instead of trying to avoid changing safe.directory).
As a result, uploadpack.packObjectsHook becomes the first 'protected
config only' variable instead of safe.directory.
Since we start the conversion with uploadpack.packObjectHook, I was
curious whether we might be able to reuse its approach of "reading the
full set of config, but checking the scope of each value", which might be
nice because we could reuse the cache in the_repository->config instead
of creating an entirely new configset. I didn't pursue it further, but
I've noted this alternative in 3/5's commit message.
* 'Protected configuration' is now defined as a set of configuration scopes
(2/5). This follows a suggestion from Junio [2], which I thought read
very clearly. We haven't done a great job at describing 'scopes' in
Documentation/git-config.txt though, so I tried to remedy that by
cleaning up a little and adding a SCOPES section (1/5).
Frankly, I'm not very happy with the end result - it's not nearly as
clear as I had hoped, and I think I might be introducing some confusion
between the words "config" and "scope". I'd appreciate any feedback that
helps us get to a good final wording.
* I added a test for "git config --show-scope" and the 'worktree' scope,
since 'worktree' wasn't listed in Documentation/git-config.txt (6/5).
= Patch organization
* Patch 1 add a section on config scopes to our docs
* Patches 2-3 define 'protected config' and create a shared implementation.
* Patch 4 refactors safe.directory to use protected config
* Patch 5 adds discovery.bare
= Series history
Changes in v4:
* 2/5's commit message now justifies what scopes are included in protected
config
* The global configset is now a file-scope static inside config.c
(previously it was a member of the_repository).
* Rename discovery_bare_config to discovery_bare_allowed
* Make discovery_bare_allowed function-scoped (instead of global).
* Add an expect_accepted helper to the discovery.bare tests.
* Add a helper to "upload-pack" that reads the protected and non-protected
config
Changes in v3:
* Rebase onto a more recent 'master'
* Reframe this feature in only in terms of the 'embedded bare repo' attack.
* Other docs improvements (thanks Stolee in particular!)
* Protected config no longer uses read_very_early_config() and is only read
once
* Protected config now includes "-c"
* uploadpack.packObjectsHook now uses protected config instead of ignoring
repo config using config scopes
Changes in v2:
* Rename safe.barerepository to discovery.bare and make it die()
* Move tests into t/t0034-discovery-bare.sh
* Avoid unnecessary config reading by using a static variable
* Add discovery.bare=cwd
* Fix typos
= Future work
* This series does not implement the "no-embedded" option [3] and I won't
work on it any time soon, but I'd be more than happy to review if someone
sends patches.
* With discovery.bare, if a builtin is marked RUN_SETUP_GENTLY, setup.c
doesn't die() and we don't tell users why their repository was rejected,
e.g. "git config" gives an opaque "fatal: not in a git directory". This
isn't a new problem though, since safe.directory has the same issue.
[1]
https://lore.kernel.org/git/pull.1261.v3.git.git.1653685761.gitgitgadget@gmail.com
[2] https://lore.kernel.org/git/xmqqh75a1rmd.fsf@gitster.g [3] This was
first suggested in
https://lore.kernel.org/git/5b969c5e-e802-c447-ad25-6acc0b784582@github.com
Glen Choo (5):
Documentation/git-config.txt: add SCOPES section
Documentation: define protected configuration
config: read protected config with `git_protected_config()`
safe.directory: use git_protected_config()
setup.c: create `discovery.bare`
Documentation/config.txt | 2 +
Documentation/config/discovery.txt | 19 +++++++
Documentation/config/safe.txt | 6 +--
Documentation/config/uploadpack.txt | 6 +--
Documentation/git-config.txt | 77 +++++++++++++++++++++++------
config.c | 51 +++++++++++++++++++
config.h | 17 +++++++
setup.c | 59 +++++++++++++++++++++-
t/t0033-safe-directory.sh | 24 ++++-----
t/t0035-discovery-bare.sh | 68 +++++++++++++++++++++++++
t/t5544-pack-objects-hook.sh | 7 ++-
upload-pack.c | 27 ++++++----
12 files changed, 316 insertions(+), 47 deletions(-)
create mode 100644 Documentation/config/discovery.txt
create mode 100755 t/t0035-discovery-bare.sh
base-commit: f9b95943b68b6b8ca5a6072f50a08411c6449b55
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1261%2Fchooglen%2Fsetup%2Fdisable-bare-repo-config-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1261/chooglen/setup/disable-bare-repo-config-v4
Pull-Request: https://github.com/git/git/pull/1261
Range-diff vs v3:
1: 575676c760d < -: ----------- Documentation: define protected configuration
-: ----------- > 1: c0e27ab3b3e Documentation/git-config.txt: add SCOPES section
5: e25d5907cd1 ! 2: a5a1dcb03e1 upload-pack: make uploadpack.packObjectsHook protected
@@ Metadata
Author: Glen Choo <chooglen@google.com>
## Commit message ##
- upload-pack: make uploadpack.packObjectsHook protected
+ Documentation: define protected configuration
- Now that protected config includes "-c", "uploadpack.packObjectsHook"
- behaves identically to a 'Protected config only' variable. Refactor it
- to use git_protected_config() and mark it 'Protected config only'.
+ For security reasons, there are config variables that are only trusted
+ when they are specified in extra-trustworthy configuration scopes, which
+ are sometimes referred to on-list as 'protected configuration' [1]. A
+ future commit will introduce another such variable, so let's define our
+ terms so that we can have consistent documentation and implementation.
+
+ In our documentation, define 'protected config' as the system, global
+ and command config scopes. As a shorthand, I will refer to variables
+ that are only respected in protected config as 'protected config only',
+ but this term is not used in the documentation.
+
+ This definition of protected configuration is based on whether or not
+ Git can reasonably protect the user by ignoring the configuration scope:
+
+ - System, global and command line config are considered protected
+ because an attacker who has control over any of those can do plenty of
+ harm without Git, so we gain very little by ignoring those scopes.
+ - On the other hand, local (and similarly, worktree) config are not
+ considered protected because it is relatively easy for an attacker to
+ control local config, e.g.:
+ - On some shared user environments, a non-admin attacker can create a
+ repository high up the directory hierarchy (e.g. C:\.git on Windows),
+ and a user may accidentally use it when their PS1 automatically
+ invokes "git" commands.
+
+ `safe.directory` prevents attacks of this form by making sure that
+ the user intended to use the shared repository. It obviously
+ shouldn't be read from the repository, because that would end up
+ trusting the repository that Git was supposed to reject.
+ - "git upload-pack" is expected to run in repositories that may not be
+ controlled by the user. We cannot ignore all config in that
+ repository (because "git upload-pack" would fail), but we can limit
+ the risks by ignoring `uploadpack.packObjectsHook`.
+
+ Only `uploadpack.packObjectsHook` is 'protected config only'. The
+ following variables are intentionally excluded:
+
+ - `safe.directory` should be 'protected config only', but it does not
+ technically fit the definition because it is not respected in the
+ "command" scope. A future commit will fix this.
+
+ - `trace2.*` happens to read the same scopes as `safe.directory` because
+ they share an implementation. However, this is not for security
+ reasons; it is because we want to start tracing so early that
+ repository-level config and "-c" are not available [2].
+
+ This requirement is unique to `trace2.*`, so it does not makes sense
+ for protected configuration to be subject to the same constraints.
+
+ [1] For example,
+ https://lore.kernel.org/git/6af83767-576b-75c4-c778-0284344a8fe7@github.com/
+ [2] https://lore.kernel.org/git/a0c89d0d-669e-bf56-25d2-cbb09b012e70@jeffhostetler.com/
Signed-off-by: Glen Choo <chooglen@google.com>
## Documentation/config/uploadpack.txt ##
-@@ Documentation/config/uploadpack.txt: uploadpack.keepAlive::
- disables keepalive packets entirely. The default is 5 seconds.
-
- uploadpack.packObjectsHook::
-- If this option is set, when `upload-pack` would run
-- `git pack-objects` to create a packfile for a client, it will
-- run this shell command instead. The `pack-objects` command and
-- arguments it _would_ have run (including the `git pack-objects`
-- at the beginning) are appended to the shell command. The stdin
-- and stdout of the hook are treated as if `pack-objects` itself
-- was run. I.e., `upload-pack` will feed input intended for
-- `pack-objects` to the hook, and expects a completed packfile on
-- stdout.
--+
+@@ Documentation/config/uploadpack.txt: uploadpack.packObjectsHook::
+ `pack-objects` to the hook, and expects a completed packfile on
+ stdout.
+ +
-Note that this configuration variable is ignored if it is seen in the
-repository-level config (this is a safety measure against fetching from
-untrusted repositories).
-+ '(Protected config only)' If this option is set, when
-+ `upload-pack` would run `git pack-objects` to create a packfile
-+ for a client, it will run this shell command instead. The
-+ `pack-objects` command and arguments it _would_ have run
-+ (including the `git pack-objects` at the beginning) are appended
-+ to the shell command. The stdin and stdout of the hook are
-+ treated as if `pack-objects` itself was run. I.e., `upload-pack`
-+ will feed input intended for `pack-objects` to the hook, and
-+ expects a completed packfile on stdout.
++Note that this configuration variable is only respected when it is specified
++in protected config (see <<SCOPES>>). This is a safety measure against
++fetching from untrusted repositories.
uploadpack.allowFilter::
If this option is set, `upload-pack` will support partial
- ## upload-pack.c ##
-@@ upload-pack.c: static int upload_pack_config(const char *var, const char *value, void *cb_data)
- data->advertise_sid = git_config_bool(var, value);
- }
+ ## Documentation/git-config.txt ##
+@@ Documentation/git-config.txt: You can change the way options are read/written by specifying the path to a
+ file (`--file`), or by specifying a configuration scope (`--system`,
+ `--global`, `--local`, `--worktree`); see <<OPTIONS>> above.
-- if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
-- current_config_scope() != CONFIG_SCOPE_WORKTREE) {
-- if (!strcmp("uploadpack.packobjectshook", var))
-- return git_config_string(&data->pack_objects_hook, var, value);
-- }
--
- if (parse_object_filter_config(var, value, data) < 0)
- return -1;
++[[SCOPES]]
+ SCOPES
+ ------
- return parse_hide_refs_config(var, value, "uploadpack");
- }
+@@ Documentation/git-config.txt: Most configuration options are respected regardless of the scope it is
+ defined in, but some options are only respected in certain scopes. See the
+ option's documentation for the full details.
-+static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
-+{
-+ struct upload_pack_data *data = cb_data;
++Protected config
++~~~~~~~~~~~~~~~~
+
-+ if (!strcmp("uploadpack.packobjectshook", var))
-+ return git_config_string(&data->pack_objects_hook, var, value);
-+ return 0;
-+}
++Protected config refers to the 'system', 'global', and 'command' scopes. Git
++considers these scopes to be especially trustworthy because they are likely
++to be controlled by the user or a trusted administrator. An attacker who
++controls these scopes can do substantial harm without using Git, so it is
++assumed that the user's environment protects these scopes against attackers.
+
- void upload_pack(const int advertise_refs, const int stateless_rpc,
- const int timeout)
- {
-@@ upload-pack.c: void upload_pack(const int advertise_refs, const int stateless_rpc,
- upload_pack_data_init(&data);
-
- git_config(upload_pack_config, &data);
-+ git_protected_config(upload_pack_protected_config, &data);
-
- data.stateless_rpc = stateless_rpc;
- data.timeout = timeout;
-@@ upload-pack.c: int upload_pack_v2(struct repository *r, struct packet_reader *request)
- data.use_sideband = LARGE_PACKET_MAX;
-
- git_config(upload_pack_config, &data);
-+ git_protected_config(upload_pack_protected_config, &data);
++For security reasons, certain options are only respected when they are
++specified in protected config, and ignored otherwise.
++
+ ENVIRONMENT
+ -----------
- while (state != FETCH_DONE) {
- switch (state) {
2: 7499a280961 ! 3: 94b40907e66 config: read protected config with `git_protected_config()`
@@ Metadata
## Commit message ##
config: read protected config with `git_protected_config()`
- Protected config is read using `read_very_early_config()`, which has
- several downsides:
-
- - Every call to `read_very_early_config()` parses global and
- system-level config files anew, but this can be optimized by just
- parsing them once [1].
- - Protected variables should respect "-c" because we can reasonably
- assume that it comes from the user. But, `read_very_early_config()`
- can't use "-c" because it is called so early that it does not have
- access to command line arguments.
-
- Introduce `git_protected_config()`, which reads protected config and
- caches the values in `the_repository.protected_config`. Then, refactor
- `safe.directory` to use `git_protected_config()`.
-
- This implementation can still be improved, however:
-
- - `git_protected_config()` iterates through every variable in
- `the_repository.protected_config`, which may still be too expensive to
- be called in every "git" invocation. There exist constant time lookup
- functions for non-protected config (repo_config_get_*()), but for
- simplicity, this commit does not implement similar functions for
- protected config.
-
- - Protected config is stored in `the_repository` so that we don't need
- to statically allocate it. But this might be confusing since protected
- config ignores repository config by definition.
-
- [1] While `git_protected_config()` should save on file I/O, I wasn't
- able to measure a meaningful difference between that and
- `read_very_early_config()` on my machine (which has an SSD).
+ `uploadpack.packObjectsHook` is the only 'protected config only'
+ variable today, but we've noted that `safe.directory` and the upcoming
+ `discovery.bare` should also be 'protected config only'. So, for
+ consistency, we'd like to have a single implementation for protected
+ config.
+
+ The primary constraints are:
+
+ 1. Reading from protected config should be as fast as possible. Nearly
+ all "git" commands inside a bare repository will read both
+ `safe.directory` and `discovery.bare`, so we cannot afford to be
+ slow.
+
+ 2. Protected config must be readable when the gitdir is not known.
+ `safe.directory` and `discovery.bare` both affect repository
+ discovery and the gitdir is not known at that point [1].
+
+ The chosen implementation in this commit is to read protected config and
+ cache the values in a global configset. This is similar to the caching
+ behavior we get with the_repository->config.
+
+ Introduce git_protected_config(), which reads protected config and
+ caches them in the global configset protected_config. Then, refactor
+ `uploadpack.packObjectsHook` to use git_protected_config().
+
+ The protected config functions are named similarly to their
+ non-protected counterparts, e.g. git_protected_config_check_init() vs
+ git_config_check_init().
+
+ In light of constraint 1, this implementation can still be improved
+ since git_protected_config() iterates through every variable in
+ protected_config, which may still be too expensive. There exist constant
+ time lookup functions for non-protected config (repo_config_get_*()),
+ but for simplicity, this commit does not implement similar functions for
+ protected config.
+
+ An alternative that avoids introducing another configset is to continue
+ to read all config using git_config(), but only accept values that have
+ the correct config scope [2]. This technically fulfills constraint 2,
+ because git_config() simply ignores the local and worktree config when
+ the gitdir is not known. However, this would read incomplete config into
+ the_repository->config, which would need to be reset when the gitdir is
+ known and git_config() needs to read the local and worktree config.
+ Resetting the_repository->config might be reasonable while we only have
+ these 'protected config only' variables, but it's not clear whether this
+ extends well to future variables.
+
+ [1] In this case, we do have a candidate gitdir though, so with a little
+ refactoring, it might be possible to provide a gitdir.
+ [2] This is how `uploadpack.packObjectsHook` was implemented prior to
+ this commit.
Signed-off-by: Glen Choo <chooglen@google.com>
## config.c ##
+@@ config.c: static enum config_scope current_parsing_scope;
+ static int pack_compression_seen;
+ static int zlib_compression_seen;
+
++/*
++ * Config that comes from trusted sources, namely:
++ * - system config files (e.g. /etc/gitconfig)
++ * - global config files (e.g. $HOME/.gitconfig,
++ * $XDG_CONFIG_HOME/git)
++ * - the command line.
++ *
++ * This is declared here for code cleanliness, but unlike the other
++ * static variables, this does not hold config parser state.
++ */
++static struct config_set protected_config;
++
+ static int config_file_fgetc(struct config_source *conf)
+ {
+ return getc_unlocked(conf->u.file);
+@@ config.c: int git_configset_add_file(struct config_set *cs, const char *filename)
+ return git_config_from_file(config_set_callback, filename, cs);
+ }
+
++int git_configset_add_parameters(struct config_set *cs)
++{
++ return git_config_from_parameters(config_set_callback, cs);
++}
++
+ int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
+ {
+ const struct string_list *values = NULL;
@@ config.c: int repo_config_get_pathname(struct repository *repo,
return ret;
}
-+/* Read protected config into the_repository->protected_config. */
++/* Read values into protected_config. */
+static void read_protected_config(void)
+{
+ char *xdg_config = NULL, *user_config = NULL, *system_config = NULL;
+
-+ CALLOC_ARRAY(the_repository->protected_config, 1);
-+ git_configset_init(the_repository->protected_config);
++ git_configset_init(&protected_config);
+
+ system_config = git_system_config();
+ git_global_config(&user_config, &xdg_config);
+
-+ git_configset_add_file(the_repository->protected_config, system_config);
-+ git_configset_add_file(the_repository->protected_config, xdg_config);
-+ git_configset_add_file(the_repository->protected_config, user_config);
++ git_configset_add_file(&protected_config, system_config);
++ git_configset_add_file(&protected_config, xdg_config);
++ git_configset_add_file(&protected_config, user_config);
++ git_configset_add_parameters(&protected_config);
+
+ free(system_config);
+ free(xdg_config);
+ free(user_config);
+}
+
-+/* Ensure that the_repository->protected_config has been initialized. */
++/* Ensure that protected_config has been initialized. */
+static void git_protected_config_check_init(void)
+{
-+ if (the_repository->protected_config &&
-+ the_repository->protected_config->hash_initialized)
++ if (protected_config.hash_initialized)
+ return;
+ read_protected_config();
+}
@@ config.c: int repo_config_get_pathname(struct repository *repo,
+void git_protected_config(config_fn_t fn, void *data)
+{
+ git_protected_config_check_init();
-+ configset_iter(the_repository->protected_config, fn, data);
++ configset_iter(&protected_config, fn, data);
+}
+
/* Functions used historically to read configuration from 'the_repository' */
@@ config.c: int repo_config_get_pathname(struct repository *repo,
{
## config.h ##
+@@ config.h: void git_configset_init(struct config_set *cs);
+ */
+ int git_configset_add_file(struct config_set *cs, const char *filename);
+
++/**
++ * Parses command line options and environment variables, and adds the
++ * variable-value pairs to the `config_set`. Returns 0 on success, or -1
++ * if there is an error in parsing. The caller decides whether to free
++ * the incomplete configset or continue using it when the function
++ * returns -1.
++ */
++int git_configset_add_parameters(struct config_set *cs);
++
+ /**
+ * Finds and returns the value list, sorted in order of increasing priority
+ * for the configuration variable `key` and config set `cs`. When the
@@ config.h: int repo_config_get_maybe_bool(struct repository *repo,
int repo_config_get_pathname(struct repository *repo,
const char *key, const char **dest);
@@ config.h: int repo_config_get_maybe_bool(struct repository *repo,
* Querying For Specific Variables
* -------------------------------
- ## repository.c ##
-@@ repository.c: void repo_clear(struct repository *repo)
- FREE_AND_NULL(repo->remote_state);
- }
-
-+ if (repo->protected_config) {
-+ git_configset_clear(repo->protected_config);
-+ FREE_AND_NULL(repo->protected_config);
-+ }
+ ## t/t5544-pack-objects-hook.sh ##
+@@ t/t5544-pack-objects-hook.sh: test_expect_success 'hook does not run from repo config' '
+ ! grep "hook running" stderr &&
+ test_path_is_missing .git/hook.args &&
+ test_path_is_missing .git/hook.stdin &&
+- test_path_is_missing .git/hook.stdout
++ test_path_is_missing .git/hook.stdout &&
+
- repo_clear_path_cache(&repo->cached_paths);
- }
++ # check that global config is used instead
++ test_config_global uploadpack.packObjectsHook ./hook &&
++ git clone --no-local . dst2.git 2>stderr &&
++ grep "hook running" stderr
+ '
+ test_expect_success 'hook works with partial clone' '
- ## repository.h ##
-@@ repository.h: struct repository {
+ ## upload-pack.c ##
+@@ upload-pack.c: static int upload_pack_config(const char *var, const char *value, void *cb_data)
+ data->advertise_sid = git_config_bool(var, value);
+ }
- struct repo_settings settings;
+- if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
+- current_config_scope() != CONFIG_SCOPE_WORKTREE) {
+- if (!strcmp("uploadpack.packobjectshook", var))
+- return git_config_string(&data->pack_objects_hook, var, value);
+- }
+-
+ if (parse_object_filter_config(var, value, data) < 0)
+ return -1;
+
+ return parse_hide_refs_config(var, value, "uploadpack");
+ }
-+ /*
-+ * Config that comes from trusted sources, namely
-+ * - system config files (e.g. /etc/gitconfig)
-+ * - global config files (e.g. $HOME/.gitconfig,
-+ * $XDG_CONFIG_HOME/git)
-+ */
-+ struct config_set *protected_config;
++static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
++{
++ struct upload_pack_data *data = cb_data;
++
++ if (!strcmp("uploadpack.packobjectshook", var))
++ return git_config_string(&data->pack_objects_hook, var, value);
++ return 0;
++}
++
++static void get_upload_pack_config(struct upload_pack_data *data)
++{
++ git_config(upload_pack_config, data);
++ git_protected_config(upload_pack_protected_config, data);
++}
+
- /* Subsystems */
- /*
- * Repository's config which contains key-value pairs from the usual
-
- ## setup.c ##
-@@ setup.c: static int ensure_valid_ownership(const char *path)
- is_path_owned_by_current_user(path))
- return 1;
+ void upload_pack(const int advertise_refs, const int stateless_rpc,
+ const int timeout)
+ {
+@@ upload-pack.c: void upload_pack(const int advertise_refs, const int stateless_rpc,
+ struct upload_pack_data data;
-- read_very_early_config(safe_directory_cb, &data);
-+ git_protected_config(safe_directory_cb, &data);
+ upload_pack_data_init(&data);
+-
+- git_config(upload_pack_config, &data);
++ get_upload_pack_config(&data);
- return data.is_safe;
- }
+ data.stateless_rpc = stateless_rpc;
+ data.timeout = timeout;
+@@ upload-pack.c: int upload_pack_v2(struct repository *r, struct packet_reader *request)
+
+ upload_pack_data_init(&data);
+ data.use_sideband = LARGE_PACKET_MAX;
+-
+- git_config(upload_pack_config, &data);
++ get_upload_pack_config(&data);
+
+ while (state != FETCH_DONE) {
+ switch (state) {
4: 66a0a208176 ! 4: 156817966fa config: include "-c" in protected config
@@ Metadata
Author: Glen Choo <chooglen@google.com>
## Commit message ##
- config: include "-c" in protected config
+ safe.directory: use git_protected_config()
- Protected config should include the command line (aka "-c") because we
- can be quite certain that this config is specified by the user.
-
- Introduce a function, `git_configset_add_parameters()`, that adds "-c"
- config to a config_set, and use it to add "-c" to protected config.
+ Use git_protected_config() to read `safe.directory` instead of
+ read_very_early_config(), making it 'protected config only'. As a
+ result, `safe.directory` now respects "-c", so update the tests and docs
+ accordingly.
Signed-off-by: Glen Choo <chooglen@google.com>
- ## Documentation/config.txt ##
-@@ Documentation/config.txt: names do not conflict with those that are used by Git itself and
- other popular tools, and describe them in your documentation.
-
- Variables marked with '(Protected config only)' are only respected when
--they are specified in protected configuration. This includes global and
--system-level config, and excludes repository config, the command line
--option `-c`, and environment variables. For more details, see the
-+they are specified in protected configuration. This includes global,
-+system-level config, the command line option `-c`, and environment
-+variables, and excludes repository config. For more details, see the
- 'protected configuration' entry in linkgit:gitglossary[7].
-
- include::config/advice.txt[]
-
- ## Documentation/glossary-content.txt ##
-@@ Documentation/glossary-content.txt: Protected configuration includes:
- - system-level config, e.g. `/etc/git/config`
- - global config, e.g. `$XDG_CONFIG_HOME/git/config` and
- `$HOME/.gitconfig`
-+- the command line option `-c` and its equivalent environment variables
+ ## Documentation/config/safe.txt ##
+@@ Documentation/config/safe.txt: via `git config --add`. To reset the list of safe directories (e.g. to
+ override any such directories specified in the system config), add a
+ `safe.directory` entry with an empty value.
+
- Protected configuration excludes:
+-This config setting is only respected when specified in a system or global
+-config, not when it is specified in a repository config, via the command
+-line option `-c safe.directory=<path>`, or in environment variables.
++This config setting is only respected in protected configuration (see
++<<SCOPES>>). This prevents the untrusted repository from tampering with this
++value.
+
- - repository config, e.g. `$GIT_DIR/config` and
- `$GIT_DIR/config.worktree`
--- the command line option `-c` and its equivalent environment variables
-
- [[def_reachable]]reachable::
- All of the ancestors of a given <<def_commit,commit>> are said to be
+ The value of this setting is interpolated, i.e. `~/<path>` expands to a
+ path relative to the home directory and `%(prefix)/<path>` expands to a
- ## config.c ##
-@@ config.c: int git_configset_add_file(struct config_set *cs, const char *filename)
- return git_config_from_file(config_set_callback, filename, cs);
- }
+ ## setup.c ##
+@@ setup.c: static int ensure_valid_ownership(const char *path)
+ is_path_owned_by_current_user(path))
+ return 1;
-+int git_configset_add_parameters(struct config_set *cs)
-+{
-+ return git_config_from_parameters(config_set_callback, cs);
-+}
-+
- int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
- {
- const struct string_list *values = NULL;
-@@ config.c: static void read_protected_config(void)
- git_configset_add_file(the_repository->protected_config, system_config);
- git_configset_add_file(the_repository->protected_config, xdg_config);
- git_configset_add_file(the_repository->protected_config, user_config);
-+ git_configset_add_parameters(the_repository->protected_config);
+- read_very_early_config(safe_directory_cb, &data);
++ git_protected_config(safe_directory_cb, &data);
- free(system_config);
- free(xdg_config);
-
- ## config.h ##
-@@ config.h: void git_configset_init(struct config_set *cs);
- */
- int git_configset_add_file(struct config_set *cs, const char *filename);
-
-+/**
-+ * Parses command line options and environment variables, and adds the
-+ * variable-value pairs to the `config_set`. Returns 0 on success, or -1
-+ * if there is an error in parsing. The caller decides whether to free
-+ * the incomplete configset or continue using it when the function
-+ * returns -1.
-+ */
-+int git_configset_add_parameters(struct config_set *cs);
-+
- /**
- * Finds and returns the value list, sorted in order of increasing priority
- * for the configuration variable `key` and config set `cs`. When the
+ return data.is_safe;
+ }
## t/t0033-safe-directory.sh ##
@@ t/t0033-safe-directory.sh: test_expect_success 'safe.directory is not set' '
@@ t/t0033-safe-directory.sh: test_expect_success 'safe.directory is not set' '
'
test_expect_success 'ignoring safe.directory in repo config' '
-
- ## t/t0035-discovery-bare.sh ##
-@@ t/t0035-discovery-bare.sh: test_expect_success 'discovery.bare on the command line' '
- git config --global discovery.bare never &&
- (
- cd outer-repo/bare-repo &&
-- test_must_fail git -c discovery.bare=always rev-parse --git-dir 2>err &&
-- grep "discovery.bare" err
-+ git -c discovery.bare=always rev-parse --git-dir
- )
- '
-
3: d5a3e9f9845 ! 5: 29053d029f8 setup.c: create `discovery.bare`
@@ setup.c
static int inside_git_dir = -1;
static int inside_work_tree = -1;
static int work_tree_config_is_bogus;
-+enum discovery_bare_config {
-+ DISCOVERY_BARE_UNKNOWN = -1,
++enum discovery_bare_allowed {
+ DISCOVERY_BARE_NEVER = 0,
+ DISCOVERY_BARE_ALWAYS,
+};
-+static enum discovery_bare_config discovery_bare_config =
-+ DISCOVERY_BARE_UNKNOWN;
static struct startup_info the_startup_info;
struct startup_info *startup_info = &the_startup_info;
@@ setup.c: static int ensure_valid_ownership(const char *path)
+static int discovery_bare_cb(const char *key, const char *value, void *d)
+{
++ enum discovery_bare_allowed *discovery_bare_allowed = d;
++
+ if (strcmp(key, "discovery.bare"))
+ return 0;
+
+ if (!strcmp(value, "never")) {
-+ discovery_bare_config = DISCOVERY_BARE_NEVER;
++ *discovery_bare_allowed = DISCOVERY_BARE_NEVER;
+ return 0;
+ }
+ if (!strcmp(value, "always")) {
-+ discovery_bare_config = DISCOVERY_BARE_ALWAYS;
++ *discovery_bare_allowed = DISCOVERY_BARE_ALWAYS;
+ return 0;
+ }
+ return -1;
+}
+
-+static int check_bare_repo_allowed(void)
++static enum discovery_bare_allowed get_discovery_bare(void)
+{
-+ if (discovery_bare_config == DISCOVERY_BARE_UNKNOWN) {
-+ discovery_bare_config = DISCOVERY_BARE_ALWAYS;
-+ git_protected_config(discovery_bare_cb, NULL);
-+ }
-+ switch (discovery_bare_config) {
-+ case DISCOVERY_BARE_NEVER:
-+ return 0;
-+ case DISCOVERY_BARE_ALWAYS:
-+ return 1;
-+ case DISCOVERY_BARE_UNKNOWN:
-+ BUG("invalid discovery_bare_config %d", discovery_bare_config);
-+ }
-+ return 0;
++ enum discovery_bare_allowed result = DISCOVERY_BARE_ALWAYS;
++ git_protected_config(discovery_bare_cb, &result);
++ return result;
+}
+
-+static const char *discovery_bare_config_to_string(void)
++static const char *discovery_bare_allowed_to_string(
++ enum discovery_bare_allowed discovery_bare_allowed)
+{
-+ switch (discovery_bare_config) {
++ switch (discovery_bare_allowed) {
+ case DISCOVERY_BARE_NEVER:
+ return "never";
+ case DISCOVERY_BARE_ALWAYS:
+ return "always";
-+ case DISCOVERY_BARE_UNKNOWN:
-+ BUG("invalid discovery_bare_config %d", discovery_bare_config);
++ default:
++ BUG("invalid discovery_bare_allowed %d",
++ discovery_bare_allowed);
+ }
+ return NULL;
+}
@@ setup.c: static enum discovery_result setup_git_directory_gently_1(struct strbuf
}
if (is_git_directory(dir->buf)) {
-+ if (!check_bare_repo_allowed())
++ if (!get_discovery_bare())
+ return GIT_DIR_DISALLOWED_BARE;
if (!ensure_valid_ownership(dir->buf))
return GIT_DIR_INVALID_OWNERSHIP;
@@ setup.c: const char *setup_git_directory_gently(int *nongit_ok)
+ if (!nongit_ok) {
+ die(_("cannot use bare repository '%s' (discovery.bare is '%s')"),
+ dir.buf,
-+ discovery_bare_config_to_string());
++ discovery_bare_allowed_to_string(get_discovery_bare()));
+ }
+ *nongit_ok = 1;
+ break;
@@ t/t0035-discovery-bare.sh (new)
+
+pwd="$(pwd)"
+
++expect_accepted () {
++ git "$@" rev-parse --git-dir
++}
++
+expect_rejected () {
-+ test_must_fail git rev-parse --git-dir 2>err &&
++ test_must_fail git "$@" rev-parse --git-dir 2>err &&
+ grep "discovery.bare" err
+}
+
@@ t/t0035-discovery-bare.sh (new)
+test_expect_success 'discovery.bare unset' '
+ (
+ cd outer-repo/bare-repo &&
-+ git rev-parse --git-dir
++ expect_accepted
+ )
+'
+
@@ t/t0035-discovery-bare.sh (new)
+ git config --global discovery.bare always &&
+ (
+ cd outer-repo/bare-repo &&
-+ git rev-parse --git-dir
++ expect_accepted
+ )
+'
+
@@ t/t0035-discovery-bare.sh (new)
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
-+ test_must_fail git -c discovery.bare=always rev-parse --git-dir 2>err &&
-+ grep "discovery.bare" err
++ expect_accepted -c discovery.bare=always &&
++ expect_rejected -c discovery.bare=
+ )
+'
+
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v4 1/5] Documentation/git-config.txt: add SCOPES section
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
@ 2022-06-07 20:57 ` Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 2/5] Documentation: define protected configuration Glen Choo via GitGitGadget
` (6 subsequent siblings)
7 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-07 20:57 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
In a subsequent commit, we will introduce "protected config", which is
easiest to describe in terms of configuration scopes (i.e. it's the
union of the 'system', 'global', and 'command' scopes). This description
is fine for ML discussions, but it's inadequate for end users because we
don't provide a good description of "config scopes" in the public docs.
145d59f482 (config: add '--show-scope' to print the scope of a config
value, 2020-02-10) introduced the word "scope" to our public docs, but
that only enumerates the scopes and assumes the user can figure out
those values mean.
Add a SCOPES section to Documentation/git-config.txt that describes the
config scopes, their corresponding CLI options, and mentions that some
configuration options are only respected in certain scopes. Then,
use the word "scope" to simplify the FILES section and change some
confusing wording.
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/git-config.txt | 64 ++++++++++++++++++++++++++++--------
1 file changed, 50 insertions(+), 14 deletions(-)
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index bdcfd94b642..5e4c95f2423 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -297,8 +297,8 @@ The default is to use a pager.
FILES
-----
-If not set explicitly with `--file`, there are four files where
-'git config' will search for configuration options:
+By default, 'git config' will read configuration options from multiple
+files:
$(prefix)/etc/gitconfig::
System-wide configuration file.
@@ -322,27 +322,63 @@ $GIT_DIR/config.worktree::
This is optional and is only searched when
`extensions.worktreeConfig` is present in $GIT_DIR/config.
-If no further options are given, all reading options will read all of these
-files that are available. If the global or the system-wide configuration
-file are not available they will be ignored. If the repository configuration
-file is not available or readable, 'git config' will exit with a non-zero
-error code. However, in neither case will an error message be issued.
+You may also provide additional configuration parameters when running any
+git command by using the `-c` option. See linkgit:git[1] for details.
+
+Options will be read from all of these files that are available. If the
+global or the system-wide configuration file are not available they will be
+ignored. If the repository configuration file is not available or readable,
+'git config' will exit with a non-zero error code. However, in neither case
+will an error message be issued.
The files are read in the order given above, with last value found taking
precedence over values read earlier. When multiple values are taken then all
values of a key from all files will be used.
-You may override individual configuration parameters when running any git
-command by using the `-c` option. See linkgit:git[1] for details.
-
-All writing options will per default write to the repository specific
+By default, options are only written to the repository specific
configuration file. Note that this also affects options like `--replace-all`
and `--unset`. *'git config' will only ever change one file at a time*.
-You can override these rules using the `--global`, `--system`,
-`--local`, `--worktree`, and `--file` command-line options; see
-<<OPTIONS>> above.
+You can change the way options are read/written by specifying the path to a
+file (`--file`), or by specifying a configuration scope (`--system`,
+`--global`, `--local`, `--worktree`); see <<OPTIONS>> above.
+
+SCOPES
+------
+
+Each configuration source falls within a configuration scope. The scopes
+are:
+
+system::
+ $(prefix)/etc/gitconfig
+
+global::
+ $XDG_CONFIG_HOME/git/config
++
+~/.gitconfig
+
+local::
+ $GIT_DIR/config
+
+worktree::
+ $GIT_DIR/config.worktree
+
+command::
+ environment variables
++
+the `-c` option
+
+With the exception of 'command', each scope corresponds to a command line
+option - `--system`, `--global`, `--local`, `--worktree`.
+
+When reading options, specifying a scope will only read options from the
+files within that scope. When writing options, specifying a scope will write
+to the files within that scope (instead of the repository specific
+configuration file). See <<OPTIONS>> above for a complete description.
+Most configuration options are respected regardless of the scope it is
+defined in, but some options are only respected in certain scopes. See the
+option's documentation for the full details.
ENVIRONMENT
-----------
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v4 2/5] Documentation: define protected configuration
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 1/5] Documentation/git-config.txt: add SCOPES section Glen Choo via GitGitGadget
@ 2022-06-07 20:57 ` Glen Choo via GitGitGadget
2022-06-22 21:58 ` Jonathan Tan
2022-06-07 20:57 ` [PATCH v4 3/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
` (5 subsequent siblings)
7 siblings, 1 reply; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-07 20:57 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
For security reasons, there are config variables that are only trusted
when they are specified in extra-trustworthy configuration scopes, which
are sometimes referred to on-list as 'protected configuration' [1]. A
future commit will introduce another such variable, so let's define our
terms so that we can have consistent documentation and implementation.
In our documentation, define 'protected config' as the system, global
and command config scopes. As a shorthand, I will refer to variables
that are only respected in protected config as 'protected config only',
but this term is not used in the documentation.
This definition of protected configuration is based on whether or not
Git can reasonably protect the user by ignoring the configuration scope:
- System, global and command line config are considered protected
because an attacker who has control over any of those can do plenty of
harm without Git, so we gain very little by ignoring those scopes.
- On the other hand, local (and similarly, worktree) config are not
considered protected because it is relatively easy for an attacker to
control local config, e.g.:
- On some shared user environments, a non-admin attacker can create a
repository high up the directory hierarchy (e.g. C:\.git on Windows),
and a user may accidentally use it when their PS1 automatically
invokes "git" commands.
`safe.directory` prevents attacks of this form by making sure that
the user intended to use the shared repository. It obviously
shouldn't be read from the repository, because that would end up
trusting the repository that Git was supposed to reject.
- "git upload-pack" is expected to run in repositories that may not be
controlled by the user. We cannot ignore all config in that
repository (because "git upload-pack" would fail), but we can limit
the risks by ignoring `uploadpack.packObjectsHook`.
Only `uploadpack.packObjectsHook` is 'protected config only'. The
following variables are intentionally excluded:
- `safe.directory` should be 'protected config only', but it does not
technically fit the definition because it is not respected in the
"command" scope. A future commit will fix this.
- `trace2.*` happens to read the same scopes as `safe.directory` because
they share an implementation. However, this is not for security
reasons; it is because we want to start tracing so early that
repository-level config and "-c" are not available [2].
This requirement is unique to `trace2.*`, so it does not makes sense
for protected configuration to be subject to the same constraints.
[1] For example,
https://lore.kernel.org/git/6af83767-576b-75c4-c778-0284344a8fe7@github.com/
[2] https://lore.kernel.org/git/a0c89d0d-669e-bf56-25d2-cbb09b012e70@jeffhostetler.com/
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config/uploadpack.txt | 6 +++---
Documentation/git-config.txt | 13 +++++++++++++
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/Documentation/config/uploadpack.txt b/Documentation/config/uploadpack.txt
index 32fad5bbe81..029abbefdff 100644
--- a/Documentation/config/uploadpack.txt
+++ b/Documentation/config/uploadpack.txt
@@ -49,9 +49,9 @@ uploadpack.packObjectsHook::
`pack-objects` to the hook, and expects a completed packfile on
stdout.
+
-Note that this configuration variable is ignored if it is seen in the
-repository-level config (this is a safety measure against fetching from
-untrusted repositories).
+Note that this configuration variable is only respected when it is specified
+in protected config (see <<SCOPES>>). This is a safety measure against
+fetching from untrusted repositories.
uploadpack.allowFilter::
If this option is set, `upload-pack` will support partial
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 5e4c95f2423..2b4334faec9 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -343,6 +343,7 @@ You can change the way options are read/written by specifying the path to a
file (`--file`), or by specifying a configuration scope (`--system`,
`--global`, `--local`, `--worktree`); see <<OPTIONS>> above.
+[[SCOPES]]
SCOPES
------
@@ -380,6 +381,18 @@ Most configuration options are respected regardless of the scope it is
defined in, but some options are only respected in certain scopes. See the
option's documentation for the full details.
+Protected config
+~~~~~~~~~~~~~~~~
+
+Protected config refers to the 'system', 'global', and 'command' scopes. Git
+considers these scopes to be especially trustworthy because they are likely
+to be controlled by the user or a trusted administrator. An attacker who
+controls these scopes can do substantial harm without using Git, so it is
+assumed that the user's environment protects these scopes against attackers.
+
+For security reasons, certain options are only respected when they are
+specified in protected config, and ignored otherwise.
+
ENVIRONMENT
-----------
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 2/5] Documentation: define protected configuration
2022-06-07 20:57 ` [PATCH v4 2/5] Documentation: define protected configuration Glen Choo via GitGitGadget
@ 2022-06-22 21:58 ` Jonathan Tan
2022-06-23 18:21 ` Glen Choo
0 siblings, 1 reply; 67+ messages in thread
From: Jonathan Tan @ 2022-06-22 21:58 UTC (permalink / raw)
To: Glen Choo via GitGitGadget
Cc: Jonathan Tan, git, Taylor Blau, brian m. carlson, Derrick Stolee,
Junio C Hamano, Emily Shaffer, Glen Choo
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Glen Choo <chooglen@google.com>
>
> For security reasons, there are config variables that are only trusted
> when they are specified in extra-trustworthy configuration scopes, which
Probably better to delete "extra-trustworthy", or at least "extra-" -
it's better to explain why and how they're trustworthy, which you have
already done in the commit message.
> diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
> index 5e4c95f2423..2b4334faec9 100644
> --- a/Documentation/git-config.txt
> +++ b/Documentation/git-config.txt
[snip]
> +Protected config refers to the 'system', 'global', and 'command' scopes. Git
> +considers these scopes to be especially trustworthy because they are likely
> +to be controlled by the user or a trusted administrator. An attacker who
> +controls these scopes can do substantial harm without using Git, so it is
> +assumed that the user's environment protects these scopes against attackers.
> +
> +For security reasons, certain options are only respected when they are
> +specified in protected config, and ignored otherwise.
Also "especially trustworthy" here.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 2/5] Documentation: define protected configuration
2022-06-22 21:58 ` Jonathan Tan
@ 2022-06-23 18:21 ` Glen Choo
0 siblings, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-23 18:21 UTC (permalink / raw)
To: Jonathan Tan, Glen Choo via GitGitGadget
Cc: Jonathan Tan, git, Taylor Blau, brian m. carlson, Derrick Stolee,
Junio C Hamano, Emily Shaffer
Jonathan Tan <jonathantanmy@google.com> writes:
> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> From: Glen Choo <chooglen@google.com>
>>
>> For security reasons, there are config variables that are only trusted
>> when they are specified in extra-trustworthy configuration scopes, which
>
> Probably better to delete "extra-trustworthy", or at least "extra-" -
> it's better to explain why and how they're trustworthy, which you have
> already done in the commit message.
Hm, do you find it superfluous, misleading or something else entirely?
The use of "extra-" was quite intentional. I'm afraid that if we
describe protected config as "trustworthy", we insinuate that
local/worktree config is "untrustworthy" (but of course this isn't
always true, Git usually uses repo config.)
>> diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
>> index 5e4c95f2423..2b4334faec9 100644
>> --- a/Documentation/git-config.txt
>> +++ b/Documentation/git-config.txt
>
> [snip]
>
>> +Protected config refers to the 'system', 'global', and 'command' scopes. Git
>> +considers these scopes to be especially trustworthy because they are likely
>> +to be controlled by the user or a trusted administrator. An attacker who
>> +controls these scopes can do substantial harm without using Git, so it is
>> +assumed that the user's environment protects these scopes against attackers.
>> +
>> +For security reasons, certain options are only respected when they are
>> +specified in protected config, and ignored otherwise.
>
> Also "especially trustworthy" here.
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v4 3/5] config: read protected config with `git_protected_config()`
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 1/5] Documentation/git-config.txt: add SCOPES section Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 2/5] Documentation: define protected configuration Glen Choo via GitGitGadget
@ 2022-06-07 20:57 ` Glen Choo via GitGitGadget
2022-06-07 22:49 ` Junio C Hamano
2022-06-07 20:57 ` [PATCH v4 4/5] safe.directory: use git_protected_config() Glen Choo via GitGitGadget
` (4 subsequent siblings)
7 siblings, 1 reply; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-07 20:57 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
`uploadpack.packObjectsHook` is the only 'protected config only'
variable today, but we've noted that `safe.directory` and the upcoming
`discovery.bare` should also be 'protected config only'. So, for
consistency, we'd like to have a single implementation for protected
config.
The primary constraints are:
1. Reading from protected config should be as fast as possible. Nearly
all "git" commands inside a bare repository will read both
`safe.directory` and `discovery.bare`, so we cannot afford to be
slow.
2. Protected config must be readable when the gitdir is not known.
`safe.directory` and `discovery.bare` both affect repository
discovery and the gitdir is not known at that point [1].
The chosen implementation in this commit is to read protected config and
cache the values in a global configset. This is similar to the caching
behavior we get with the_repository->config.
Introduce git_protected_config(), which reads protected config and
caches them in the global configset protected_config. Then, refactor
`uploadpack.packObjectsHook` to use git_protected_config().
The protected config functions are named similarly to their
non-protected counterparts, e.g. git_protected_config_check_init() vs
git_config_check_init().
In light of constraint 1, this implementation can still be improved
since git_protected_config() iterates through every variable in
protected_config, which may still be too expensive. There exist constant
time lookup functions for non-protected config (repo_config_get_*()),
but for simplicity, this commit does not implement similar functions for
protected config.
An alternative that avoids introducing another configset is to continue
to read all config using git_config(), but only accept values that have
the correct config scope [2]. This technically fulfills constraint 2,
because git_config() simply ignores the local and worktree config when
the gitdir is not known. However, this would read incomplete config into
the_repository->config, which would need to be reset when the gitdir is
known and git_config() needs to read the local and worktree config.
Resetting the_repository->config might be reasonable while we only have
these 'protected config only' variables, but it's not clear whether this
extends well to future variables.
[1] In this case, we do have a candidate gitdir though, so with a little
refactoring, it might be possible to provide a gitdir.
[2] This is how `uploadpack.packObjectsHook` was implemented prior to
this commit.
Signed-off-by: Glen Choo <chooglen@google.com>
---
config.c | 51 ++++++++++++++++++++++++++++++++++++
config.h | 17 ++++++++++++
t/t5544-pack-objects-hook.sh | 7 ++++-
upload-pack.c | 27 ++++++++++++-------
4 files changed, 91 insertions(+), 11 deletions(-)
diff --git a/config.c b/config.c
index fa471dbdb89..56b7ed5ffe8 100644
--- a/config.c
+++ b/config.c
@@ -81,6 +81,18 @@ static enum config_scope current_parsing_scope;
static int pack_compression_seen;
static int zlib_compression_seen;
+/*
+ * Config that comes from trusted sources, namely:
+ * - system config files (e.g. /etc/gitconfig)
+ * - global config files (e.g. $HOME/.gitconfig,
+ * $XDG_CONFIG_HOME/git)
+ * - the command line.
+ *
+ * This is declared here for code cleanliness, but unlike the other
+ * static variables, this does not hold config parser state.
+ */
+static struct config_set protected_config;
+
static int config_file_fgetc(struct config_source *conf)
{
return getc_unlocked(conf->u.file);
@@ -2373,6 +2385,11 @@ int git_configset_add_file(struct config_set *cs, const char *filename)
return git_config_from_file(config_set_callback, filename, cs);
}
+int git_configset_add_parameters(struct config_set *cs)
+{
+ return git_config_from_parameters(config_set_callback, cs);
+}
+
int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
{
const struct string_list *values = NULL;
@@ -2614,6 +2631,40 @@ int repo_config_get_pathname(struct repository *repo,
return ret;
}
+/* Read values into protected_config. */
+static void read_protected_config(void)
+{
+ char *xdg_config = NULL, *user_config = NULL, *system_config = NULL;
+
+ git_configset_init(&protected_config);
+
+ system_config = git_system_config();
+ git_global_config(&user_config, &xdg_config);
+
+ git_configset_add_file(&protected_config, system_config);
+ git_configset_add_file(&protected_config, xdg_config);
+ git_configset_add_file(&protected_config, user_config);
+ git_configset_add_parameters(&protected_config);
+
+ free(system_config);
+ free(xdg_config);
+ free(user_config);
+}
+
+/* Ensure that protected_config has been initialized. */
+static void git_protected_config_check_init(void)
+{
+ if (protected_config.hash_initialized)
+ return;
+ read_protected_config();
+}
+
+void git_protected_config(config_fn_t fn, void *data)
+{
+ git_protected_config_check_init();
+ configset_iter(&protected_config, fn, data);
+}
+
/* Functions used historically to read configuration from 'the_repository' */
void git_config(config_fn_t fn, void *data)
{
diff --git a/config.h b/config.h
index 7654f61c634..e3ff1fcf683 100644
--- a/config.h
+++ b/config.h
@@ -446,6 +446,15 @@ void git_configset_init(struct config_set *cs);
*/
int git_configset_add_file(struct config_set *cs, const char *filename);
+/**
+ * Parses command line options and environment variables, and adds the
+ * variable-value pairs to the `config_set`. Returns 0 on success, or -1
+ * if there is an error in parsing. The caller decides whether to free
+ * the incomplete configset or continue using it when the function
+ * returns -1.
+ */
+int git_configset_add_parameters(struct config_set *cs);
+
/**
* Finds and returns the value list, sorted in order of increasing priority
* for the configuration variable `key` and config set `cs`. When the
@@ -505,6 +514,14 @@ int repo_config_get_maybe_bool(struct repository *repo,
int repo_config_get_pathname(struct repository *repo,
const char *key, const char **dest);
+/*
+ * Functions for reading protected config. By definition, protected
+ * config ignores repository config, so it is unnecessary to read
+ * protected config from any `struct repository` other than
+ * the_repository.
+ */
+void git_protected_config(config_fn_t fn, void *data);
+
/**
* Querying For Specific Variables
* -------------------------------
diff --git a/t/t5544-pack-objects-hook.sh b/t/t5544-pack-objects-hook.sh
index dd5f44d986f..54f54f8d2eb 100755
--- a/t/t5544-pack-objects-hook.sh
+++ b/t/t5544-pack-objects-hook.sh
@@ -56,7 +56,12 @@ test_expect_success 'hook does not run from repo config' '
! grep "hook running" stderr &&
test_path_is_missing .git/hook.args &&
test_path_is_missing .git/hook.stdin &&
- test_path_is_missing .git/hook.stdout
+ test_path_is_missing .git/hook.stdout &&
+
+ # check that global config is used instead
+ test_config_global uploadpack.packObjectsHook ./hook &&
+ git clone --no-local . dst2.git 2>stderr &&
+ grep "hook running" stderr
'
test_expect_success 'hook works with partial clone' '
diff --git a/upload-pack.c b/upload-pack.c
index 3a851b36066..09f48317b02 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1321,18 +1321,27 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
data->advertise_sid = git_config_bool(var, value);
}
- if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
- current_config_scope() != CONFIG_SCOPE_WORKTREE) {
- if (!strcmp("uploadpack.packobjectshook", var))
- return git_config_string(&data->pack_objects_hook, var, value);
- }
-
if (parse_object_filter_config(var, value, data) < 0)
return -1;
return parse_hide_refs_config(var, value, "uploadpack");
}
+static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+{
+ struct upload_pack_data *data = cb_data;
+
+ if (!strcmp("uploadpack.packobjectshook", var))
+ return git_config_string(&data->pack_objects_hook, var, value);
+ return 0;
+}
+
+static void get_upload_pack_config(struct upload_pack_data *data)
+{
+ git_config(upload_pack_config, data);
+ git_protected_config(upload_pack_protected_config, data);
+}
+
void upload_pack(const int advertise_refs, const int stateless_rpc,
const int timeout)
{
@@ -1340,8 +1349,7 @@ void upload_pack(const int advertise_refs, const int stateless_rpc,
struct upload_pack_data data;
upload_pack_data_init(&data);
-
- git_config(upload_pack_config, &data);
+ get_upload_pack_config(&data);
data.stateless_rpc = stateless_rpc;
data.timeout = timeout;
@@ -1695,8 +1703,7 @@ int upload_pack_v2(struct repository *r, struct packet_reader *request)
upload_pack_data_init(&data);
data.use_sideband = LARGE_PACKET_MAX;
-
- git_config(upload_pack_config, &data);
+ get_upload_pack_config(&data);
while (state != FETCH_DONE) {
switch (state) {
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 3/5] config: read protected config with `git_protected_config()`
2022-06-07 20:57 ` [PATCH v4 3/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
@ 2022-06-07 22:49 ` Junio C Hamano
2022-06-08 0:22 ` Glen Choo
0 siblings, 1 reply; 67+ messages in thread
From: Junio C Hamano @ 2022-06-07 22:49 UTC (permalink / raw)
To: Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee,
Emily Shaffer, Glen Choo
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> diff --git a/upload-pack.c b/upload-pack.c
> index 3a851b36066..09f48317b02 100644
> --- a/upload-pack.c
> +++ b/upload-pack.c
> @@ -1321,18 +1321,27 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
> data->advertise_sid = git_config_bool(var, value);
> }
>
> - if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
> - current_config_scope() != CONFIG_SCOPE_WORKTREE) {
> - if (!strcmp("uploadpack.packobjectshook", var))
> - return git_config_string(&data->pack_objects_hook, var, value);
> - }
> -
The lossage of this block is because this general git_config()
callback routine that is used to read from any scope is no longer
used to pick up the sensitive variable. Instead, we need to get it
with a different API, namely, git_protected_config().
It is probably is good that in the new code we are not encouraging
folks to write random comparisons on current_config_scope(), and
instead uniformly use a git_protected_config(). That may promote
consistency.
An obvious alternative to achieve the same consistency would be to
introduce a helper, and rewrite (instead of removing) the above part
like so:
if (in_protected_scope()) {
... parse sensitive variable ...
}
We would not need any other change to this file in this patch if we
go that route, I suspect.
> if (parse_object_filter_config(var, value, data) < 0)
> return -1;
>
> return parse_hide_refs_config(var, value, "uploadpack");
> }
>
> +static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
> +{
> + struct upload_pack_data *data = cb_data;
> +
> + if (!strcmp("uploadpack.packobjectshook", var))
> + return git_config_string(&data->pack_objects_hook, var, value);
> + return 0;
> +}
> +
> +static void get_upload_pack_config(struct upload_pack_data *data)
> +{
> + git_config(upload_pack_config, data);
> + git_protected_config(upload_pack_protected_config, data);
> +}
Where we used to just do git_config(upload_pack_config), we now need
to do a separate git_protected_config(). It feels a bit wasteful to
iterate over the same configset twice, but it is not like we are
doing the IO and text file parsing multiple times. This looks quite
straight-forward.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 3/5] config: read protected config with `git_protected_config()`
2022-06-07 22:49 ` Junio C Hamano
@ 2022-06-08 0:22 ` Glen Choo
0 siblings, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-08 0:22 UTC (permalink / raw)
To: Junio C Hamano, Glen Choo via GitGitGadget
Cc: git, Taylor Blau, brian m. carlson, Derrick Stolee, Emily Shaffer
Junio C Hamano <gitster@pobox.com> writes:
> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
>> diff --git a/upload-pack.c b/upload-pack.c
>> index 3a851b36066..09f48317b02 100644
>> --- a/upload-pack.c
>> +++ b/upload-pack.c
>> @@ -1321,18 +1321,27 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
>> data->advertise_sid = git_config_bool(var, value);
>> }
>>
>> - if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
>> - current_config_scope() != CONFIG_SCOPE_WORKTREE) {
>> - if (!strcmp("uploadpack.packobjectshook", var))
>> - return git_config_string(&data->pack_objects_hook, var, value);
>> - }
>> -
>
> The lossage of this block is because this general git_config()
> callback routine that is used to read from any scope is no longer
> used to pick up the sensitive variable. Instead, we need to get it
> with a different API, namely, git_protected_config().
>
> It is probably is good that in the new code we are not encouraging
> folks to write random comparisons on current_config_scope(), and
> instead uniformly use a git_protected_config(). That may promote
> consistency.
>
> An obvious alternative to achieve the same consistency would be to
> introduce a helper, and rewrite (instead of removing) the above part
> like so:
>
> if (in_protected_scope()) {
> ... parse sensitive variable ...
> }
>
> We would not need any other change to this file in this patch if we
> go that route, I suspect.
Yes, and as noted in the commit message, this approach seems to work for
`safe.directory` and `discovery.bare` too.
>> if (parse_object_filter_config(var, value, data) < 0)
>> return -1;
>>
>> return parse_hide_refs_config(var, value, "uploadpack");
>> }
>>
>> +static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
>> +{
>> + struct upload_pack_data *data = cb_data;
>> +
>> + if (!strcmp("uploadpack.packobjectshook", var))
>> + return git_config_string(&data->pack_objects_hook, var, value);
>> + return 0;
>> +}
>> +
>> +static void get_upload_pack_config(struct upload_pack_data *data)
>> +{
>> + git_config(upload_pack_config, data);
>> + git_protected_config(upload_pack_protected_config, data);
>> +}
>
> Where we used to just do git_config(upload_pack_config), we now need
> to do a separate git_protected_config(). It feels a bit wasteful to
> iterate over the same configset twice, but it is not like we are
> doing the IO and text file parsing multiple times. This looks quite
> straight-forward.
Yeah it's not optimal, but at the very least, I think it's easy enough
to understand that we could replace it with something more economical in
the future.
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v4 4/5] safe.directory: use git_protected_config()
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (2 preceding siblings ...)
2022-06-07 20:57 ` [PATCH v4 3/5] config: read protected config with `git_protected_config()` Glen Choo via GitGitGadget
@ 2022-06-07 20:57 ` Glen Choo via GitGitGadget
2022-06-07 20:57 ` [PATCH v4 5/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
` (3 subsequent siblings)
7 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-07 20:57 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
Use git_protected_config() to read `safe.directory` instead of
read_very_early_config(), making it 'protected config only'. As a
result, `safe.directory` now respects "-c", so update the tests and docs
accordingly.
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config/safe.txt | 6 +++---
setup.c | 2 +-
t/t0033-safe-directory.sh | 24 ++++++++++--------------
3 files changed, 14 insertions(+), 18 deletions(-)
diff --git a/Documentation/config/safe.txt b/Documentation/config/safe.txt
index ae0e2e3bdb4..2a7d2324250 100644
--- a/Documentation/config/safe.txt
+++ b/Documentation/config/safe.txt
@@ -12,9 +12,9 @@ via `git config --add`. To reset the list of safe directories (e.g. to
override any such directories specified in the system config), add a
`safe.directory` entry with an empty value.
+
-This config setting is only respected when specified in a system or global
-config, not when it is specified in a repository config, via the command
-line option `-c safe.directory=<path>`, or in environment variables.
+This config setting is only respected in protected configuration (see
+<<SCOPES>>). This prevents the untrusted repository from tampering with this
+value.
+
The value of this setting is interpolated, i.e. `~/<path>` expands to a
path relative to the home directory and `%(prefix)/<path>` expands to a
diff --git a/setup.c b/setup.c
index f818dd858c6..847d47f9195 100644
--- a/setup.c
+++ b/setup.c
@@ -1128,7 +1128,7 @@ static int ensure_valid_ownership(const char *path)
is_path_owned_by_current_user(path))
return 1;
- read_very_early_config(safe_directory_cb, &data);
+ git_protected_config(safe_directory_cb, &data);
return data.is_safe;
}
diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh
index 238b25f91a3..5a1cd0d0947 100755
--- a/t/t0033-safe-directory.sh
+++ b/t/t0033-safe-directory.sh
@@ -16,24 +16,20 @@ test_expect_success 'safe.directory is not set' '
expect_rejected_dir
'
-test_expect_success 'ignoring safe.directory on the command line' '
- test_must_fail git -c safe.directory="$(pwd)" status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory on the command line' '
+ git -c safe.directory="$(pwd)" status
'
-test_expect_success 'ignoring safe.directory in the environment' '
- test_must_fail env GIT_CONFIG_COUNT=1 \
- GIT_CONFIG_KEY_0="safe.directory" \
- GIT_CONFIG_VALUE_0="$(pwd)" \
- git status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory in the environment' '
+ env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0="safe.directory" \
+ GIT_CONFIG_VALUE_0="$(pwd)" \
+ git status
'
-test_expect_success 'ignoring safe.directory in GIT_CONFIG_PARAMETERS' '
- test_must_fail env \
- GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
- git status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory in GIT_CONFIG_PARAMETERS' '
+ env GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
+ git status
'
test_expect_success 'ignoring safe.directory in repo config' '
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v4 5/5] setup.c: create `discovery.bare`
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (3 preceding siblings ...)
2022-06-07 20:57 ` [PATCH v4 4/5] safe.directory: use git_protected_config() Glen Choo via GitGitGadget
@ 2022-06-07 20:57 ` Glen Choo via GitGitGadget
2022-06-07 21:37 ` Glen Choo
2022-06-22 22:03 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Jonathan Tan
` (2 subsequent siblings)
7 siblings, 1 reply; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-07 20:57 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
There is a known social engineering attack that takes advantage of the
fact that a working tree can include an entire bare repository,
including a config file. A user could run a Git command inside the bare
repository thinking that the config file of the 'outer' repository would
be used, but in reality, the bare repository's config file (which is
attacker-controlled) is used, which may result in arbitrary code
execution. See [1] for a fuller description and deeper discussion.
A simple mitigation is to forbid bare repositories unless specified via
`--git-dir` or `GIT_DIR`. In environments that don't use bare
repositories, this would be minimally disruptive.
Create a config variable, `discovery.bare`, that tells Git whether or
not to die() when it discovers a bare repository. This only affects
repository discovery, thus it has no effect if discovery was not
done (e.g. `--git-dir` was passed).
This config is an enum of:
- "always": always allow bare repositories (this is the default)
- "never": never allow bare repositories
If we want to protect users from such attacks by default, neither value
will suffice - "always" provides no protection, but "never" is
impractical for bare repository users. A more usable default would be to
allow only non-embedded bare repositories ([2] contains one such
proposal), but detecting if a repository is embedded is potentially
non-trivial, so this work is not implemented in this series.
[1]: https://lore.kernel.org/git/kl6lsfqpygsj.fsf@chooglen-macbookpro.roam.corp.google.com
[2]: https://lore.kernel.org/git/5b969c5e-e802-c447-ad25-6acc0b784582@github.com
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config.txt | 2 +
Documentation/config/discovery.txt | 19 +++++++++
setup.c | 57 ++++++++++++++++++++++++-
t/t0035-discovery-bare.sh | 68 ++++++++++++++++++++++++++++++
4 files changed, 145 insertions(+), 1 deletion(-)
create mode 100644 Documentation/config/discovery.txt
create mode 100755 t/t0035-discovery-bare.sh
diff --git a/Documentation/config.txt b/Documentation/config.txt
index e284b042f22..9a5e1329772 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -409,6 +409,8 @@ include::config/diff.txt[]
include::config/difftool.txt[]
+include::config/discovery.txt[]
+
include::config/extensions.txt[]
include::config/fastimport.txt[]
diff --git a/Documentation/config/discovery.txt b/Documentation/config/discovery.txt
new file mode 100644
index 00000000000..fbe93597e7c
--- /dev/null
+++ b/Documentation/config/discovery.txt
@@ -0,0 +1,19 @@
+discovery.bare::
+ '(Protected config only)' Specifies whether Git will work with a
+ bare repository that it found during repository discovery. This
+ has no effect if the repository is specified directly via the
+ --git-dir command-line option or the GIT_DIR environment
+ variable (see linkgit:git[1]).
++
+The currently supported values are:
++
+* `always`: Git always works with bare repositories
+* `never`: Git never works with bare repositories
++
+This defaults to `always`, but this default may change in the future.
++
+If you do not use bare repositories in your workflow, then it may be
+beneficial to set `discovery.bare` to `never` in your global config.
+This will protect you from attacks that involve cloning a repository
+that contains a bare repository and running a Git command within that
+directory.
diff --git a/setup.c b/setup.c
index 847d47f9195..4d8d3c1bc7d 100644
--- a/setup.c
+++ b/setup.c
@@ -10,6 +10,10 @@
static int inside_git_dir = -1;
static int inside_work_tree = -1;
static int work_tree_config_is_bogus;
+enum discovery_bare_allowed {
+ DISCOVERY_BARE_NEVER = 0,
+ DISCOVERY_BARE_ALWAYS,
+};
static struct startup_info the_startup_info;
struct startup_info *startup_info = &the_startup_info;
@@ -1133,6 +1137,46 @@ static int ensure_valid_ownership(const char *path)
return data.is_safe;
}
+static int discovery_bare_cb(const char *key, const char *value, void *d)
+{
+ enum discovery_bare_allowed *discovery_bare_allowed = d;
+
+ if (strcmp(key, "discovery.bare"))
+ return 0;
+
+ if (!strcmp(value, "never")) {
+ *discovery_bare_allowed = DISCOVERY_BARE_NEVER;
+ return 0;
+ }
+ if (!strcmp(value, "always")) {
+ *discovery_bare_allowed = DISCOVERY_BARE_ALWAYS;
+ return 0;
+ }
+ return -1;
+}
+
+static enum discovery_bare_allowed get_discovery_bare(void)
+{
+ enum discovery_bare_allowed result = DISCOVERY_BARE_ALWAYS;
+ git_protected_config(discovery_bare_cb, &result);
+ return result;
+}
+
+static const char *discovery_bare_allowed_to_string(
+ enum discovery_bare_allowed discovery_bare_allowed)
+{
+ switch (discovery_bare_allowed) {
+ case DISCOVERY_BARE_NEVER:
+ return "never";
+ case DISCOVERY_BARE_ALWAYS:
+ return "always";
+ default:
+ BUG("invalid discovery_bare_allowed %d",
+ discovery_bare_allowed);
+ }
+ return NULL;
+}
+
enum discovery_result {
GIT_DIR_NONE = 0,
GIT_DIR_EXPLICIT,
@@ -1142,7 +1186,8 @@ enum discovery_result {
GIT_DIR_HIT_CEILING = -1,
GIT_DIR_HIT_MOUNT_POINT = -2,
GIT_DIR_INVALID_GITFILE = -3,
- GIT_DIR_INVALID_OWNERSHIP = -4
+ GIT_DIR_INVALID_OWNERSHIP = -4,
+ GIT_DIR_DISALLOWED_BARE = -5,
};
/*
@@ -1239,6 +1284,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
}
if (is_git_directory(dir->buf)) {
+ if (!get_discovery_bare())
+ return GIT_DIR_DISALLOWED_BARE;
if (!ensure_valid_ownership(dir->buf))
return GIT_DIR_INVALID_OWNERSHIP;
strbuf_addstr(gitdir, ".");
@@ -1385,6 +1432,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
}
*nongit_ok = 1;
break;
+ case GIT_DIR_DISALLOWED_BARE:
+ if (!nongit_ok) {
+ die(_("cannot use bare repository '%s' (discovery.bare is '%s')"),
+ dir.buf,
+ discovery_bare_allowed_to_string(get_discovery_bare()));
+ }
+ *nongit_ok = 1;
+ break;
case GIT_DIR_NONE:
/*
* As a safeguard against setup_git_directory_gently_1 returning
diff --git a/t/t0035-discovery-bare.sh b/t/t0035-discovery-bare.sh
new file mode 100755
index 00000000000..0b345d361e6
--- /dev/null
+++ b/t/t0035-discovery-bare.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+test_description='verify discovery.bare checks'
+
+. ./test-lib.sh
+
+pwd="$(pwd)"
+
+expect_accepted () {
+ git "$@" rev-parse --git-dir
+}
+
+expect_rejected () {
+ test_must_fail git "$@" rev-parse --git-dir 2>err &&
+ grep "discovery.bare" err
+}
+
+test_expect_success 'setup bare repo in worktree' '
+ git init outer-repo &&
+ git init --bare outer-repo/bare-repo
+'
+
+test_expect_success 'discovery.bare unset' '
+ (
+ cd outer-repo/bare-repo &&
+ expect_accepted
+ )
+'
+
+test_expect_success 'discovery.bare=always' '
+ git config --global discovery.bare always &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_accepted
+ )
+'
+
+test_expect_success 'discovery.bare=never' '
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_rejected
+ )
+'
+
+test_expect_success 'discovery.bare in the repository' '
+ (
+ cd outer-repo/bare-repo &&
+ # Temporarily set discovery.bare=always, otherwise git
+ # config fails with "fatal: not in a git directory"
+ # (like safe.directory)
+ git config --global discovery.bare always &&
+ git config discovery.bare always &&
+ git config --global discovery.bare never &&
+ expect_rejected
+ )
+'
+
+test_expect_success 'discovery.bare on the command line' '
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_accepted -c discovery.bare=always &&
+ expect_rejected -c discovery.bare=
+ )
+'
+
+test_done
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 5/5] setup.c: create `discovery.bare`
2022-06-07 20:57 ` [PATCH v4 5/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
@ 2022-06-07 21:37 ` Glen Choo
0 siblings, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-07 21:37 UTC (permalink / raw)
To: Glen Choo via GitGitGadget, git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> diff --git a/Documentation/config/discovery.txt b/Documentation/config/discovery.txt
> new file mode 100644
> index 00000000000..fbe93597e7c
> --- /dev/null
> +++ b/Documentation/config/discovery.txt
> @@ -0,0 +1,19 @@
> +discovery.bare::
> + '(Protected config only)' Specifies whether Git will work with a
> + bare repository that it found during repository discovery. This
> + has no effect if the repository is specified directly via the
> + --git-dir command-line option or the GIT_DIR environment
> + variable (see linkgit:git[1]).
Ugh, I forgot to update the docs for `discovery.bare`. This should be
reworded to be consistent with `safe.directory` and
`uploadpack.packObjectsHook`, e.g.
discovery.bare::
Specifies whether Git will work with a bare repository that it found
during repository discovery. This has no effect if the repository is
specified directly via the --git-dir command-line option or the
GIT_DIR environment variable (see linkgit:git[1]).
This config setting is only respected in protected configuration
(see <<SCOPES>>). This prevents the untrusted repository from
tampering with this value.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 0/5] config: introduce discovery.bare and protected config
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (4 preceding siblings ...)
2022-06-07 20:57 ` [PATCH v4 5/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
@ 2022-06-22 22:03 ` Jonathan Tan
2022-06-23 17:13 ` Glen Choo
2022-06-27 18:19 ` Glen Choo
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
7 siblings, 1 reply; 67+ messages in thread
From: Jonathan Tan @ 2022-06-22 22:03 UTC (permalink / raw)
To: Glen Choo via GitGitGadget
Cc: Jonathan Tan, git, Taylor Blau, brian m. carlson, Derrick Stolee,
Junio C Hamano, Emily Shaffer, Glen Choo
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> Glen Choo (5):
> Documentation/git-config.txt: add SCOPES section
> Documentation: define protected configuration
Forgot to mention when I was sending my comments on patch 2: we should
standardize on "protected config" and not use "protected configuration"
anywhere.
> config: read protected config with `git_protected_config()`
> safe.directory: use git_protected_config()
> setup.c: create `discovery.bare`
Thanks - I think this is a nice feature to have. Everything looks good
except for some minor comments on text in patch 2, which I have sent.
One alternative design would have been to have separate configsets for
protected config and non-protected config (or even better, separate
configsets for trace2 config, protected config minus trace2 config, and
non-protected config) but that doesn't have to block the submission of
this patch set.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 0/5] config: introduce discovery.bare and protected config
2022-06-22 22:03 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Jonathan Tan
@ 2022-06-23 17:13 ` Glen Choo
2022-06-23 18:32 ` Junio C Hamano
0 siblings, 1 reply; 67+ messages in thread
From: Glen Choo @ 2022-06-23 17:13 UTC (permalink / raw)
To: Jonathan Tan, Glen Choo via GitGitGadget
Cc: Jonathan Tan, git, Taylor Blau, brian m. carlson, Derrick Stolee,
Junio C Hamano, Emily Shaffer
Jonathan Tan <jonathantanmy@google.com> writes:
> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>> Glen Choo (5):
>> Documentation/git-config.txt: add SCOPES section
>> Documentation: define protected configuration
>
> Forgot to mention when I was sending my comments on patch 2: we should
> standardize on "protected config" and not use "protected configuration"
> anywhere.
Makes sense.
> One alternative design would have been to have separate configsets for
> protected config and non-protected config (or even better, separate
> configsets for trace2 config, protected config minus trace2 config, and
> non-protected config) but that doesn't have to block the submission of
> this patch set.
I suppose that the idea behind this is that we only parse and store each
config file exactly once. It's a good goal, but the whole point of the
configset is that we can query a single struct to figure out the value
of a config variable. Having multiple configsets starts to shift more of
the burden to the callers because they now have to query multiple
configsets to find their desired config value, and we already start to
see some of this unpleasantness in this series.
An alternative that I'd been thinking about is to make a few changes to
the git_config_* + configset API to allow us to use a single configset
for all of our needs:
1. Keep track of what config we've read when reading into
the_repository->config, i.e. instead of a boolean "all config has
been [un]read", we can express "system and global config has been
read, but not local or command config". Then, use this information to
load config from sources as they become available. This will allow us
to read incomplete config for trace2 and setup.c (discovery.bare and
safe.directory), and only read what we need later on.
This assumes that when Git reads config, that config is always valid
later on. So this is broken if, e.g. we read global config file A
during setup, but when we discover the repo, we discard A and read
global config file B instead. I don't know if we do this or if we are
planning to in the future.
2. Add an additional argument that specifies what scopes to respect when
reading config (maybe as a set of flags). This gives us extra
specificity when using the git_config*() functions, so we could get
rid of git_protected_config() like so:
/* Change enum config_scope into flags first... */
#define WIP_SCOPES_PROTECTED = CONFIG_SCOPE_SYSTEM & \
CONFIG_SCOPE_GLOBAL & CONFIG_SCOPE_COMMAND
static enum discovery_bare_allowed get_discovery_bare(void)
{
enum discovery_bare_allowed result = DISCOVERY_BARE_ALWAYS;
git_config(discovery_bare_cb, &result, WIP_SCOPES_PROTECTED);
return result;
}
And as an added bonus, this gives us an easy way to implement the
constant time git_config_*() functions for protected config. We could
even do this without doing 1. first. I haven't looked into whether
we could turn the enum into flags, but otherwise, I think this is
pretty feasible.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 0/5] config: introduce discovery.bare and protected config
2022-06-23 17:13 ` Glen Choo
@ 2022-06-23 18:32 ` Junio C Hamano
2022-06-27 17:34 ` Glen Choo
0 siblings, 1 reply; 67+ messages in thread
From: Junio C Hamano @ 2022-06-23 18:32 UTC (permalink / raw)
To: Glen Choo
Cc: Jonathan Tan, Glen Choo via GitGitGadget, git, Taylor Blau,
brian m. carlson, Derrick Stolee, Emily Shaffer
Glen Choo <chooglen@google.com> writes:
> Jonathan Tan <jonathantanmy@google.com> writes:
>
>> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>>> Glen Choo (5):
>>> Documentation/git-config.txt: add SCOPES section
>>> Documentation: define protected configuration
>>
>> Forgot to mention when I was sending my comments on patch 2: we should
>> standardize on "protected config" and not use "protected configuration"
>> anywhere.
>
> Makes sense.
Using a single word consistently does make sense, but why favor a
non-word over a proper word ;-)?
> I suppose that the idea behind this is that we only parse and store each
> config file exactly once. It's a good goal, but the whole point of the
> configset is that we can query a single struct to figure out the value
> of a config variable. Having multiple configsets starts to shift more of
> the burden to the callers because they now have to query multiple
> configsets to find their desired config value, and we already start to
> see some of this unpleasantness in this series.
Yes, I was worried about this, too. "parse and store exactly once"
may merely be a performance thing, but it still matters, even though
it is not worse than making duplicate callbacks to overwrite globals
that have been already set earlier, which will affect correctness ;-)
> An alternative that I'd been thinking about is to make a few changes to
> the git_config_* + configset API to allow us to use a single configset
> for all of our needs:
>
> 1. Keep track of what config we've read when reading into
> the_repository->config, i.e. instead of a boolean "all config has
> been [un]read", we can express "system and global config has been
> read, but not local or command config". Then, use this information to
> load config from sources as they become available. This will allow us
> to read incomplete config for trace2 and setup.c (discovery.bare and
> safe.directory), and only read what we need later on.
That is not a bad direction to go, but are we sure that we always
read in the right order (and there is one single right order) and
stop at the right step?
config.c::do_git_config_sequence() reads the system and then the
global before the local, the worktree, and the command line. We
would allow the values of "protected" configuration variables to be
inspected by stopping after the first two and inspecting the result
before the local and the rest overrides them, but will we need
*only* that kind of partial configuration reading that stops exactly
there? Even with the proposed "protected" scheme, I thought we plan
to honor the command line ones, so we may need to read
system+global+command without reading anything else to grab the
values only from the protected sources (ah, I like the application
of the adjective "protected" to the source, not variables, because
that is what we are really talking about---alternatively we could
call it "safe"). But if we later read local and worktree ones
lazily, unless we _insert_ them before what we read from the command
line, we'll break the last-one-wins property, so we need to be
careful. I guess each configuration value in the configset knows
where it came from, so it probably is possible to insert the ones
you read lazily later in the right spot.
> 2. Add an additional argument that specifies what scopes to respect when
> reading config (maybe as a set of flags). This gives us extra
> specificity when using the git_config*() functions, so we could get
> rid of git_protected_config() like so:
>
> /* Change enum config_scope into flags first... */
>
> #define WIP_SCOPES_PROTECTED = CONFIG_SCOPE_SYSTEM & \
> CONFIG_SCOPE_GLOBAL & CONFIG_SCOPE_COMMAND
>
> static enum discovery_bare_allowed get_discovery_bare(void)
> {
> enum discovery_bare_allowed result = DISCOVERY_BARE_ALWAYS;
> git_config(discovery_bare_cb, &result, WIP_SCOPES_PROTECTED);
> return result;
> }
Alternatively, we could make the callback aware of the scope for
each var-value it is called and have it filter, but that would be a
bigger surgery.
I think a new iterator git_config_in_scope(), instead of updating
git_config(), would make sense. By definition, all existing
git_config() callers do not need the scope specifiers, and
"protected" may be the first one but will not be the last one that
needs to read from particular scopes.
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 0/5] config: introduce discovery.bare and protected config
2022-06-23 18:32 ` Junio C Hamano
@ 2022-06-27 17:34 ` Glen Choo
0 siblings, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-27 17:34 UTC (permalink / raw)
To: Junio C Hamano
Cc: Jonathan Tan, Glen Choo via GitGitGadget, git, Taylor Blau,
brian m. carlson, Derrick Stolee, Emily Shaffer
Junio C Hamano <gitster@pobox.com> writes:
> Glen Choo <chooglen@google.com> writes:
>
>> Jonathan Tan <jonathantanmy@google.com> writes:
>>
>>> "Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
>>>> Glen Choo (5):
>>>> Documentation/git-config.txt: add SCOPES section
>>>> Documentation: define protected configuration
>>>
>>> Forgot to mention when I was sending my comments on patch 2: we should
>>> standardize on "protected config" and not use "protected configuration"
>>> anywhere.
>>
>> Makes sense.
>
> Using a single word consistently does make sense, but why favor a
> non-word over a proper word ;-)?
Hm, I guess there's an argument that "config" is a term of art that
specifically refers to things from "git config". From that lens, it's much less
confusing to see the CONFIGURATION section in
Documentation/git-config.txt. But the argument is a little flimsy
because I don't think that's something we've stuck to anywhere.
I'll use "configuration" if it's not too unwieldy.
>> I suppose that the idea behind this is that we only parse and store each
>> config file exactly once. It's a good goal, but the whole point of the
>> configset is that we can query a single struct to figure out the value
>> of a config variable. Having multiple configsets starts to shift more of
>> the burden to the callers because they now have to query multiple
>> configsets to find their desired config value, and we already start to
>> see some of this unpleasantness in this series.
>
> Yes, I was worried about this, too. "parse and store exactly once"
> may merely be a performance thing, but it still matters, even though
> it is not worse than making duplicate callbacks to overwrite globals
> that have been already set earlier, which will affect correctness ;-)
Exactly.
>> An alternative that I'd been thinking about is to make a few changes to
>> the git_config_* + configset API to allow us to use a single configset
>> for all of our needs:
>>
>> 1. Keep track of what config we've read when reading into
>> the_repository->config, i.e. instead of a boolean "all config has
>> been [un]read", we can express "system and global config has been
>> read, but not local or command config". Then, use this information to
>> load config from sources as they become available. This will allow us
>> to read incomplete config for trace2 and setup.c (discovery.bare and
>> safe.directory), and only read what we need later on.
>
> That is not a bad direction to go, but are we sure that we always
> read in the right order (and there is one single right order) and
> stop at the right step?
>
> config.c::do_git_config_sequence() reads the system and then the
> global before the local, the worktree, and the command line. We
> would allow the values of "protected" configuration variables to be
> inspected by stopping after the first two and inspecting the result
> before the local and the rest overrides them, but will we need
> *only* that kind of partial configuration reading that stops exactly
> there? Even with the proposed "protected" scheme, I thought we plan
> to honor the command line ones, so we may need to read
> system+global+command without reading anything else to grab the
> values only from the protected sources (ah, I like the application
> of the adjective "protected" to the source, not variables, because
> that is what we are really talking about---alternatively we could
> call it "safe"). But if we later read local and worktree ones
> lazily, unless we _insert_ them before what we read from the command
> line, we'll break the last-one-wins property, so we need to be
> careful. I guess each configuration value in the configset knows
> where it came from, so it probably is possible to insert the ones
> you read lazily later in the right spot.
Yeah, last-one-wins makes this a lot trickier. I thought that it would
be nice to have insert-with-priority because that also eliminates some
of the correctness concerns in this series, i.e. that ensures protected
config has the same priority as regular config, but that's a bigger
undertaking and I'm not certain about the performance.
>> 2. Add an additional argument that specifies what scopes to respect when
>> reading config (maybe as a set of flags). This gives us extra
>> specificity when using the git_config*() functions, so we could get
>> rid of git_protected_config() like so:
>>
>> /* Change enum config_scope into flags first... */
>>
>> #define WIP_SCOPES_PROTECTED = CONFIG_SCOPE_SYSTEM & \
>> CONFIG_SCOPE_GLOBAL & CONFIG_SCOPE_COMMAND
>>
>> static enum discovery_bare_allowed get_discovery_bare(void)
>> {
>> enum discovery_bare_allowed result = DISCOVERY_BARE_ALWAYS;
>> git_config(discovery_bare_cb, &result, WIP_SCOPES_PROTECTED);
>> return result;
>> }
>
> Alternatively, we could make the callback aware of the scope for
> each var-value it is called and have it filter, but that would be a
> bigger surgery.
>
> I think a new iterator git_config_in_scope(), instead of updating
> git_config(), would make sense. By definition, all existing
> git_config() callers do not need the scope specifiers, and
> "protected" may be the first one but will not be the last one that
> needs to read from particular scopes.
Makes sense. The signature of git_config() could stay the same, but we
could refactor it to use git_config_in_scope().
^ permalink raw reply [flat|nested] 67+ messages in thread
* Re: [PATCH v4 0/5] config: introduce discovery.bare and protected config
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (5 preceding siblings ...)
2022-06-22 22:03 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Jonathan Tan
@ 2022-06-27 18:19 ` Glen Choo
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
7 siblings, 0 replies; 67+ messages in thread
From: Glen Choo @ 2022-06-27 18:19 UTC (permalink / raw)
To: Glen Choo via GitGitGadget, git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer
"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> This round doesn't introduce any major code changes. The most notable
> changes are:
>
[...]
>
> * I added a test for "git config --show-scope" and the 'worktree' scope,
> since 'worktree' wasn't listed in Documentation/git-config.txt (6/5).
Erratum: This patch was sent as gc/document-config-worktree-scope [1].
Patch 6/5 obviously doesn't exist.
[1] https://lore.kernel.org/git/pull.1274.git.git.1654637044966.gitgitgadget@gmail.com
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v5 0/5] config: introduce discovery.bare and protected config
2022-06-07 20:57 ` [PATCH v4 0/5] config: introduce discovery.bare and protected config Glen Choo via GitGitGadget
` (6 preceding siblings ...)
2022-06-27 18:19 ` Glen Choo
@ 2022-06-27 18:36 ` Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 1/5] Documentation/git-config.txt: add SCOPES section Glen Choo via GitGitGadget
` (4 more replies)
7 siblings, 5 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-27 18:36 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Jonathan Tan, Glen Choo
The previous round of this series was picked up by the Google-hosted "Review
Club" (the event is on http://tinyurl.com/gitcal). This round incorporates
Jonathan Tan's feedback (thanks!) as well some feedback from Review Club
itself.
This round only contains changes to commit messages and documentation. As
requested in Review Club, I've included a full "Description" section in this
cover letter for the convenience of new readers.
= Description
There is a known social engineering attack that takes advantage of the fact
that a working tree can include an entire bare repository, including a
config file. A user could run a Git command inside the bare repository
thinking that the config file of the 'outer' repository would be used, but
in reality, the bare repository's config file (which is attacker-controlled)
is used, which may result in arbitrary code execution. See [1] for a fuller
description and deeper discussion.
This series implements a simple way of preventing such attacks: create a
config option, discovery.bare, that tells Git whether or not to die when it
finds a bare repository. discovery.bare has two values:
* "always": always allow bare repositories (default), identical to current
behavior
* "never": never allow bare repositories
and users/system administrators who never expect to work with bare
repositories can secure their environments using "never". discovery.bare has
no effect if --git-dir or GIT_DIR is passed because we are confident that
the user is not confused about which repository is being used.
This series does not change the default behavior, but in the long-run, a
"no-embedded" option might be a safe and usable default [2]. "never" is too
restrictive and unlikely to be the default.
For security reasons, discovery.bare cannot be read from repository-level
config (because we would end up trusting the embedded bare repository that
we aren't supposed to trust to begin with). Since this would introduce a 3rd
variable that is only read from 'protected/trusted configuration' (the
others are safe.directory and uploadpack.packObjectsHook) this series also
defines and creates a shared implementation for 'protected configuration'
= Patch organization
* Patch 1 add a section on configuration scopes to our docs
* Patches 2-3 define 'protected configuration' and create a shared
implementation.
* Patch 4 refactors safe.directory to use protected configuration
* Patch 5 adds discovery.bare
= Series history
Changes in v5:
* Standardize the usage of "protected configuration" instead of mixing
"config" and "configuration". This required some unfortunate rewrapping.
* Remove mentions of "trustworthiness" when discussing protected
configuration and focus on what Git does instead.
* The rationale of protected vs non-protected is still kept.
* Fix the stale documentation entry for discovery.bare.
* Include a fuller description of how discovery.bare and "--git-dir"
interact instead of saying "has no effect".
Changes in v4:
* 2/5's commit message now justifies what scopes are included in protected
config
* The global configset is now a file-scope static inside config.c
(previously it was a member of the_repository).
* Rename discovery_bare_config to discovery_bare_allowed
* Make discovery_bare_allowed function-scoped (instead of global).
* Add an expect_accepted helper to the discovery.bare tests.
* Add a helper to "upload-pack" that reads the protected and non-protected
config
Changes in v3:
* Rebase onto a more recent 'master'
* Reframe this feature in only in terms of the 'embedded bare repo' attack.
* Other docs improvements (thanks Stolee in particular!)
* Protected config no longer uses read_very_early_config() and is only read
once
* Protected config now includes "-c"
* uploadpack.packObjectsHook now uses protected config instead of ignoring
repo config using config scopes
Changes in v2:
* Rename safe.barerepository to discovery.bare and make it die()
* Move tests into t/t0034-discovery-bare.sh
* Avoid unnecessary config reading by using a static variable
* Add discovery.bare=cwd
* Fix typos
= Future work
* This series does not implement the "no-embedded" option [2] and I won't
work on it any time soon, but I'd be more than happy to review if someone
sends patches.
* With discovery.bare, if a builtin is marked RUN_SETUP_GENTLY, setup.c
doesn't die() and we don't tell users why their repository was rejected,
e.g. "git config" gives an opaque "fatal: not in a git directory". This
isn't a new problem though, since safe.directory has the same issue.
[1]
https://lore.kernel.org/git/kl6lsfqpygsj.fsf@chooglen-macbookpro.roam.corp.google.com
[2] This was first suggested in
https://lore.kernel.org/git/5b969c5e-e802-c447-ad25-6acc0b784582@github.com
Glen Choo (5):
Documentation/git-config.txt: add SCOPES section
Documentation: define protected configuration
config: learn `git_protected_config()`
safe.directory: use git_protected_config()
setup.c: create `discovery.bare`
Documentation/config.txt | 2 +
Documentation/config/discovery.txt | 23 +++++++++
Documentation/config/safe.txt | 6 +--
Documentation/config/uploadpack.txt | 6 +--
Documentation/git-config.txt | 77 +++++++++++++++++++++++------
config.c | 51 +++++++++++++++++++
config.h | 17 +++++++
setup.c | 59 +++++++++++++++++++++-
t/t0033-safe-directory.sh | 24 ++++-----
t/t0035-discovery-bare.sh | 68 +++++++++++++++++++++++++
t/t5544-pack-objects-hook.sh | 7 ++-
upload-pack.c | 27 ++++++----
12 files changed, 320 insertions(+), 47 deletions(-)
create mode 100644 Documentation/config/discovery.txt
create mode 100755 t/t0035-discovery-bare.sh
base-commit: f770e9f396d48b567ef7b37d273e91ad570a3522
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1261%2Fchooglen%2Fsetup%2Fdisable-bare-repo-config-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1261/chooglen/setup/disable-bare-repo-config-v5
Pull-Request: https://github.com/git/git/pull/1261
Range-diff vs v4:
1: c0e27ab3b3e ! 1: ee9619f6ec0 Documentation/git-config.txt: add SCOPES section
@@ Metadata
## Commit message ##
Documentation/git-config.txt: add SCOPES section
- In a subsequent commit, we will introduce "protected config", which is
- easiest to describe in terms of configuration scopes (i.e. it's the
- union of the 'system', 'global', and 'command' scopes). This description
- is fine for ML discussions, but it's inadequate for end users because we
- don't provide a good description of "config scopes" in the public docs.
+ In a subsequent commit, we will introduce "protected configuration",
+ which is easiest to describe in terms of configuration scopes (i.e. it's
+ the union of the 'system', 'global', and 'command' scopes). This
+ description is fine for ML discussions, but it's inadequate for end
+ users because we don't provide a good description of "configuration
+ scopes" in the public docs.
145d59f482 (config: add '--show-scope' to print the scope of a config
value, 2020-02-10) introduced the word "scope" to our public docs, but
@@ Commit message
those values mean.
Add a SCOPES section to Documentation/git-config.txt that describes the
- config scopes, their corresponding CLI options, and mentions that some
- configuration options are only respected in certain scopes. Then,
+ configuration scopes, their corresponding CLI options, and mentions that
+ some configuration options are only respected in certain scopes. Then,
use the word "scope" to simplify the FILES section and change some
confusing wording.
2: a5a1dcb03e1 ! 2: 43627c05c0b Documentation: define protected configuration
@@ Commit message
Documentation: define protected configuration
For security reasons, there are config variables that are only trusted
- when they are specified in extra-trustworthy configuration scopes, which
- are sometimes referred to on-list as 'protected configuration' [1]. A
- future commit will introduce another such variable, so let's define our
- terms so that we can have consistent documentation and implementation.
+ when they are specified in certain configuration scopes, which are
+ sometimes referred to on-list as 'protected configuration' [1]. A future
+ commit will introduce another such variable, so let's define our terms
+ so that we can have consistent documentation and implementation.
- In our documentation, define 'protected config' as the system, global
- and command config scopes. As a shorthand, I will refer to variables
- that are only respected in protected config as 'protected config only',
- but this term is not used in the documentation.
+ In our documentation, define 'protected configuration' as the system,
+ global and command config scopes. As a shorthand, I will refer to
+ variables that are only respected in protected config as 'protected
+ configuration only', but this term is not used in the documentation.
- This definition of protected configuration is based on whether or not
- Git can reasonably protect the user by ignoring the configuration scope:
+ This definition of protected config is based on whether or not Git can
+ reasonably protect the user by ignoring the configuration scope:
- System, global and command line config are considered protected
because an attacker who has control over any of those can do plenty of
@@ Commit message
considered protected because it is relatively easy for an attacker to
control local config, e.g.:
- On some shared user environments, a non-admin attacker can create a
- repository high up the directory hierarchy (e.g. C:\.git on Windows),
- and a user may accidentally use it when their PS1 automatically
- invokes "git" commands.
+ repository high up the directory hierarchy (e.g. C:\.git on
+ Windows), and a user may accidentally use it when their PS1
+ automatically invokes "git" commands.
`safe.directory` prevents attacks of this form by making sure that
the user intended to use the shared repository. It obviously
@@ Commit message
repository (because "git upload-pack" would fail), but we can limit
the risks by ignoring `uploadpack.packObjectsHook`.
- Only `uploadpack.packObjectsHook` is 'protected config only'. The
+ Only `uploadpack.packObjectsHook` is 'protected configuration only'. The
following variables are intentionally excluded:
- - `safe.directory` should be 'protected config only', but it does not
- technically fit the definition because it is not respected in the
+ - `safe.directory` should be 'protected configuration only', but it does
+ not technically fit the definition because it is not respected in the
"command" scope. A future commit will fix this.
- `trace2.*` happens to read the same scopes as `safe.directory` because
@@ Documentation/git-config.txt: Most configuration options are respected regardles
defined in, but some options are only respected in certain scopes. See the
option's documentation for the full details.
-+Protected config
-+~~~~~~~~~~~~~~~~
-+
-+Protected config refers to the 'system', 'global', and 'command' scopes. Git
-+considers these scopes to be especially trustworthy because they are likely
-+to be controlled by the user or a trusted administrator. An attacker who
-+controls these scopes can do substantial harm without using Git, so it is
-+assumed that the user's environment protects these scopes against attackers.
++Protected configuration
++~~~~~~~~~~~~~~~~~~~~~~~
+
++Protected configuration refers to the 'system', 'global', and 'command' scopes.
+For security reasons, certain options are only respected when they are
-+specified in protected config, and ignored otherwise.
++specified in protected configuration, and ignored otherwise.
++
++Git treats these scopes as if they are controlled by the user or a trusted
++administrator. This is because an attacker who controls these scopes can do
++substantial harm without using Git, so it is assumed that the user's environment
++protects these scopes against attackers.
+
ENVIRONMENT
-----------
3: 94b40907e66 ! 3: 3efe282e6b9 config: read protected config with `git_protected_config()`
@@ Metadata
Author: Glen Choo <chooglen@google.com>
## Commit message ##
- config: read protected config with `git_protected_config()`
+ config: learn `git_protected_config()`
- `uploadpack.packObjectsHook` is the only 'protected config only'
+ `uploadpack.packObjectsHook` is the only 'protected configuration only'
variable today, but we've noted that `safe.directory` and the upcoming
- `discovery.bare` should also be 'protected config only'. So, for
+ `discovery.bare` should also be 'protected configuration only'. So, for
consistency, we'd like to have a single implementation for protected
config.
The primary constraints are:
- 1. Reading from protected config should be as fast as possible. Nearly
- all "git" commands inside a bare repository will read both
+ 1. Reading from protected configuration should be as fast as possible.
+ Nearly all "git" commands inside a bare repository will read both
`safe.directory` and `discovery.bare`, so we cannot afford to be
slow.
@@ Commit message
`safe.directory` and `discovery.bare` both affect repository
discovery and the gitdir is not known at that point [1].
- The chosen implementation in this commit is to read protected config and
- cache the values in a global configset. This is similar to the caching
- behavior we get with the_repository->config.
+ The chosen implementation in this commit is to read protected
+ configuration and cache the values in a global configset. This is
+ similar to the caching behavior we get with the_repository->config.
- Introduce git_protected_config(), which reads protected config and
- caches them in the global configset protected_config. Then, refactor
+ Introduce git_protected_config(), which reads protected configuration
+ and caches them in the global configset protected_config. Then, refactor
`uploadpack.packObjectsHook` to use git_protected_config().
- The protected config functions are named similarly to their
+ The protected configuration functions are named similarly to their
non-protected counterparts, e.g. git_protected_config_check_init() vs
git_config_check_init().
In light of constraint 1, this implementation can still be improved
since git_protected_config() iterates through every variable in
protected_config, which may still be too expensive. There exist constant
- time lookup functions for non-protected config (repo_config_get_*()),
- but for simplicity, this commit does not implement similar functions for
- protected config.
+ time lookup functions for non-protected configuration
+ (repo_config_get_*()), but for simplicity, this commit does not
+ implement similar functions for protected configuration.
An alternative that avoids introducing another configset is to continue
to read all config using git_config(), but only accept values that have
@@ Commit message
the_repository->config, which would need to be reset when the gitdir is
known and git_config() needs to read the local and worktree config.
Resetting the_repository->config might be reasonable while we only have
- these 'protected config only' variables, but it's not clear whether this
- extends well to future variables.
+ these 'protected configuration only' variables, but it's not clear
+ whether this extends well to future variables.
[1] In this case, we do have a candidate gitdir though, so with a little
refactoring, it might be possible to provide a gitdir.
4: 156817966fa ! 4: ec925823414 safe.directory: use git_protected_config()
@@ Commit message
safe.directory: use git_protected_config()
Use git_protected_config() to read `safe.directory` instead of
- read_very_early_config(), making it 'protected config only'. As a
- result, `safe.directory` now respects "-c", so update the tests and docs
- accordingly.
+ read_very_early_config(), making it 'protected configuration only'.
+
+ As a result, `safe.directory` now respects "-c", so update the tests and
+ docs accordingly. It used to ignore "-c" due to how it was implemented,
+ not because of security or correctness concerns [1].
+
+ [1] https://lore.kernel.org/git/xmqqlevabcsu.fsf@gitster.g/
Signed-off-by: Glen Choo <chooglen@google.com>
5: 29053d029f8 ! 5: 14411512783 setup.c: create `discovery.bare`
@@ Commit message
Create a config variable, `discovery.bare`, that tells Git whether or
not to die() when it discovers a bare repository. This only affects
repository discovery, thus it has no effect if discovery was not
- done (e.g. `--git-dir` was passed).
+ done, e.g. if the user passes `--git-dir=my-dir`, discovery will be
+ skipped and my-dir will be used as the repo regardless of the
+ `discovery.bare` value.
This config is an enum of:
@@ Documentation/config.txt: include::config/diff.txt[]
## Documentation/config/discovery.txt (new) ##
@@
+discovery.bare::
-+ '(Protected config only)' Specifies whether Git will work with a
-+ bare repository that it found during repository discovery. This
-+ has no effect if the repository is specified directly via the
-+ --git-dir command-line option or the GIT_DIR environment
-+ variable (see linkgit:git[1]).
++ Specifies whether Git will work with a bare repository that it
++ found during repository discovery. If the repository is
++ specified directly via the --git-dir command-line option or the
++ GIT_DIR environment variable (see linkgit:git[1]), Git will
++ always use the specified repository, regardless of this value.
+++
++This config setting is only respected in protected configuration (see
++<<SCOPES>>). This prevents the untrusted repository from tampering with
++this value.
++
+The currently supported values are:
++
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v5 1/5] Documentation/git-config.txt: add SCOPES section
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
@ 2022-06-27 18:36 ` Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 2/5] Documentation: define protected configuration Glen Choo via GitGitGadget
` (3 subsequent siblings)
4 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-27 18:36 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Jonathan Tan, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
In a subsequent commit, we will introduce "protected configuration",
which is easiest to describe in terms of configuration scopes (i.e. it's
the union of the 'system', 'global', and 'command' scopes). This
description is fine for ML discussions, but it's inadequate for end
users because we don't provide a good description of "configuration
scopes" in the public docs.
145d59f482 (config: add '--show-scope' to print the scope of a config
value, 2020-02-10) introduced the word "scope" to our public docs, but
that only enumerates the scopes and assumes the user can figure out
those values mean.
Add a SCOPES section to Documentation/git-config.txt that describes the
configuration scopes, their corresponding CLI options, and mentions that
some configuration options are only respected in certain scopes. Then,
use the word "scope" to simplify the FILES section and change some
confusing wording.
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/git-config.txt | 64 ++++++++++++++++++++++++++++--------
1 file changed, 50 insertions(+), 14 deletions(-)
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 9376e39aef2..f93d437b898 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -297,8 +297,8 @@ The default is to use a pager.
FILES
-----
-If not set explicitly with `--file`, there are four files where
-'git config' will search for configuration options:
+By default, 'git config' will read configuration options from multiple
+files:
$(prefix)/etc/gitconfig::
System-wide configuration file.
@@ -322,27 +322,63 @@ $GIT_DIR/config.worktree::
This is optional and is only searched when
`extensions.worktreeConfig` is present in $GIT_DIR/config.
-If no further options are given, all reading options will read all of these
-files that are available. If the global or the system-wide configuration
-file are not available they will be ignored. If the repository configuration
-file is not available or readable, 'git config' will exit with a non-zero
-error code. However, in neither case will an error message be issued.
+You may also provide additional configuration parameters when running any
+git command by using the `-c` option. See linkgit:git[1] for details.
+
+Options will be read from all of these files that are available. If the
+global or the system-wide configuration file are not available they will be
+ignored. If the repository configuration file is not available or readable,
+'git config' will exit with a non-zero error code. However, in neither case
+will an error message be issued.
The files are read in the order given above, with last value found taking
precedence over values read earlier. When multiple values are taken then all
values of a key from all files will be used.
-You may override individual configuration parameters when running any git
-command by using the `-c` option. See linkgit:git[1] for details.
-
-All writing options will per default write to the repository specific
+By default, options are only written to the repository specific
configuration file. Note that this also affects options like `--replace-all`
and `--unset`. *'git config' will only ever change one file at a time*.
-You can override these rules using the `--global`, `--system`,
-`--local`, `--worktree`, and `--file` command-line options; see
-<<OPTIONS>> above.
+You can change the way options are read/written by specifying the path to a
+file (`--file`), or by specifying a configuration scope (`--system`,
+`--global`, `--local`, `--worktree`); see <<OPTIONS>> above.
+
+SCOPES
+------
+
+Each configuration source falls within a configuration scope. The scopes
+are:
+
+system::
+ $(prefix)/etc/gitconfig
+
+global::
+ $XDG_CONFIG_HOME/git/config
++
+~/.gitconfig
+
+local::
+ $GIT_DIR/config
+
+worktree::
+ $GIT_DIR/config.worktree
+
+command::
+ environment variables
++
+the `-c` option
+
+With the exception of 'command', each scope corresponds to a command line
+option - `--system`, `--global`, `--local`, `--worktree`.
+
+When reading options, specifying a scope will only read options from the
+files within that scope. When writing options, specifying a scope will write
+to the files within that scope (instead of the repository specific
+configuration file). See <<OPTIONS>> above for a complete description.
+Most configuration options are respected regardless of the scope it is
+defined in, but some options are only respected in certain scopes. See the
+option's documentation for the full details.
ENVIRONMENT
-----------
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v5 2/5] Documentation: define protected configuration
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 1/5] Documentation/git-config.txt: add SCOPES section Glen Choo via GitGitGadget
@ 2022-06-27 18:36 ` Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 3/5] config: learn `git_protected_config()` Glen Choo via GitGitGadget
` (2 subsequent siblings)
4 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-27 18:36 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Jonathan Tan, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
For security reasons, there are config variables that are only trusted
when they are specified in certain configuration scopes, which are
sometimes referred to on-list as 'protected configuration' [1]. A future
commit will introduce another such variable, so let's define our terms
so that we can have consistent documentation and implementation.
In our documentation, define 'protected configuration' as the system,
global and command config scopes. As a shorthand, I will refer to
variables that are only respected in protected config as 'protected
configuration only', but this term is not used in the documentation.
This definition of protected config is based on whether or not Git can
reasonably protect the user by ignoring the configuration scope:
- System, global and command line config are considered protected
because an attacker who has control over any of those can do plenty of
harm without Git, so we gain very little by ignoring those scopes.
- On the other hand, local (and similarly, worktree) config are not
considered protected because it is relatively easy for an attacker to
control local config, e.g.:
- On some shared user environments, a non-admin attacker can create a
repository high up the directory hierarchy (e.g. C:\.git on
Windows), and a user may accidentally use it when their PS1
automatically invokes "git" commands.
`safe.directory` prevents attacks of this form by making sure that
the user intended to use the shared repository. It obviously
shouldn't be read from the repository, because that would end up
trusting the repository that Git was supposed to reject.
- "git upload-pack" is expected to run in repositories that may not be
controlled by the user. We cannot ignore all config in that
repository (because "git upload-pack" would fail), but we can limit
the risks by ignoring `uploadpack.packObjectsHook`.
Only `uploadpack.packObjectsHook` is 'protected configuration only'. The
following variables are intentionally excluded:
- `safe.directory` should be 'protected configuration only', but it does
not technically fit the definition because it is not respected in the
"command" scope. A future commit will fix this.
- `trace2.*` happens to read the same scopes as `safe.directory` because
they share an implementation. However, this is not for security
reasons; it is because we want to start tracing so early that
repository-level config and "-c" are not available [2].
This requirement is unique to `trace2.*`, so it does not makes sense
for protected configuration to be subject to the same constraints.
[1] For example,
https://lore.kernel.org/git/6af83767-576b-75c4-c778-0284344a8fe7@github.com/
[2] https://lore.kernel.org/git/a0c89d0d-669e-bf56-25d2-cbb09b012e70@jeffhostetler.com/
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config/uploadpack.txt | 6 +++---
Documentation/git-config.txt | 13 +++++++++++++
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/Documentation/config/uploadpack.txt b/Documentation/config/uploadpack.txt
index 32fad5bbe81..029abbefdff 100644
--- a/Documentation/config/uploadpack.txt
+++ b/Documentation/config/uploadpack.txt
@@ -49,9 +49,9 @@ uploadpack.packObjectsHook::
`pack-objects` to the hook, and expects a completed packfile on
stdout.
+
-Note that this configuration variable is ignored if it is seen in the
-repository-level config (this is a safety measure against fetching from
-untrusted repositories).
+Note that this configuration variable is only respected when it is specified
+in protected config (see <<SCOPES>>). This is a safety measure against
+fetching from untrusted repositories.
uploadpack.allowFilter::
If this option is set, `upload-pack` will support partial
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index f93d437b898..f1810952891 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -343,6 +343,7 @@ You can change the way options are read/written by specifying the path to a
file (`--file`), or by specifying a configuration scope (`--system`,
`--global`, `--local`, `--worktree`); see <<OPTIONS>> above.
+[[SCOPES]]
SCOPES
------
@@ -380,6 +381,18 @@ Most configuration options are respected regardless of the scope it is
defined in, but some options are only respected in certain scopes. See the
option's documentation for the full details.
+Protected configuration
+~~~~~~~~~~~~~~~~~~~~~~~
+
+Protected configuration refers to the 'system', 'global', and 'command' scopes.
+For security reasons, certain options are only respected when they are
+specified in protected configuration, and ignored otherwise.
+
+Git treats these scopes as if they are controlled by the user or a trusted
+administrator. This is because an attacker who controls these scopes can do
+substantial harm without using Git, so it is assumed that the user's environment
+protects these scopes against attackers.
+
ENVIRONMENT
-----------
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v5 3/5] config: learn `git_protected_config()`
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 1/5] Documentation/git-config.txt: add SCOPES section Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 2/5] Documentation: define protected configuration Glen Choo via GitGitGadget
@ 2022-06-27 18:36 ` Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 4/5] safe.directory: use git_protected_config() Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 5/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
4 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-27 18:36 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Jonathan Tan, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
`uploadpack.packObjectsHook` is the only 'protected configuration only'
variable today, but we've noted that `safe.directory` and the upcoming
`discovery.bare` should also be 'protected configuration only'. So, for
consistency, we'd like to have a single implementation for protected
config.
The primary constraints are:
1. Reading from protected configuration should be as fast as possible.
Nearly all "git" commands inside a bare repository will read both
`safe.directory` and `discovery.bare`, so we cannot afford to be
slow.
2. Protected config must be readable when the gitdir is not known.
`safe.directory` and `discovery.bare` both affect repository
discovery and the gitdir is not known at that point [1].
The chosen implementation in this commit is to read protected
configuration and cache the values in a global configset. This is
similar to the caching behavior we get with the_repository->config.
Introduce git_protected_config(), which reads protected configuration
and caches them in the global configset protected_config. Then, refactor
`uploadpack.packObjectsHook` to use git_protected_config().
The protected configuration functions are named similarly to their
non-protected counterparts, e.g. git_protected_config_check_init() vs
git_config_check_init().
In light of constraint 1, this implementation can still be improved
since git_protected_config() iterates through every variable in
protected_config, which may still be too expensive. There exist constant
time lookup functions for non-protected configuration
(repo_config_get_*()), but for simplicity, this commit does not
implement similar functions for protected configuration.
An alternative that avoids introducing another configset is to continue
to read all config using git_config(), but only accept values that have
the correct config scope [2]. This technically fulfills constraint 2,
because git_config() simply ignores the local and worktree config when
the gitdir is not known. However, this would read incomplete config into
the_repository->config, which would need to be reset when the gitdir is
known and git_config() needs to read the local and worktree config.
Resetting the_repository->config might be reasonable while we only have
these 'protected configuration only' variables, but it's not clear
whether this extends well to future variables.
[1] In this case, we do have a candidate gitdir though, so with a little
refactoring, it might be possible to provide a gitdir.
[2] This is how `uploadpack.packObjectsHook` was implemented prior to
this commit.
Signed-off-by: Glen Choo <chooglen@google.com>
---
config.c | 51 ++++++++++++++++++++++++++++++++++++
config.h | 17 ++++++++++++
t/t5544-pack-objects-hook.sh | 7 ++++-
upload-pack.c | 27 ++++++++++++-------
4 files changed, 91 insertions(+), 11 deletions(-)
diff --git a/config.c b/config.c
index 9b0e9c93285..29e62f5d0ed 100644
--- a/config.c
+++ b/config.c
@@ -81,6 +81,18 @@ static enum config_scope current_parsing_scope;
static int pack_compression_seen;
static int zlib_compression_seen;
+/*
+ * Config that comes from trusted sources, namely:
+ * - system config files (e.g. /etc/gitconfig)
+ * - global config files (e.g. $HOME/.gitconfig,
+ * $XDG_CONFIG_HOME/git)
+ * - the command line.
+ *
+ * This is declared here for code cleanliness, but unlike the other
+ * static variables, this does not hold config parser state.
+ */
+static struct config_set protected_config;
+
static int config_file_fgetc(struct config_source *conf)
{
return getc_unlocked(conf->u.file);
@@ -2378,6 +2390,11 @@ int git_configset_add_file(struct config_set *cs, const char *filename)
return git_config_from_file(config_set_callback, filename, cs);
}
+int git_configset_add_parameters(struct config_set *cs)
+{
+ return git_config_from_parameters(config_set_callback, cs);
+}
+
int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
{
const struct string_list *values = NULL;
@@ -2619,6 +2636,40 @@ int repo_config_get_pathname(struct repository *repo,
return ret;
}
+/* Read values into protected_config. */
+static void read_protected_config(void)
+{
+ char *xdg_config = NULL, *user_config = NULL, *system_config = NULL;
+
+ git_configset_init(&protected_config);
+
+ system_config = git_system_config();
+ git_global_config(&user_config, &xdg_config);
+
+ git_configset_add_file(&protected_config, system_config);
+ git_configset_add_file(&protected_config, xdg_config);
+ git_configset_add_file(&protected_config, user_config);
+ git_configset_add_parameters(&protected_config);
+
+ free(system_config);
+ free(xdg_config);
+ free(user_config);
+}
+
+/* Ensure that protected_config has been initialized. */
+static void git_protected_config_check_init(void)
+{
+ if (protected_config.hash_initialized)
+ return;
+ read_protected_config();
+}
+
+void git_protected_config(config_fn_t fn, void *data)
+{
+ git_protected_config_check_init();
+ configset_iter(&protected_config, fn, data);
+}
+
/* Functions used historically to read configuration from 'the_repository' */
void git_config(config_fn_t fn, void *data)
{
diff --git a/config.h b/config.h
index 7654f61c634..e3ff1fcf683 100644
--- a/config.h
+++ b/config.h
@@ -446,6 +446,15 @@ void git_configset_init(struct config_set *cs);
*/
int git_configset_add_file(struct config_set *cs, const char *filename);
+/**
+ * Parses command line options and environment variables, and adds the
+ * variable-value pairs to the `config_set`. Returns 0 on success, or -1
+ * if there is an error in parsing. The caller decides whether to free
+ * the incomplete configset or continue using it when the function
+ * returns -1.
+ */
+int git_configset_add_parameters(struct config_set *cs);
+
/**
* Finds and returns the value list, sorted in order of increasing priority
* for the configuration variable `key` and config set `cs`. When the
@@ -505,6 +514,14 @@ int repo_config_get_maybe_bool(struct repository *repo,
int repo_config_get_pathname(struct repository *repo,
const char *key, const char **dest);
+/*
+ * Functions for reading protected config. By definition, protected
+ * config ignores repository config, so it is unnecessary to read
+ * protected config from any `struct repository` other than
+ * the_repository.
+ */
+void git_protected_config(config_fn_t fn, void *data);
+
/**
* Querying For Specific Variables
* -------------------------------
diff --git a/t/t5544-pack-objects-hook.sh b/t/t5544-pack-objects-hook.sh
index dd5f44d986f..54f54f8d2eb 100755
--- a/t/t5544-pack-objects-hook.sh
+++ b/t/t5544-pack-objects-hook.sh
@@ -56,7 +56,12 @@ test_expect_success 'hook does not run from repo config' '
! grep "hook running" stderr &&
test_path_is_missing .git/hook.args &&
test_path_is_missing .git/hook.stdin &&
- test_path_is_missing .git/hook.stdout
+ test_path_is_missing .git/hook.stdout &&
+
+ # check that global config is used instead
+ test_config_global uploadpack.packObjectsHook ./hook &&
+ git clone --no-local . dst2.git 2>stderr &&
+ grep "hook running" stderr
'
test_expect_success 'hook works with partial clone' '
diff --git a/upload-pack.c b/upload-pack.c
index 3a851b36066..09f48317b02 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -1321,18 +1321,27 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
data->advertise_sid = git_config_bool(var, value);
}
- if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
- current_config_scope() != CONFIG_SCOPE_WORKTREE) {
- if (!strcmp("uploadpack.packobjectshook", var))
- return git_config_string(&data->pack_objects_hook, var, value);
- }
-
if (parse_object_filter_config(var, value, data) < 0)
return -1;
return parse_hide_refs_config(var, value, "uploadpack");
}
+static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
+{
+ struct upload_pack_data *data = cb_data;
+
+ if (!strcmp("uploadpack.packobjectshook", var))
+ return git_config_string(&data->pack_objects_hook, var, value);
+ return 0;
+}
+
+static void get_upload_pack_config(struct upload_pack_data *data)
+{
+ git_config(upload_pack_config, data);
+ git_protected_config(upload_pack_protected_config, data);
+}
+
void upload_pack(const int advertise_refs, const int stateless_rpc,
const int timeout)
{
@@ -1340,8 +1349,7 @@ void upload_pack(const int advertise_refs, const int stateless_rpc,
struct upload_pack_data data;
upload_pack_data_init(&data);
-
- git_config(upload_pack_config, &data);
+ get_upload_pack_config(&data);
data.stateless_rpc = stateless_rpc;
data.timeout = timeout;
@@ -1695,8 +1703,7 @@ int upload_pack_v2(struct repository *r, struct packet_reader *request)
upload_pack_data_init(&data);
data.use_sideband = LARGE_PACKET_MAX;
-
- git_config(upload_pack_config, &data);
+ get_upload_pack_config(&data);
while (state != FETCH_DONE) {
switch (state) {
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v5 4/5] safe.directory: use git_protected_config()
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
` (2 preceding siblings ...)
2022-06-27 18:36 ` [PATCH v5 3/5] config: learn `git_protected_config()` Glen Choo via GitGitGadget
@ 2022-06-27 18:36 ` Glen Choo via GitGitGadget
2022-06-27 18:36 ` [PATCH v5 5/5] setup.c: create `discovery.bare` Glen Choo via GitGitGadget
4 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-27 18:36 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Jonathan Tan, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
Use git_protected_config() to read `safe.directory` instead of
read_very_early_config(), making it 'protected configuration only'.
As a result, `safe.directory` now respects "-c", so update the tests and
docs accordingly. It used to ignore "-c" due to how it was implemented,
not because of security or correctness concerns [1].
[1] https://lore.kernel.org/git/xmqqlevabcsu.fsf@gitster.g/
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config/safe.txt | 6 +++---
setup.c | 2 +-
t/t0033-safe-directory.sh | 24 ++++++++++--------------
3 files changed, 14 insertions(+), 18 deletions(-)
diff --git a/Documentation/config/safe.txt b/Documentation/config/safe.txt
index fa02f3ccc54..f72b4408798 100644
--- a/Documentation/config/safe.txt
+++ b/Documentation/config/safe.txt
@@ -12,9 +12,9 @@ via `git config --add`. To reset the list of safe directories (e.g. to
override any such directories specified in the system config), add a
`safe.directory` entry with an empty value.
+
-This config setting is only respected when specified in a system or global
-config, not when it is specified in a repository config, via the command
-line option `-c safe.directory=<path>`, or in environment variables.
+This config setting is only respected in protected configuration (see
+<<SCOPES>>). This prevents the untrusted repository from tampering with this
+value.
+
The value of this setting is interpolated, i.e. `~/<path>` expands to a
path relative to the home directory and `%(prefix)/<path>` expands to a
diff --git a/setup.c b/setup.c
index faf5095e44d..c8e3c32814d 100644
--- a/setup.c
+++ b/setup.c
@@ -1137,7 +1137,7 @@ static int ensure_valid_ownership(const char *path)
is_path_owned_by_current_user(path))
return 1;
- read_very_early_config(safe_directory_cb, &data);
+ git_protected_config(safe_directory_cb, &data);
return data.is_safe;
}
diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh
index 238b25f91a3..5a1cd0d0947 100755
--- a/t/t0033-safe-directory.sh
+++ b/t/t0033-safe-directory.sh
@@ -16,24 +16,20 @@ test_expect_success 'safe.directory is not set' '
expect_rejected_dir
'
-test_expect_success 'ignoring safe.directory on the command line' '
- test_must_fail git -c safe.directory="$(pwd)" status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory on the command line' '
+ git -c safe.directory="$(pwd)" status
'
-test_expect_success 'ignoring safe.directory in the environment' '
- test_must_fail env GIT_CONFIG_COUNT=1 \
- GIT_CONFIG_KEY_0="safe.directory" \
- GIT_CONFIG_VALUE_0="$(pwd)" \
- git status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory in the environment' '
+ env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0="safe.directory" \
+ GIT_CONFIG_VALUE_0="$(pwd)" \
+ git status
'
-test_expect_success 'ignoring safe.directory in GIT_CONFIG_PARAMETERS' '
- test_must_fail env \
- GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
- git status 2>err &&
- grep "unsafe repository" err
+test_expect_success 'safe.directory in GIT_CONFIG_PARAMETERS' '
+ env GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
+ git status
'
test_expect_success 'ignoring safe.directory in repo config' '
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread
* [PATCH v5 5/5] setup.c: create `discovery.bare`
2022-06-27 18:36 ` [PATCH v5 " Glen Choo via GitGitGadget
` (3 preceding siblings ...)
2022-06-27 18:36 ` [PATCH v5 4/5] safe.directory: use git_protected_config() Glen Choo via GitGitGadget
@ 2022-06-27 18:36 ` Glen Choo via GitGitGadget
4 siblings, 0 replies; 67+ messages in thread
From: Glen Choo via GitGitGadget @ 2022-06-27 18:36 UTC (permalink / raw)
To: git
Cc: Taylor Blau, brian m. carlson, Derrick Stolee, Junio C Hamano,
Emily Shaffer, Jonathan Tan, Glen Choo, Glen Choo
From: Glen Choo <chooglen@google.com>
There is a known social engineering attack that takes advantage of the
fact that a working tree can include an entire bare repository,
including a config file. A user could run a Git command inside the bare
repository thinking that the config file of the 'outer' repository would
be used, but in reality, the bare repository's config file (which is
attacker-controlled) is used, which may result in arbitrary code
execution. See [1] for a fuller description and deeper discussion.
A simple mitigation is to forbid bare repositories unless specified via
`--git-dir` or `GIT_DIR`. In environments that don't use bare
repositories, this would be minimally disruptive.
Create a config variable, `discovery.bare`, that tells Git whether or
not to die() when it discovers a bare repository. This only affects
repository discovery, thus it has no effect if discovery was not
done, e.g. if the user passes `--git-dir=my-dir`, discovery will be
skipped and my-dir will be used as the repo regardless of the
`discovery.bare` value.
This config is an enum of:
- "always": always allow bare repositories (this is the default)
- "never": never allow bare repositories
If we want to protect users from such attacks by default, neither value
will suffice - "always" provides no protection, but "never" is
impractical for bare repository users. A more usable default would be to
allow only non-embedded bare repositories ([2] contains one such
proposal), but detecting if a repository is embedded is potentially
non-trivial, so this work is not implemented in this series.
[1]: https://lore.kernel.org/git/kl6lsfqpygsj.fsf@chooglen-macbookpro.roam.corp.google.com
[2]: https://lore.kernel.org/git/5b969c5e-e802-c447-ad25-6acc0b784582@github.com
Signed-off-by: Glen Choo <chooglen@google.com>
---
Documentation/config.txt | 2 +
Documentation/config/discovery.txt | 23 ++++++++++
setup.c | 57 ++++++++++++++++++++++++-
t/t0035-discovery-bare.sh | 68 ++++++++++++++++++++++++++++++
4 files changed, 149 insertions(+), 1 deletion(-)
create mode 100644 Documentation/config/discovery.txt
create mode 100755 t/t0035-discovery-bare.sh
diff --git a/Documentation/config.txt b/Documentation/config.txt
index e284b042f22..9a5e1329772 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -409,6 +409,8 @@ include::config/diff.txt[]
include::config/difftool.txt[]
+include::config/discovery.txt[]
+
include::config/extensions.txt[]
include::config/fastimport.txt[]
diff --git a/Documentation/config/discovery.txt b/Documentation/config/discovery.txt
new file mode 100644
index 00000000000..bbcf89bb0b5
--- /dev/null
+++ b/Documentation/config/discovery.txt
@@ -0,0 +1,23 @@
+discovery.bare::
+ Specifies whether Git will work with a bare repository that it
+ found during repository discovery. If the repository is
+ specified directly via the --git-dir command-line option or the
+ GIT_DIR environment variable (see linkgit:git[1]), Git will
+ always use the specified repository, regardless of this value.
++
+This config setting is only respected in protected configuration (see
+<<SCOPES>>). This prevents the untrusted repository from tampering with
+this value.
++
+The currently supported values are:
++
+* `always`: Git always works with bare repositories
+* `never`: Git never works with bare repositories
++
+This defaults to `always`, but this default may change in the future.
++
+If you do not use bare repositories in your workflow, then it may be
+beneficial to set `discovery.bare` to `never` in your global config.
+This will protect you from attacks that involve cloning a repository
+that contains a bare repository and running a Git command within that
+directory.
diff --git a/setup.c b/setup.c
index c8e3c32814d..16938fd5a24 100644
--- a/setup.c
+++ b/setup.c
@@ -10,6 +10,10 @@
static int inside_git_dir = -1;
static int inside_work_tree = -1;
static int work_tree_config_is_bogus;
+enum discovery_bare_allowed {
+ DISCOVERY_BARE_NEVER = 0,
+ DISCOVERY_BARE_ALWAYS,
+};
static struct startup_info the_startup_info;
struct startup_info *startup_info = &the_startup_info;
@@ -1142,6 +1146,46 @@ static int ensure_valid_ownership(const char *path)
return data.is_safe;
}
+static int discovery_bare_cb(const char *key, const char *value, void *d)
+{
+ enum discovery_bare_allowed *discovery_bare_allowed = d;
+
+ if (strcmp(key, "discovery.bare"))
+ return 0;
+
+ if (!strcmp(value, "never")) {
+ *discovery_bare_allowed = DISCOVERY_BARE_NEVER;
+ return 0;
+ }
+ if (!strcmp(value, "always")) {
+ *discovery_bare_allowed = DISCOVERY_BARE_ALWAYS;
+ return 0;
+ }
+ return -1;
+}
+
+static enum discovery_bare_allowed get_discovery_bare(void)
+{
+ enum discovery_bare_allowed result = DISCOVERY_BARE_ALWAYS;
+ git_protected_config(discovery_bare_cb, &result);
+ return result;
+}
+
+static const char *discovery_bare_allowed_to_string(
+ enum discovery_bare_allowed discovery_bare_allowed)
+{
+ switch (discovery_bare_allowed) {
+ case DISCOVERY_BARE_NEVER:
+ return "never";
+ case DISCOVERY_BARE_ALWAYS:
+ return "always";
+ default:
+ BUG("invalid discovery_bare_allowed %d",
+ discovery_bare_allowed);
+ }
+ return NULL;
+}
+
enum discovery_result {
GIT_DIR_NONE = 0,
GIT_DIR_EXPLICIT,
@@ -1151,7 +1195,8 @@ enum discovery_result {
GIT_DIR_HIT_CEILING = -1,
GIT_DIR_HIT_MOUNT_POINT = -2,
GIT_DIR_INVALID_GITFILE = -3,
- GIT_DIR_INVALID_OWNERSHIP = -4
+ GIT_DIR_INVALID_OWNERSHIP = -4,
+ GIT_DIR_DISALLOWED_BARE = -5,
};
/*
@@ -1248,6 +1293,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
}
if (is_git_directory(dir->buf)) {
+ if (!get_discovery_bare())
+ return GIT_DIR_DISALLOWED_BARE;
if (!ensure_valid_ownership(dir->buf))
return GIT_DIR_INVALID_OWNERSHIP;
strbuf_addstr(gitdir, ".");
@@ -1394,6 +1441,14 @@ const char *setup_git_directory_gently(int *nongit_ok)
}
*nongit_ok = 1;
break;
+ case GIT_DIR_DISALLOWED_BARE:
+ if (!nongit_ok) {
+ die(_("cannot use bare repository '%s' (discovery.bare is '%s')"),
+ dir.buf,
+ discovery_bare_allowed_to_string(get_discovery_bare()));
+ }
+ *nongit_ok = 1;
+ break;
case GIT_DIR_NONE:
/*
* As a safeguard against setup_git_directory_gently_1 returning
diff --git a/t/t0035-discovery-bare.sh b/t/t0035-discovery-bare.sh
new file mode 100755
index 00000000000..0b345d361e6
--- /dev/null
+++ b/t/t0035-discovery-bare.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+test_description='verify discovery.bare checks'
+
+. ./test-lib.sh
+
+pwd="$(pwd)"
+
+expect_accepted () {
+ git "$@" rev-parse --git-dir
+}
+
+expect_rejected () {
+ test_must_fail git "$@" rev-parse --git-dir 2>err &&
+ grep "discovery.bare" err
+}
+
+test_expect_success 'setup bare repo in worktree' '
+ git init outer-repo &&
+ git init --bare outer-repo/bare-repo
+'
+
+test_expect_success 'discovery.bare unset' '
+ (
+ cd outer-repo/bare-repo &&
+ expect_accepted
+ )
+'
+
+test_expect_success 'discovery.bare=always' '
+ git config --global discovery.bare always &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_accepted
+ )
+'
+
+test_expect_success 'discovery.bare=never' '
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_rejected
+ )
+'
+
+test_expect_success 'discovery.bare in the repository' '
+ (
+ cd outer-repo/bare-repo &&
+ # Temporarily set discovery.bare=always, otherwise git
+ # config fails with "fatal: not in a git directory"
+ # (like safe.directory)
+ git config --global discovery.bare always &&
+ git config discovery.bare always &&
+ git config --global discovery.bare never &&
+ expect_rejected
+ )
+'
+
+test_expect_success 'discovery.bare on the command line' '
+ git config --global discovery.bare never &&
+ (
+ cd outer-repo/bare-repo &&
+ expect_accepted -c discovery.bare=always &&
+ expect_rejected -c discovery.bare=
+ )
+'
+
+test_done
--
gitgitgadget
^ permalink raw reply [flat|nested] 67+ messages in thread