Skip to content

Commit 77e522c

Browse files
jeffhostetlergitster
authored andcommitted
unix-socket: disallow chdir() when creating unix domain sockets
Calls to `chdir()` are dangerous in a multi-threaded context. If `unix_stream_listen()` or `unix_stream_connect()` is given a socket pathname that is too long to fit in a `sockaddr_un` structure, it will `chdir()` to the parent directory of the requested socket pathname, create the socket using a relative pathname, and then `chdir()` back. This is not thread-safe. Teach `unix_sockaddr_init()` to not allow calls to `chdir()` when this flag is set. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 55144cc commit 77e522c

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

builtin/credential-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
static int send_request(const char *socket, const struct strbuf *out)
1515
{
1616
int got_data = 0;
17-
int fd = unix_stream_connect(socket);
17+
int fd = unix_stream_connect(socket, 0);
1818

1919
if (fd < 0)
2020
return -1;

unix-socket.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ static void unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx)
3030
}
3131

3232
static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
33-
struct unix_sockaddr_context *ctx)
33+
struct unix_sockaddr_context *ctx,
34+
int disallow_chdir)
3435
{
3536
int size = strlen(path) + 1;
3637

3738
ctx->orig_dir = NULL;
3839
if (size > sizeof(sa->sun_path)) {
39-
const char *slash = find_last_dir_sep(path);
40+
const char *slash;
4041
const char *dir;
4142
struct strbuf cwd = STRBUF_INIT;
4243

44+
if (disallow_chdir) {
45+
errno = ENAMETOOLONG;
46+
return -1;
47+
}
48+
49+
slash = find_last_dir_sep(path);
4350
if (!slash) {
4451
errno = ENAMETOOLONG;
4552
return -1;
@@ -65,13 +72,13 @@ static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
6572
return 0;
6673
}
6774

68-
int unix_stream_connect(const char *path)
75+
int unix_stream_connect(const char *path, int disallow_chdir)
6976
{
7077
int fd = -1, saved_errno;
7178
struct sockaddr_un sa;
7279
struct unix_sockaddr_context ctx;
7380

74-
if (unix_sockaddr_init(&sa, path, &ctx) < 0)
81+
if (unix_sockaddr_init(&sa, path, &ctx, disallow_chdir) < 0)
7582
return -1;
7683
fd = socket(AF_UNIX, SOCK_STREAM, 0);
7784
if (fd < 0)
@@ -101,7 +108,7 @@ int unix_stream_listen(const char *path,
101108

102109
unlink(path);
103110

104-
if (unix_sockaddr_init(&sa, path, &ctx) < 0)
111+
if (unix_sockaddr_init(&sa, path, &ctx, opts->disallow_chdir) < 0)
105112
return -1;
106113
fd = socket(AF_UNIX, SOCK_STREAM, 0);
107114
if (fd < 0)

unix-socket.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
struct unix_stream_listen_opts {
55
int listen_backlog_size;
6+
unsigned int disallow_chdir:1;
67
};
78

89
#define UNIX_STREAM_LISTEN_OPTS_INIT { 0 }
910

10-
int unix_stream_connect(const char *path);
11+
int unix_stream_connect(const char *path, int disallow_chdir);
1112
int unix_stream_listen(const char *path,
1213
const struct unix_stream_listen_opts *opts);
1314

0 commit comments

Comments
 (0)