Thread View: linux.kernel.git
9 messages
9 total messages
Started by Junio C Hamano
Mon, 19 Oct 2026 14:27
[RFC/PATCH] config: honor file permission bits
Author: Junio C Hamano
Date: Mon, 19 Oct 2026 14:27
Date: Mon, 19 Oct 2026 14:27
102 lines
12899 bytes
12899 bytes
Because our updates to an existing file almost never makes
modifications in place, and instead create a new "lockfile",
propagate the permission bits from the original, copy the contents
from the original file while tweaking the contents as needed, and
then finally rename the lockfile to replace the original file, you
cannot "protect" your .global configuration file from accidental
clobbering by doing something like this:
$ chmod a-w $HOME/.gitconfig
Of course, if you make your $HOME directory unwritable, it may
protect your .gitconfig from getting updated, but that goes totally
against the intuition and is not how you would protect a regular
file from getting overwritten.
Teach the configuration file writing machinery to inspect the
permission bits of the original with access(2) and refrain from
doing the usual "write a lockfile and replace the original with it"
dance ourselves when we shouldn't be able to write it.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
config.c | 22 ++++++++++++++++++++++
t/t1300-config.sh | 30 ++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git c/config.c w/config.c
index e0bb29b53d..2b84fb215c 100644
--- c/config.c
+++ w/config.c
@@ -3065,8 +3065,30 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
if (!config_filename)
config_filename = filename_buf = repo_git_path(r, "config");
+ /*
+ * Do not allow updating a read-only configuration file; it is
+ * annoying and alarming that "chmod a-w $HOME/.gitconfig" does not
+ * prevent the global configuration file from getting clobbered by
+ * mistake with the usual "create a new lockfile, copy the original
+ * while setting a new value, propagate the permission bits to the
+ * lockfile, and then rename it to replace the original" dance.
+ */
+ if (!access(config_filename, W_OK)) {
+ ; /* happy: we can write to it */
+ } else if (errno == ENOENT) {
+ ; /* happy: we are creating this file anew */
+ } else {
+ /*
+ * most likely EACCES but other errors also indicate
+ * that we cannot write to it anyway.
+ */
+ error_errno(_("file %s unwritable"), config_filename);
+ ret = CONFIG_NO_WRITE;
+ goto out_free;
+ }
+
/*
* The lock serves a purpose in addition to locking: the new
* contents of .git/config will be written into it.
*/
diff --git c/t/t1300-config.sh w/t/t1300-config.sh
index e3f8064889..876d376cc7 100755
--- c/t/t1300-config.sh
+++ w/t/t1300-config.sh
@@ -3018,5 +3018,35 @@ test_expect_success 'writing config retries until lock is released' '
git -c core.configLockTimeout=5000 config retried.key value &&
test "$(git config retried.key)" = value
'
+test_expect_success POSIXPERM 'refuse to "overwrite" a read-only file' '
+ gconf=custom-global-config &&
+ test_when_finished "chmod +w $gconf" &&
+ (
+ GIT_CONFIG_GLOBAL=$gconf &&
+ export GIT_CONFIG_GLOBAL &&
+ git config set --global some.variable some-value &&
+ git config list --global >expect &&
+
+ chmod a-w $gconf &&
+ # "get" from read-only file should just work fine.
+ git config list --global >actual &&
+
+ # "set" to read-only file should be refused.
+ test_must_fail git config set --global some.variable other-value &&
+ git config list --global >actual &&
+ test_cmp expect actual &&
+
+ # traditional "config var val" syntax to set
+ test_must_fail git config --global some.variable third-value &&
+ git config list --global >actual &&
+ test_cmp expect actual &&
+
+ # "unset" to read-only file should be refused.
+ test_must_fail git config unset --global some.variable &&
+ git config list --global >actual &&
+ test_cmp expect actual
+ )
+'
+
test_done
Re: [RFC/PATCH] config: honor file permission bits
Author: Junio C Hamano
Date: Thu, 22 Oct 2026 20:59
Date: Thu, 22 Oct 2026 20:59
37 lines
13876 bytes
13876 bytes
Junio C Hamano <gitster@pobox.com> writes: > Because our updates to an existing file almost never makes > modifications in place, and instead create a new "lockfile", > propagate the permission bits from the original, copy the contents > from the original file while tweaking the contents as needed, and > then finally rename the lockfile to replace the original file, you > cannot "protect" your .global configuration file from accidental > clobbering by doing something like this: > > $ chmod a-w $HOME/.gitconfig > > Of course, if you make your $HOME directory unwritable, it may > protect your .gitconfig from getting updated, but that goes totally > against the intuition and is not how you would protect a regular > file from getting overwritten. > > Teach the configuration file writing machinery to inspect the > permission bits of the original with access(2) and refrain from > doing the usual "write a lockfile and replace the original with it" > dance ourselves when we shouldn't be able to write it. > > Signed-off-by: Junio C Hamano <gitster@pobox.com> > --- > config.c | 22 ++++++++++++++++++++++ > t/t1300-config.sh | 30 ++++++++++++++++++++++++++++++ > 2 files changed, 52 insertions(+) Hmph, no responses to this RFC. I somehow find it hard to believe that I was the only person who got annoyed by this behaviour [*]. [Footnote] * I was very tempted to write "unexpected behaviour", but I know what happens in the code path, so it is not unexpected at least to me.
Re: [RFC/PATCH] config: honor file permission bits
Author: "brian m. carlson"
Date: Thu, 22 Oct 2026 21:08
Date: Thu, 22 Oct 2026 21:08
61 lines
11447 bytes
11447 bytes
--Ng5psN6Kn2xcPgeM Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2026-10-22 at 20:59:57, Junio C Hamano wrote: > Junio C Hamano <gitster@pobox.com> writes: >=20 > > Because our updates to an existing file almost never makes > > modifications in place, and instead create a new "lockfile", > > propagate the permission bits from the original, copy the contents > > from the original file while tweaking the contents as needed, and > > then finally rename the lockfile to replace the original file, you > > cannot "protect" your .global configuration file from accidental > > clobbering by doing something like this: > > > > $ chmod a-w $HOME/.gitconfig > > > > Of course, if you make your $HOME directory unwritable, it may > > protect your .gitconfig from getting updated, but that goes totally > > against the intuition and is not how you would protect a regular > > file from getting overwritten. > > > > Teach the configuration file writing machinery to inspect the > > permission bits of the original with access(2) and refrain from > > doing the usual "write a lockfile and replace the original with it" > > dance ourselves when we shouldn't be able to write it. > > > > Signed-off-by: Junio C Hamano <gitster@pobox.com> > > --- > > config.c | 22 ++++++++++++++++++++++ > > t/t1300-config.sh | 30 ++++++++++++++++++++++++++++++ > > 2 files changed, 52 insertions(+) >=20 > Hmph, no responses to this RFC. I somehow find it hard to believe > that I was the only person who got annoyed by this behaviour [*]. Sorry, I looked but forgot to respond. It seems sensible to me. I don't personally make that file read-only, so it doesn't affect me at all; instead, I just overwrite it with my personal configuration every time I deploy my dotfiles. I still think it's a reasonable thing to do to help those who need it, though. --=20 brian m. carlson (they/them) Toronto, Ontario, CA --Ng5psN6Kn2xcPgeM Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- wr0EABYKAG8Fgmraez4JEHwMSWKIh6KBRxQAAAAAAB4AIHNhbHRAbm90YXRpb25z LnNlcXVvaWEtcGdwLm9yZ1uD6T7PD6J/uZMcfj4RYf/1WuIPuele/Wy4Q2klWWQT FiEECCzmip28ZfuD0cORfAxJYoiHooEAAOpyAQCEaT0EEM88uz8bMrYVv+Zq7/x1 WOUDIBZd0gfJmz/ulQEAsA9WYzKzfNRlHhKZy1U8C3BO4vrsPP/FTMDpYhTSYg8= =MHB5 -----END PGP SIGNATURE----- --Ng5psN6Kn2xcPgeM--
Re: [RFC/PATCH] config: honor file permission bits
Author: Junio C Hamano
Date: Thu, 22 Oct 2026 23:29
Date: Thu, 22 Oct 2026 23:29
19 lines
13393 bytes
13393 bytes
"brian m. carlson" <sandals@crustytoothpaste.net> writes: > I don't personally make that file read-only, so it doesn't affect me at > all; instead, I just overwrite it with my personal configuration every > time I deploy my dotfiles. > I don't personally make that file read-only, so it doesn't affect me at > all; instead, I just overwrite it with my personal configuration every > time I deploy my dotfiles. Well, I deploy my dotfiles from the freshly built one (the sources of dotfiles are kept elsewhere under version control), too, but when "installing", I make them read-only, just like all other "installed" files like the contents of my ~/bin/ directory. My personal makefiles have "dry-run" support to tell me what the changes are in the version I am about to install in 'patch' form, and that is how I noticed that "git config" allowed to make such a mistake, even though the file is read-only, to some failed experiment I made earlier.
Re: [RFC/PATCH] config: honor file permission bits
Author: Junio C Hamano
Date: Fri, 23 Oct 2026 14:54
Date: Fri, 23 Oct 2026 14:54
26 lines
13769 bytes
13769 bytes
Patrick Steinhardt <ps@pks.im> writes: > What I wonder is what makes our config file so special compared to > everything else that uses lockfiles. Or put differently: if we think > it's important to honor permissions bits (which I think is sensible), > shouldn't we do that in a central location like in "lockfile.c" so that > all users of this interface behave the same? The $HOME/.gitconfig file and its XDG counterparts are the only out-of-tree files that suffer from this issue, and being outside the repository is what makes them special. Theoretically, /etc/gitconfig on a multi-user system could have the same issue, but the containing directory would typically be read-only for regular users, which makes the issue irrelevant there. I do not see any sensible reason why a user would want to "protect" in-tree files like .git/index from Git, either by running "chmod -w" on them or by any other means. We make files under the .git/objects/ hierarchy read-only, but that is to prevent accidental overwriting by tools other than Git, so it does not serve as an example of Git needing to respect read-only bits in the general lockfile machinery. Having said all that, the patch was intentionally isolated to the caller's side of the API, primarily because auditing all code paths that use the lockfile API was far beyond my available time budget.
[PATCH v2] config: honor file permission bits
Author: Junio C Hamano
Date: Mon, 26 Oct 2026 14:59
Date: Mon, 26 Oct 2026 14:59
103 lines
16232 bytes
16232 bytes
When dotfile sources are kept under version control, you might
protect installed files like ~/.gitconfig from accidental changes by
making them read-only. This fails because updating a file writes a
new lockfile, copies and tweaks the content, and renames the
lockfile over the original. The original file's permission bits are
ignored in this process.
Teach the configuration writing machinery to inspect the original
file's permissions with access(2) and avoid replacing it when the
file should not be writable.
This implementation targets configuration files rather than the
lockfile API layer. While in-tree files like .git/index do not need
such protection, out-of-tree files like ~/.gitconfig and its XDG
counterparts are unique in this regard. System-wide configuration
like /etc/gitconfig is typically in a directory already read-only to
normal users, making the issue irrelevant there.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
* Only the commit message has been updated to mention that this is
deliberately done at the configuration API, not at the lockfile
layer.
config.c | 22 ++++++++++++++++++++++
t/t1300-config.sh | 30 ++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/config.c b/config.c
index e0bb29b53d..2b84fb215c 100644
--- a/config.c
+++ b/config.c
@@ -3066,6 +3066,28 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
if (!config_filename)
config_filename = filename_buf = repo_git_path(r, "config");
+ /*
+ * Do not allow updating a read-only configuration file; it is
+ * annoying and alarming that "chmod a-w $HOME/.gitconfig" does not
+ * prevent the global configuration file from getting clobbered by
+ * mistake with the usual "create a new lockfile, copy the original
+ * while setting a new value, propagate the permission bits to the
+ * lockfile, and then rename it to replace the original" dance.
+ */
+ if (!access(config_filename, W_OK)) {
+ ; /* happy: we can write to it */
+ } else if (errno == ENOENT) {
+ ; /* happy: we are creating this file anew */
+ } else {
+ /*
+ * most likely EACCES but other errors also indicate
+ * that we cannot write to it anyway.
+ */
+ error_errno(_("file %s unwritable"), config_filename);
+ ret = CONFIG_NO_WRITE;
+ goto out_free;
+ }
+
/*
* The lock serves a purpose in addition to locking: the new
* contents of .git/config will be written into it.
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index e3f8064889..876d376cc7 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -3019,4 +3019,34 @@ test_expect_success 'writing config retries until lock is released' '
test "$(git config retried.key)" = value
'
+test_expect_success POSIXPERM 'refuse to "overwrite" a read-only file' '
+ gconf=custom-global-config &&
+ test_when_finished "chmod +w $gconf" &&
+ (
+ GIT_CONFIG_GLOBAL=$gconf &&
+ export GIT_CONFIG_GLOBAL &&
+ git config set --global some.variable some-value &&
+ git config list --global >expect &&
+
+ chmod a-w $gconf &&
+ # "get" from read-only file should just work fine.
+ git config list --global >actual &&
+
+ # "set" to read-only file should be refused.
+ test_must_fail git config set --global some.variable other-value &&
+ git config list --global >actual &&
+ test_cmp expect actual &&
+
+ # traditional "config var val" syntax to set
+ test_must_fail git config --global some.variable third-value &&
+ git config list --global >actual &&
+ test_cmp expect actual &&
+
+ # "unset" to read-only file should be refused.
+ test_must_fail git config unset --global some.variable &&
+ git config list --global >actual &&
+ test_cmp expect actual
+ )
+'
+
test_done
--
2.56.0-369-gb8347ef8b9
Re: [RFC/PATCH] config: honor file permission bits
Author: Junio C Hamano
Date: Mon, 26 Oct 2026 15:01
Date: Mon, 26 Oct 2026 15:01
28 lines
13764 bytes
13764 bytes
Patrick Steinhardt <ps@pks.im> writes: > On Fri, Oct 23, 2026 at 07:54:13AM -0700, Junio C Hamano wrote: >> Patrick Steinhardt <ps@pks.im> writes: >> >> > What I wonder is what makes our config file so special compared to >> > everything else that uses lockfiles. Or put differently: if we think >> > it's important to honor permissions bits (which I think is sensible), >> > shouldn't we do that in a central location like in "lockfile.c" so that >> > all users of this interface behave the same? >> >> The $HOME/.gitconfig file and its XDG counterparts are the only >> out-of-tree files that suffer from this issue, and being outside the >> repository is what makes them special. Theoretically, /etc/gitconfig >> on a multi-user system could have the same issue, but the containing >> directory would typically be read-only for regular users, which makes >> the issue irrelevant there. > > That explanation makes sense to me. Probably something to put into the > commit message :) I ended up rewriting the whole thing from scratch to follow the recommended flow more closely, starting from an observation of an end-user perceived problem, analysis of the issue, and the solution. The above naturally became a sidenote for the solution description. Thanks.
Re: [PATCH v2] config: honor file permission bits
Author: Junio C Hamano
Date: Tue, 03 Nov 2026 19:11
Date: Tue, 03 Nov 2026 19:11
20 lines
13230 bytes
13230 bytes
Patrick Steinhardt <ps@pks.im> writes:
> Nit: a curious reader might wonder why such in-tree files don't need
> such protection.
But isn't it self evident that repository contents are supposed to
be mutable, and you are not even supposed to muck with filesystem
level tools like chmod or mv to begin with?
>> + error_errno(_("file %s unwritable"), config_filename);
>
> Should we quote '%s' here? The function is quite inconsistent, where it
> sometimes quotes and sometimes doesn't. I'd say that quoting is the
> better choice though.
While it probably makes sense, many of the existing messages in the
same function also lack the quote. I would rather leave it for
others to clean up the subsystem as a whole, as I expect there would
probably be a huge fallout in expected test output ;-).
Re: [PATCH v2] config: honor file permission bits
Author: Junio C Hamano
Date: Thu, 05 Nov 2026 18:12
Date: Thu, 05 Nov 2026 18:12
18 lines
13244 bytes
13244 bytes
Patrick Steinhardt <ps@pks.im> writes: > I don't think the other messages would need to be adapted. We already > have a mixture of both styles in this files, so I'd prefer to at least > not make the status quo worse by adding more instances that we know to > be suboptimal. > > In any case, these are just nits and I don't feel strongly about any of > them. I scanned the entire file and got the impression that majority of the messages were without quotes. While I agree with you that should be fixed, the patch followed that majority. You are welcome to direct somebody else to go in and fix all of them, but I do not want to see you doing so yourself, as your time and productivity are more valuable elsewhere ;-)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads