zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH] Silence compilation warnings about setuid, setgid
Date: Thu, 14 Jun 2018 09:41:23 +0100	[thread overview]
Message-ID: <20180614084126eucas1p28dc51492cd295fc0c7df99529c94221a~3_jDOvnXx1349013490eucas1p2N@eucas1p2.samsung.com> (raw)
In-Reply-To: <25A87D83-CBB6-446E-9008-DA413639AC7D@dana.is>

On Wed, 13 Jun 2018 13:41:25 -0500
dana <dana@dana.is> wrote:
> On 13 Jun 2018, at 12:19, Peter Stephenson <p.stephenson@samsung.com>
> wrote:
> >+	(void)setgid(getgid());  
> 
> Casting to void doesn't actually silence this warning with (at least
> some versions of) GCC/glibc. If the function has the
> warn_unused_result attribute, you have to (a) disable the warning
> entirely, (b) actually use the result somehow, or (c) use some hack
> like this:
> http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/ignore-value.h

Something like the following therefore ought to work generally.
Disabling an explicit cast to void is pretty broken behaviour, so I'm
gradually losing interest if for some reason this doesn't work.

pws

diff --git a/Src/options.c b/Src/options.c
index 590652e..600b649 100644
--- a/Src/options.c
+++ b/Src/options.c
@@ -769,15 +769,32 @@ dosetopt(int optno, int value, int force, char *new_opts)
     } else if(optno == PRIVILEGED && !value) {
 	/* unsetting PRIVILEGED causes the shell to make itself unprivileged */
 #ifdef HAVE_SETUID
-	setuid(getuid());
-	setgid(getgid());
-        if (setuid(getuid())) {
-            zwarn("failed to change user ID: %e", errno);
-            return -1;
-	} else if (setgid(getgid())) {
+	int ignore_err;
+	errno = 0;
+	/*
+	 * Set the GID first as if we set the UID to non-privileged it
+	 * might be impossible to restore the GID.
+	 *
+	 * Some OSes (possibly no longer around) have been known to
+	 * fail silently the first time, so we attempt the change twice.
+	 * If it fails we are guaranteed to pick this up the second
+	 * time, so ignore the first time.
+	 *
+	 * Some versions of gcc make it hard to ignore the results the
+	 * first time, hence the following.  (These are probably not
+	 * systems that require the doubled calls.)
+	 */
+	ignore_err = setgid(getgid());
+	(void)ignore_err;
+	ignore_err = setuid(getuid());
+	(void)ignore_err;
+	if (setgid(getgid())) {
             zwarn("failed to change group ID: %e", errno);
             return -1;
-        }
+        } else if (setuid(getuid())) {
+            zwarn("failed to change user ID: %e", errno);
+            return -1;
+	}
 #else
         zwarn("setuid not available");
         return -1;


  reply	other threads:[~2018-06-14  8:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-07 11:18 Sebastian Gniazdowski
2018-06-13 11:49 ` Eitan Adler
2018-06-13 13:10   ` Peter Stephenson
2018-06-13 15:08     ` Bart Schaefer
2018-06-13 15:16       ` Peter Stephenson
2018-06-13 17:13       ` Eitan Adler
2018-06-13 17:19         ` Peter Stephenson
2018-06-13 18:41           ` dana
2018-06-14  8:41             ` Peter Stephenson [this message]
2018-06-14 10:31               ` dana
2018-06-14 10:40                 ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='20180614084126eucas1p28dc51492cd295fc0c7df99529c94221a~3_jDOvnXx1349013490eucas1p2N@eucas1p2.samsung.com' \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).