Skip to content

Commit

Permalink
parasite: fix error paths in parasite_init
Browse files Browse the repository at this point in the history
* Call restore_ns on error paths.
* Use close_safe for the socket, because it can be reused

CID 996194 (cyrillos#1 of 3): Resource leak (RESOURCE_LEAK)
6. leaked_handle: Handle variable "rst" going out of scope leaks the
handle.
  • Loading branch information
avagin committed Apr 6, 2013
1 parent 0ddb50e commit ae0b769
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions parasite-syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,19 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
}

sock = socket(PF_UNIX, SOCK_DGRAM, 0);
if (sock < 0) {
if (sock < 0)
pr_perror("Can't create socket");

if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
return -1;
if (sock < 0)
return -1;
}

if (bind(sock, (struct sockaddr *)&args->h_addr, args->h_addr_len) < 0) {
pr_perror("Can't bind socket");
goto err;
}

if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
goto err;
} else {
struct sockaddr addr = { .sa_family = AF_UNSPEC, };

Expand Down Expand Up @@ -350,7 +351,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
ctl->tsock = sock;
return 0;
err:
close(sock);
close_safe(&sock);
return -1;
}

Expand Down

0 comments on commit ae0b769

Please sign in to comment.