From c0937a36f2674c3064137d574b599592249e099c Mon Sep 17 00:00:00 2001 From: Grisha Levit Date: Fri, 30 Jun 2023 03:05:21 -0400 Subject: [PATCH] avoid calling setgid when ids already match On some Android versions attemptig to set the current egid causes a SIGSYS to be sent even when ids already match. Avoid calling the sete[gu]id functions when it would be a noop. --- shell.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/shell.c b/shell.c index 9f1934d2..2f17cfdc 100644 --- a/shell.c +++ b/shell.c @@ -1321,10 +1321,11 @@ disable_priv_mode (void) { int e; + if (current_user.euid != current_user.uid && #if HAVE_SETRESUID - if (setresuid (current_user.uid, current_user.uid, current_user.uid) < 0) + setresuid (current_user.uid, current_user.uid, current_user.uid) < 0) #else - if (setuid (current_user.uid) < 0) + setuid (current_user.uid) < 0) #endif { e = errno; @@ -1332,17 +1333,20 @@ disable_priv_mode (void) #if defined (EXIT_ON_SETUID_FAILURE) if (e == EAGAIN) exit (e); + current_user.euid = current_user.uid; #endif } + + if (current_user.egid != current_user.gid && #if HAVE_SETRESGID - if (setresgid (current_user.gid, current_user.gid, current_user.gid) < 0) + setresgid (current_user.gid, current_user.gid, current_user.gid) < 0) #else - if (setgid (current_user.gid) < 0) + setgid (current_user.gid) < 0) #endif - sys_error (_("cannot set gid to %d: effective gid %d"), current_user.gid, current_user.egid); - - current_user.euid = current_user.uid; - current_user.egid = current_user.gid; + { + sys_error (_("cannot set gid to %d: effective gid %d"), current_user.gid, current_user.egid); + current_user.egid = current_user.gid; + } } #if defined (WORDEXP_OPTION) -- 2.41.0