1

I have a Solaris 10 system:

# uname -a
SunOS edddev03 5.10 Generic_150400-04 sun4v sparc SUNW,SPARC-Enterprise-T5120

I wrote a program printing the uid:

# cat getuid.c
int main (void) {
  printf ("%d\n", getuid());
}

It works as expected:

# gcc -o /bin/getuid getuid.c
# getuid
0
# su nobody -c /bin/getuid
60001

Now I changed it to be a setuid program:

# chmod 4555 /bin/getuid
# ls -la /bin/getuid
-r-sr-xr-x   1 root     root        6424 May 18 13:04 /bin/getuid

But it is not working as expected:

# su nobody -c /bin/getuid
60001

I expected a 0. Why is it not working?

  • 2
    I think the setuid bit just sets the euid. you have to all setuid in your program to change the uid of the program. – Bratchley May 18 '15 at 11:37
  • 2
    I think a better way of saying essentially the same thing is, "The setuid bit sets just the Effective UID (EUID).  You need to call geteuid() in your program to see that your EUID has changed." – G-Man Says 'Reinstate Monica' May 18 '15 at 13:10
1

I found this Q still-unanswered; G-Man and Bratchley have pointed out the error; getuid() returns the "real user ID of the calling process" while geteuid() returns the "effective user ID of the calling process".

You can see the difference with this program:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main (void) {
  printf ("getuid=%d, geteuid=%d\n", getuid(), geteuid());
  setuid(geteuid());
  printf ("getuid=%d, geteuid=%d\n", getuid(), geteuid());
}

$ gcc -o getuid getuid.c
$ sudo chown root getuid
$ sudo chmod u+s getuid
$ su nobody -c ./getuid
getuid=60001, geteuid=0
getuid=0, geteuid=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.