Allow shell script to modify only containing directory

58 Views Asked by At

I am writing a posix compliment shell script that will, amongst other things, clone a git repository and then execute a script (that was cloned along with the repository) inside the repository.

For example:

git clone [email protected]:torvalds/linux.git
cd linux
./Kconfig

The idea would be that people would use it for good, not evil, but you know.... So really I would like stop people from doing putting a line like:

rm -rf /

Inside the script.
Or perhaps something slightly less evil like:

rm -rf ../../

Is it possible for me to somehow change the permissions of the script (after the clone) so that it is only able to modify things inside the cloned repository?

1

There are 1 best solutions below

0
On BEST ANSWER

Basically the answer for your question is the chroot command, which allows you to lock in processes in a directory as if it was the root directory. chroot requires root privileges to setup, but there are alternative implementations such has schroot, fakechroot, or proot that don't. Because all file system access (also read) is restricted, you will need to hand in anything that the scripts need to function into the chrooted environment. How to do that conveniently depends on your distribution.

That doesn't necessarily mean it is perfectly secure, because it provides only file system isolation.