hgiesel
hgiesel

Reputation: 5648

Using the setuid bit in Linux

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?

Upvotes: 3

Views: 552

Answers (2)

Giuseppe Guerrini
Giuseppe Guerrini

Reputation: 4426

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

Upvotes: 1

dbush
dbush

Reputation: 223699

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.

Upvotes: 4

Related Questions