Effective user ID does not change after creating root file

I am currently reading a book about programming using C, I got the part where I need to write a program that displays the real uid and effective uid in which the file is running. After compiling the code using gcc , I enter the command to see the current uOwner and gOwner ls- l id_demo , the output is as follows:

 -rwxrwxr-x 1 user user 8629 Sep 21 13:04 id_demo 

Then I run the program itself, this is what I get:

 real uid: 1000 effective uid: 1000 

... so far so good. Then I enter the command to change the owner of the file:

 sudo chown root:root ./id_demo 

ls -l confirms that the owner has been changed to root:

 -rwxrwxr-x 1 root root 8629 Sep 21 13:04 id_demo 

Again, running the program shows real uid and uid as 1000. The last step after which uid should be 0: sudo chmod u+s ./uid_demo , but for me they remain as 1000, where in the book the output clearly shows the following:

 real uid: 1000 effective uid: 0 

Any ideas why this is happening?

UPDATE

id_demo source code:

 #include <stdio.h> int main () { printf("real uid: %d\n", getuid()); printf("effective uid: %d\n", geteuid()); } 

UPDATE 2 Screenshots

ss # 1

ls -l testuid

PLEASE, HELP. I'm going crazy. I spent 6 hours finding a solution, and I need to move on.

+6
source share
2 answers

We figured it out. The reason is the ecryptfs home directory. The mount output contains the following line:

 /home/evgeny/.Private on /home/evgeny type ecryptfs 

This means that the home directory is not really part of the root file system (having the necessary suid flag), but its own virtual file system, which apparently does not support setuid binaries by default. I successfully reproduced the problem using a test user with an encrypted home directory.

You can add the suid flag to ecryptfs with the following command:

 sudo mount -i -o remount,suid /home/evgeny 

I'm not sure how safe this is, and how to change it forever so that it survives a reboot.

+8
source

This works for me:

compilation

 $ gcc uid_demo.c -o uid_demo $ ll total 12 -rwxrwxr-x 1 saml saml 6743 Sep 21 17:05 uid_demo -rw-rw-r-- 1 saml saml 116 Sep 21 16:58 uid_demo.c 

Chown

 $ sudo chown root:root uid_demo $ ll total 12 -rwxrwxr-x 1 root root 6743 Sep 21 17:05 uid_demo -rw-rw-r-- 1 saml saml 116 Sep 21 16:58 uid_demo.c 

Chmod

 $ sudo chmod u+s uid_demo $ ll total 12 -rwsrwxr-x 1 root root 6743 Sep 21 17:05 uid_demo -rw-rw-r-- 1 saml saml 116 Sep 21 16:58 uid_demo.c 

run

 $ ./uid_demo real uid: 500 effective uid: 0 
+2
source

Source: https://habr.com/ru/post/954328/


All Articles