setuid() C function changes euid value too?

897 views Asked by At

This sample suid program

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

void main() {
int ret;
printf("uid=%d, euid=%d\n", getuid(), geteuid());
ret = setuid(1000);
printf("uid=%d, euid=%d\n", getuid(), geteuid());
}

has 'noemi' (id=1001) as owner:

sarah-$ logname
sarah
sarah-$ ls -l p.bin
-rwsr-xr-x 1 noemi noemi 7028 17 dic 10.30 p.bin

If started from user 'sarah' (id=1000) euid changes to 1000
Why? p.bin changes only uid (this should have no effect, since uid was 1000 when p.bin was started by 'sarah'):

sarah-$ ./p.bin
uid=1000, euid=1001
uid=1000, euid=1000
sarah-$

I am using Debian 6 64 bit.
Please help me understand. Thank you

1

There are 1 answers

0
hek2mgl On BEST ANSWER

Check man 2 setuid:

setuid() sets the effective user ID of the calling process. If the effective UID of the caller is root, the real UID and saved set-user-ID are also set.

So, as you already have observed, when you are executing setuid() as regular user it will only change the effective user id.