New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double close happens if connecting IPC failed #431

Open
yasuoka opened this Issue Oct 4, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@yasuoka

yasuoka commented Oct 4, 2018

https://github.com/heimdal/heimdal/blob/master/lib/ipc/client.c#L322-L341

    322 static int
    323 connect_unix(struct path_ctx *s)
    324 {
    325     struct sockaddr_un addr;
    326 
    327     addr.sun_family = AF_UNIX;
    328     strlcpy(addr.sun_path, s->path, sizeof(addr.sun_path));
    329 
    330     s->fd = socket(AF_UNIX, SOCK_STREAM, 0);
    331     if (s->fd < 0)
    332         return errno;
    333     rk_cloexec(s->fd);
    334 
    335     if (connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    336         close(s->fd);
    337         return errno;
    338     }
    339 
    340     return 0;
    341 }

When connect(2) failed at 335 the fd is closed and the caller calls common_release() which also close(2) the fd. https://github.com/heimdal/heimdal/blob/master/lib/ipc/client.c#L364-L378

    364 static int
    365 unix_socket_init(const char *service,
    366                  void **ctx)
    367 {
    368     int ret;
    369 
    370     ret = common_path_init(service, "socket", ctx);
    371     if (ret)
    372         return ret;
    373     ret = connect_unix(*ctx);
    374     if (ret)
    375         common_release(*ctx);
    376 
    377     return ret;
    378 }

The following diff will fix the problem.

index 82ef2bfc2..0a4ab7314 100644
--- a/lib/ipc/client.c
+++ b/lib/ipc/client.c
@@ -334,6 +334,7 @@ connect_unix(struct path_ctx *s)
 
     if (connect(s->fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
        close(s->fd);
+       s->fd = -1;
        return errno;
     }
 

I found this because my program which runs with multi threads caused random crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment