I've compiled a binary Authorization
and here's its permission
-rwsr-sr-x 1 root wheel 18464 10 26 22:07 ./Authorization
I ran it with the root permission
sudo ./Authorization
so in the beginning, the uid(real uid) and euid(effective uid) of my process is
uid:0 euid:0
then my program would invoke seteuid(501) to change the euid, now it's
uid:0 euid:501
At last, my program would invoke setuid(501), I expected the result is
uid:501 euid: 501
According to manual of of setuid()
The setuid() function is permitted if the effective user ID is that of the super user, or if the specified user ID is the same as the effective user ID.
However, setuid(501) return -1 which is not expected, and not the behavior described in the manual, WHY??
Here's my code
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char * argv[]) {
printf("uid: %d euid: %d\n", getuid(), geteuid());
if (seteuid(501) == -1) {
printf("seteuid error\n");
}
printf("seteuid(501)> uid: %d euid: %d\n", getuid(), geteuid());
if (setuid(501) == -1) {
printf("setuid error\n");
}
printf("setuid(501)> uid: %d euid: %d\n", getuid(), geteuid());
return 0;
}