File U_09-Use-rmdir_recursive.patch of Package slurm.32313

From: Tim Wickberg <tim@schedmd.com>
Date: Wed Oct 11 12:45:25 2023 -0600
Subject: [PATCH 9/19]Use rmdir_recursive().
Patch-mainline: Upstream
Git-repo: https://github.com/SchedMD/slurm
Git-commit: bd9cda8db4051e754528cff7ef44507d895250a4
References: bsc#1216207
Signed-off-by: Egbert Eich <eich@suse.de>

Instead of vulnerable local _recursive_rmdir() function.

Co-authored-by: Alejandro Sanchez <alex@schedmd.com>
---
 src/plugins/switch/cray_aries/switch_cray_aries.h |  1 +
 src/plugins/switch/cray_aries/util.c              | 72 +----------------------
 2 files changed, 2 insertions(+), 71 deletions(-)

diff --git a/src/plugins/switch/cray_aries/switch_cray_aries.h b/src/plugins/switch/cray_aries/switch_cray_aries.h
index 9c539b7328..0d9fe15a43 100644
--- a/src/plugins/switch/cray_aries/switch_cray_aries.h
+++ b/src/plugins/switch/cray_aries/switch_cray_aries.h
@@ -45,6 +45,7 @@
 #include <stdint.h>
 
 #include "src/common/bitstring.h"
+#include "src/common/fd.h"
 #include "src/common/log.h"
 #include "src/common/slurm_protocol_defs.h"
 #include "src/slurmd/slurmstepd/slurmstepd_job.h"
diff --git a/src/plugins/switch/cray_aries/util.c b/src/plugins/switch/cray_aries/util.c
index 090cd17752..b9ddc8e5de 100644
--- a/src/plugins/switch/cray_aries/util.c
+++ b/src/plugins/switch/cray_aries/util.c
@@ -50,7 +50,6 @@
 
 
 #if defined(HAVE_NATIVE_CRAY) || defined(HAVE_CRAY_NETWORK)
-static void _recursive_rmdir(const char *dirnm);
 
 /*
  * Create APID directory with given uid/gid as the owner.
@@ -116,7 +115,7 @@ int remove_spool_files(uint64_t apid)
 
 	// Remove the apid directory LEGACY_SPOOL_DIR/<APID>
 	path_name = xstrdup_printf(LEGACY_SPOOL_DIR "%" PRIu64, apid);
-	_recursive_rmdir(path_name);
+	rmdir_recursive(path_name, true);
 	xfree(path_name);
 
 	// Remove the backwards compatibility ALPS placement file
@@ -299,75 +298,6 @@ int list_str_to_array(char *list, int *cnt, int32_t **numbers)
 	return ret;
 }
 
-/*
- * Recursive directory delete
- *
- * Call with a directory name and this function will delete
- * all files and directories rooted in this name. Finally
- * the named directory will be deleted.
- * If called with a file name, only that file will be deleted.
- */
-static void _recursive_rmdir(const char *dirnm)
-{
-	int st;
-	size_t dirnm_len, fnm_len, name_len;
-	char *fnm = 0;
-	DIR *dirp;
-	struct dirent *dir;
-	struct stat st_buf;
-
-	/* Don't do anything if there is no directory name */
-	if (!dirnm) {
-		return;
-	}
-	dirp = opendir(dirnm);
-	if (!dirp) {
-		if (errno == ENOTDIR)
-			goto fileDel;
-		CRAY_ERR("Error opening directory %s", dirnm);
-		return;
-	}
-
-	dirnm_len = strlen(dirnm);
-	if (dirnm_len == 0)
-		return;
-	while ((dir = readdir(dirp))) {
-		name_len = strlen(dir->d_name);
-		if (name_len == 1 && dir->d_name[0] == '.')
-			continue;
-		if (name_len == 2 && xstrcmp(dir->d_name, "..") == 0)
-			continue;
-		fnm_len = dirnm_len + name_len + 2;
-		free(fnm);
-		fnm = malloc(fnm_len);
-		snprintf(fnm, fnm_len, "%s/%s", dirnm, dir->d_name);
-		st = stat(fnm, &st_buf);
-		if (st < 0) {
-			CRAY_ERR("stat of %s", fnm);
-			continue;
-		}
-		if (st_buf.st_mode & S_IFDIR) {
-			_recursive_rmdir(fnm);
-		} else {
-
-			st = unlink(fnm);
-			if (st < 0 && errno == EISDIR)
-				st = rmdir(fnm);
-			if (st < 0 && errno != ENOENT) {
-				CRAY_ERR("Error removing %s", fnm);
-			}
-		}
-	}
-	free(fnm);
-	closedir(dirp);
-fileDel: st = unlink(dirnm);
-	if (st < 0 && errno == EISDIR)
-		st = rmdir(dirnm);
-	if (st < 0 && errno != ENOENT) {
-		CRAY_ERR("Error removing %s", dirnm);
-	}
-}
-
 void print_jobinfo(slurm_cray_jobinfo_t *job)
 {
 	int i;
openSUSE Build Service is sponsored by