File Simplify-logic-in-_become_user.patch of Package slurm.13628

From: Harald Barth <haba@kth.se>
Date: Wed Dec 18 21:05:41 2019 -0700
Subject: Simplify logic in _become_user().
Patch-mainline: Not yet
Git-commit: a928f4e9add097b28c00375caefd7f7a5e38b48e
References: bsc#1159692,CVE-2019-19728

Bug 8084.

Signed-off-by: Egbert Eich <eich@suse.de>
Signed-off-by: Egbert Eich <eich@suse.com>
---
 src/srun/libsrun/srun_job.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/src/srun/libsrun/srun_job.c b/src/srun/libsrun/srun_job.c
index 109aa40..59e0edc 100644
--- a/src/srun/libsrun/srun_job.c
+++ b/src/srun/libsrun/srun_job.c
@@ -1581,24 +1581,22 @@ _normalize_hostlist(const char *hostlist)
 static int _become_user (void)
 {
 	char *user = uid_to_string(opt.uid);
-	gid_t gid = gid_from_uid(opt.uid);
+
+	/* Already the user, so there's nothing to change. Return early. */
+	if (opt.uid == getuid())
+		return 0;
 
 	if (xstrcmp(user, "nobody") == 0) {
 		xfree(user);
 		return (error ("Invalid user id %u: %m", opt.uid));
 	}
 
-	if (opt.uid == getuid ()) {
-		xfree(user);
-		return (0);
-	}
-
 	if ((opt.egid != (gid_t) -1) && (setgid (opt.egid) < 0)) {
 		xfree(user);
 		return (error ("setgid: %m"));
 	}
 
-	if (initgroups(user, gid))
+	if (initgroups(user, gid_from_uid(opt.uid)))
 		return (error ("initgroups: %m"));
 
 	xfree(user);