git: a573243370d3 - main - netdump: Fix leaking debugnet state on errors.

Bryan Drewery bdrewery at FreeBSD.org
Tue Jul 27 16:06:27 UTC 2021


The branch main has been updated by bdrewery:

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

commit a573243370d3f4ffad81c740591004851f2beb45
Author:     Bryan Drewery <bdrewery at FreeBSD.org>
AuthorDate: 2021-07-26 23:27:07 +0000
Commit:     Bryan Drewery <bdrewery at FreeBSD.org>
CommitDate: 2021-07-27 16:06:23 +0000

    netdump: Fix leaking debugnet state on errors.
    
    Reviewed by:    cem, markj
    Sponsored by:   Dell EMC
    Differential Revision: https://reviews.freebsd.org/D31319
---
 sys/netinet/netdump/netdump_client.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/sys/netinet/netdump/netdump_client.c b/sys/netinet/netdump/netdump_client.c
index 06a833c20c07..2669ec879d75 100644
--- a/sys/netinet/netdump/netdump_client.c
+++ b/sys/netinet/netdump/netdump_client.c
@@ -85,6 +85,7 @@ __FBSDID("$FreeBSD$");
 		printf(("%s: " f), __func__, ## __VA_ARGS__);		\
 } while (0)
 
+static void	 netdump_cleanup(void);
 static int	 netdump_configure(struct diocskerneldump_arg *,
 		    struct thread *);
 static int	 netdump_dumper(void *priv __unused, void *virtual,
@@ -254,12 +255,13 @@ netdump_dumper(void *priv __unused, void *virtual,
 			printf("failed to close the transaction\n");
 		else
 			printf("\nnetdump finished.\n");
-		debugnet_free(nd_conf.nd_pcb);
-		nd_conf.nd_pcb = NULL;
+		netdump_cleanup();
 		return (0);
 	}
-	if (length > sizeof(nd_buf))
+	if (length > sizeof(nd_buf)) {
+		netdump_cleanup();
 		return (ENOSPC);
+	}
 
 	if (nd_conf.nd_buf_len + length > sizeof(nd_buf) ||
 	    (nd_conf.nd_buf_len != 0 && nd_conf.nd_tx_off +
@@ -267,6 +269,7 @@ netdump_dumper(void *priv __unused, void *virtual,
 		error = netdump_flush_buf();
 		if (error != 0) {
 			dump_failed = 1;
+			netdump_cleanup();
 			return (error);
 		}
 		nd_conf.nd_tx_off = offset;
@@ -344,20 +347,37 @@ netdump_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh,
 
 	error = netdump_flush_buf();
 	if (error != 0)
-		return (error);
+		goto out;
 	memcpy(nd_buf, kdh, sizeof(*kdh));
 	error = debugnet_send(nd_conf.nd_pcb, NETDUMP_KDH, nd_buf,
 	    sizeof(*kdh), NULL);
 	if (error == 0 && keysize > 0) {
-		if (keysize > sizeof(nd_buf))
-			return (EINVAL);
+		if (keysize > sizeof(nd_buf)) {
+			error = EINVAL;
+			goto out;
+		}
 		memcpy(nd_buf, key, keysize);
 		error = debugnet_send(nd_conf.nd_pcb, NETDUMP_EKCD_KEY, nd_buf,
 		    keysize, NULL);
 	}
+out:
+	if (error != 0)
+		netdump_cleanup();
 	return (error);
 }
 
+/*
+ * Cleanup routine for a possibly failed netdump.
+ */
+static void
+netdump_cleanup(void)
+{
+	if (nd_conf.nd_pcb != NULL) {
+		debugnet_free(nd_conf.nd_pcb);
+		nd_conf.nd_pcb = NULL;
+	}
+}
+
 /*-
  * KLD specific code.
  */


More information about the dev-commits-src-all mailing list