git: 2909ddd17cb4 - main - ctld: plug memory leaks

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Fri, 14 Jun 2024 18:09:14 UTC
The branch main has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=2909ddd17cb4d750852dc04128e584f93f8c5058

commit 2909ddd17cb4d750852dc04128e584f93f8c5058
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2024-06-12 21:34:05 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2024-06-14 18:08:38 +0000

    ctld: plug memory leaks
    
    MFC after:      2 weeks
    Reviewed by:    mav
    Sponsored by:   Axcient
    Reported by:    valgrind
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1288
---
 usr.sbin/ctld/ctld.c   |  1 +
 usr.sbin/ctld/kernel.c | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/usr.sbin/ctld/ctld.c b/usr.sbin/ctld/ctld.c
index c31ac328c84e..bf2791040125 100644
--- a/usr.sbin/ctld/ctld.c
+++ b/usr.sbin/ctld/ctld.c
@@ -2873,6 +2873,7 @@ main(int argc, char **argv)
 			error = conf_apply(oldconf, newconf);
 			if (error != 0)
 				log_warnx("failed to apply configuration");
+			conf_delete(newconf);
 			conf_delete(oldconf);
 			oldconf = NULL;
 
diff --git a/usr.sbin/ctld/kernel.c b/usr.sbin/ctld/kernel.c
index dd1c89d0e9b8..ae455e7815f7 100644
--- a/usr.sbin/ctld/kernel.c
+++ b/usr.sbin/ctld/kernel.c
@@ -614,6 +614,22 @@ retry_port:
 		}
 		cp->p_ctl_port = port->port_id;
 	}
+	while ((port = STAILQ_FIRST(&devlist.port_list))) {
+		struct cctl_lun_nv *nv;
+
+		STAILQ_REMOVE_HEAD(&devlist.port_list, links);
+		free(port->port_frontend);
+		free(port->port_name);
+		free(port->cfiscsi_target);
+		free(port->ctld_portal_group_name);
+		while ((nv = STAILQ_FIRST(&port->attr_list))) {
+			STAILQ_REMOVE_HEAD(&port->attr_list, links);
+			free(nv->value);
+			free(nv->name);
+			free(nv);
+		}
+		free(port);
+	}
 	free(name);
 
 	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
@@ -664,6 +680,18 @@ retry_port:
 				    cl->l_name);
 		}
 	}
+	while ((lun = STAILQ_FIRST(&devlist.lun_list))) {
+		struct cctl_lun_nv *nv;
+
+		STAILQ_REMOVE_HEAD(&devlist.lun_list, links);
+		while ((nv = STAILQ_FIRST(&lun->attr_list))) {
+			STAILQ_REMOVE_HEAD(&lun->attr_list, links);
+			free(nv->value);
+			free(nv->name);
+			free(nv);
+		}
+		free(lun);
+	}
 
 	return (conf);
 }
@@ -741,12 +769,14 @@ kernel_lun_add(struct lun *lun)
 
 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
 		if (req.args == NULL) {
+			nvlist_destroy(req.args_nvl);
 			log_warn("error packing nvlist");
 			return (1);
 		}
 	}
 
 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
+	free(req.args);
 	nvlist_destroy(req.args_nvl);
 
 	if (error != 0) {
@@ -824,12 +854,14 @@ kernel_lun_modify(struct lun *lun)
 
 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
 		if (req.args == NULL) {
+			nvlist_destroy(req.args_nvl);
 			log_warn("error packing nvlist");
 			return (1);
 		}
 	}
 
 	error = ioctl(ctl_fd, CTL_LUN_REQ, &req);
+	free(req.args);
 	nvlist_destroy(req.args_nvl);
 
 	if (error != 0) {
@@ -1052,6 +1084,7 @@ kernel_port_add(struct port *port)
 
 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
 		if (req.args == NULL) {
+			nvlist_destroy(req.args_nvl);
 			log_warn("error packing nvlist");
 			return (1);
 		}
@@ -1059,6 +1092,7 @@ kernel_port_add(struct port *port)
 		req.result = result_buf;
 		req.result_len = sizeof(result_buf);
 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
+		free(req.args);
 		nvlist_destroy(req.args_nvl);
 
 		if (error != 0) {
@@ -1202,11 +1236,13 @@ kernel_port_remove(struct port *port)
 
 		req.args = nvlist_pack(req.args_nvl, &req.args_len);
 		if (req.args == NULL) {
+			nvlist_destroy(req.args_nvl);
 			log_warn("error packing nvlist");
 			return (1);
 		}
 
 		error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
+		free(req.args);
 		nvlist_destroy(req.args_nvl);
 
 		if (error != 0) {