Skip to contentSkip to conversation

libtirpc: Closed a number of memory leaks

PATCH In review Steve Dickson 2 participants 17 messages Sep 7, 2018 – Oct 29, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover libtirpc: Closed a number of memory leaks

There patches close a number of memory leaks that were found using a covscan. Most them were in error recovery code but a few of them were in main line code.

Steve Dickson (13):
auth_gss.c: resource_leak
auth_gss.c: buffer_size_warning
clnt_bcast.c: resource_leak
clnt_vc.c: resource_leak
getnetconfig.c: cppcheck_warning
getnetpath.c: resource_leak
rpc_generic.c: resource_leak
rpc_soc.c: resource_leak
rpc_soc.c: buffer_size_warning
rpcb_clnt.c: resource_leak
rtime.c: resource_leak
svc_generic.c: resource_leak
svc_simple.c: resource_leak

 src/auth_gss.c     | 3 ++-
 src/clnt_bcast.c   | 1 +
 src/clnt_vc.c      | 2 ++
 src/getnetconfig.c | 2 ++
 src/getnetpath.c   | 1 +
 src/rpc_generic.c  | 1 +
 src/rpc_soc.c      | 8 ++++++--
 src/rpcb_clnt.c    | 1 +
 src/rtime.c        | 1 +
 src/svc_generic.c  | 1 +
 src/svc_simple.c   | 1 +
 11 files changed, 19 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/13 auth_gss.c: resource_leak

Variable "gd" going out of scope leaks the storage it points to.

---
 src/auth_gss.c | 1 +
 1 file changed, 1 insertion(+)
src/auth_gss.c +1 -0
index 5959893..289bd5b 100644
@@ -207,6 +207,7 @@ authgss_create(CLIENT *clnt, gss_name_t name, struct rpc_gss_sec *sec)
 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
 			rpc_createerr.cf_error.re_errno = ENOMEM;
 			free(auth);
+			free(gd);
 			return (NULL);
 		}
 	}
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/13 auth_gss.c: buffer_size_warning

In reply to Steve Dickson

Calling strncpy with a maximum size argument of 128 bytes on destination array "options_ret->actual_mechanism" of size 128 bytes might leave the destination string unterminated

---
 src/auth_gss.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/auth_gss.c +1 -1
index 289bd5b..7d08262 100644
@@ -593,7 +593,7 @@ _rpc_gss_refresh(AUTH *auth, rpc_gss_options_ret_t *options_ret)
 			if (rpc_gss_oid_to_mech(actual_mech_type, &mechanism)) {
 				strncpy(options_ret->actual_mechanism,
 					mechanism,
-					sizeof(options_ret->actual_mechanism));
+					(sizeof(options_ret->actual_mechanism)-1));
 			}
 
 			gd->established = TRUE;
Signature · 1 line
2.17.1
#3 lore raw

PATCH 3/13 clnt_bcast.c: resource_leak

In reply to Steve Dickson

Variable "sys_auth" going out of scope leaks the storage it points to.

---
 src/clnt_bcast.c | 1 +
 1 file changed, 1 insertion(+)
src/clnt_bcast.c +1 -0
index 98cf061..2ad6c89 100644
@@ -330,6 +330,7 @@ rpc_broadcast_exp(prog, vers, proc, xargs, argsp, xresults, resultsp,
 	if (nettype == NULL)
 		nettype = "datagram_n";
 	if ((handle = __rpc_setconf(nettype)) == NULL) {
+		AUTH_DESTROY(sys_auth);
 		return (RPC_UNKNOWNPROTO);
 	}
 	while ((nconf = __rpc_getconf(handle)) != NULL) {
Signature · 1 line
2.17.1
#4 lore raw

PATCH 4/13 clnt_vc.c: resource_leak

In reply to Steve Dickson

Variable "ct" going out of scope leaks the storage it points to.

---
 src/clnt_vc.c | 2 ++
 1 file changed, 2 insertions(+)
src/clnt_vc.c +2 -0
index 3d775c7..10ee91a 100644
@@ -325,6 +325,8 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
 	    cl->cl_private, read_vc, write_vc);
+	mem_free(ct->ct_addr.buf, ct->ct_addr.len);
+	mem_free(ct, sizeof (struct ct_data));
 	return (cl);
 
 err:
Signature · 1 line
2.17.1
#5 lore raw

Re: [Libtirpc-devel] [PATCH 04/13] clnt_vc.c: resource_leak

Quoted 12 lines · Steve Dickson

Variable "ct" going out of scope leaks the storage it points to.

---
 src/clnt_vc.c | 2 ++
 1 file changed, 2 insertions(+)
src/clnt_vc.c +0 -0
index 3d775c7..10ee91a 100644

src/clnt_vc.c clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz) · line 332

Quoted 8 lines
src/clnt_vc.c +2 -0
@@ -325,6 +325,8 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
 	    cl->cl_private, read_vc, write_vc);
+	mem_free(ct->ct_addr.buf, ct->ct_addr.len);
+	mem_free(ct, sizeof (struct ct_data));
 	return (cl);
 
 err:

Are you sure about this one Steve?

aka:

        /*
         * Create a client handle which uses xdrrec for serialization
         * and authnone for authentication.
         */
        cl->cl_ops = clnt_vc_ops();
        cl->cl_private = ct;             <------?
        cl->cl_auth = authnone_create();
        sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
        recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
        xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
            cl->cl_private, read_vc, write_vc);
        return (cl);
#6 lore raw

Re: [Libtirpc-devel] [PATCH 04/13] clnt_vc.c: resource_leak

Quoted 25 lines · Ian Kent
Quoted 12 lines · Steve Dickson

Variable "ct" going out of scope leaks the storage it points to.

---
 src/clnt_vc.c | 2 ++
 1 file changed, 2 insertions(+)
src/clnt_vc.c +0 -0
index 3d775c7..10ee91a 100644

src/clnt_vc.c clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz) · line 332

Quoted 8 lines
src/clnt_vc.c +2 -0
@@ -325,6 +325,8 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
 	recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
 	xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
 	    cl->cl_private, read_vc, write_vc);
+	mem_free(ct->ct_addr.buf, ct->ct_addr.len);
+	mem_free(ct, sizeof (struct ct_data));
 	return (cl);
 
 err:

Are you sure about this one Steve?

aka:

Quoted 12 lines
        /*
         * Create a client handle which uses xdrrec for serialization
         * and authnone for authentication.
         */
        cl->cl_ops = clnt_vc_ops();
        cl->cl_private = ct;             <------?
        cl->cl_auth = authnone_create();
        sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz);
        recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz);
        xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
            cl->cl_private, read_vc, write_vc);
        return (cl);

Oh!

My bad, reverted in commit e49077d2fa.

#7 lore raw

PATCH 5/13 getnetconfig.c: cppcheck_warning

In reply to Steve Dickson

Memory leak: p
Memory leak: tmp

---
 src/getnetconfig.c | 2 ++
 1 file changed, 2 insertions(+)
src/getnetconfig.c +2 -0
index 92e7c43..d67d97d 100644
@@ -709,6 +709,8 @@ struct netconfig	*ncp;
     p->nc_lookups = (char **)malloc((size_t)(p->nc_nlookups+1) * sizeof(char *));
     if (p->nc_lookups == NULL) {
 	free(p->nc_netid);
+	free(p);
+	free(tmp);
 	return(NULL);
     }
     for (i=0; i < p->nc_nlookups; i++) {
Signature · 1 line
2.17.1
#8 lore raw

PATCH 6/13 getnetpath.c: resource_leak

In reply to Steve Dickson

Variable "np_sessionp" going out of scope leaks the storage it points to.

---
 src/getnetpath.c | 1 +
 1 file changed, 1 insertion(+)
src/getnetpath.c +1 -0
index 7c19932..ea1a18c 100644
@@ -88,6 +88,7 @@ setnetpath()
     }
     if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
 	syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
+	free(np_sessionp);
 	return (NULL);
     }
     np_sessionp->valid = NP_VALID;
Signature · 1 line
2.17.1
#9 lore raw

PATCH 7/13 rpc_generic.c: resource_leak

In reply to Steve Dickson

Variable "handle" going out of scope leaks the storage it points to.

---
 src/rpc_generic.c | 1 +
 1 file changed, 1 insertion(+)
src/rpc_generic.c +1 -0
index 589cbd5..51f36ac 100644
@@ -319,6 +319,7 @@ __rpc_setconf(nettype)
 		handle->nflag = FALSE;
 		break;
 	default:
+		free(handle);
 		return (NULL);
 	}
 
Signature · 1 line
2.17.1
#10 lore raw

PATCH 8/13 rpc_soc.c: resource_leak

In reply to Steve Dickson

Variable "localhandle" going out of scope leaks the storage it points to. Returning without closing handle "sock" leaks it.

---
 src/rpc_soc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/rpc_soc.c +5 -1
index 5a6eeb7..59e0882 100644
@@ -663,8 +663,10 @@ svcunix_create(sock, sendsize, recvsize, path)
 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
 			break;
 	}
-	if (nconf == NULL)
+	if (nconf == NULL) {
+		endnetconfig(localhandle);
 		return(xprt);
+	}
 
 	if ((sock = __rpc_nconf2fd(nconf)) < 0)
 		goto done;
@@ -692,6 +694,8 @@ svcunix_create(sock, sendsize, recvsize, path)
 	}
 
 	xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
+	if (xprt == NULL)
+		close(sock);
 
 done:
 	endnetconfig(localhandle);
Signature · 1 line
2.17.1
#11 lore raw

PATCH 9/13 rpc_soc.c: buffer_size_warning

In reply to Steve Dickson

Calling strncpy with a maximum size argument of 108 bytes on destination array "sun.sun_path" of size 108 bytes might leave the destination string unterminated.

---
 src/rpc_soc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/rpc_soc.c +1 -1
index 59e0882..a85cb17 100644
@@ -673,7 +673,7 @@ svcunix_create(sock, sendsize, recvsize, path)
 
 	memset(&sun, 0, sizeof sun);
 	sun.sun_family = AF_LOCAL;
-	strncpy(sun.sun_path, path, sizeof(sun.sun_path));
+	strncpy(sun.sun_path, path, (sizeof(sun.sun_path)-1));
 	addrlen = sizeof(struct sockaddr_un);
 	sa = (struct sockaddr *)&sun;
 
Signature · 1 line
2.17.1
#12 lore raw

PATCH 10/13 rpcb_clnt.c: resource_leak

In reply to Steve Dickson

Variable "nc_handle" going out of scope leaks the storage it points to.

---
 src/rpcb_clnt.c | 1 +
 1 file changed, 1 insertion(+)
src/rpcb_clnt.c +1 -0
index e45736a..0c34cb7 100644
@@ -547,6 +547,7 @@ try_nconf:
 		if (tmpnconf == NULL) {
  			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
 			mutex_unlock(&loopnconf_lock);
+			endnetconfig(nc_handle);
 			return (NULL);
 		}
 		loopnconf = getnetconfigent(tmpnconf->nc_netid);
Signature · 1 line
2.17.1
#13 lore raw

PATCH 11/13 rtime.c: resource_leak

In reply to Steve Dickson

Handle variable "s" going out of scope leaks the handle.

---
 src/rtime.c | 1 +
 1 file changed, 1 insertion(+)
src/rtime.c +1 -0
index b642840..29fbf0a 100644
@@ -90,6 +90,7 @@ rtime(addrp, timep, timeout)
 
 	/* TCP and UDP port are the same in this case */
 	if ((serv = getservbyname("time", "tcp")) == NULL) {
+		do_close(s);
 		return(-1);
 	}
 
Signature · 1 line
2.17.1
#14 lore raw

PATCH 12/13 svc_generic.c: resource_leak

In reply to Steve Dickson

Variable "handle" going out of scope leaks the storage it points to.

---
 src/svc_generic.c | 1 +
 1 file changed, 1 insertion(+)
src/svc_generic.c +1 -0
index 52a56c2..20abaa2 100644
@@ -113,6 +113,7 @@ svc_create(dispatch, prognum, versnum, nettype)
 				if (l == NULL) {
 					warnx("svc_create: no memory");
 					mutex_unlock(&xprtlist_lock);
+					__rpc_endconf(handle);
 					return (0);
 				}
 				l->xprt = xprt;
Signature · 1 line
2.17.1
#15 lore raw

PATCH 13/13 svc_simple.c: resource_leak

In reply to Steve Dickson

Variable "xdrbuf" going out of scope leaks the storage it points to.

---
 src/svc_simple.c | 1 +
 1 file changed, 1 insertion(+)
src/svc_simple.c +1 -0
index cb58002..c32fe0a 100644
@@ -157,6 +157,7 @@ rpc_reg(prognum, versnum, procnum, progname, inproc, outproc, nettype)
 				((netid = strdup(nconf->nc_netid)) == NULL)) {
 				warnx(rpc_reg_err, rpc_reg_msg, __no_mem_str);
 				SVC_DESTROY(svcxprt);
+				free(xdrbuf);
 				break;
 			}
 			madenow = TRUE;
Signature · 1 line
2.17.1
#16 lore raw
In reply to Steve Dickson
Quoted 20 lines · Steve Dickson

There patches close a number of memory leaks that were found using a covscan. Most them were in error recovery code but a few of them were in main line code.

Steve Dickson (13):
auth_gss.c: resource_leak
auth_gss.c: buffer_size_warning
clnt_bcast.c: resource_leak
clnt_vc.c: resource_leak
getnetconfig.c: cppcheck_warning
getnetpath.c: resource_leak
rpc_generic.c: resource_leak
rpc_soc.c: resource_leak
rpc_soc.c: buffer_size_warning
rpcb_clnt.c: resource_leak
rtime.c: resource_leak
svc_generic.c: resource_leak
svc_simple.c: resource_leak

Quoted 13 lines
 src/auth_gss.c     | 3 ++-
 src/clnt_bcast.c   | 1 +
 src/clnt_vc.c      | 2 ++
 src/getnetconfig.c | 2 ++
 src/getnetpath.c   | 1 +
 src/rpc_generic.c  | 1 +
 src/rpc_soc.c      | 8 ++++++--
 src/rpcb_clnt.c    | 1 +
 src/rtime.c        | 1 +
 src/svc_generic.c  | 1 +
 src/svc_simple.c   | 1 +
 11 files changed, 19 insertions(+), 3 deletions(-)

Committed (tag: libtirpc-1-1-5-rc1)