setuid bit on, yet program can't open a superuser file

607 views Asked by At

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:-

  1. 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.

  2. 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 when sudo chmod 4775 setuidDemonstration had been run. But it couldn't. Also, when I printed euid while running it from normal mode, it printed 1000 and not 0. 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.

1

There are 1 answers

0
Emil Vikström On

Because /superuser.cpp is the name of a file in the filesystem root, not in the current working directory. Use superuser.cpp or ./superuser.cpp if the file is in the same path as you are when you run the program.