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

Commit 4698c8f

Browse files
jrngitster
authored andcommitted
config: allow inaccessible configuration under $HOME
The changes v1.7.12.1~2^2~4 (config: warn on inaccessible files, 2012-08-21) and v1.8.1.1~22^2~2 (config: treat user and xdg config permission problems as errors, 2012-10-13) were intended to prevent important configuration (think "[transfer] fsckobjects") from being ignored when the configuration is unintentionally unreadable (for example with EIO on a flaky filesystem, or with ENOMEM due to a DoS attack). Usually ~/.gitconfig and ~/.config/git are readable by the current user, and if they aren't then it would be easy to fix those permissions, so the damage from adding this check should have been minimal. Unfortunately the access() check often trips when git is being run as a server. A daemon (such as inetd or git-daemon) starts as "root", creates a listening socket, and then drops privileges, meaning that when git commands are invoked they cannot access $HOME and die with fatal: unable to access '/root/.config/git/config': Permission denied Any patch to fix this would have one of three problems: 1. We annoy sysadmins who need to take an extra step to handle HOME when dropping privileges (the current behavior, or any other proposal that they have to opt into). 2. We annoy sysadmins who want to set HOME when dropping privileges, either by making what they want to do impossible, or making them set an extra variable or option to accomplish what used to work (e.g., a patch to git-daemon to set HOME when --user is passed). 3. We loosen the check, so some cases which might be noteworthy are not caught. This patch is of type (3). Treat user and xdg configuration that are inaccessible due to permissions (EACCES) as though no user configuration was provided at all. An alternative method would be to check if $HOME is readable, but that would not help in cases where the user who dropped privileges had a globally readable HOME with only .config or .gitconfig being private. This does not change the behavior when /etc/gitconfig or .git/config is unreadable (since those are more serious configuration errors), nor when ~/.gitconfig or ~/.config/git is unreadable due to problems other than permissions. Signed-off-by: Jonathan Nieder <[email protected]> Improved-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a46221e commit 4698c8f

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

builtin/config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
379379
*/
380380
die("$HOME not set");
381381

382-
if (access_or_warn(user_config, R_OK) &&
383-
xdg_config && !access_or_warn(xdg_config, R_OK))
382+
if (access_or_warn(user_config, R_OK, 0) &&
383+
xdg_config && !access_or_warn(xdg_config, R_OK, 0))
384384
given_config_file = xdg_config;
385385
else
386386
given_config_file = user_config;

config.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
5858
path = buf.buf;
5959
}
6060

61-
if (!access_or_die(path, R_OK)) {
61+
if (!access_or_die(path, R_OK, 0)) {
6262
if (++inc->depth > MAX_INCLUDE_DEPTH)
6363
die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
6464
cf && cf->name ? cf->name : "the command line");
@@ -954,23 +954,23 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
954954

955955
home_config_paths(&user_config, &xdg_config, "config");
956956

957-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK)) {
957+
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
958958
ret += git_config_from_file(fn, git_etc_gitconfig(),
959959
data);
960960
found += 1;
961961
}
962962

963-
if (xdg_config && !access_or_die(xdg_config, R_OK)) {
963+
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
964964
ret += git_config_from_file(fn, xdg_config, data);
965965
found += 1;
966966
}
967967

968-
if (user_config && !access_or_die(user_config, R_OK)) {
968+
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
969969
ret += git_config_from_file(fn, user_config, data);
970970
found += 1;
971971
}
972972

973-
if (repo_config && !access_or_die(repo_config, R_OK)) {
973+
if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
974974
ret += git_config_from_file(fn, repo_config, data);
975975
found += 1;
976976
}

dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,9 +1637,9 @@ void setup_standard_excludes(struct dir_struct *dir)
16371637
home_config_paths(NULL, &xdg_path, "ignore");
16381638
excludes_file = xdg_path;
16391639
}
1640-
if (!access_or_warn(path, R_OK))
1640+
if (!access_or_warn(path, R_OK, 0))
16411641
add_excludes_from_file(dir, path);
1642-
if (excludes_file && !access_or_warn(excludes_file, R_OK))
1642+
if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
16431643
add_excludes_from_file(dir, excludes_file);
16441644
}
16451645

git-compat-util.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,9 @@ int remove_or_warn(unsigned int mode, const char *path);
687687
* Call access(2), but warn for any error except "missing file"
688688
* (ENOENT or ENOTDIR).
689689
*/
690-
int access_or_warn(const char *path, int mode);
691-
int access_or_die(const char *path, int mode);
690+
#define ACCESS_EACCES_OK (1U << 0)
691+
int access_or_warn(const char *path, int mode, unsigned flag);
692+
int access_or_die(const char *path, int mode, unsigned flag);
692693

693694
/* Warn on an inaccessible file that ought to be accessible */
694695
void warn_on_inaccessible(const char *path);

wrapper.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,24 @@ void warn_on_inaccessible(const char *path)
408408
warning(_("unable to access '%s': %s"), path, strerror(errno));
409409
}
410410

411-
int access_or_warn(const char *path, int mode)
411+
static int access_error_is_ok(int err, unsigned flag)
412+
{
413+
return err == ENOENT || err == ENOTDIR ||
414+
((flag & ACCESS_EACCES_OK) && err == EACCES);
415+
}
416+
417+
int access_or_warn(const char *path, int mode, unsigned flag)
412418
{
413419
int ret = access(path, mode);
414-
if (ret && errno != ENOENT && errno != ENOTDIR)
420+
if (ret && !access_error_is_ok(errno, flag))
415421
warn_on_inaccessible(path);
416422
return ret;
417423
}
418424

419-
int access_or_die(const char *path, int mode)
425+
int access_or_die(const char *path, int mode, unsigned flag)
420426
{
421427
int ret = access(path, mode);
422-
if (ret && errno != ENOENT && errno != ENOTDIR)
428+
if (ret && !access_error_is_ok(errno, flag))
423429
die_errno(_("unable to access '%s'"), path);
424430
return ret;
425431
}

0 commit comments

Comments
 (0)