From: Corinna Vinschen <[email protected]>

Commit dc7b67316d01 ("Cygwin: uinfo: prefer token primary group")
broke the code overriding the primary group twofold.

First, it invalidated one of the conditions used to check if the
override is supposed to happen.  Do not check against myself->gid,
but against the desires primary GID value instead.

Second, the followup code expected myself->gid to be set correctly
to the desired primary GID already, but it wasn't anymore.  This is
a subtil error, because it leads to having the wrong primary GID in
`id' output, while the primary group SID in the user token was
correctly set.  But as soon as you run this under strace or GDB,
the problem disappears, because the second process tree under GDB
or strace takes over from the already changed user token.  Override
myself->gid once again after successfully changing the primary GID.

Fixes: dc7b67316d01 ("Cygwin: uinfo: prefer token primary group")
Signed-off-by: Corinna Vinschen <[email protected]>
---
 winsup/cygwin/uinfo.cc | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/uinfo.cc b/winsup/cygwin/uinfo.cc
index ffe71ee0726c..8e9b9e07de9d 100644
--- a/winsup/cygwin/uinfo.cc
+++ b/winsup/cygwin/uinfo.cc
@@ -174,7 +174,7 @@ internal_getlogin (cygheap_user &user)
       gsid = cygheap->dom.account_sid ();
       gsid.append (DOMAIN_GROUP_RID_USERS);
       if (!pgrp
-         || (myself->gid != pgrp->gr_gid
+         || (pwd->pw_gid != pgrp->gr_gid
              && cygheap->dom.account_sid () != cygheap->dom.primary_sid ()
              && RtlEqualSid (gsid, user.groups.pgsid)))
        {
@@ -209,7 +209,10 @@ internal_getlogin (cygheap_user &user)
                        myself->gid = pwd->pw_gid = pgrp->gr_gid;
                    }
                  else
-                   user.groups.pgsid = gsid;
+                   {
+                     user.groups.pgsid = gsid;
+                     myself->gid = pwd->pw_gid;
+                   }
                  clear_procimptoken ();
                }
            }
-- 
2.51.1

Reply via email to