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