3

I have this C file:

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    printf("%s\n", getlogin());
    printf("%i\n", getuid());
}

I compile it, set the UID and GID both to root and set the setuid bit, so that it looks like this:

-rwsrwsr-x 1 root    root    8735 Apr  8 19:51 a.out

However when I call $ ./a.out I still get:

user
1000

What am I doing wrong?

hgiesel
  • 5,430
  • 2
  • 29
  • 56

2 Answers2

4

The real user ID is still the user that called the program, but the effective user ID is root. In a setuid program, they are not the same.

To get the effective user ID, call geteuid(). You can also use cuserid() to get the name associated with the effective user ID.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

Your program has got only the permission to change its uid. To actually switch to root you have to call setuid(0) in it. Please have a look here

Giuseppe Guerrini
  • 4,274
  • 17
  • 32