From db0fe22e8aae7869d106d099dd8fc7307757c5bf Mon Sep 17 00:00:00 2001
From: John Thiltges <jthiltges2@unl.edu>
Date: Fri, 6 May 2016 10:29:42 -0700
Subject: [PATCH] Fix for slurmstepd setfault

With slurm-15.08.10, we're seeing occasional segfaults in slurmstepd. The logs point to the following line: slurm-15.08.10/src/slurmd/slurmstepd/mgr.c:2612

On that line, _get_primary_group() is accessing the results of getpwnam_r():
    *gid = pwd0->pw_gid;

If getpwnam_r() cannot find a matching password record, it will set the result (pwd0) to NULL, but still return 0. When the pointer is accessed, it will cause a segfault.

Checking the result variable (pwd0) to determine success should fix the issue.
---
 NEWS                        | 1 +
 src/slurmd/slurmstepd/mgr.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index e2edf06b763..bbfb4a1bf34 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@ documents those changes that are of interest to users and administrators.
 ===========================
  -- Do not attempt to power down a node which has never responded if the
     slurmctld daemon restarts without state.
+ -- Fix for possible slurmstepd segfault on invalid userr ID.
 
 * Changes in Slurm 15.08.11
 ===========================
diff --git a/src/slurmd/slurmstepd/mgr.c b/src/slurmd/slurmstepd/mgr.c
index 48df1be02eb..439c0ac3241 100644
--- a/src/slurmd/slurmstepd/mgr.c
+++ b/src/slurmd/slurmstepd/mgr.c
@@ -2604,7 +2604,7 @@ _get_primary_group(const char *user, gid_t *gid)
 	int cc;
 
 	cc = getpwnam_r(user, &pwd, buf, sizeof(buf), &pwd0);
-	if (cc != 0) {
+	if ((cc != 0) || (pwd0 == NULL)) {
 		error("%s: getpwnam_r() failed: %m", __func__);
 		return -1;
 	}
-- 
GitLab