-3

I am trying to run a script from my c++ code owned by root. But it throws me an error permission denied to run the script.

Permissions on files are as follows:

-rwx------ 1 root    mygame   39 Dec 24 19:11 script.sh
-rwsr-xr-x 1 gag5kor mygame 7999 Dec 26 12:23 a.out

C++ code:

   int err = system("./script.sh");
   cout << "Before err: " << err << endl;

   cout << "setuid: " << seteuid(0) << endl;

   err = system("./script.sh");
   cout << "After err: " << err << endl;

getuid() and geteuid() function returns me same value (say 1234) even after I call setuid(0) to get the root permissions.

What I am doing wrong here?

I read the other answers on stackoverflow but not able to understand properly.

Daemon
  • 1,575
  • 1
  • 17
  • 37
  • 3
    Why do you think a simple function call (seteuid) is enough to get root access from a non-root program? Of course it's not that easy. – deviantfan Dec 26 '15 at 09:56
  • @deviantfan how can I achieve what I am trying. Can you please tell what all modifications will be required. – Daemon Dec 26 '15 at 17:05
  • people who have downvoted can you please explain the reason for the same. – Daemon Dec 26 '15 at 17:08
  • First decide what you want. a) Running the script owned by root [with the permissions of your user account], as you're saying, or b) running the script owned by root *as root*, which is what you're trying in the code apparently. .Then tell us too as which user your a.out program should run. Then tell us what "other answers" you read and what part you couldn't understand. Then tell us why you're mixing EUID and UID. Then tell us why you thought your seteuid call will work. ... etc.etc. – deviantfan Dec 26 '15 at 18:15
  • One of the downvotes is from me because there are so many unclear and nonsensical things here, but I'll gladly reverse it if you can answer these things. – deviantfan Dec 26 '15 at 18:15
  • @deviantfan 1. script is owned by root. – Daemon Dec 27 '15 at 10:48
  • @deviantfan 1. script is owned by root and only root has execute permissions. 2. normal user other than root will execute the a.out file. As script is not accessible to normal user other than root, so I am trying to gain root privileges to run the script. 3. Whatever I am trying is it possible. 4. Can i achieve sudo kind of behavior through code? – Daemon Dec 27 '15 at 10:56
  • 1) This doesn't answer the question 2) Neither does this 3) Upgrading the permissions of some existing program while running is not possible. The only time where a process can get root permissions is when it is started. And no, doing something like sudo in your code is not possible either; sudo just works because of it's SUID [which needs root permissions to set if you want it for your own program, so no help either] – deviantfan Dec 27 '15 at 11:18

1 Answers1

-1

chmod 0555 script.sh should fix this

EDIT

chown root script.sh
chmod 0500 script.sh

Should fulfill your requirements

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • If I do chmod 0555 then this script can be run by everyone. I want only root should be able to execute script file. So I have given permission as 0700 to script.sh – Daemon Dec 26 '15 at 09:51