Why sudo cp doesn't work when I run in a custom Mac App but works when I run in the shell of Terminal?

81 Views Asked by At

In Terminal I move to Desktop and I create a folder App1.app with this commands (change <your_user> for your own Mac user):

cd /Users/<your_user>/Desktop
mkdir App1.app

Then I move into App1.app:

cd App1.app

I create a new file with next command:

touch App1

I type this text inside this new file to run as script this script (using vi App1 command) :

#!/bin/sh
export PASSWORD=<CHANGE THIS FOUR YOUR ROOT PASSWORD>
#CHANGE THIS FOUR YOUR PATH TO A FILE OF INSTALLED APP LIKE WHATSAPP
export FILE_PATH=/Applications/WhatsApp.app/Contents/Info.plist

echo "$PASSWORD" | sudo -S cp "$FILE_PATH" "${FILE_PATH}.backup"
echo "$PASSWORD" | sudo -S cp "$FILE_PATH" "/Users/<your_user>/Desktop/Info_plist.backup"
exit

Once I write this file (with 'Esc' ':' 'wq' 'Enter') I change the user and group of the app:

chown -R root:wheel .

At last I try to run this App using double click from the Desktop or from Terminal using this command:

open /Users/<your_user>/Desktop/App1.app

This action copy the file in Desktop, but the file inside the installed application is not copied.

But if I return to terminal and I run:

/Users/<your_user>/Desktop/App1.app/App1

the two files are copied, even the installed application one.

Is there any way to get the same behavior of the terminal when I run the script like an standar application?

NOTE: I tryped to copy a file of the inside of TextEdit app but it won't copy inside the TextEdit.app folder.

NOTE 2: I tryed to added the same permission to the App1 like the Terminal, even the Total disk access, but may be I was doing something wrong or I forgot some grant.

EDIT: As sugest me in the comments I put the result of the trace information of the fail here:

+ export PASSWORD=<my root password>
+ PASSWORD=<my root password>
+ export FILE_PATH=/Applications/WhatsApp.app/Contents/Info.plist
+ FILE_PATH=/Applications/WhatsApp.app/Contents/Info.plist
+ echo <my root password>
+ sudo -S cp /Applications/WhatsApp.app/Contents/Info.plist /Applications/WhatsApp.app/Contents/Info.plist.backup
Password:cp: /Applications/WhatsApp.app/Contents/Info.plist.backup: Operation not permitted

And I put also the trace information when I run in the Terminal:

+ export PASSWORD=<my root password>
+ PASSWORD=<my root password>
+ export FILE_PATH=/Applications/WhatsApp.app/Contents/Info.plist
+ FILE_PATH=/Applications/WhatsApp.app/Contents/Info.plist
+ echo <my root password>
+ sudo -S cp /Applications/WhatsApp.app/Contents/Info.plist /Applications/WhatsApp.app/Contents/Info.plist.backup
Password:+ echo <my root password>
+ sudo -S cp /Applications/WhatsApp.app/Contents/Info.plist /Users/acayon/Desktop/Info_plist.backup
+ exit
1

There are 1 best solutions below

2
Asier Cayón Francisco On

I found by mylef a solution for this problem. It seems that it can be solved with "the setuid bit).

According with man of chmod command:

4000    (the setuid bit).  Executable files with this bit set will run with effective uid set to the uid of
                   the file owner.  Directories with this bit set will force all files and sub-directories created in
                   them to be owned by the directory owner and not by the uid of the creating process, if the
                   underlying file system supports this feature: see chmod(2) and the suiddir option to mount(8).

I suppose that it allows to run the internal script of the my custom App with root privileges.

Because of it I can remove the sudo command of the script and with a simply cp command the files can be copied.