简体   繁体   中英

-SUID- Can't run a “-rwsr-xr-x” file as root

I am using Ubuntu 16.10.

I was following along a book, doing something like this:

#include <stdio.h>

int main() {
    printf("Real UID:\t%d\n", getuid());
    printf("Effective UID:\t%d\n", geteuid());
}

And to run this file as sudo without using the sudo command, after compiling with gcc, I changed the owner and group to root.

$ gcc -o test test.c
$ sudo chown root:root ./test
$ chmod u+s ./test
$ ls -l
-rwsr-xr-x 1 root root 8512 Mar 9 test

Now, this is what I got when I executed the file. My UID is 1000.

$ ./test
Real UID:        1000
Effective UID:   1000

The book I was reading said the result should be like this:

$ ./test
Real UID:        1000
Effective UID:   0

The UID for root is 0, right? Does this mean that I am running an -rwsr-xr-x file, owned by root, with my own user privilege? I don't understand.

Is your book a little on the older side? It seems like modern *nix variants widely ignore the sticky bit on executable files:

[...] the Linux kernel ignores the sticky bit on files. [...] When the sticky bit is set on a directory, files in that directory may only be unlinked or renamed by root or the directory owner or the file owner.[4]

https://en.wikipedia.org/wiki/Sticky_bit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM