[CRIU] [PATCH] parasite: fix error paths in parasite_init

Andrew Vagin avagin at parallels.com
Wed Apr 10 06:27:59 EDT 2013


On Wed, Apr 10, 2013 at 01:26:08AM +0400, Pavel Emelyanov wrote:
> On 04/06/2013 01:28 PM, Andrey Vagin wrote:
> > * Call restore_ns on error paths.
> > * Use close_safe for the socket, because it can be reused
> > 
> > CID 996194 (#1 of 3): Resource leak (RESOURCE_LEAK)
> > 6. leaked_handle: Handle variable "rst" going out of scope leaks the
> > handle.
> > 
> > Signed-off-by: Andrey Vagin <avagin at openvz.org>
> > ---
> >  parasite-syscall.c | 11 ++++++-----
> >  1 file changed, 6 insertions(+), 5 deletions(-)
> > 
> > diff --git a/parasite-syscall.c b/parasite-syscall.c
> > index d82995e..653db25 100644
> > --- a/parasite-syscall.c
> > +++ b/parasite-syscall.c
> > @@ -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)
> 
> Can we have less if (sock < 0) checks?

No, because restore_ns overwrites errno.

> 
> >  			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, };
> >  
> > @@ -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;
> >  }
> >  
> > 
> 
> 


More information about the CRIU mailing list