File U_13-Use-safe_unpackstr.patch of Package slurm.32296

From: Tim Wickberg <tim@schedmd.com>
Date: Tue Nov 28 23:23:57 2023 -0700
Subject: [PATCH 13/28]Use safe_unpackstr().
Patch-mainline: Upstream
Git-repo: https://github.com/SchedMD/slurm
Git-commit: 5e46d704f9b39ca44db5766ac3def9e834642ad8
References: bsc#1218046, bsc#1218050, bsc#1218051, bsc#1218053
Signed-off-by: Egbert Eich <eich@suse.de>

This ensures these array strings are escaped properly in slurmdbd.

Move the for loop variable declaration in while changing that line here.

Signed-off-by: Egbert Eich <eich@suse.com>
---
 src/common/pack.c    | 15 ++++-----------
 src/common/pack.h    |  8 ++++++++
 src/common/xmalloc.c | 14 ++++++++++++++
 src/common/xmalloc.h |  3 +++
 4 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/src/common/pack.c b/src/common/pack.c
index a5c9ac1d91..8da83e4018 100644
--- a/src/common/pack.c
+++ b/src/common/pack.c
@@ -1075,10 +1075,8 @@ void packstr_array(char **valp, uint32_t size_val, Buf buffer)
  */
 int unpackstr_array(char ***valp, uint32_t * size_valp, Buf buffer)
 {
-	int i;
-	uint32_t uint32_tmp;
-
 	*valp = NULL;
+	uint32_t i;
 	safe_unpack32(size_valp, buffer);
 
 	if (!*size_valp)
@@ -1091,22 +1089,17 @@ int unpackstr_array(char ***valp, uint32_t * size_valp, Buf buffer)
 	}
 
 	safe_xcalloc(*valp, *size_valp + 1, sizeof(char *));
-	for (i = 0; i < *size_valp; i++) {
-		if (unpackstr_xmalloc(&(*valp)[i], &uint32_tmp, buffer)) {
-			*size_valp = 0;
-			xfree_array(*valp);
-			return SLURM_ERROR;
-		}
-	}
+	for (i = 0; i < *size_valp; i++)
+		safe_unpackstr(&(*valp)[i], buffer);
 	/*
 	 * NULL terminate array so execle() can detect end of array
 	 */
 	(*valp)[i] = NULL;
-
 	return SLURM_SUCCESS;
 
 unpack_error:
 	*size_valp = 0;
+	xfree_array(*valp);
 	return SLURM_ERROR;
 }
 
diff --git a/src/common/pack.h b/src/common/pack.h
index 2e7a28f6c0..2b04ae8c3e 100644
--- a/src/common/pack.h
+++ b/src/common/pack.h
@@ -333,6 +333,14 @@ int	unpackmem_array(char *valp, uint32_t size_valp, Buf buffer);
 #define safe_unpackstr_malloc	                        \
         safe_unpackmem_malloc
 
+
+#define safe_unpackstr(valp, buf) do {                          \
+        uint32_t size_valp;                                     \
+        xassert(buf->magic == BUF_MAGIC);                       \
+        if (unpackstr_xmalloc_chooser(valp, &size_valp, buf))   \
+                goto unpack_error;                              \
+} while (0)
+
 #define safe_unpackstr_xmalloc(valp, size_valp, buf) do {	\
 	xassert(sizeof(*size_valp) == sizeof(uint32_t));	\
 	xassert(buf->magic == BUF_MAGIC);		        \
diff --git a/src/common/xmalloc.c b/src/common/xmalloc.c
index 5c2aff17f8..44fa7ffed7 100644
--- a/src/common/xmalloc.c
+++ b/src/common/xmalloc.c
@@ -215,6 +215,20 @@ void slurm_xfree(void **item)
 	}
 }
 
+/*
+ * Free a NULL-terminated xmalloc()'d array of pointers to further xmalloc()'d
+ * elements, and NULL the original pointer to prevent accidental re-use.
+ */
+void slurm_xfree_array(void ***array)
+{
+	if (!*array || !**array)
+		return;
+
+        for (int i = 0; (*array)[i]; i++)
+		xfree((*array)[i]);
+	xfree(*array);
+}
+
 /*
  * Since xfree() is a macro it cannot be used for the ListDelF in list_create()
  * and a number of locations where handling it as a function-pointer is
diff --git a/src/common/xmalloc.h b/src/common/xmalloc.h
index 21b2b4aeea..f1db7b5f09 100644
--- a/src/common/xmalloc.h
+++ b/src/common/xmalloc.h
@@ -91,6 +91,8 @@
 
 #define xfree(__p) slurm_xfree((void **)&(__p))
 
+#define xfree_array(__p) slurm_xfree_array((void ***)&(__p))
+
 #define xrecalloc(__p, __cnt, __sz) \
         slurm_xrecalloc((void **)&(__p), __cnt, __sz, true, false, __FILE__, __LINE__, __func__)
 
@@ -105,6 +107,7 @@
 
 void *slurm_xcalloc(size_t, size_t, bool, bool, const char *, int, const char *);
 void slurm_xfree(void **);
+void slurm_xfree_array(void ***);
 void *slurm_xrecalloc(void **, size_t, size_t, bool, bool, const char *, int, const char *);
 
 size_t xsize(void *item);
openSUSE Build Service is sponsored by