Contents: Securing Network ServicesProxiesSpecific Examples

About this document

2. chrooting

2.1. What is a chroot jail?

To minimise the consequences of compromise of a daemon, run publicly-accessible services within a "jail" (from the daemon's point of view) or "sandpit" (from ours). The simplest way to do this is to make use of the chroot command, which wraps the chroot() system call: chroot() changes the root directory of a process (/proc/<procno>/root). Should an intruder manage to gain a shell on a host via a chooted daemon, they will have to break out of the "jail" too in order to set up their own services or cause (other) system damage.

2.2. How to chroot a daemon — outline

Recipes exist for some daemons/services, notably Apache and Bind, but for most it's a case of following the following guidelines and being prepared for a little trial and error:

If you follow a chroot HOWTO (e.g. that the widely-available ones for BIND and Apache, or that for Squid below) and it doesn't work for you, judicious use of strace (and perhaps fgrep, as above) will likely be your saviour.

2.3. An Example — chrooting The Squid Proxy (with PAM Authentication and syslogd)

Here we outline how to chroot the Squid Web proxy. (Squid actually contains a configuration option to chroot itself upon startup. Nevertheless, this example well illustrates the principles involved.)

N.B. These notes were written from memory, after chrooting Squid and have not been tested (by building a second jail from scratch by following these notes) — so beware! Corrections welcome.

  1. First, make the chroot directory, e.g.,
          prompt> mkdir /chroot_squid
    and the directories below this (bin, dev, etc, lib, usr, usr/local...).

  2. Build and install Squid in the usual place, e.g., /usr/local/squid.

  3. Determine which shared-object libraries ldd indicates are needed
            ldd /usr/local/squid/sbin/squid
   => /lib/ 
   => /lib/
   => /lib/ 
   => /lib/ 
   => /lib/ 
   => /lib/ 
            /lib/ => /lib/
    and copy these into the /chroot_squid tree, i.e., to /chroot_squid/lib.

    In simple cases of chrooting --- if you are lucky --- this will be sufficient; mostly it is not. In this case
            chroot /chroot_squid /usr/local/squid/sbin/squid
    will fail.

  4. Run
            strace -o /tmp/squid.strace -v -f -r -e trace=open -tt \
    and, after a few seconds, kill the strace/squid process and examine the contents of /tmp/squid.strace, e.g.,
            fgrep \.so /tmp/squid.strace
    which gives us this list:
    Copy the extra libraries from /lib to /chroot_squid/lib.

  5. Next up, Squid can be configured to use PAM authentication --- here is the pertinent line from squid.conf:
            auth_param basic program /usr/local/squid/libexec/pam_auth
    and ldd /usr/local/squid/libexec/pam_auth gives some further libraries to copy to our chroot_squid tree:
   => /lib/ 
   => /lib/tls/
    Supporting PAM configuration and libraries must also be installed:

  6. Ok, so now we're feeling confident, so we try to start Squid,
            chroot /chroot_squid /usr/local/squid/sbin/squid
    but again it fails. It's time for a brute force:
            strace -o /tmp/squid.strace -v -f -r /usr/local/squid/sbin/squid
    and examining the (tedious) output shows two devices that are required in out chroot jail:
    For the first ls -l /dev/null
            crw-rw-rw-  1 root root 1, 3 2005-03-21 08:39 /dev/null
            cd /chroot_squid/dev
            mknod null c 1 3

  7. To "enable" /dev/log, i.e., syslogging for Squid under out chroot jail, we note the relevant comments in the widely-available "Chroot Bind Howto" and also examine the syslogd man page. Hence, we edit /etc/init.d/syslogd to contain
            SYSLOGD="-m 0 -a /chroot_squid/dev/log"
    which is equivalent to
            /sbin/syslogd -m 0 -a /chroot_squid/dev/log
    We now have our two required devices:
            ls -l /chroot_squid/dev
            srw-rw-rw-    1 root     root            0 2004-06-21 18:08 log
            crw-rw-rw-    1 root     root       1,   3 2004-06-21 18:23 null
    and Squid will now start in the chroot jail.

2.4. More

Docs -- examples :

 -- Bind-Chroot-Howto (Debian):

 -- apache:


     -- 'chroot' an Apache tree with Linux and Solaris (Howto)
        Published on 2001-02-26 - by Denice Deatrich, ©Denice Deatrich.

     -- Apache chrooting made simple
        Published on 2004 - by Ivan Ristic, ©Ivan Ristic.

     -- Chroot-BIND HOWTO
        Published on December 01, 2001 - by Scott Wunsch,

     -- Chroot-BIND8 HOWTO
        Published on July 01, 2001 - by Scott Wunsch,

     -- Chrooting daemons and system processes HOW-TO
        Published on October 21, 2002 - by Jonathan,

     -- Linux Process Containment – A practical look at chroot and 
            User Mode Linux
        Published on June 03, 2003 - by Paul Lessard, ©SANS Institute.

     -- Setting up chrooted sftp in linux
        Published on 2003 - by James Dennis, ©James Dennis.

...previousup (conts)next...