set_git_dir: fix crash when used with real_path()
`real_path()` returns result from a shared buffer, inviting subtle reentrance bugs. One of these bugs occur when invoked this way: set_git_dir(real_path(git_dir)) In this case, `real_path()` has reentrance: real_path read_gitfile_gently repo_set_gitdir setup_git_env set_git_dir_1 set_git_dir Later, `set_git_dir()` uses its now-dead parameter: !is_absolute_path(path) Fix this by using a dedicated `strbuf` to hold `strbuf_realpath()`. Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexandr Miloslavskiy committed
Mar 6, 2020 at 19:03 UTC
0915a5b4cdf00a8c6c755b77b854725a183993b4
5 files changed
+23
-14
builtin/init-db.c
+2
-2
@@ -356,12 +356,12 @@ int init_db(const char *git_dir, const char *real_git_dir,
356
if (!exist_ok && !stat(real_git_dir, &st))
357
die(_("%s already exists"), real_git_dir);
358
359
- set_git_dir(real_path(real_git_dir));
359
+ set_git_dir(real_git_dir, 1);
360
git_dir = get_git_dir();
361
separate_git_dir(git_dir, original_git_dir);
362
}
363
else {
364
- set_git_dir(real_path(git_dir));
364
+ set_git_dir(git_dir, 1);
365
git_dir = get_git_dir();
366
}
367
startup_info->have_repository = 1;
cache.h
+1
-1
@@ -543,7 +543,7 @@ const char *get_git_common_dir(void);
543
char *get_object_directory(void);
544
char *get_index_file(void);
545
char *get_graft_file(struct repository *r);
546
-void set_git_dir(const char *path);
546
+void set_git_dir(const char *path, int make_realpath);
547
int get_common_dir_noenv(struct strbuf *sb, const char *gitdir);
548
int get_common_dir(struct strbuf *sb, const char *gitdir);
549
const char *get_git_namespace(void);
environment.c
+10
-1
@@ -345,11 +345,20 @@ static void update_relative_gitdir(const char *name,
345
free(path);
346
}
347
348
-void set_git_dir(const char *path)
348
+void set_git_dir(const char *path, int make_realpath)
349
{
350
+ struct strbuf realpath = STRBUF_INIT;
351
+
352
+ if (make_realpath) {
353
+ strbuf_realpath(&realpath, path, 1);
354
+ path = realpath.buf;
355
+ }
356
+
357
set_git_dir_1(path);
358
if (!is_absolute_path(path))
359
chdir_notify_register(NULL, update_relative_gitdir, NULL);
360
+
361
+ strbuf_release(&realpath);
362
}
363
364
const char *get_log_output_encoding(void)
path.c
+1
-1
@@ -850,7 +850,7 @@ const char *enter_repo(const char *path, int strict)
850
}
851
852
if (is_git_directory(".")) {
853
- set_git_dir(".");
853
+ set_git_dir(".", 0);
854
check_repository_format();
855
return path;
856
}
setup.c
+9
-9
@@ -725,7 +725,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
725
}
726
727
/* #18, #26 */
728
- set_git_dir(gitdirenv);
728
+ set_git_dir(gitdirenv, 0);
729
free(gitfile);
730
return NULL;
731
}
@@ -747,7 +747,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
747
}
748
else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
749
/* #16d */
750
- set_git_dir(gitdirenv);
750
+ set_git_dir(gitdirenv, 0);
751
free(gitfile);
752
return NULL;
753
}
@@ -759,14 +759,14 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
759
760
/* both get_git_work_tree() and cwd are already normalized */
761
if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
762
- set_git_dir(gitdirenv);
762
+ set_git_dir(gitdirenv, 0);
763
free(gitfile);
764
return NULL;
765
}
766
767
offset = dir_inside_of(cwd->buf, worktree);
768
if (offset >= 0) { /* cwd inside worktree? */
769
- set_git_dir(real_path(gitdirenv));
769
+ set_git_dir(gitdirenv, 1);
770
if (chdir(worktree))
771
die_errno(_("cannot chdir to '%s'"), worktree);
772
strbuf_addch(cwd, '/');
@@ -775,7 +775,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
775
}
776
777
/* cwd outside worktree */
778
- set_git_dir(gitdirenv);
778
+ set_git_dir(gitdirenv, 0);
779
free(gitfile);
780
return NULL;
781
}
@@ -804,7 +804,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
804
805
/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
806
if (is_bare_repository_cfg > 0) {
807
- set_git_dir(offset == cwd->len ? gitdir : real_path(gitdir));
807
+ set_git_dir(gitdir, (offset != cwd->len));
808
if (chdir(cwd->buf))
809
die_errno(_("cannot come back to cwd"));
810
return NULL;
@@ -813,7 +813,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
813
/* #0, #1, #5, #8, #9, #12, #13 */
814
set_git_work_tree(".");
815
if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
816
- set_git_dir(gitdir);
816
+ set_git_dir(gitdir, 0);
817
inside_git_dir = 0;
818
inside_work_tree = 1;
819
if (offset >= cwd->len)
@@ -856,10 +856,10 @@ static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
856
die_errno(_("cannot come back to cwd"));
857
root_len = offset_1st_component(cwd->buf);
858
strbuf_setlen(cwd, offset > root_len ? offset : root_len);
859
- set_git_dir(cwd->buf);
859
+ set_git_dir(cwd->buf, 0);
860
}
861
else
862
- set_git_dir(".");
862
+ set_git_dir(".", 0);
863
return NULL;
864
}
865