Advanced Linux Networking
Necessary chroot Environment Files
The first task in configuring a server to operate in a chroot jail is to prepare the jail. This means you must create an appropriate directory tree, copy system files, and copy the server's files. In some sense, you set up a miniature Linux system in the chroot jail, but this miniature system is missing most of the programs and configuration files that make up a normal Linux system.
NOTE
Preparing a Directory Tree
A chroot jail needs a directory to call its own. In principle, you can place this directory anywhere in the Linux filesystem tree, except for pseudo-filesystem directories like /proc . If your server needs to be able to write files, the chroot directory tree must exist on a read/write medium. The examples in this chapter use /opt/chroot as the root of the chroot jail, but this is an arbitrary placement. Within your chroot tree, you should create directories modeled after those of the normal Linux root directory, including subdirectories. Chances are you'll need only a small subset of the directories needed in a regular Linux installation, though. Common directories you might need to create include /bin , /sbin , /usr , /lib , /etc , and /var . You may need to add to this directory list as you add server-specific files. You should not populate these directories with all the files that reside in their regular counterparts; much of the point of running in a chroot jail is to deprive a would-be cracker of access to the regular mix of tools available in these directories. If you want to run multiple chroot servers, you should create a separate chroot tree for each one. For instance, you might create /opt/chroot/ftp and /opt/chroot/sendmail . Copying Server Files
Once you've created a basic chroot jail directory tree, you need to copy files into that tree. There are actually two different conditions that may apply. First, the server may directly support chroot operations. In this case, it may not be necessary to copy the server's executable to the chroot jail. Instead, you run the server from outside the jail, but tell it where the jail is. The server then issues a chroot() command internally, and thereafter it's locked into the jail. The server might read its configuration files from outside of the jail, too, so this operation can be nearly transparent ”you only need to provide the jail itself and whatever files the server needs during normal operation. FTP servers often lock themselves into chroot jails in this way, particularly when they run as anonymous FTP servers. Chapter 21, Running FTP Servers, covers anonymous FTP server operation, including setting up the necessary chroot jail directory. The other option is to run a server that doesn't include built-in chroot support by using the chroot program. In this approach, you must copy the server executable file, its configuration files, and any files it requires during normal operation to the jail. You'll also have to copy some more general system files, as described in the next section, "Copying System Files." Tracking down the files that the server needs can be tricky, because there's no simple rule that will always find the necessary files. You may be able to find leads by checking the server's documentation and by examining the list of files that come with the distribution package. For instance, you can use tar , rpm , or dpkg to find the files in the original server package. You may not need to copy all of these files. For instance, you can leave documentation files outside of the chroot jail. Another trick is to use the strace program to discover what files a server opens. You can run the server with a command like strace serverprog and examine the output to discover what serverprog is doing, including what files it's opening.
NOTE
Copying System Files
After you've copied the basic server files to the chroot jail, you must copy any general-purpose Linux system files upon which the server depends. Precisely what files are needed varies from one server to another, but some common files you might need include the following:
For servers that include explicit internal chroot() support, chances are good that you'll need to copy fewer system files than for servers that don't include this support. The servers that include internal chroot() calls can often load libraries, system files, and so on before running, and so don't need files to be stored in their chroot environments, even if those files are required for ordinary operation.
TIP
|