I have a file superuser.cpp created by the superuser with access permissions 770. Now, the superuser creates a file setuidDemonstration.cpp in which superuser.cpp is opened using open("superuser.cpp", O_RDONLY). The .cpp and the object file of setuidDemonstration.cpp have permissions rwxrwxr-x. Now, my questions are:-
When I ran the program setuidDemonstration, in both normal and superuser mode it could not open superuser.cpp. Why? At least, the superuser mode should have succeeded in opening it.
Now, I do
sudo chmod 4775 setuidDemonstration. This should allow the program to open superuser.cpp even in normal mode because it would get euid of superuser during execution as the setuid bit has been set whensudo chmod 4775 setuidDemonstrationhad been run. But it couldn't. Also, when I printed euid while running it from normal mode, it printed1000and not0. Why?
UPDATE: Thanks for pointing out the mistake. I have removed '/' from the file path and now it does work for superuser. But even now after sudo chmod 4775 setuidDemonstration, the normal mode run program falis to open the file. Pls explain.
Because
/superuser.cppis the name of a file in the filesystem root, not in the current working directory. Usesuperuser.cppor./superuser.cppif the file is in the same path as you are when you run the program.