0

I created a binary file with Go Language. The owner of the file will be "userA" and the execution permission is set as "userA". The above configurations is enabled using the following commands

sudo chown userA:userA ./program
sudo chmod a+s ./program

Is it possible to change the ownership of the program to userB and execution permission to userB inside the program binary file.

./program change-permission

This command should change the ownership of the running program to userB and execution permission to userB inside the program binary file.

I tried the above commands as a shell script and tried change the ownership and execution permission of the same shell script and it didn't work as expected.

Kindly can anyone advise on this and provide some sample command or code?

sabby
  • 1
  • 1
  • What do you have in yout `Golang` code? And why it didn't work? Did you get any error? – Edgar Magallon Jan 04 '23 at 09:42
  • 2
    "_the execution permission is set as "userA"_" - no it's not. You've set the owner as root so the execution is performed as root. – roaima Jan 04 '23 at 09:46
  • @EdgarMagallon I have used the exec.command to execute command that prints the current user. I tried with a shell script too. It didn't work as expected. No error in the execution. It didn't work as expected. Instead of printing the execution user. It prints the current user. – sabby Jan 04 '23 at 12:52
  • @roaima It's the sample of the command I have used. It's not the same command. I tried the same command as userA as well. – sabby Jan 04 '23 at 12:53
  • If you don't actually put real information in your question you're not going to get useful answers – roaima Jan 04 '23 at 12:59
  • @roaima Hope you understand the privacy. I was not allowed to share the user data here. So i am sharing the sample of the commands. I will edit the commands as userA. I request you not to add comment irrelevant to the question. – sabby Jan 04 '23 at 13:16
  • 1
    Pewex I have not added any irrelevant comments. You incorrectly described setuid root as being setuid userA. I picked you up on that, and then asked that you give us a representation of reality so that we can give you decent answers. You fixed your question. Let's move on. – roaima Jan 04 '23 at 13:35
  • Linux systems do not allow a non-root user to change to ownership of a file. They also do not honor suid on shell scripts. You have not described the problem you're trying to solve but you'll probably want to use `sudo` with a crafted set of allowed commands. – doneal24 Jan 04 '23 at 13:38
  • If the process runs under the same UID as the program file owner (or root), it can change the permissions of the file. If it runs as root, it can change the owner of the file. Provided you find the program file, as it's not necessary for a process to know where it is. I'm not sure if you're running setuid there, as in if "the execution permission is set as "userA"" refers to setuid or just the usual permission bits, i.e. who is allowed to execute the file. (`a+s` looks odd) Anyway, the usual way to install a program file would be a separate tool or script (which likely needs to run as root) – ilkkachu Jan 04 '23 at 14:21
  • @doneal24 Thanks. I understood that the shell script won't work for the specificed case. I will try it as the binary file and will update the status. – sabby Jan 05 '23 at 06:55
  • Thanks @ilkkachu. Will create a seperate tool as you suggested. I referred the first answer for the a+s command for setting the execution permission from this https://stackoverflow.com/questions/26421803/run-my-executable-automatically-as-root-ubuntu. – sabby Jan 05 '23 at 07:02

0 Answers0