Blame SOURCES/0001-util-fix-overflow-in-remap_node_name.patch

c673be
From 5075b961a29ff9c418e1fefe78432e95dd0a5fcc Mon Sep 17 00:00:00 2001
c673be
From: Michal Schmidt <mschmidt@redhat.com>
c673be
Date: Wed, 1 Feb 2023 22:41:06 +0100
c673be
Subject: [PATCH 1/3] util: fix overflow in remap_node_name()
c673be
c673be
The function remap_node_name() assumes the parameter 'nodedesc' is at
c673be
least IB_SMP_DATA_SIZE + 1 (i.e. 65) bytes long, because it passes it to
c673be
clean_nodedesc() that writes a nul-terminator to it at offset
c673be
IB_SMP_DATA_SIZE. Callers in infiniband-diags/saquery.c pass
c673be
a (struct ib_node_desc_t).description as the argument, which is only
c673be
IB_NODE_DESCRIPTION_SIZE (i.e. 64) bytes long. This is an overflow.
c673be
c673be
An odd thing about remap_node_name() is that it may (but does not
c673be
always) rewrite the nodedesc in-place. Callers do not appear to
c673be
appreciate this behavior. Most of them are various print_* and dump_*
c673be
functions where rewriting the input makes no sense. Some callers make a
c673be
local copy of the nodedesc first, possibly to protect the original.
c673be
One caller (infiniband-diags/saquery.c:print_node_records()) checks if
c673be
either the original description or the remapped one matches a given
c673be
requested_name - so it looks like it prefers the original to be
c673be
not rewritten.
c673be
c673be
Let's make remap_node_name() a bit safer and more convenient to use.
c673be
Allocate a fixed-sized copy first. Then use strncpy to copy from
c673be
'nodedesc', never reading more than IB_SMP_DATA_SIZE (64) bytes.
c673be
Apply clean_nodedesc() on the correctly-sized copy. This solves the
c673be
overflow bug. Also, the in-place rewrite of 'nodedesc' is gone and it
c673be
can become a (const char*).
c673be
c673be
The overflow was found by a static checker (covscan).
c673be
c673be
Fixes: d974c4e398d2 ("Fix max length of node description (ibnetdiscover and smpquery)")
c673be
Signed-off-by: Michal Schmidt <mschmidt@redhat.com>
c673be
---
c673be
 util/node_name_map.c | 12 +++++++++---
c673be
 util/node_name_map.h |  3 +--
c673be
 2 files changed, 10 insertions(+), 5 deletions(-)
c673be
c673be
diff --git a/util/node_name_map.c b/util/node_name_map.c
c673be
index 30b73eb1448e..511cb92ef19c 100644
c673be
--- a/util/node_name_map.c
c673be
+++ b/util/node_name_map.c
c673be
@@ -95,7 +95,7 @@ void close_node_name_map(nn_map_t * map)
c673be
 	free(map);
c673be
 }
c673be
 
c673be
-char *remap_node_name(nn_map_t * map, uint64_t target_guid, char *nodedesc)
c673be
+char *remap_node_name(nn_map_t * map, uint64_t target_guid, const char *nodedesc)
c673be
 {
c673be
 	char *rc = NULL;
c673be
 	name_map_item_t *item = NULL;
c673be
@@ -108,8 +108,14 @@ char *remap_node_name(nn_map_t * map, uint64_t target_guid, char *nodedesc)
c673be
 		rc = strdup(item->name);
c673be
 
c673be
 done:
c673be
-	if (rc == NULL)
c673be
-		rc = strdup(clean_nodedesc(nodedesc));
c673be
+	if (rc == NULL) {
c673be
+		rc = malloc(IB_SMP_DATA_SIZE + 1);
c673be
+		if (rc) {
c673be
+			strncpy(rc, nodedesc, IB_SMP_DATA_SIZE);
c673be
+			rc[IB_SMP_DATA_SIZE] = '\0';
c673be
+			clean_nodedesc(rc);
c673be
+		}
c673be
+	}
c673be
 	return (rc);
c673be
 }
c673be
 
c673be
diff --git a/util/node_name_map.h b/util/node_name_map.h
c673be
index e78d274b116e..d83d672782c4 100644
c673be
--- a/util/node_name_map.h
c673be
+++ b/util/node_name_map.h
c673be
@@ -12,8 +12,7 @@ typedef struct nn_map nn_map_t;
c673be
 
c673be
 nn_map_t *open_node_name_map(const char *node_name_map);
c673be
 void close_node_name_map(nn_map_t *map);
c673be
-/* NOTE: parameter "nodedesc" may be modified here. */
c673be
-char *remap_node_name(nn_map_t *map, uint64_t target_guid, char *nodedesc);
c673be
+char *remap_node_name(nn_map_t *map, uint64_t target_guid, const char *nodedesc);
c673be
 char *clean_nodedesc(char *nodedesc);
c673be
 
c673be
 #endif
c673be
-- 
c673be
2.39.1
c673be