setregid() and setreuid() implementation proposal

Pierre A. Humblet
Thu Jan 23 03:03:00 GMT 2003

At 06:42 PM 1/22/2003 +0100, Corinna Vinschen wrote:

Back to earth and to the original thread...

>Your code:
>    if (rgid != -1)
>      ERROR;
>    return setegid (egid);
>My tiny extension:
>    if (rgid != -1)
>      if (rgid == egid)
>        return setuid (rgid);
>      ERROR;
>    return setegid (egid);

I think the following is a full implementation. Cygwin doesn't
really care about the real uid, it's just bookkeeping. However
there is a posix requirement that setuid(ruid) must succeed, the
hard part of which would be to change the effective uid.
The only way to know is to do a dry run, that's the first seteuid32
below. The second one is for real. The third is to back out of 
the dry run if needed. Ditto for gid. Neither compiled nor tested.


extern "C" int
setreuid32 (__uid32_t ruid, __uid32_t euid)
  int ret = 0;
  bool tried;	
  __uid32_t old_euid = myself->uid;

  if ((tried = ruid != ILLEGAL_UID && cygheap->user.real_uid != ruid && euid != ruid))
    ret = seteuid32 (ruid);
  if (!ret && euid != ILLEGAL_UID)
    ret = seteuid32 (euid); 
  if (tried && (ret || euid == ILLEGAL_UID) && seteuid32 (old_euid))
    system_printf ("Cannot restore original euid %u\n", old_euid);
  if (!ret && ruid != ILLEGAL_UID)
    cygheap->user.real_uid = ruid;
  debug_printf ("real: %d, effective: %d", cygheap->user.real_uid, myself->uid);
  return ret;

More information about the Cygwin-developers mailing list