I got the same uid and euid even though the file belongs to root and has the suid bit set. Does anybody know how to make a test case to let getuid() and geteuid() return different results? Thanks.
$ cat main.py
#!/usr/bin/env python3
import os
print(os.getuid())
print(os.geteuid())
$ dir
total 4.0K
-rwsr-xr-x 1 root staff 154 2021/02/02-10:48:27 main.py
$ ./main.py
504
504
$ id
EDIT: I tried a C program. uid and euid are still the same.
$ cat main.c
// vim: set noexpandtab tabstop=2:
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main() {
uid_t uid = getuid();
uid_t euid = getuid();
printf("%d\n", uid);
printf("%d\n", euid);
}
$ ls -l ./main.exe
-rwsr-xr-x 1 root dialout 16656 Feb 2 12:14 ./main.exe
$ ./main.exe
504
504
Typo!
uid_t euid = getuid();
should read
uid_t euid = geteuid();
Then the C program will work. Don't make setuid #!
scripts. That's not implemented for security reasons.
The suidperl
story contains within it the description of why setuid won't work on #!
scripts.