πΉSandboxing
Sandboxing involves providing a safe environment for a program or software so that you can play around with it without hurting your system. It actually keeps your program isolated from the rest of the system, by using any one of the different methods available in the Linux kernel.
chroot
chroot command in Linux/Unix system is used to change the root directory. Every process/command in Linux/Unix like systems has a current working directory called root directory. It changes the root directory for currently running processes as well as its child processes. A process/command that runs in such a modified environment cannot access files outside the root directory. This modified environment is known as βchroot jailβ or βjailed directoryβ. Some root user and privileged process are allowed to use chroot command.
systemv based
First, create a directory to set as root a file system for a process:
Next, make the required directory inside it.
Now, the most important step is to copy the executable and libraries. To get the shell inside the chroot, you also need /bin/bash
To see the libraries required for this script, run the following command:
Now, copy these files to the lib or lib64 of /chroot as required.
Once you have copied all the necessary files, itΒs time to enter the chroot.
You will be prompted with a shell running inside your virtual environment. Here, you donΒt have much to run besides ls, but it has changed the root file system for this process to /chroot.
To get a more full-featured environment you can use the debootstrap utility to bootstrap a basic Debian system:
It will download a minimal system to run under chroot. You can use this to even test 32-bit applications on 64-bit systems or for testing your program before installation. To get process management, mount proc to the chroot, and to make the contents of home Βlost on exit, mount tmpfs at /home//:
To get Internet connection inside, use the following command:
After that, you are ready to enter your environment.
Here, you get a whole basic operating system inside your chroot. But it differs from your main system by mount point, because it only uses the mount property as the isolator. It has the same hostname, IP address and process running as in the main system. Thatβs why it is much less secure (this is even mentioned in the man page of chroot), and any running process can still harm your computer by killing your tasks or affecting network based services.
__ To run graphical applications inside chroot, open x server by running the following command on the main system:
xhost +
and on chroot system
export DISPLAY=:0.0
systemd based
On systemd based systems, chrooting is pretty straightforward. Its needed to define the root directory on the processes unit file only.
Here RootDirectory shows where the root directory is for the foobar process.
The program script path has to be inside chroot, which makes the full path of that process script as /chroot/bin/my_program
.
Before the daemon is started, a shell script pre.sh is invoked, the purpose of which is to set up the chroot environment as necessary, i.e., mount /proc and similar file systems into it, depending on what the service might need. You can start your service by using the following command:
chroot Jail
The basic command to create a chroot jail is as follows:
Only a root/privileged user can use the chroot system call. A non-privileged user with the access to the command can bypass the chroot jail.
create a mini-jail for the βbashβ and the βlsβ command
Create a directory which will act as the root of the command.
Create all the essential directories for the command to run
Depending on your operating system, the required directories may change. Logically, we create all these directories to keep a copy of required libraries. To see what all directories are required, see Step 4.
Run the βwhichβ command
Run the βwhichβ command to find the location of ls and bash command.After running which command,copy those binaries in the βbinβ directory of our jail. Make sure you donβt have any of these commands aliased. From now on, we would be referring to our directory as βJailedβ directory for convenience.
Copy appropriate libraries/objects
For the executables in our Jaileddirectory to work we need to copy the appropriate libraries/objects in the JAILED directory. By default, the executable looks at the locations starting with β/β. To find the dependencies we use the command βlddβ
Run the following commands to create appropriate directories.
Similarly for ls,
**Sudo chroot:**Run this command to change the root to the JAILED directory, along with the path to the shell. By default it will try to load β/bin/shβ shell.
You might face this error while running the chroot command**:**
This may be due to 2 reasons, either the file does not exist(which is obvious), or when the loading library fails or is not available. Double-Check if the libraries are in correct location.
**A new shell must pop up:**Its our jailed bash. We currently have only 2 commands installed, bash and ls. Fortunately cd and pwd are builtin commands in bash shell, and so you can use them as well.
Roam around the directory, try accessing βcd /../β or something similar. Try to break the jail, probably you wonβt be able to.
To exit from the jail**:**
The most important and interesting part is that, when you run:
and find the process, youβll find that there is only one process:
Interestingly, processes in the jailed shell run as a simple child process of this shell. All the processes inside the JAILED environment, are just simple user level process in the host OS and are isolated by the namespaces provided by the kernel, thus there is minimal overhead and as an added benefit we get isolation.
sample script
Ip-netns
The Ip-netns utility is one of the few that directly use network namespaces to create virtual interfaces. To create a new network namespace, use the following command:
To check the interfaces inside, use the command shown below:
You can even get the shell inside it, as follows:
This will take you inside the network namespace, which has only a single network interface with no IP. So, you are not connected with the external network and also cant ping.
This will bring the loop interface up. But to connect to the external network you need to create a virtual Ethernet and add it to netns as follows:
Now, its time to set the IP to these devices, as follows:
Unshare
The unshare utility is used to create any namespace isolated environment and run a program or shell inside it.
To get a network namespace and run the shell inside it, use the command shown below:
The shell you get back will come with a different network stack. You can check this by using #ip addr, as follows:
To create a user namespace environment, use the following command:
You can check your user inside the shell by using the command below:
To get the PID namespace, use the following command:
Inside this namespace, you can see all the processes but cannot kill any.
To get a whole different degree of process tree isolation you need to mount another proc for the namespace, as follows:
In this way, you can use unshare to create a single namespace. More about it can be found out on the man page of unshare.
A namespace created by using unshare can also be combined to create a single shell which uses different namespaces. For example:
This will create an isolated environment using the PID and user namespaces.
Firejail
Firejail is an SUID sandbox program that is used to isolate programs for testing or security purposes. It is written in C and can be configured to use most of the namespaces. To start a service in firejail, use the following command:
It will start Firefox in a sandbox with the root file system mounted as read only. To start Firefox with only ~/Downloads and ~/.mozilla mounted to write, use the following command:
Firejail, by default, uses the user namespace and mounts empty temporary file systems (tmpfs) on top of the user home directory in private mode. To start a program in private mode, use the command given below:
To start firejail in a new network stack, use the following command:
To assign an IP address to the sandbox, use the following command:
To sandbox all programs running by a user, you can change the default shell of that user to /usr/bin/firejail.
Last updated