[dpdk-dev,v5,2/5] pdump: check getenv return value

Message ID 1466786183-3772-3-git-send-email-reshma.pattan@intel.com (mailing list archive)
State Accepted, archived
Headers

Commit Message

Pattan, Reshma June 24, 2016, 4:36 p.m. UTC
  inside pdump_get_socket_path(), getenv can return
a NULL pointer if the match for SOCKET_PATH_HOME is
not found in the environment. NULL check is added to
return -1 immediately. Since pdump_get_socket_path()
returns -1 now, wherever this function is called
there the return value is checked and error message
is logged.

Coverity issue 127344:  return value check
Coverity issue 127347:  null pointer dereference

Fixes: 278f945402c5 ("pdump: add new library for packet capture")

Signed-off-by: Reshma Pattan <reshma.pattan@intel.com>
---
 lib/librte_pdump/rte_pdump.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
  

Comments

John McNamara June 24, 2016, 10:50 p.m. UTC | #1
> -----Original Message-----
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Reshma Pattan
> Sent: Friday, June 24, 2016 5:36 PM
> To: dev@dpdk.org
> Cc: Pattan, Reshma <reshma.pattan@intel.com>
> Subject: [dpdk-dev] [PATCH v5 2/5] pdump: check getenv return value
> 
> inside pdump_get_socket_path(), getenv can return a NULL pointer if the
> match for SOCKET_PATH_HOME is not found in the environment. NULL check is
> added to return -1 immediately. Since pdump_get_socket_path() returns -1
> now, wherever this function is called there the return value is checked
> and error message is logged.
> 
> Coverity issue 127344:  return value check Coverity issue 127347:  null
> pointer dereference
> 
> Fixes: 278f945402c5 ("pdump: add new library for packet capture")
> 
> Signed-off-by: Reshma Pattan <reshma.pattan@intel.com>

Acked-by: John McNamara <john.mcnamara@intel.com>
  

Patch

diff --git a/lib/librte_pdump/rte_pdump.c b/lib/librte_pdump/rte_pdump.c
index 70efd96..e3b03a6 100644
--- a/lib/librte_pdump/rte_pdump.c
+++ b/lib/librte_pdump/rte_pdump.c
@@ -443,7 +443,7 @@  set_pdump_rxtx_cbs(struct pdump_request *p)
 }
 
 /* get socket path (/var/run if root, $HOME otherwise) */
-static void
+static int
 pdump_get_socket_path(char *buffer, int bufsz, enum rte_pdump_socktype type)
 {
 	char dpdk_dir[PATH_MAX] = {0};
@@ -457,6 +457,13 @@  pdump_get_socket_path(char *buffer, int bufsz, enum rte_pdump_socktype type)
 	else {
 		if (getuid() != 0) {
 			dir_home = getenv(SOCKET_PATH_HOME);
+			if (!dir_home) {
+				RTE_LOG(ERR, PDUMP,
+					"Failed to get environment variable"
+					" value for %s, %s:%d\n",
+					SOCKET_PATH_HOME, __func__, __LINE__);
+				return -1;
+			}
 			snprintf(dpdk_dir, sizeof(dpdk_dir), "%s%s",
 					dir_home, DPDK_DIR);
 		} else
@@ -474,6 +481,8 @@  pdump_get_socket_path(char *buffer, int bufsz, enum rte_pdump_socktype type)
 	else
 		snprintf(buffer, bufsz, CLIENT_SOCKET, dir, getpid(),
 				rte_sys_gettid());
+
+	return 0;
 }
 
 static int
@@ -483,8 +492,14 @@  pdump_create_server_socket(void)
 	struct sockaddr_un addr;
 	socklen_t addr_len;
 
-	pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
+	ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
 				RTE_PDUMP_SOCKET_SERVER);
+	if (ret != 0) {
+		RTE_LOG(ERR, PDUMP,
+			"Failed to get server socket path: %s:%d\n",
+			__func__, __LINE__);
+		return -1;
+	}
 	addr.sun_family = AF_UNIX;
 
 	/* remove if file already exists */
@@ -615,8 +630,14 @@  rte_pdump_uninit(void)
 
 	struct sockaddr_un addr;
 
-	pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
+	ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
 				RTE_PDUMP_SOCKET_SERVER);
+	if (ret != 0) {
+		RTE_LOG(ERR, PDUMP,
+			"Failed to get server socket path: %s:%d\n",
+			__func__, __LINE__);
+		return -1;
+	}
 	ret = unlink(addr.sun_path);
 	if (ret != 0) {
 		RTE_LOG(ERR, PDUMP,
@@ -650,8 +671,14 @@  pdump_create_client_socket(struct pdump_request *p)
 		return ret;
 	}
 
-	pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
+	ret = pdump_get_socket_path(addr.sun_path, sizeof(addr.sun_path),
 				RTE_PDUMP_SOCKET_CLIENT);
+	if (ret != 0) {
+		RTE_LOG(ERR, PDUMP,
+			"Failed to get client socket path: %s:%d\n",
+			__func__, __LINE__);
+		return -1;
+	}
 	addr.sun_family = AF_UNIX;
 	addr_len = sizeof(struct sockaddr_un);
 
@@ -667,9 +694,15 @@  pdump_create_client_socket(struct pdump_request *p)
 
 		serv_len = sizeof(struct sockaddr_un);
 		memset(&serv_addr, 0, sizeof(serv_addr));
-		pdump_get_socket_path(serv_addr.sun_path,
+		ret = pdump_get_socket_path(serv_addr.sun_path,
 					sizeof(serv_addr.sun_path),
 					RTE_PDUMP_SOCKET_SERVER);
+		if (ret != 0) {
+			RTE_LOG(ERR, PDUMP,
+				"Failed to get server socket path: %s:%d\n",
+				__func__, __LINE__);
+			break;
+		}
 		serv_addr.sun_family = AF_UNIX;
 
 		n =  sendto(socket_fd, p, sizeof(struct pdump_request), 0,