From: Corinna Vinschen <[email protected]>

Commit dc7b67316d01 ("Cygwin: uinfo: prefer token primary group")
broke the code overriding the primary group in two different ways:

- It changed the way myself->gid was set before checking its value.

  Prior to dc7b67316d01, myself->gid was always set to the primary group
  from the passwd entry (pw_gid).  With the patch, it was set to the
  primary group from the Windows user token (token_gid) in the first
  place.

  The following condition checking if pw_gid is different
  from token_gid did so, by checking token_gid against myself->gid,
  rather than against pw_gid.  After dc7b67316d01 this was always
  false and the code block overriding the primary group in Cygwin and
  the Windows user token with pw_gid was never called anymore.

  The solution is obvious: Do not check token_gid against myself->gid,
  but against the desires primary GID value in pw_gid instead.

- The code block overriding the primary group simply assumed that
  myself->gid was already set to pw_gid, but, as outlined above,
  this was not true anymore after dc7b67316d01.

  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.

  The solution is to override myself->gid with pw_gid once more, after
  successfully changing the primary GID to pw_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.52.0

Reply via email to