Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the setuid bit in Linux

Tags:

c

linux

unix

setuid

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?

like image 914
hgiesel Avatar asked Jul 26 '26 21:07

hgiesel


2 Answers

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.

like image 131
dbush Avatar answered Jul 28 '26 10:07

dbush


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

like image 27
Giuseppe Guerrini Avatar answered Jul 28 '26 11:07

Giuseppe Guerrini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!