Skip to content

Commit 1eb10f4

Browse files
peffgitster
authored andcommitted
unix-socket: handle long socket pathnames
On many systems, the sockaddr_un.sun_path field is quite small. Even on Linux, it is only 108 characters. A user of the credential-cache daemon can easily surpass this, especially if their home directory is in a deep directory tree (since the default location expands ~/.git-credentials). We can hack around this in the unix-socket.[ch] code by doing a chdir() to the enclosing directory, feeding the relative basename to the socket functions, and then restoring the working directory. This introduces several new possible error cases for creating a socket, including an irrecoverable one in the case that we can't restore the working directory. In the case of the credential-cache code, we could perhaps get away with simply chdir()-ing to the socket directory and never coming back. However, I'd rather do it at the lower level for a few reasons: 1. It keeps the hackery behind an opaque interface instead of polluting the main program logic. 2. A hack in credential-cache won't help any unix-socket users who come along later. 3. The chdir trickery isn't that likely to fail (basically it's only a problem if your cwd is missing or goes away while you're running). And because we only enable the hack when we get a too-long name, it can only fail in cases that would have failed under the previous code anyway. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98c2924 commit 1eb10f4

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

unix-socket.c

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,109 @@ static int unix_stream_socket(void)
99
return fd;
1010
}
1111

12-
static void unix_sockaddr_init(struct sockaddr_un *sa, const char *path)
12+
static int chdir_len(const char *orig, int len)
13+
{
14+
char *path = xmemdupz(orig, len);
15+
int r = chdir(path);
16+
free(path);
17+
return r;
18+
}
19+
20+
struct unix_sockaddr_context {
21+
char orig_dir[PATH_MAX];
22+
};
23+
24+
static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
25+
{
26+
if (!ctx->orig_dir[0])
27+
return;
28+
/*
29+
* If we fail, we can't just return an error, since we have
30+
* moved the cwd of the whole process, which could confuse calling
31+
* code. We are better off to just die.
32+
*/
33+
if (chdir(ctx->orig_dir) < 0)
34+
die("unable to restore original working directory");
35+
}
36+
37+
static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
38+
struct unix_sockaddr_context *ctx)
1339
{
1440
int size = strlen(path) + 1;
15-
if (size > sizeof(sa->sun_path))
16-
die("socket path is too long to fit in sockaddr");
41+
42+
ctx->orig_dir[0] = '\0';
43+
if (size > sizeof(sa->sun_path)) {
44+
const char *slash = find_last_dir_sep(path);
45+
const char *dir;
46+
47+
if (!slash) {
48+
errno = ENAMETOOLONG;
49+
return -1;
50+
}
51+
52+
dir = path;
53+
path = slash + 1;
54+
size = strlen(path) + 1;
55+
if (size > sizeof(sa->sun_path)) {
56+
errno = ENAMETOOLONG;
57+
return -1;
58+
}
59+
60+
if (!getcwd(ctx->orig_dir, sizeof(ctx->orig_dir))) {
61+
errno = ENAMETOOLONG;
62+
return -1;
63+
}
64+
if (chdir_len(dir, slash - dir) < 0)
65+
return -1;
66+
}
67+
1768
memset(sa, 0, sizeof(*sa));
1869
sa->sun_family = AF_UNIX;
1970
memcpy(sa->sun_path, path, size);
71+
return 0;
2072
}
2173

2274
int unix_stream_connect(const char *path)
2375
{
2476
int fd;
2577
struct sockaddr_un sa;
78+
struct unix_sockaddr_context ctx;
2679

27-
unix_sockaddr_init(&sa, path);
80+
if (unix_sockaddr_init(&sa, path, &ctx) < 0)
81+
return -1;
2882
fd = unix_stream_socket();
2983
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
84+
unix_sockaddr_cleanup(&ctx);
3085
close(fd);
3186
return -1;
3287
}
88+
unix_sockaddr_cleanup(&ctx);
3389
return fd;
3490
}
3591

3692
int unix_stream_listen(const char *path)
3793
{
3894
int fd;
3995
struct sockaddr_un sa;
96+
struct unix_sockaddr_context ctx;
4097

41-
unix_sockaddr_init(&sa, path);
98+
if (unix_sockaddr_init(&sa, path, &ctx) < 0)
99+
return -1;
42100
fd = unix_stream_socket();
43101

44102
unlink(path);
45103
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
104+
unix_sockaddr_cleanup(&ctx);
46105
close(fd);
47106
return -1;
48107
}
49108

50109
if (listen(fd, 5) < 0) {
110+
unix_sockaddr_cleanup(&ctx);
51111
close(fd);
52112
return -1;
53113
}
54114

115+
unix_sockaddr_cleanup(&ctx);
55116
return fd;
56117
}

0 commit comments

Comments
 (0)