Skip to content

Commit 2a24444

Browse files
committed
Merge branch 'jg/credential-cache-chdir-to-sockdir'
The "credential-cache" daemon process used to run in whatever directory it happened to start in, but this made umount(2)ing the filesystem that houses the repository harder; now the process chdir()s to the directory that house its own socket on startup. * jg/credential-cache-chdir-to-sockdir: credential-cache--daemon: change to the socket dir on startup credential-cache--daemon: disallow relative socket path credential-cache--daemon: refactor check_socket_directory
2 parents 225caa7 + 6e61449 commit 2a24444

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

Documentation/git-credential-cache.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ OPTIONS
3636
cache daemon if one is not started). Defaults to
3737
`~/.git-credential-cache/socket`. If your home directory is on a
3838
network-mounted filesystem, you may need to change this to a
39-
local filesystem.
39+
local filesystem. You must specify an absolute path.
4040

4141
CONTROLLING THE DAEMON
4242
----------------------

credential-cache--daemon.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static const char permissions_advice[] =
215215
"users may be able to read your cached credentials. Consider running:\n"
216216
"\n"
217217
" chmod 0700 %s";
218-
static void check_socket_directory(const char *path)
218+
static void init_socket_directory(const char *path)
219219
{
220220
struct stat st;
221221
char *path_copy = xstrdup(path);
@@ -224,20 +224,27 @@ static void check_socket_directory(const char *path)
224224
if (!stat(dir, &st)) {
225225
if (st.st_mode & 077)
226226
die(permissions_advice, dir);
227-
free(path_copy);
228-
return;
227+
} else {
228+
/*
229+
* We must be sure to create the directory with the correct mode,
230+
* not just chmod it after the fact; otherwise, there is a race
231+
* condition in which somebody can chdir to it, sleep, then try to open
232+
* our protected socket.
233+
*/
234+
if (safe_create_leading_directories_const(dir) < 0)
235+
die_errno("unable to create directories for '%s'", dir);
236+
if (mkdir(dir, 0700) < 0)
237+
die_errno("unable to mkdir '%s'", dir);
229238
}
230239

231-
/*
232-
* We must be sure to create the directory with the correct mode,
233-
* not just chmod it after the fact; otherwise, there is a race
234-
* condition in which somebody can chdir to it, sleep, then try to open
235-
* our protected socket.
236-
*/
237-
if (safe_create_leading_directories_const(dir) < 0)
238-
die_errno("unable to create directories for '%s'", dir);
239-
if (mkdir(dir, 0700) < 0)
240-
die_errno("unable to mkdir '%s'", dir);
240+
if (chdir(dir))
241+
/*
242+
* We don't actually care what our cwd is; we chdir here just to
243+
* be a friendly daemon and avoid tying up our original cwd.
244+
* If this fails, it's OK to just continue without that benefit.
245+
*/
246+
;
247+
241248
free(path_copy);
242249
}
243250

@@ -264,7 +271,10 @@ int main(int argc, const char **argv)
264271
if (!socket_path)
265272
usage_with_options(usage, options);
266273

267-
check_socket_directory(socket_path);
274+
if (!is_absolute_path(socket_path))
275+
die("socket directory must be an absolute path");
276+
277+
init_socket_directory(socket_path);
268278
register_tempfile(&socket_file, socket_path);
269279

270280
if (ignore_sighup)

0 commit comments

Comments
 (0)