File U_13-Use-safe_unpackstr.patch of Package slurm.32299
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: 0f7ee86c64933c4e189eff4513c19e5fdbdd9d17
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 | 2 ++
4 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/src/common/pack.c b/src/common/pack.c
index d982f654bd..dbeca17067 100644
--- a/src/common/pack.c
+++ b/src/common/pack.c
@@ -1100,10 +1100,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)
@@ -1116,22 +1114,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 c5691aa7ea..93c0e72dc3 100644
--- a/src/common/pack.h
+++ b/src/common/pack.h
@@ -348,6 +348,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 { \
assert(sizeof(*size_valp) == sizeof(uint32_t)); \
assert(buf->magic == BUF_MAGIC); \
diff --git a/src/common/xmalloc.c b/src/common/xmalloc.c
index 94223e994c..3b24703cc9 100644
--- a/src/common/xmalloc.c
+++ b/src/common/xmalloc.c
@@ -225,6 +225,20 @@ void slurm_xfree(void **item, const char *file, int line, const char *func)
}
}
+/*
+ * 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 a9c46332e2..58615d893e 100644
--- a/src/common/xmalloc.h
+++ b/src/common/xmalloc.h
@@ -92,6 +92,8 @@
#define xfree(__p) \
slurm_xfree((void **)&(__p), __FILE__, __LINE__, __func__)
+#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__)