Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit daa22c6

Browse files
Eric Wonggitster
authored andcommitted
config: preserve config file permissions on edits
Users may already store sensitive data such as imap.pass in .git/config; making the file world-readable when "git config" is called to edit means their password would be compromised on a shared system. [v2: updated for section renames, as noted by Junio] Signed-off-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0bc85ab commit daa22c6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

config.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,13 @@ int git_config_set_multivar_in_file(const char *config_filename,
16341634
MAP_PRIVATE, in_fd, 0);
16351635
close(in_fd);
16361636

1637+
if (fchmod(fd, st.st_mode & 07777) < 0) {
1638+
error("fchmod on %s failed: %s",
1639+
lock->filename, strerror(errno));
1640+
ret = CONFIG_NO_WRITE;
1641+
goto out_free;
1642+
}
1643+
16371644
if (store.seen == 0)
16381645
store.seen = 1;
16391646

@@ -1782,6 +1789,7 @@ int git_config_rename_section_in_file(const char *config_filename,
17821789
int out_fd;
17831790
char buf[1024];
17841791
FILE *config_file;
1792+
struct stat st;
17851793

17861794
if (new_name && !section_name_is_ok(new_name)) {
17871795
ret = error("invalid section name: %s", new_name);
@@ -1803,6 +1811,14 @@ int git_config_rename_section_in_file(const char *config_filename,
18031811
goto unlock_and_out;
18041812
}
18051813

1814+
fstat(fileno(config_file), &st);
1815+
1816+
if (fchmod(out_fd, st.st_mode & 07777) < 0) {
1817+
ret = error("fchmod on %s failed: %s",
1818+
lock->filename, strerror(errno));
1819+
goto out;
1820+
}
1821+
18061822
while (fgets(buf, sizeof(buf), config_file)) {
18071823
int i;
18081824
int length;

t/t1300-repo-config.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,4 +1154,14 @@ test_expect_failure 'adding a key into an empty section reuses header' '
11541154
test_cmp expect .git/config
11551155
'
11561156

1157+
test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
1158+
chmod 0600 .git/config &&
1159+
git config imap.pass Hunter2 &&
1160+
perl -e \
1161+
"die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
1162+
git config --rename-section imap pop &&
1163+
perl -e \
1164+
"die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
1165+
'
1166+
11571167
test_done

0 commit comments

Comments
 (0)