Blob Blame History Raw
From a01d0ba2d810336870deb8fd4f5366fee45865fe Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Thu, 6 Jun 2024 17:38:20 +0200
Subject: [PATCH 06/15] libtracefs: Don't leak socket file descriptor in
 open_vsock()

Close the socket file descriptor if connect() fails.

Fixes a RESSOURCE_LEAK error (CWE-772)

Link: https://lore.kernel.org/linux-trace-devel/20240606153830.2666120-7-jmarchan@redhat.com

Fixes: 5ea41283fa071 ("libtracefs: Add tracefs_find_cid_pid() API")
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 src/tracefs-vsock.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/tracefs-vsock.c b/src/tracefs-vsock.c
index e171382..9171321 100644
--- a/src/tracefs-vsock.c
+++ b/src/tracefs-vsock.c
@@ -19,8 +19,10 @@ static int open_vsock(unsigned int cid, unsigned int port)
 	if (sd < 0)
 		return -1;
 
-	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)))
+	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr))) {
+		close(sd);
 		return -1;
+	}
 
 	return sd;
 }
-- 
2.45.2