Skip to content

Commit c37c6dc

Browse files
kevinoidgitster
authored andcommitted
setup: don't die if realpath(3) fails on getcwd(3)
Prior to Git 2.35.0, git could be run from an inaccessible working directory so long as the git repository specified by options and/or environment variables was accessible. For example: git init repo mkdir -p a/b cd a/b chmod u-x .. git -C "${PWD%/a/b}/repo" status If this example seems a bit contrived, consider running with the repository owner as a substitute UID (e.g. with runuser(1) or sudo(8)) without ensuring the working directory is accessible by that user. The code added by e6f8861 ("setup: introduce startup_info->original_cwd") to preserve the working directory attempts to normalize the path using strbuf_realpath(). If that fails, as in the case above, it is treated as a fatal error. This commit treats strbuf_realpath() errors as non-fatal. If an error occurs, setup_original_cwd() will continue without applying removal prevention for cwd, resulting in the pre-2.35.0 behavior. The risk should be minimal, since git will not operate on a repository with inaccessible ancestors, this behavior is only known to occur when cwd is a descendant of the repository, an ancestor of cwd is inaccessible, and no ancestors of the repository are inaccessible. Signed-off-by: Kevin Locke <[email protected]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a3eb28 commit c37c6dc

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

setup.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,16 @@ static void setup_original_cwd(void)
459459
*/
460460

461461
/* Normalize the directory */
462-
strbuf_realpath(&tmp, tmp_original_cwd, 1);
462+
if (!strbuf_realpath(&tmp, tmp_original_cwd, 0)) {
463+
trace2_data_string("setup", the_repository,
464+
"realpath-path", tmp_original_cwd);
465+
trace2_data_string("setup", the_repository,
466+
"realpath-failure", strerror(errno));
467+
free((char*)tmp_original_cwd);
468+
tmp_original_cwd = NULL;
469+
return;
470+
}
471+
463472
free((char*)tmp_original_cwd);
464473
tmp_original_cwd = NULL;
465474
startup_info->original_cwd = strbuf_detach(&tmp, NULL);

0 commit comments

Comments
 (0)