Blame SOURCES/0004-zsh-5.5.1-CVE-2019-20044.patch

d51014
From e6dea148252c9d8cb3de0965f2e558ac13e12f06 Mon Sep 17 00:00:00 2001
d51014
From: Daniel Shahaf <danielsh@apache.org>
d51014
Date: Thu, 26 Dec 2019 11:49:45 +0000
d51014
Subject: [PATCH 1/7] internal: Allow %L in zerrmsg() in non-debug builds, too.
d51014
d51014
This will let error messages include long integers.
d51014
d51014
Upstream-commit: 81185f4c6106d7ea2f7beaabbec7360c08e400d2
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Src/utils.c | 2 --
d51014
 1 file changed, 2 deletions(-)
d51014
d51014
diff --git a/Src/utils.c b/Src/utils.c
d51014
index 32f6008..2ddc596 100644
d51014
--- a/Src/utils.c
d51014
+++ b/Src/utils.c
d51014
@@ -325,12 +325,10 @@ zerrmsg(FILE *file, const char *fmt, va_list ap)
d51014
 		nicezputs(s, file);
d51014
 		break;
d51014
 	    }
d51014
-#ifdef DEBUG
d51014
 	    case 'L':
d51014
 		lnum = va_arg(ap, long);
d51014
 		fprintf(file, "%ld", lnum);
d51014
 		break;
d51014
-#endif
d51014
 	    case 'd':
d51014
 		num = va_arg(ap, int);
d51014
 		fprintf(file, "%d", num);
d51014
-- 
d51014
2.21.1
d51014
d51014
d51014
From 4907caaf15e5a054088e05534c5500679c15b105 Mon Sep 17 00:00:00 2001
d51014
From: dana <dana@dana.is>
d51014
Date: Thu, 26 Dec 2019 14:57:07 -0600
d51014
Subject: [PATCH 2/7] unposted: zerrmsg(): Fix macro guard missed in previous
d51014
 commit
d51014
d51014
Upstream-commit: ed21a7b70068b4250a25dcdc5b7213a789b0d0ca
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Src/utils.c | 2 --
d51014
 1 file changed, 2 deletions(-)
d51014
d51014
diff --git a/Src/utils.c b/Src/utils.c
d51014
index 2ddc596..4a1dcc4 100644
d51014
--- a/Src/utils.c
d51014
+++ b/Src/utils.c
d51014
@@ -287,9 +287,7 @@ zerrmsg(FILE *file, const char *fmt, va_list ap)
d51014
 {
d51014
     const char *str;
d51014
     int num;
d51014
-#ifdef DEBUG
d51014
     long lnum;
d51014
-#endif
d51014
 #ifdef HAVE_STRERROR_R
d51014
 #define ERRBUFSIZE (80)
d51014
     int olderrno;
d51014
-- 
d51014
2.21.1
d51014
d51014
d51014
From a6763e5de6eebc5994097fc4d778094e7254589c Mon Sep 17 00:00:00 2001
d51014
From: Sam Foxman <samfoxman320@gmail.com>
d51014
Date: Sun, 22 Dec 2019 17:30:28 -0500
d51014
Subject: [PATCH 3/7] Drop privileges securely
d51014
d51014
Upstream-commit: 24e993db62cf146fb76ebcf677a4a7aa3766fc74
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Src/options.c | 146 +++++++++++++++++++++++++++++++++++++++++---------
d51014
 configure.ac  |   4 +-
d51014
 2 files changed, 125 insertions(+), 25 deletions(-)
d51014
d51014
diff --git a/Src/options.c b/Src/options.c
d51014
index 590652e..c9608af 100644
d51014
--- a/Src/options.c
d51014
+++ b/Src/options.c
d51014
@@ -576,6 +576,7 @@ int
d51014
 bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 {
d51014
     int action, optno, match = 0;
d51014
+    int retval = 0;
d51014
 
d51014
     /* With no arguments or options, display options. */
d51014
     if (!*args) {
d51014
@@ -603,18 +604,28 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 		    inittyptab();
d51014
 		    return 1;
d51014
 		}
d51014
-		if(!(optno = optlookup(*args)))
d51014
+		if(!(optno = optlookup(*args))) {
d51014
 		    zwarnnam(nam, "no such option: %s", *args);
d51014
-		else if(dosetopt(optno, action, 0, opts))
d51014
-		    zwarnnam(nam, "can't change option: %s", *args);
d51014
+		    retval = 1;
d51014
+		} else {
d51014
+		    retval = !!dosetopt(optno, action, 0, opts);
d51014
+		    if (retval) {
d51014
+			zwarnnam(nam, "can't change option: %s", *args);
d51014
+		    }
d51014
+		}
d51014
 		break;
d51014
 	    } else if(**args == 'm') {
d51014
 		match = 1;
d51014
 	    } else {
d51014
-	    	if (!(optno = optlookupc(**args)))
d51014
+	    	if (!(optno = optlookupc(**args))) {
d51014
 		    zwarnnam(nam, "bad option: -%c", **args);
d51014
-		else if(dosetopt(optno, action, 0, opts))
d51014
-		    zwarnnam(nam, "can't change option: -%c", **args);
d51014
+		    retval = 1;
d51014
+		} else {
d51014
+		    retval = !!dosetopt(optno, action, 0, opts);
d51014
+		    if (retval) {
d51014
+			zwarnnam(nam, "can't change option: -%c", **args);
d51014
+		    }
d51014
+		}
d51014
 	    }
d51014
 	}
d51014
 	args++;
d51014
@@ -624,10 +635,15 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
     if (!match) {
d51014
 	/* Not globbing the arguments -- arguments are simply option names. */
d51014
 	while (*args) {
d51014
-	    if(!(optno = optlookup(*args++)))
d51014
+	    if(!(optno = optlookup(*args++))) {
d51014
 		zwarnnam(nam, "no such option: %s", args[-1]);
d51014
-	    else if(dosetopt(optno, !isun, 0, opts))
d51014
-		zwarnnam(nam, "can't change option: %s", args[-1]);
d51014
+		retval = 1;
d51014
+	    } else {
d51014
+		retval = !!dosetopt(optno, !isun, 0, opts);
d51014
+		if (retval) {
d51014
+		    zwarnnam(nam, "can't change option: %s", args[-1]);
d51014
+		}
d51014
+	    }
d51014
 	}
d51014
     } else {
d51014
 	/* Globbing option (-m) set. */
d51014
@@ -650,7 +666,8 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 	    tokenize(s);
d51014
 	    if (!(pprog = patcompile(s, PAT_HEAPDUP, NULL))) {
d51014
 		zwarnnam(nam, "bad pattern: %s", *args);
d51014
-		continue;
d51014
+		retval = 1;
d51014
+		break;
d51014
 	    }
d51014
 	    /* Loop over expansions. */
d51014
 	    scanmatchtable(optiontab, pprog, 0, 0, OPT_ALIAS,
d51014
@@ -659,7 +676,7 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 	}
d51014
     }
d51014
     inittyptab();
d51014
-    return 0;
d51014
+    return retval;
d51014
 }
d51014
 
d51014
 /* Identify an option name */
d51014
@@ -768,20 +785,101 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
 	    return -1;
d51014
     } else if(optno == PRIVILEGED && !value) {
d51014
 	/* unsetting PRIVILEGED causes the shell to make itself unprivileged */
d51014
-#ifdef HAVE_SETUID
d51014
-	setuid(getuid());
d51014
-	setgid(getgid());
d51014
-        if (setuid(getuid())) {
d51014
-            zwarn("failed to change user ID: %e", errno);
d51014
-            return -1;
d51014
-	} else if (setgid(getgid())) {
d51014
-            zwarn("failed to change group ID: %e", errno);
d51014
-            return -1;
d51014
-        }
d51014
+
d51014
+	int skip_setuid = 0;
d51014
+	int skip_setgid = 0;
d51014
+
d51014
+#if defined(HAVE_GETEGID) && defined(HAVE_SETGID) && defined(HAVE_GETUID)
d51014
+	int orig_egid = getegid();
d51014
+#endif
d51014
+
d51014
+#if defined(HAVE_GETEUID) && defined(HAVE_GETUID)
d51014
+	if (geteuid() == getuid()) {
d51014
+	    skip_setuid = 1;
d51014
+	}
d51014
+#endif
d51014
+
d51014
+#if defined(HAVE_GETEGID) && defined(HAVE_GETGID)
d51014
+	if (getegid() == getgid()) {
d51014
+	    skip_setgid = 1;
d51014
+	}
d51014
+#endif
d51014
+
d51014
+	if (!skip_setgid) {
d51014
+	    int setgid_err;
d51014
+#ifdef HAVE_SETRESGID
d51014
+	    setgid_err = setresgid(getgid(), getgid(), getgid());
d51014
+#elif defined(HAVE_SETREGID)
d51014
+#if defined(HAVE_GETEGID) && defined(HAVE_SETGID) && defined(HAVE_GETUID)
d51014
+	    setgid_err = setregid(getgid(), getgid());
d51014
+#else
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: can't drop privileges; setregid available, but cannot check if saved gid changed");
d51014
+	    return -1;
d51014
+#endif
d51014
+#else
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresgid and setregid not available");
d51014
+	    return -1;
d51014
+#endif
d51014
+	    if (setgid_err) {
d51014
+		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change group ID: %e", errno);
d51014
+		return -1;
d51014
+	    }
d51014
+	}
d51014
+
d51014
+	if (!skip_setuid) {
d51014
+#if defined(HAVE_GETEUID) && defined(HAVE_SETUID)
d51014
+	    int orig_euid = geteuid();
d51014
+#endif
d51014
+	    int setuid_err;
d51014
+#if defined(HAVE_GETEUID) && defined(HAVE_INITGROUPS) && defined(HAVE_GETPWUID)
d51014
+	    if (geteuid() == 0) {
d51014
+		struct passwd *pw = getpwuid(getuid());
d51014
+		if (pw == NULL) {
d51014
+		    zwarnnam("unsetopt", "can't drop privileges; failed to get user information for uid %d: %e",
d51014
+			    getuid(), errno);
d51014
+		    return -1;
d51014
+		}
d51014
+		if (initgroups(pw->pw_name, pw->pw_gid)) {
d51014
+		    zwarnnam("unsetopt", "can't drop privileges; failed to set supplementary group list: %e", errno);
d51014
+		    return -1;
d51014
+		}
d51014
+	    }
d51014
+#endif
d51014
+
d51014
+#ifdef HAVE_SETRESUID
d51014
+	    setuid_err = setresuid(getuid(), getuid(), getuid());
d51014
+#elif defined(HAVE_SETREUID)
d51014
+#if defined(HAVE_GETEUID) && defined(HAVE_SETUID) && defined(HAVE_GETUID)
d51014
+	    setuid_err = setreuid(getuid(), getuid());
d51014
+#else
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: can't drop privileges; setreuid available, but cannot check if saved uid changed");
d51014
+	    return -1;
d51014
+#endif
d51014
 #else
d51014
-        zwarn("setuid not available");
d51014
-        return -1;
d51014
-#endif /* not HAVE_SETUID */
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresuid and setreuid not available");
d51014
+	    return -1;
d51014
+#endif
d51014
+	    if (setuid_err) {
d51014
+		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change user ID: %e", errno);
d51014
+		return -1;
d51014
+	    }
d51014
+#if defined(HAVE_GETEUID) && defined(HAVE_SETUID) && defined(HAVE_GETUID)
d51014
+	    if (getuid() != 0 && !setuid(orig_euid)) {
d51014
+		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the euid");
d51014
+		return -1;
d51014
+	    }
d51014
+#endif
d51014
+	}
d51014
+
d51014
+#if defined(HAVE_GETEGID) && defined(HAVE_SETGID) && defined(HAVE_GETUID)
d51014
+	if (getuid() != 0 && !skip_setgid && !setgid(orig_egid)) {
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the egid");
d51014
+	    return -1;
d51014
+	}
d51014
+#endif
d51014
+
d51014
 #ifdef JOB_CONTROL
d51014
     } else if (!force && optno == MONITOR && value) {
d51014
 	if (new_opts[optno] == value)
d51014
diff --git a/configure.ac b/configure.ac
d51014
index d15a6cd..51cdf89 100644
d51014
--- a/configure.ac
d51014
+++ b/configure.ac
d51014
@@ -1300,7 +1300,9 @@ AC_CHECK_FUNCS(strftime strptime mktime timelocal \
d51014
 	       inet_aton inet_pton inet_ntop \
d51014
 	       getlogin getpwent getpwnam getpwuid getgrgid getgrnam \
d51014
 	       initgroups nis_list \
d51014
-	       setuid seteuid setreuid setresuid setsid \
d51014
+	       getuid setuid seteuid setreuid setresuid setsid \
d51014
+	       getgid setgid setegid setregid setresgid \
d51014
+	       geteuid getegid \
d51014
 	       memcpy memmove strstr strerror strtoul \
d51014
 	       getrlimit getrusage \
d51014
 	       setlocale \
d51014
-- 
d51014
2.21.1
d51014
d51014
d51014
From 07ec3e46db743ff37fd2bfeafbb4a44ddc7b4aaa Mon Sep 17 00:00:00 2001
d51014
From: Daniel Shahaf <danielsh@apache.org>
d51014
Date: Thu, 26 Dec 2019 09:16:19 +0000
d51014
Subject: [PATCH 4/7] Improve PRIVILEGED fixes
d51014
MIME-Version: 1.0
d51014
Content-Type: text/plain; charset=UTF-8
d51014
Content-Transfer-Encoding: 8bit
d51014
d51014
- Fix retval handling in bin_setopt()
d51014
d51014
- Don't skip_setuid / skip_setgid.  It's not our place to optimize away noops
d51014
  (that might not even _be_ noops; they might change the saved uid…).
d51014
d51014
- Remove HAVE_* guard checks around functions that are used unguarded elsewhere.
d51014
d51014
- Use bsd-setres_id.c from OpenSSH to provide setresuid() / setresgid()
d51014
  everywhere, and thus simplify the ifdef soup.  Fix some preëxisting
d51014
  bugs in the macro definitions of setuid() (do we still need that one?).
d51014
d51014
- Fix zwarning() format codes for variadic arguments type safety
d51014
d51014
- Restored a comment from HEAD
d51014
d51014
- Fix failure modes around initgroups()
d51014
d51014
- Compared privilege restoration code with OpenSSH's permanently_drop_uid() and
d51014
  updated as needed
d51014
d51014
- Add E01 PRIVILEGED sanity checks
d51014
d51014
Upstream-commit: 8250c5c168f07549ed646e6848e6dda118271e23
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Src/openssh_bsd_setres_id.c | 129 +++++++++++++++++++++++++++++++
d51014
 Src/options.c               | 148 ++++++++++++++++--------------------
d51014
 Src/zsh.mdd                 |   3 +-
d51014
 Src/zsh_system.h            |  94 ++++++++++++++++++-----
d51014
 Test/E01options.ztst        |  15 ++++
d51014
 configure.ac                |   5 +-
d51014
 6 files changed, 292 insertions(+), 102 deletions(-)
d51014
 create mode 100644 Src/openssh_bsd_setres_id.c
d51014
d51014
diff --git a/Src/openssh_bsd_setres_id.c b/Src/openssh_bsd_setres_id.c
d51014
new file mode 100644
d51014
index 0000000..65e91a4
d51014
--- /dev/null
d51014
+++ b/Src/openssh_bsd_setres_id.c
d51014
@@ -0,0 +1,129 @@
d51014
+/*
d51014
+ * Copyright (c) 2012 Darren Tucker (dtucker at zip com au).
d51014
+ *
d51014
+ * Permission to use, copy, modify, and distribute this software for any
d51014
+ * purpose with or without fee is hereby granted, provided that the above
d51014
+ * copyright notice and this permission notice appear in all copies.
d51014
+ *
d51014
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
d51014
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
d51014
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
d51014
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
d51014
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
d51014
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
d51014
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
d51014
+ */
d51014
+/*
d51014
+ * openssh_bsd_setres_id.c - setresuid() and setresgid() wrappers
d51014
+ *
d51014
+ * This file is part of zsh, the Z shell.
d51014
+ *
d51014
+ * It is based on the file openbsd-compat/bsd-setres_id.c in OpenSSH 7.9p1,
d51014
+ * which is subject to the copyright notice above.  The zsh modifications are
d51014
+ * licensed as follows:
d51014
+ *
d51014
+ * Copyright (c) 2019 Daniel Shahaf
d51014
+ * All rights reserved.
d51014
+ *
d51014
+ * Permission is hereby granted, without written agreement and without
d51014
+ * license or royalty fees, to use, copy, modify, and distribute this
d51014
+ * software and to distribute modified versions of this software for any
d51014
+ * purpose, provided that the above copyright notice and the following
d51014
+ * two paragraphs appear in all copies of this software.
d51014
+ *
d51014
+ * In no event shall Daniel Shahaf or the Zsh Development Group be liable
d51014
+ * to any party for direct, indirect, special, incidental, or consequential
d51014
+ * damages arising out of the use of this software and its documentation,
d51014
+ * even if Daniel Shahaf and the Zsh Development Group have been advised of
d51014
+ * the possibility of such damage.
d51014
+ *
d51014
+ * Daniel Shahaf and the Zsh Development Group specifically disclaim any
d51014
+ * warranties, including, but not limited to, the implied warranties of
d51014
+ * merchantability and fitness for a particular purpose.  The software
d51014
+ * provided hereunder is on an "as is" basis, and Daniel Shahaf and the
d51014
+ * Zsh Development Group have no obligation to provide maintenance,
d51014
+ * support, updates, enhancements, or modifications.
d51014
+ *
d51014
+ */
d51014
+
d51014
+
d51014
+#include <sys/types.h>
d51014
+
d51014
+#include <stdarg.h>
d51014
+#include <unistd.h>
d51014
+#include <string.h>
d51014
+
d51014
+#include "zsh.mdh"
d51014
+
d51014
+#if defined(ZSH_IMPLEMENT_SETRESGID) || defined(BROKEN_SETRESGID)
d51014
+int
d51014
+setresgid(gid_t rgid, gid_t egid, gid_t sgid)
d51014
+{
d51014
+	int ret = 0, saved_errno;
d51014
+
d51014
+	if (rgid != sgid) {
d51014
+		errno = ENOSYS;
d51014
+		return -1;
d51014
+	}
d51014
+#if defined(ZSH_HAVE_NATIVE_SETREGID) && !defined(BROKEN_SETREGID)
d51014
+	if (setregid(rgid, egid) < 0) {
d51014
+		saved_errno = errno;
d51014
+		zwarnnam("setregid", "to gid %L: %e", (long)rgid, errno);
d51014
+		errno = saved_errno;
d51014
+		ret = -1;
d51014
+	}
d51014
+#else
d51014
+	if (setegid(egid) < 0) {
d51014
+		saved_errno = errno;
d51014
+		zwarnnam("setegid", "to gid %L: %e", (long)(unsigned int)egid, errno);
d51014
+		errno = saved_errno;
d51014
+		ret = -1;
d51014
+	}
d51014
+	if (setgid(rgid) < 0) {
d51014
+		saved_errno = errno;
d51014
+		zwarnnam("setgid", "to gid %L: %e", (long)rgid, errno);
d51014
+		errno = saved_errno;
d51014
+		ret = -1;
d51014
+	}
d51014
+#endif
d51014
+	return ret;
d51014
+}
d51014
+#endif
d51014
+
d51014
+#if defined(ZSH_IMPLEMENT_SETRESUID) || defined(BROKEN_SETRESUID)
d51014
+int
d51014
+setresuid(uid_t ruid, uid_t euid, uid_t suid)
d51014
+{
d51014
+	int ret = 0, saved_errno;
d51014
+
d51014
+	if (ruid != suid) {
d51014
+		errno = ENOSYS;
d51014
+		return -1;
d51014
+	}
d51014
+#if defined(ZSH_HAVE_NATIVE_SETREUID) && !defined(BROKEN_SETREUID)
d51014
+	if (setreuid(ruid, euid) < 0) {
d51014
+		saved_errno = errno;
d51014
+		zwarnnam("setreuid", "to uid %L: %e", (long)ruid, errno);
d51014
+		errno = saved_errno;
d51014
+		ret = -1;
d51014
+	}
d51014
+#else
d51014
+
d51014
+# ifndef SETEUID_BREAKS_SETUID
d51014
+	if (seteuid(euid) < 0) {
d51014
+		saved_errno = errno;
d51014
+		zwarnnam("seteuid", "to uid %L: %e", (long)euid, errno);
d51014
+		errno = saved_errno;
d51014
+		ret = -1;
d51014
+	}
d51014
+# endif
d51014
+	if (setuid(ruid) < 0) {
d51014
+		saved_errno = errno;
d51014
+		zwarnnam("setuid", "to uid %L: %e", (long)ruid, errno);
d51014
+		errno = saved_errno;
d51014
+		ret = -1;
d51014
+	}
d51014
+#endif
d51014
+	return ret;
d51014
+}
d51014
+#endif
d51014
diff --git a/Src/options.c b/Src/options.c
d51014
index c9608af..deec560 100644
d51014
--- a/Src/options.c
d51014
+++ b/Src/options.c
d51014
@@ -606,25 +606,21 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 		}
d51014
 		if(!(optno = optlookup(*args))) {
d51014
 		    zwarnnam(nam, "no such option: %s", *args);
d51014
-		    retval = 1;
d51014
-		} else {
d51014
-		    retval = !!dosetopt(optno, action, 0, opts);
d51014
-		    if (retval) {
d51014
-			zwarnnam(nam, "can't change option: %s", *args);
d51014
-		    }
d51014
+		    retval |= 1;
d51014
+		} else if (dosetopt(optno, action, 0, opts)) {
d51014
+		    zwarnnam(nam, "can't change option: %s", *args);
d51014
+		    retval |= 1;
d51014
 		}
d51014
 		break;
d51014
 	    } else if(**args == 'm') {
d51014
 		match = 1;
d51014
 	    } else {
d51014
-	    	if (!(optno = optlookupc(**args))) {
d51014
+		if (!(optno = optlookupc(**args))) {
d51014
 		    zwarnnam(nam, "bad option: -%c", **args);
d51014
-		    retval = 1;
d51014
-		} else {
d51014
-		    retval = !!dosetopt(optno, action, 0, opts);
d51014
-		    if (retval) {
d51014
-			zwarnnam(nam, "can't change option: -%c", **args);
d51014
-		    }
d51014
+		    retval |= 1;
d51014
+		} else if (dosetopt(optno, action, 0, opts)) {
d51014
+		    zwarnnam(nam, "can't change option: -%c", **args);
d51014
+		    retval |= 1;
d51014
 		}
d51014
 	    }
d51014
 	}
d51014
@@ -637,12 +633,10 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 	while (*args) {
d51014
 	    if(!(optno = optlookup(*args++))) {
d51014
 		zwarnnam(nam, "no such option: %s", args[-1]);
d51014
-		retval = 1;
d51014
-	    } else {
d51014
-		retval = !!dosetopt(optno, !isun, 0, opts);
d51014
-		if (retval) {
d51014
-		    zwarnnam(nam, "can't change option: %s", args[-1]);
d51014
-		}
d51014
+		retval |= 1;
d51014
+	    } else if (dosetopt(optno, !isun, 0, opts)) {
d51014
+		zwarnnam(nam, "can't change option: %s", args[-1]);
d51014
+		retval |= 1;
d51014
 	    }
d51014
 	}
d51014
     } else {
d51014
@@ -666,7 +660,7 @@ bin_setopt(char *nam, char **args, UNUSED(Options ops), int isun)
d51014
 	    tokenize(s);
d51014
 	    if (!(pprog = patcompile(s, PAT_HEAPDUP, NULL))) {
d51014
 		zwarnnam(nam, "bad pattern: %s", *args);
d51014
-		retval = 1;
d51014
+		retval |= 1;
d51014
 		break;
d51014
 	    }
d51014
 	    /* Loop over expansions. */
d51014
@@ -786,100 +780,92 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
     } else if(optno == PRIVILEGED && !value) {
d51014
 	/* unsetting PRIVILEGED causes the shell to make itself unprivileged */
d51014
 
d51014
-	int skip_setuid = 0;
d51014
-	int skip_setgid = 0;
d51014
-
d51014
-#if defined(HAVE_GETEGID) && defined(HAVE_SETGID) && defined(HAVE_GETUID)
d51014
-	int orig_egid = getegid();
d51014
-#endif
d51014
+	/* If set, return -1 so lastval will be non-zero. */
d51014
+	int failed = 0;
d51014
 
d51014
-#if defined(HAVE_GETEUID) && defined(HAVE_GETUID)
d51014
-	if (geteuid() == getuid()) {
d51014
-	    skip_setuid = 1;
d51014
-	}
d51014
+#ifdef HAVE_SETUID
d51014
+	const int orig_euid = geteuid();
d51014
 #endif
d51014
+	const int orig_egid = getegid();
d51014
 
d51014
-#if defined(HAVE_GETEGID) && defined(HAVE_GETGID)
d51014
-	if (getegid() == getgid()) {
d51014
-	    skip_setgid = 1;
d51014
-	}
d51014
-#endif
d51014
-
d51014
-	if (!skip_setgid) {
d51014
-	    int setgid_err;
d51014
-#ifdef HAVE_SETRESGID
d51014
-	    setgid_err = setresgid(getgid(), getgid(), getgid());
d51014
-#elif defined(HAVE_SETREGID)
d51014
-#if defined(HAVE_GETEGID) && defined(HAVE_SETGID) && defined(HAVE_GETUID)
d51014
-	    setgid_err = setregid(getgid(), getgid());
d51014
-#else
d51014
-	    zwarnnam("unsetopt",
d51014
-		"PRIVILEGED: can't drop privileges; setregid available, but cannot check if saved gid changed");
d51014
+	/*
d51014
+	 * Set the GID first as if we set the UID to non-privileged it
d51014
+	 * might be impossible to restore the GID.
d51014
+	 */
d51014
+	{
d51014
+#ifndef HAVE_SETRESGID
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresgid() and friends not available");
d51014
 	    return -1;
d51014
-#endif
d51014
 #else
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresgid and setregid not available");
d51014
-	    return -1;
d51014
-#endif
d51014
+	    int setgid_err;
d51014
+	    setgid_err = setresgid(getgid(), getgid(), getgid());
d51014
 	    if (setgid_err) {
d51014
 		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change group ID: %e", errno);
d51014
 		return -1;
d51014
 	    }
d51014
+#endif
d51014
 	}
d51014
 
d51014
-	if (!skip_setuid) {
d51014
-#if defined(HAVE_GETEUID) && defined(HAVE_SETUID)
d51014
-	    int orig_euid = geteuid();
d51014
-#endif
d51014
+	/* Set the UID second. */
d51014
+	{
d51014
+#ifndef HAVE_SETRESUID
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresuid() and friends not available");
d51014
+	    return -1;
d51014
+#else
d51014
 	    int setuid_err;
d51014
-#if defined(HAVE_GETEUID) && defined(HAVE_INITGROUPS) && defined(HAVE_GETPWUID)
d51014
+
d51014
+# ifdef HAVE_INITGROUPS
d51014
+	    /* Set the supplementary groups list. */
d51014
 	    if (geteuid() == 0) {
d51014
 		struct passwd *pw = getpwuid(getuid());
d51014
 		if (pw == NULL) {
d51014
-		    zwarnnam("unsetopt", "can't drop privileges; failed to get user information for uid %d: %e",
d51014
-			    getuid(), errno);
d51014
-		    return -1;
d51014
-		}
d51014
-		if (initgroups(pw->pw_name, pw->pw_gid)) {
d51014
+		    zwarnnam("unsetopt", "can't drop privileges; failed to get user information for uid %L: %e",
d51014
+			    (long)getuid(), errno);
d51014
+		    failed = 1;
d51014
+		} else if (initgroups(pw->pw_name, pw->pw_gid)) {
d51014
 		    zwarnnam("unsetopt", "can't drop privileges; failed to set supplementary group list: %e", errno);
d51014
 		    return -1;
d51014
 		}
d51014
+	    } else if (getuid() != 0 &&
d51014
+		    (geteuid() != getuid() || orig_egid != getegid())) {
d51014
+		zwarnnam("unsetopt", "PRIVILEGED: supplementary group list not changed due to lack of permissions: EUID=%L",
d51014
+			(long)geteuid());
d51014
+		failed = 1;
d51014
 	    }
d51014
-#endif
d51014
+# else
d51014
+	    /* initgroups() isn't in POSIX.  If it's not available on the system,
d51014
+	     * we silently skip it. */
d51014
+# endif
d51014
 
d51014
-#ifdef HAVE_SETRESUID
d51014
 	    setuid_err = setresuid(getuid(), getuid(), getuid());
d51014
-#elif defined(HAVE_SETREUID)
d51014
-#if defined(HAVE_GETEUID) && defined(HAVE_SETUID) && defined(HAVE_GETUID)
d51014
-	    setuid_err = setreuid(getuid(), getuid());
d51014
-#else
d51014
-	    zwarnnam("unsetopt",
d51014
-		"PRIVILEGED: can't drop privileges; setreuid available, but cannot check if saved uid changed");
d51014
-	    return -1;
d51014
-#endif
d51014
-#else
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresuid and setreuid not available");
d51014
-	    return -1;
d51014
-#endif
d51014
 	    if (setuid_err) {
d51014
 		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change user ID: %e", errno);
d51014
 		return -1;
d51014
 	    }
d51014
-#if defined(HAVE_GETEUID) && defined(HAVE_SETUID) && defined(HAVE_GETUID)
d51014
-	    if (getuid() != 0 && !setuid(orig_euid)) {
d51014
-		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the euid");
d51014
-		return -1;
d51014
-	    }
d51014
 #endif
d51014
 	}
d51014
 
d51014
-#if defined(HAVE_GETEGID) && defined(HAVE_SETGID) && defined(HAVE_GETUID)
d51014
-	if (getuid() != 0 && !skip_setgid && !setgid(orig_egid)) {
d51014
+#ifdef HAVE_SETGID
d51014
+	if (getuid() != 0 && orig_egid != getegid() &&
d51014
+		(setgid(orig_egid) != -1 || setegid(orig_egid) != -1)) {
d51014
 	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the egid");
d51014
 	    return -1;
d51014
 	}
d51014
 #endif
d51014
 
d51014
+#ifdef HAVE_SETUID
d51014
+	if (getuid() != 0 && orig_euid != geteuid() &&
d51014
+		(setuid(orig_euid) != -1 || seteuid(orig_euid) != -1)) {
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the euid");
d51014
+	    return -1;
d51014
+	}
d51014
+#endif
d51014
+
d51014
+	if (failed) {
d51014
+	    /* A warning message has been printed. */
d51014
+	    return -1;
d51014
+	}
d51014
+
d51014
 #ifdef JOB_CONTROL
d51014
     } else if (!force && optno == MONITOR && value) {
d51014
 	if (new_opts[optno] == value)
d51014
diff --git a/Src/zsh.mdd b/Src/zsh.mdd
d51014
index 324435d..a2590dc 100644
d51014
--- a/Src/zsh.mdd
d51014
+++ b/Src/zsh.mdd
d51014
@@ -13,7 +13,8 @@ objects="builtin.o compat.o cond.o context.o \
d51014
 exec.o glob.o hashtable.o hashnameddir.o \
d51014
 hist.o init.o input.o jobs.o lex.o linklist.o loop.o math.o \
d51014
 mem.o module.o options.o params.o parse.o pattern.o prompt.o signals.o \
d51014
-signames.o sort.o string.o subst.o text.o utils.o watch.o"
d51014
+signames.o sort.o string.o subst.o text.o utils.o watch.o \
d51014
+openssh_bsd_setres_id.o"
d51014
 
d51014
 headers="../config.h zsh_system.h zsh.h sigcount.h signals.h \
d51014
 prototypes.h hashtable.h ztype.h"
d51014
diff --git a/Src/zsh_system.h b/Src/zsh_system.h
d51014
index 5339b49..18fe09b 100644
d51014
--- a/Src/zsh_system.h
d51014
+++ b/Src/zsh_system.h
d51014
@@ -456,30 +456,90 @@ struct timezone {
d51014
 # define setpgrp setpgid
d51014
 #endif
d51014
 
d51014
-/* can we set the user/group id of a process */
d51014
+/* compatibility wrappers */
d51014
 
d51014
-#ifndef HAVE_SETUID
d51014
+/* Our strategy is as follows:
d51014
+ *
d51014
+ * - Ensure that either setre[ug]id() or set{e,}[ug]id() is available.
d51014
+ * - If setres[ug]id() are missing, provide them in terms of either
d51014
+ *   setre[ug]id() or set{e,}[ug]id(), whichever is available.
d51014
+ * - Provide replacement setre[ug]id() or set{e,}[ug]id() if they are not
d51014
+ *   available natively.
d51014
+ *
d51014
+ * There isn't a circular dependency because, right off the bat, we check that
d51014
+ * there's an end condition, and #error out otherwise.
d51014
+ */
d51014
+#if !defined(HAVE_SETREUID) && !(defined(HAVE_SETEUID) && defined(HAVE_SETUID))
d51014
+  /*
d51014
+   * If you run into this error, you have two options:
d51014
+   * - Teach zsh how to do the equivalent of setreuid() on your system
d51014
+   * - Remove support for PRIVILEGED option, and then remove the #error.
d51014
+   */
d51014
+# error "Don't know how to change UID"
d51014
+#endif
d51014
+#if !defined(HAVE_SETREGID) && !(defined(HAVE_SETEGID) && defined(HAVE_SETGID))
d51014
+  /* See above comment. */
d51014
+# error "Don't know how to change GID"
d51014
+#endif
d51014
+
d51014
+/* Provide setresuid(). */
d51014
+#ifndef HAVE_SETRESUID
d51014
+int	setresuid(uid_t, uid_t, uid_t);
d51014
+# define HAVE_SETRESUID
d51014
+# define ZSH_IMPLEMENT_SETRESUID
d51014
 # ifdef HAVE_SETREUID
d51014
-#  define setuid(X) setreuid(X,X)
d51014
-#  define setgid(X) setregid(X,X)
d51014
-#  define HAVE_SETUID
d51014
+#  define ZSH_HAVE_NATIVE_SETREUID
d51014
 # endif
d51014
 #endif
d51014
 
d51014
-/* can we set the effective user/group id of a process */
d51014
+/* Provide setresgid(). */
d51014
+#ifndef HAVE_SETRESGID
d51014
+int	setresgid(gid_t, gid_t, gid_t);
d51014
+# define HAVE_SETRESGID
d51014
+# define ZSH_IMPLEMENT_SETRESGID
d51014
+# ifdef HAVE_SETREGID
d51014
+#  define ZSH_HAVE_NATIVE_SETREGID
d51014
+# endif
d51014
+#endif
d51014
 
d51014
+/* Provide setreuid(). */
d51014
+#ifndef HAVE_SETREUID
d51014
+# define setreuid(X, Y) setresuid((X), (Y), -1)
d51014
+# define HAVE_SETREUID
d51014
+#endif
d51014
+
d51014
+/* Provide setregid(). */
d51014
+#ifndef HAVE_SETREGID
d51014
+# define setregid(X, Y) setresgid((X), (Y), -1)
d51014
+# define HAVE_SETREGID
d51014
+#endif
d51014
+
d51014
+/* Provide setuid(). */
d51014
+/* ### TODO: Either remove this (this function has been standard since 1985),
d51014
+ * ###       or rewrite this without multiply-evaluating the argument */
d51014
+#ifndef HAVE_SETUID
d51014
+# define setuid(X) setreuid((X), (X))
d51014
+# define HAVE_SETUID
d51014
+#endif
d51014
+
d51014
+/* Provide setgid(). */
d51014
+#ifndef HAVE_SETGID
d51014
+/* ### TODO: Either remove this (this function has been standard since 1985),
d51014
+ * ###       or rewrite this without multiply-evaluating the argument */
d51014
+#  define setgid(X) setregid((X), (X))
d51014
+#  define HAVE_SETGID
d51014
+#endif
d51014
+
d51014
+/* Provide seteuid(). */
d51014
 #ifndef HAVE_SETEUID
d51014
-# ifdef HAVE_SETREUID
d51014
-#  define seteuid(X) setreuid(-1,X)
d51014
-#  define setegid(X) setregid(-1,X)
d51014
-#  define HAVE_SETEUID
d51014
-# else
d51014
-#  ifdef HAVE_SETRESUID
d51014
-#   define seteuid(X) setresuid(-1,X,-1)
d51014
-#   define setegid(X) setresgid(-1,X,-1)
d51014
-#   define HAVE_SETEUID
d51014
-#  endif
d51014
-# endif
d51014
+# define seteuid(X) setreuid(-1, (X))
d51014
+# define HAVE_SETEUID
d51014
+#endif
d51014
+
d51014
+/* Provide setegid(). */
d51014
+#ifndef HAVE_SETEGID
d51014
+# define setegid(X) setregid(-1, (X))
d51014
+# define HAVE_SETEGID
d51014
 #endif
d51014
 
d51014
 #ifdef HAVE_SYS_RESOURCE_H
d51014
diff --git a/Test/E01options.ztst b/Test/E01options.ztst
d51014
index 0f6bb34..c4c3d67 100644
d51014
--- a/Test/E01options.ztst
d51014
+++ b/Test/E01options.ztst
d51014
@@ -1391,3 +1391,18 @@ F:Regression test for workers/41811
d51014
 ?(anon):4: `break' active at end of function scope
d51014
 ?(anon):4: `break' active at end of function scope
d51014
 ?(anon):4: `break' active at end of function scope
d51014
+
d51014
+# There are further tests for PRIVILEGED in P01privileged.ztst.
d51014
+ if [[ -o privileged ]]; then
d51014
+   unsetopt privileged
d51014
+ fi
d51014
+ unsetopt privileged
d51014
+0:PRIVILEGED sanity check: unsetting is idempotent
d51014
+F:If this test fails at the first unsetopt, refer to P01privileged.ztst.
d51014
+
d51014
+  if [[ -o privileged ]]; then
d51014
+    (( UID != EUID ))
d51014
+  else
d51014
+    (( UID == EUID ))
d51014
+  fi
d51014
+0:PRIVILEGED sanity check: default value is correct
d51014
diff --git a/configure.ac b/configure.ac
d51014
index 51cdf89..4e30ad1 100644
d51014
--- a/configure.ac
d51014
+++ b/configure.ac
d51014
@@ -1300,9 +1300,8 @@ AC_CHECK_FUNCS(strftime strptime mktime timelocal \
d51014
 	       inet_aton inet_pton inet_ntop \
d51014
 	       getlogin getpwent getpwnam getpwuid getgrgid getgrnam \
d51014
 	       initgroups nis_list \
d51014
-	       getuid setuid seteuid setreuid setresuid setsid \
d51014
-	       getgid setgid setegid setregid setresgid \
d51014
-	       geteuid getegid \
d51014
+	       setuid seteuid setreuid setresuid setsid \
d51014
+	       setgid setegid setregid setresgid \
d51014
 	       memcpy memmove strstr strerror strtoul \
d51014
 	       getrlimit getrusage \
d51014
 	       setlocale \
d51014
-- 
d51014
2.21.1
d51014
d51014
d51014
From 6caecc2efb2fa4c708fd27858962ace55d957dce Mon Sep 17 00:00:00 2001
d51014
From: dana <dana@dana.is>
d51014
Date: Sun, 29 Dec 2019 02:41:11 +0000
d51014
Subject: [PATCH 5/7] Improve PRIVILEGED fixes (again)
d51014
d51014
* Pass RGID instead of passwd GID to initgroups()
d51014
d51014
* Clean up #ifdefs, avoid unnecessary checks
d51014
d51014
* Flatten conditions
d51014
d51014
Upstream-commit: 26d02efa7a9b0a6b32e1a8bbc6aca6c544b94211
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Src/options.c | 92 ++++++++++++++++++++++++---------------------------
d51014
 1 file changed, 43 insertions(+), 49 deletions(-)
d51014
d51014
diff --git a/Src/options.c b/Src/options.c
d51014
index deec560..8599ed3 100644
d51014
--- a/Src/options.c
d51014
+++ b/Src/options.c
d51014
@@ -780,91 +780,85 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
     } else if(optno == PRIVILEGED && !value) {
d51014
 	/* unsetting PRIVILEGED causes the shell to make itself unprivileged */
d51014
 
d51014
+/* For simplicity's sake, require both setresgid() and setresuid() up-front. */
d51014
+#if !defined(HAVE_SETRESGID)
d51014
+	zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresgid() and friends not available");
d51014
+	return -1;
d51014
+#elif !defined(HAVE_SETRESUID)
d51014
+	zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresuid() and friends not available");
d51014
+	return -1;
d51014
+#else
d51014
 	/* If set, return -1 so lastval will be non-zero. */
d51014
 	int failed = 0;
d51014
-
d51014
-#ifdef HAVE_SETUID
d51014
 	const int orig_euid = geteuid();
d51014
-#endif
d51014
 	const int orig_egid = getegid();
d51014
 
d51014
 	/*
d51014
 	 * Set the GID first as if we set the UID to non-privileged it
d51014
 	 * might be impossible to restore the GID.
d51014
 	 */
d51014
-	{
d51014
-#ifndef HAVE_SETRESGID
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresgid() and friends not available");
d51014
+	if (setresgid(getgid(), getgid(), getgid())) {
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change group ID: %e", errno);
d51014
 	    return -1;
d51014
-#else
d51014
-	    int setgid_err;
d51014
-	    setgid_err = setresgid(getgid(), getgid(), getgid());
d51014
-	    if (setgid_err) {
d51014
-		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change group ID: %e", errno);
d51014
-		return -1;
d51014
-	    }
d51014
-#endif
d51014
 	}
d51014
 
d51014
-	/* Set the UID second. */
d51014
-	{
d51014
-#ifndef HAVE_SETRESUID
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresuid() and friends not available");
d51014
-	    return -1;
d51014
-#else
d51014
-	    int setuid_err;
d51014
-
d51014
 # ifdef HAVE_INITGROUPS
d51014
-	    /* Set the supplementary groups list. */
d51014
-	    if (geteuid() == 0) {
d51014
-		struct passwd *pw = getpwuid(getuid());
d51014
-		if (pw == NULL) {
d51014
-		    zwarnnam("unsetopt", "can't drop privileges; failed to get user information for uid %L: %e",
d51014
-			    (long)getuid(), errno);
d51014
-		    failed = 1;
d51014
-		} else if (initgroups(pw->pw_name, pw->pw_gid)) {
d51014
-		    zwarnnam("unsetopt", "can't drop privileges; failed to set supplementary group list: %e", errno);
d51014
-		    return -1;
d51014
-		}
d51014
-	    } else if (getuid() != 0 &&
d51014
-		    (geteuid() != getuid() || orig_egid != getegid())) {
d51014
-		zwarnnam("unsetopt", "PRIVILEGED: supplementary group list not changed due to lack of permissions: EUID=%L",
d51014
-			(long)geteuid());
d51014
+	/* Set the supplementary groups list.
d51014
+	 *
d51014
+	 * Note that on macOS, FreeBSD, and possibly some other platforms,
d51014
+	 * initgroups() resets the EGID to its second argument (see setgroups(2) for
d51014
+	 * details). This has the potential to leave the EGID in an unexpected
d51014
+	 * state. However, it seems common in other projects that do this dance to
d51014
+	 * simply re-use the same GID that's going to become the EGID anyway, in
d51014
+	 * which case it doesn't matter. That's what we do here. It's therefore
d51014
+	 * possible, in some probably uncommon cases, that the shell ends up not
d51014
+	 * having the privileges of the RUID user's primary/passwd group. */
d51014
+	if (geteuid() == 0) {
d51014
+	    struct passwd *pw = getpwuid(getuid());
d51014
+	    if (pw == NULL) {
d51014
+		zwarnnam("unsetopt", "can't drop privileges; failed to get user information for uid %L: %e",
d51014
+		    (long)getuid(), errno);
d51014
 		failed = 1;
d51014
+	    /* This may behave strangely in the unlikely event that the same user
d51014
+	     * name appears with multiple UIDs in the passwd database */
d51014
+	    } else if (initgroups(pw->pw_name, getgid())) {
d51014
+		zwarnnam("unsetopt", "can't drop privileges; failed to set supplementary group list: %e", errno);
d51014
+		return -1;
d51014
 	    }
d51014
+	} else if (getuid() != 0 &&
d51014
+	    (geteuid() != getuid() || orig_egid != getegid())) {
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: supplementary group list not changed due to lack of permissions: EUID=%L",
d51014
+		(long)geteuid());
d51014
+	    failed = 1;
d51014
+	}
d51014
 # else
d51014
-	    /* initgroups() isn't in POSIX.  If it's not available on the system,
d51014
-	     * we silently skip it. */
d51014
+	/* initgroups() isn't in POSIX.  If it's not available on the system,
d51014
+	 * we silently skip it. */
d51014
 # endif
d51014
 
d51014
-	    setuid_err = setresuid(getuid(), getuid(), getuid());
d51014
-	    if (setuid_err) {
d51014
-		zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change user ID: %e", errno);
d51014
-		return -1;
d51014
-	    }
d51014
-#endif
d51014
+	/* Set the UID second. */
d51014
+	if (setresuid(getuid(), getuid(), getuid())) {
d51014
+	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change user ID: %e", errno);
d51014
+	    return -1;
d51014
 	}
d51014
 
d51014
-#ifdef HAVE_SETGID
d51014
 	if (getuid() != 0 && orig_egid != getegid() &&
d51014
 		(setgid(orig_egid) != -1 || setegid(orig_egid) != -1)) {
d51014
 	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the egid");
d51014
 	    return -1;
d51014
 	}
d51014
-#endif
d51014
 
d51014
-#ifdef HAVE_SETUID
d51014
 	if (getuid() != 0 && orig_euid != geteuid() &&
d51014
 		(setuid(orig_euid) != -1 || seteuid(orig_euid) != -1)) {
d51014
 	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the euid");
d51014
 	    return -1;
d51014
 	}
d51014
-#endif
d51014
 
d51014
 	if (failed) {
d51014
 	    /* A warning message has been printed. */
d51014
 	    return -1;
d51014
 	}
d51014
+#endif /* HAVE_SETRESGID && HAVE_SETRESUID */
d51014
 
d51014
 #ifdef JOB_CONTROL
d51014
     } else if (!force && optno == MONITOR && value) {
d51014
-- 
d51014
2.21.1
d51014
d51014
d51014
From 7d224cc6d93933db596ecb4ec78ba5bf48ef2d32 Mon Sep 17 00:00:00 2001
d51014
From: dana <dana@dana.is>
d51014
Date: Sun, 29 Dec 2019 02:43:14 +0000
d51014
Subject: [PATCH 6/7] Clean up error-message white space
d51014
d51014
Upstream-commit: 4ce66857b71b40a0661df3780ff557f2b0f4cb13
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Src/options.c | 30 +++++++++++++++++++++---------
d51014
 1 file changed, 21 insertions(+), 9 deletions(-)
d51014
d51014
diff --git a/Src/options.c b/Src/options.c
d51014
index 8599ed3..5b972d4 100644
d51014
--- a/Src/options.c
d51014
+++ b/Src/options.c
d51014
@@ -782,10 +782,12 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
 
d51014
 /* For simplicity's sake, require both setresgid() and setresuid() up-front. */
d51014
 #if !defined(HAVE_SETRESGID)
d51014
-	zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresgid() and friends not available");
d51014
+	zwarnnam("unsetopt",
d51014
+	    "PRIVILEGED: can't drop privileges; setresgid() and friends not available");
d51014
 	return -1;
d51014
 #elif !defined(HAVE_SETRESUID)
d51014
-	zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; setresuid() and friends not available");
d51014
+	zwarnnam("unsetopt",
d51014
+	    "PRIVILEGED: can't drop privileges; setresuid() and friends not available");
d51014
 	return -1;
d51014
 #else
d51014
 	/* If set, return -1 so lastval will be non-zero. */
d51014
@@ -798,7 +800,9 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
 	 * might be impossible to restore the GID.
d51014
 	 */
d51014
 	if (setresgid(getgid(), getgid(), getgid())) {
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change group ID: %e", errno);
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: can't drop privileges; failed to change group ID: %e",
d51014
+		errno);
d51014
 	    return -1;
d51014
 	}
d51014
 
d51014
@@ -816,18 +820,22 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
 	if (geteuid() == 0) {
d51014
 	    struct passwd *pw = getpwuid(getuid());
d51014
 	    if (pw == NULL) {
d51014
-		zwarnnam("unsetopt", "can't drop privileges; failed to get user information for uid %L: %e",
d51014
+		zwarnnam("unsetopt",
d51014
+		    "can't drop privileges; failed to get user information for uid %L: %e",
d51014
 		    (long)getuid(), errno);
d51014
 		failed = 1;
d51014
 	    /* This may behave strangely in the unlikely event that the same user
d51014
 	     * name appears with multiple UIDs in the passwd database */
d51014
 	    } else if (initgroups(pw->pw_name, getgid())) {
d51014
-		zwarnnam("unsetopt", "can't drop privileges; failed to set supplementary group list: %e", errno);
d51014
+		zwarnnam("unsetopt",
d51014
+		    "can't drop privileges; failed to set supplementary group list: %e",
d51014
+		    errno);
d51014
 		return -1;
d51014
 	    }
d51014
 	} else if (getuid() != 0 &&
d51014
 	    (geteuid() != getuid() || orig_egid != getegid())) {
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: supplementary group list not changed due to lack of permissions: EUID=%L",
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: supplementary group list not changed due to lack of permissions: EUID=%L",
d51014
 		(long)geteuid());
d51014
 	    failed = 1;
d51014
 	}
d51014
@@ -838,19 +846,23 @@ dosetopt(int optno, int value, int force, char *new_opts)
d51014
 
d51014
 	/* Set the UID second. */
d51014
 	if (setresuid(getuid(), getuid(), getuid())) {
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; failed to change user ID: %e", errno);
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: can't drop privileges; failed to change user ID: %e",
d51014
+		errno);
d51014
 	    return -1;
d51014
 	}
d51014
 
d51014
 	if (getuid() != 0 && orig_egid != getegid() &&
d51014
 		(setgid(orig_egid) != -1 || setegid(orig_egid) != -1)) {
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the egid");
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: can't drop privileges; was able to restore the egid");
d51014
 	    return -1;
d51014
 	}
d51014
 
d51014
 	if (getuid() != 0 && orig_euid != geteuid() &&
d51014
 		(setuid(orig_euid) != -1 || seteuid(orig_euid) != -1)) {
d51014
-	    zwarnnam("unsetopt", "PRIVILEGED: can't drop privileges; was able to restore the euid");
d51014
+	    zwarnnam("unsetopt",
d51014
+		"PRIVILEGED: can't drop privileges; was able to restore the euid");
d51014
 	    return -1;
d51014
 	}
d51014
 
d51014
-- 
d51014
2.21.1
d51014
d51014
d51014
From a86d3b201352f14bc17c9521baeb5eaa2ad60f1a Mon Sep 17 00:00:00 2001
d51014
From: dana <dana@dana.is>
d51014
Date: Sat, 28 Dec 2019 20:45:55 -0600
d51014
Subject: [PATCH 7/7] Add unsetopt/PRIVILEGED tests
d51014
d51014
Upstream-commit: b15bd4aa590db8087d1e8f2eb1af2874f5db814d
d51014
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
d51014
---
d51014
 Test/E01options.ztst    |  10 +-
d51014
 Test/P01privileged.ztst | 197 ++++++++++++++++++++++++++++++++++++++++
d51014
 Test/README             |   1 +
d51014
 3 files changed, 207 insertions(+), 1 deletion(-)
d51014
 create mode 100644 Test/P01privileged.ztst
d51014
d51014
diff --git a/Test/E01options.ztst b/Test/E01options.ztst
d51014
index c4c3d67..767879a 100644
d51014
--- a/Test/E01options.ztst
d51014
+++ b/Test/E01options.ztst
d51014
@@ -74,7 +74,6 @@
d51014
 #    HASH_LIST_ALL )
d51014
 #    PRINT_EXIT_STATUS   haven't worked out what this does yet, although
d51014
 #                        Bart suggested a fix.
d51014
-#    PRIVILEGED (similar to GLOBAL_RCS)
d51014
 #    RCS        (  "      "    "    " )
d51014
 #    SH_OPTION_LETTERS   even I found this too dull to set up a test for
d51014
 #    SINGLE_COMMAND      kills shell
d51014
@@ -95,6 +94,15 @@
d51014
 
d51014
 %test
d51014
 
d51014
+  # setopt should move on to the next operation in the face of an error, but
d51014
+  # preserve the >0 return code
d51014
+  unsetopt aliases
d51014
+  setopt not_a_real_option aliases && return 2
d51014
+  print -r - $options[aliases]
d51014
+0:setopt error handling
d51014
+?(eval):setopt:4: no such option: not_a_real_option
d51014
+>on
d51014
+
d51014
   alias echo='print foo'
d51014
   unsetopt aliases
d51014
   # use eval else aliases are all parsed at start
d51014
diff --git a/Test/P01privileged.ztst b/Test/P01privileged.ztst
d51014
new file mode 100644
d51014
index 0000000..c54112b
d51014
--- /dev/null
d51014
+++ b/Test/P01privileged.ztst
d51014
@@ -0,0 +1,197 @@
d51014
+# This file contains tests related to the PRIVILEGED option. In order to run,
d51014
+# it requires that the test process itself have super-user privileges (or that
d51014
+# one of the environment variables described below be set). This can be achieved
d51014
+# via, e.g., `sudo make check TESTNUM=P`.
d51014
+#
d51014
+# Optionally, the environment variables ZSH_TEST_UNPRIVILEGED_UID and/or
d51014
+# ZSH_TEST_UNPRIVILEGED_GID may be set to UID:EUID or GID:EGID pairs, where the
d51014
+# two IDs in each pair are different, non-0 IDs valid on the system being used
d51014
+# to run the tests. (The UIDs must both be non-0 to effectively test downgrading
d51014
+# of privileges, and they must be non-matching to test auto-enabling of
d51014
+# PRIVILEGED and to ensure that disabling PRIVILEGED correctly resets the saved
d51014
+# UID. Technically GID 0 is not special, but for simplicity's sake we apply the
d51014
+# same requirements here.)
d51014
+#
d51014
+# If either of the aforementioned environment variables is not set, the test
d51014
+# script will try to pick the first two >0 IDs from the passwd/group databases
d51014
+# on the current system.
d51014
+#
d51014
+# If either variable is set, the tests will run, but they will likely fail
d51014
+# without super-user privileges.
d51014
+
d51014
+%prep
d51014
+
d51014
+  # Mind your empty lines here. The logic in this %prep section is somewhat
d51014
+  # complex compared to most others; to avoid lots of nested/duplicated
d51014
+  # conditions we need to make sure that this all gets executed as a single
d51014
+  # function from which we can return early
d51014
+  [[ $EUID == 0 || -n $ZSH_TEST_UNPRIVILEGED_UID$ZSH_TEST_UNPRIVILEGED_GID ]] || {
d51014
+    ZTST_unimplemented='PRIVILEGED tests require super-user privileges (or env var)'
d51014
+    return 1
d51014
+  }
d51014
+  (( $+commands[perl] )) || { # @todo Eliminate this dependency with a C wrapper?
d51014
+    ZTST_unimplemented='PRIVILEGED tests require Perl'
d51014
+    return 1
d51014
+  }
d51014
+  grep -qE '#define HAVE_SETRES?UID' $ZTST_testdir/../config.h || {
d51014
+    ZTST_unimplemented='PRIVILEGED tests require setreuid()/setresuid()'
d51014
+    return 1
d51014
+  }
d51014
+  #
d51014
+  ruid= euid= rgid= egid=
d51014
+  #
d51014
+  if [[ -n $ZSH_TEST_UNPRIVILEGED_UID ]]; then
d51014
+    ruid=${ZSH_TEST_UNPRIVILEGED_UID%%:*}
d51014
+    euid=${ZSH_TEST_UNPRIVILEGED_UID##*:}
d51014
+  else
d51014
+    print -ru$ZTST_fd 'Selecting unprivileged UID:EUID pair automatically'
d51014
+    local tmp=$( getent passwd 2> /dev/null || < /etc/passwd )
d51014
+    # Note: Some awks require -v and its argument to be separate
d51014
+    ruid=$( awk -F:            '$3 > 0 { print $3; exit; }' <<< $tmp )
d51014
+    euid=$( awk -F: -v u=$ruid '$3 > u { print $3; exit; }' <<< $tmp )
d51014
+  fi
d51014
+  #
d51014
+  if [[ -n $ZSH_TEST_UNPRIVILEGED_GID ]]; then
d51014
+    rgid=${ZSH_TEST_UNPRIVILEGED_GID%%:*}
d51014
+    egid=${ZSH_TEST_UNPRIVILEGED_GID##*:}
d51014
+  else
d51014
+    print -ru$ZTST_fd 'Selecting unprivileged GID:EGID pair automatically'
d51014
+    local tmp=$( getent group 2> /dev/null || < /etc/group )
d51014
+    # Note: Some awks require -v and its argument to be separate
d51014
+    rgid=$( awk -F:            '$3 > 0 { print $3; exit; }' <<< $tmp )
d51014
+    egid=$( awk -F: -v g=$rgid '$3 > g { print $3; exit; }' <<< $tmp )
d51014
+  fi
d51014
+  #
d51014
+  [[ $ruid/$euid == <1->/<1-> && $ruid != $euid ]] || ruid= euid=
d51014
+  [[ $rgid/$egid == <1->/<1-> && $rgid != $egid ]] || rgid= egid=
d51014
+  #
d51014
+  [[ -n $ruid && -n $euid ]] || {
d51014
+    ZTST_unimplemented='PRIVILEGED tests require unprivileged UID:EUID'
d51014
+    return 1
d51014
+  }
d51014
+  [[ -n $rgid || -n $egid ]] || {
d51014
+    ZTST_unimplemented='PRIVILEGED tests require unprivileged GID:EGID'
d51014
+    return 1
d51014
+  }
d51014
+  #
d51014
+  print -ru$ZTST_fd \
d51014
+    "Using unprivileged UID $ruid, EUID $euid, GID $rgid, EGID $egid"
d51014
+  #
d51014
+  # Execute process with specified UID and EUID
d51014
+  # $1     => Real UID
d51014
+  # $2     => Effective UID
d51014
+  # $3     => Real GID
d51014
+  # $4     => Effective GID
d51014
+  # $5 ... => Command + args to execute (must NOT be a shell command string)
d51014
+  re_exec() {
d51014
+    perl -e '
d51014
+      die("re_exec: not enough arguments") unless (@ARGV >= 5);
d51014
+      my ($ruid, $euid, $rgid, $egid, @cmd) = @ARGV;
d51014
+      foreach my $id ($ruid, $euid, $rgid, $egid) {
d51014
+        die("re_exec: invalid ID: $id") unless ($id =~ /^(-1|\d+)$/a);
d51014
+      }
d51014
+      $< = 0 + $ruid if ($ruid >= 0);
d51014
+      $> = 0 + $euid if ($euid >= 0);
d51014
+      $( = 0 + $rgid if ($rgid >= 0);
d51014
+      $) = 0 + $egid if ($egid >= 0);
d51014
+      exec(@cmd);
d51014
+      die("re_exec: exec failed: $!");
d51014
+    ' -- "$@"
d51014
+  }
d51014
+  #
d51014
+  # Convenience wrapper for re_exec to call `zsh -c`
d51014
+  # -* ... => (optional) Command-line options to zsh
d51014
+  # $1     => Real UID
d51014
+  # $2     => Effective UID
d51014
+  # $3     => Real GID
d51014
+  # $4     => Effective GID
d51014
+  # $5 ... => zsh command string; multiple strings are joined by \n
d51014
+  re_zsh() {
d51014
+    local -a opts
d51014
+    while [[ $1 == -[A-Za-z-]* ]]; do
d51014
+      opts+=( $1 )
d51014
+      shift
d51014
+    done
d51014
+    re_exec "$1" "$2" "$3" "$4" $ZTST_exe $opts -fc \
d51014
+      "MODULE_PATH=${(q)MODULE_PATH}; ${(F)@[5,-1]}"
d51014
+  }
d51014
+  #
d51014
+  # Return one or more random unused UIDs
d51014
+  # $1 ... => Names of parameters to store UIDs in
d51014
+  get_unused_uid() {
d51014
+    while (( $# )); do
d51014
+      local i_=0 uid_=
d51014
+      until [[ -n $uid_ ]]; do
d51014
+        (( ++i_ > 99 )) && return 1
d51014
+        uid_=$RANDOM
d51014
+        id $uid_ &> /dev/null || break
d51014
+        uid_=
d51014
+      done
d51014
+      : ${(P)1::=$uid_}
d51014
+      shift
d51014
+    done
d51014
+  }
d51014
+
d51014
+%test
d51014
+
d51014
+  re_zsh $ruid $ruid -1 -1 'echo $UID/$EUID $options[privileged]'
d51014
+  re_zsh $euid $euid -1 -1 'echo $UID/$EUID $options[privileged]'
d51014
+  re_zsh $ruid $euid -1 -1 'echo $UID/$EUID $options[privileged]'
d51014
+0q:PRIVILEGED automatically enabled when RUID != EUID
d51014
+>$ruid/$ruid off
d51014
+>$euid/$euid off
d51014
+>$ruid/$euid on
d51014
+
d51014
+  re_zsh -1 -1 $rgid $rgid 'echo $GID/$EGID $options[privileged]'
d51014
+  re_zsh -1 -1 $egid $egid 'echo $GID/$EGID $options[privileged]'
d51014
+  re_zsh -1 -1 $rgid $egid 'echo $GID/$EGID $options[privileged]'
d51014
+0q:PRIVILEGED automatically enabled when RGID != EGID
d51014
+>$rgid/$rgid off
d51014
+>$egid/$egid off
d51014
+>$rgid/$egid on
d51014
+
d51014
+  re_zsh $ruid $euid -1 -1 'unsetopt privileged; echo $UID/$EUID'
d51014
+0q:EUID set to RUID after disabling PRIVILEGED
d51014
+*?zsh:unsetopt:1: PRIVILEGED: supplementary group list not changed *
d51014
+*?zsh:unsetopt:1: can't change option: privileged
d51014
+>$ruid/$ruid
d51014
+
d51014
+  re_zsh 0 $euid -1 -1 'unsetopt privileged && echo $UID/$EUID'
d51014
+0:RUID/EUID set to 0/0 when privileged after disabling PRIVILEGED
d51014
+>0/0
d51014
+
d51014
+  re_zsh $ruid $euid -1 -1 "unsetopt privileged; UID=$euid" ||
d51014
+  re_zsh $ruid $euid -1 -1 "unsetopt privileged; EUID=$euid"
d51014
+1:not possible to regain EUID when unprivileged after disabling PRIVILEGED
d51014
+*?zsh:unsetopt:1: PRIVILEGED: supplementary group list not changed *
d51014
+*?zsh:unsetopt:1: can't change option: privileged
d51014
+*?zsh:1: failed to change user ID: *
d51014
+*?zsh:unsetopt:1: PRIVILEGED: supplementary group list not changed *
d51014
+*?zsh:unsetopt:1: can't change option: privileged
d51014
+*?zsh:1: failed to change effective user ID: *
d51014
+
d51014
+  re_zsh -1 -1 $rgid $egid 'unsetopt privileged && echo $GID/$EGID'
d51014
+0q:EGID set to RGID after disabling PRIVILEGED
d51014
+>$rgid/$rgid
d51014
+
d51014
+# This test also confirms that we can't revert to the original EUID's primary
d51014
+# GID, which initgroups() may reset the EGID to on some systems
d51014
+  re_zsh $ruid 0 $rgid 0 'unsetopt privileged; GID=0' ||
d51014
+  re_zsh $ruid 0 $rgid 0 'unsetopt privileged; EGID=0'
d51014
+1:not possible to regain EGID when unprivileged after disabling PRIVILEGED
d51014
+*?zsh:1: failed to change group ID: *
d51014
+*?zsh:1: failed to change effective group ID: *
d51014
+
d51014
+  local rruid
d51014
+  grep -qF '#define HAVE_INITGROUPS' $ZTST_testdir/../config.h || {
d51014
+    ZTST_skip='initgroups() not available'
d51014
+    return 1
d51014
+  }
d51014
+  get_unused_uid rruid || {
d51014
+    ZTST_skip="Can't get unused UID"
d51014
+    return 1
d51014
+  }
d51014
+  re_zsh $rruid 0 -1 -1 'unsetopt privileged'
d51014
+1:getpwuid() fails with non-existent RUID and 0 EUID
d51014
+*?zsh:unsetopt:1: can't drop privileges; failed to get user information *
d51014
+*?zsh:unsetopt:1: can't change option: privileged
d51014
diff --git a/Test/README b/Test/README
d51014
index d012277..726d68e 100644
d51014
--- a/Test/README
d51014
+++ b/Test/README
d51014
@@ -6,6 +6,7 @@ scripts names:
d51014
  C: shell commands with special syntax
d51014
  D: substititution
d51014
  E: options
d51014
+ P: privileged (needs super-user privileges)
d51014
  V: modules
d51014
  W: builtin interactive commands and constructs
d51014
  X: line editing
d51014
-- 
d51014
2.21.1
d51014