Skip to content

Commit

Permalink
rpc: avoid socket double free
Browse files Browse the repository at this point in the history
From man:

clnt_destroy(CLIENT *clnt);
 A macro that destroys the client's RPC handle.  Destruction usually involves
 deallocation of  private data  structures,  including clnt itself.  Use of
 clnt is undefined after calling clnt_destroy(). If the RPC library opened the
 associated socket, it will close it also. Otherwise, the socket remains open.

we are closing the socket twice in this case,
i.e. by calling clnt_destroy and then close(sockfd)

Thanks to Pranith for the RC.

Tested-by: Xiubo Li <xiubli@redhat.com>
Reviewed-by: Xiubo Li <xiubli@redhat.com>
Reviewed-by: Pranith Kumar K <pkarampu@redhat.com>
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
  • Loading branch information
Prasanna Kumar Kalever authored and pkalever committed Jul 18, 2018
1 parent 32ee766 commit 766f97e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cli/gluster-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ glusterBlockCliRPC_1(void *cobj, clioperations opt)
{
CLIENT *clnt = NULL;
int ret = -1;
int sockfd = -1;
int sockfd = RPC_ANYSOCK;
struct sockaddr_un saun = {0,};
blockCreateCli *create_obj;
blockDeleteCli *delete_obj;
Expand Down Expand Up @@ -204,7 +204,7 @@ glusterBlockCliRPC_1(void *cobj, clioperations opt)
clnt_destroy (clnt);
}

if (sockfd != -1) {
if (sockfd != RPC_ANYSOCK) {
close (sockfd);
}

Expand Down
8 changes: 4 additions & 4 deletions daemon/gluster-blockd.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ glusterBlockCliThreadProc (void *vargp)
{
register SVCXPRT *transp = NULL;
struct sockaddr_un saun = {0, };
int sockfd = -1;
int sockfd = RPC_ANYSOCK;


if (strlen(GB_UNIX_ADDRESS) > SUN_PATH_MAX) {
Expand Down Expand Up @@ -116,7 +116,7 @@ glusterBlockCliThreadProc (void *vargp)
svc_destroy(transp);
}

if (sockfd != -1) {
if (sockfd != RPC_ANYSOCK) {
close(sockfd);
}

Expand All @@ -129,7 +129,7 @@ glusterBlockServerThreadProc(void *vargp)
{
register SVCXPRT *transp = NULL;
struct sockaddr_in sain = {0, };
int sockfd;
int sockfd = RPC_ANYSOCK;
int opt = 1;
char errMsg[2048] = {0};

Expand Down Expand Up @@ -177,7 +177,7 @@ glusterBlockServerThreadProc(void *vargp)
svc_destroy(transp);
}

if (sockfd != -1) {
if (sockfd != RPC_ANYSOCK) {
close(sockfd);
}

Expand Down
10 changes: 3 additions & 7 deletions rpc/block_svc_routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ glusterBlockGetSockaddr(char *host)

static int glusterBlockHostConnect(char *host)
{
int sockfd = -1;
int sockfd = RPC_ANYSOCK;
int errsv = 0;
struct addrinfo *res = NULL;

Expand Down Expand Up @@ -522,7 +522,7 @@ static int glusterBlockHostConnect(char *host)
freeaddrinfo(res);
}

if (sockfd != -1) {
if (sockfd != RPC_ANYSOCK) {
close(sockfd);
}

Expand Down Expand Up @@ -560,7 +560,7 @@ glusterBlockCallRPC_1(char *host, void *cobj,
{
CLIENT *clnt = NULL;
int ret = -1;
int sockfd = -1;
int sockfd = RPC_ANYSOCK;
int errsv = 0;
size_t i;
blockResponse reply = {0,};
Expand Down Expand Up @@ -690,10 +690,6 @@ glusterBlockCallRPC_1(char *host, void *cobj,
freeaddrinfo(res);
}

if (sockfd != -1) {
close(sockfd);
}

if (errsv) {
errno = errsv;
}
Expand Down

0 comments on commit 766f97e

Please sign in to comment.