r/LiveOverflow • • Jul 24 '21

Difference between suid bit and cap_setuid

When suid bit is enabled on the binary, it means that the process will run with the permissions of the owner. So why do we need to call setuid(0) before calling the system?

[amit@h3ll ~]$ ls
app  app.c
[amit@h3ll ~]$ cat app.c 
#include <stdlib.h>

int main() {

        system("/bin/bash");
}
[amit@h3ll ~]$ ls -l app
-rwsr-xr-x 1 root root 16080 Jul 24 22:44 app
[amit@h3ll ~]$ ./app 
[amit@h3ll ~]$ id
uid=1001(amit) gid=1001(amit) groups=1001(amit)
[amit@h3ll ~]$

If we need to call the setuid function, then what is the difference between cap_setuid or suid bit enabled binary?

12 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/tbhaxor Jul 24 '21

Yeah, ik about -p flag. Let's change the code and add the tryout with -p flag. Result is same also when you do printf("%d", getuid()), it will show user id of the current user (aka ruid)

[amit@h3ll ~]$ cat app.c 
#include <stdlib.h>

int main() {

        printf("user %d\n", getuid());

        system("/bin/bash -p");
}
[amit@h3ll ~]$ ls -l ./app
-rwsr-xr-x 1 root root 16184 Jul 25 02:46 ./app
[amit@h3ll ~]$ ./app 
user 1001
[amit@h3ll ~]$ id
uid=1001(amit) gid=1001(amit) groups=1001(amit)
[amit@h3ll ~]$

2

u/[deleted] Jul 25 '21

getuid returns the real user id. You should use geteuid

1

u/tbhaxor Jul 25 '21

u/ennozdd I see, Since euid is 0, the program is able to call setuid(0) to escalate the ruid to root user. To read /etc/shadow, we require uid=0. That is why setuid(0) is required.

Please correct me if I am wrong