Chroot Protection and Breaking

3,505 views
3,363 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,505
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chroot Protection and Breaking

  1. 1. Chroot protection and breaking<br />Anton Chuvakin, Ph.D. <br />WRITTEN: 2001-2003 (unknown)<br />DISCLAIMER:<br />Security is a rapidly changing field of human endeavor. Threats we face literally change every day; moreover, many security professionals consider the rate of change to be accelerating. On top of that, to be able to stay in touch with such ever-changing reality, one has to evolve with the space as well. Thus, even though I hope that this document will be useful for to my readers, please keep in mind that is was possibly written years ago. Also, keep in mind that some of the URL might have gone 404, please Google around.<br />Chroot command and chroot system call might sound like a good security<br />measure - one command executed and plain old UNIX " cd /" no longer<br />transports you to a root directory of the system. Instead, you are<br />bound to stay in the restricted part of the filesystem, surrounded<br />only by files chosen by a paranoid system administrator. In fact, that<br />is how it should be.<br />Is it possible to break out of chroot solitary confinement? Yes, if<br />certain conditions are met. In this paper we will analyze what chroot<br />is good for and also what chroot is bad for. <br />First, how does it work? When one types '/sbin/chroot directory_name'<br />on the UNIX system command line one sees that the new root is now<br />'directory_name' (the '/bin/ls /' command produces the listing of<br />files from 'directory_name' presuming that you have an 'ls' command<br />located within your new root). Chroot shell command changes the root<br />directory for a process, goes into this directory and then starts a<br />shell or a user-specified command.<br />Chroot command uses a chroot() system call. The command and the system<br />call have an important difference between them: unlike the shell<br />command, chroot() call does not change your working directory to the<br />one inside chrooted jail. The source of chroot.c (shell command, in<br />Linux part of sh-utils) shows the following sequence of system calls:<br />-----------------<br />chroot (argv[1]);<br />chdir (" /" );<br />-----------------<br />As will be seen further, it will allow for easy chroot jail breaking.<br />Chroot is often used as a security measure. If one has ever used an<br />anonymous ftp server, one has used chroot. Ftp server chroots itself<br />into a special directory upon the anonymous ftp login. DNS (Domain<br />Name System) daemon bind is often chrooted as well. People also<br />suggested chrooting telnet/ssh remote shell users into their<br />corresponding home directories, so they can only update their web<br />pages. Web servers can be run chrooted too. Smap secure email wrapper<br />from FWTK firewall tool kit runs chrooted to the mail spool directory.<br />When chroot is implemented, programs running inside cannot access any<br />system resources on the outside. Thus all system libraries,<br />configuration files and even devices files should be recreated within<br />the chroot jail.<br />What daemons can be chrooted? If a daemon has to access files that are<br />not easily collectible in one place, chrooting it will be hard. For<br />example, sendmail needs mail spool (/var/spool/mail), other files in<br />spool (such as mqueue), user's home directories (to check for .forward<br />files) and system configuration files in /etc. There is no place on<br />the filesystem where sendmail can be effectively confined. Of course,<br />some makeshift solution is possible, but it is not clear that it will<br />add to security, but rather cause it to lax. If sendmail functionality<br />is separated into spool daemon and mail transfer program (like done in<br />FWTK smap and smapd), than chrooting is entirely possible.<br />Chrooting shell users is possible if there is a business need to keep<br />them in some particular directory. Suggestions for doing this with<br />ssh2 are provided here: http://www.ssh.com/faq/index.cfm?id=687, and<br />for openssh here: http://hints.linuxfromscratch.org/hints/openssh.txt<br />and: http://archives.neohapsis.com/archives/sf/linux/2001-q4/0197.html<br />However, it might involve copying multiple system libraries, files and<br />other resources such as Linux Pluggable Authentication Modules (PAM)<br />architecture, used by most modern Linux distributions. <br />Anything else such as bind, apache, squid can be chrooted, but<br />sometimes the benefits are unclear, especially for daemons that run as<br />root. This URL (http://www.networkdweebs.com/chroot.html) provides a<br />nice list of daemons that its author chrooted successfully.<br />" What daemon should be chrooted?" is an entirely different question<br />from " What daemons can be chrooted?" Before we answer it, lets analyze<br />how attackers break out of chroot.<br />First, the more software is deployed within chroot environment, the<br />more dangerous it becomes, since it is hard to keep track of programs<br />that can be used by the attacker to elevate permission and escape.<br />Second, the number of ways that root user can break out of chroot is<br />huge. Starting from simple use of a chroot() call with no chdir() [see<br />code below] to esoteric methods as the creation of your own /dev/hda<br />or /dev/kmem devices, injection code into the running kernel<br />(http://www.big.net.au/~silvio/runtime-kernel-kmem-patching.txt),<br />using open directory handles outside chroot or chroot-breaking buffer<br />overflows. While system capabilities can be used to render inoperable<br />many of these methods, new ones will likely be found by smart<br />attackers.<br />---------------<br />Sample code to break out of chroot:<br />#include <stdlib.h><br />#include <stdio.h><br />#include <unistd.h><br />#include <sys/stat.h><br />#include <sys/types.h><br />int main(void)<br />{<br /> int i;<br /> mkdir(" breakout" , 0700);<br /> chroot(" breakout" );<br /> for (i = 0; i < 100; i++)<br /> chdir(" .." ) ;<br /> chroot(" ." );<br /> execl(" /bin/sh" , " /bin/sh" ,NULL);<br />}<br />compile statically (using " gcc -static" ) and run within chrooted<br />directory (after doing " chroot ." or similar from shell prompt) to<br />escape. <br />--------------<br />Third, if there is no root user defined within the chroot environment,<br />no SUID binaries, no devices, and the daemon itself dropped root<br />privileges right after calling chroot() call (like in the code below),<br />breaking out of chroot appears to be impossible. In other words, if<br />there is no way to gain root shell or perform actions that only root<br />can usually perform (e.g. create devices, or access raw memory)<br />breaking chroot is not clearly possible. Ideally, if the custom<br />software uses chroot for security the sequence of calls should be:<br />---------------<br />chdir(" /home/safedir" );<br />chroot(" /home/safedir" );<br />setuid(500);<br />---------------<br />Keep in mind, that after these lines are executed there will be no way<br />for the program to regain root privileges. <br />Fourth, in some cases attackers might not be able to break (i.e. run<br />processes outside of chrooted directory), but instead will be able to<br />somewhat affect such processes. For example, if bind is chrooted,<br />several devices should be created. One of them is /dev/log, necessary<br />for logging bind messages into the regular system logs. By crafting a<br />malicious log message and sending it into /dev/log from within the<br />chrooted directory attacker will influence the behavior of syslog<br />daemon running outside the chroot. If there is a buffer overflow in<br />syslog (which runs as root), additional privileges can be obtained.<br />What daemons can be chrooted but with no valuable security outcome? In<br />light of the above, chrooting programs that do not drop root<br />privileges while running, or programs that provide root shell access<br />(sshd, telnet with a root account within chrooted directory) does not<br />provide any extra security.<br />To conclude, chroot() is a good way to increase the security of the<br />software provided that secure programming guidelines are utilized and<br />chroot() system call limitations are taken into account. Chrooting<br />will prevent an attacker from reading files outside the chroot jail<br />and will prevent many local UNIX attacks (such as SUID abuse and /tmp<br />race conditions).<br />ABOUT THE AUTHOR:<br />This is an updated author bio, added to the paper at the time of reposting in 2009. <br />Dr. Anton Chuvakin (http://www.chuvakin.org) is a recognized security expert in the field of log management and PCI DSS compliance. He is an author of books " Security Warrior" and " PCI Compliance" and a contributor to " Know Your Enemy II" , " Information Security Management Handbook" and others. Anton has published dozens of papers on log management, correlation, data analysis, PCI DSS, security management (see list www.info-secure.org) . His blog http://www.securitywarrior.org is one of the most popular in the industry.<br />In addition, Anton teaches classes and presents at many security conferences across the world; he recently addressed audiences in United States, UK, Singapore, Spain, Russia and other countries. He works on emerging security standards and serves on the advisory boards of several security start-ups.<br />Currently, Anton is developing his security consulting practice, focusing on logging and PCI DSS compliance for security vendors and Fortune 500 organizations. Dr. Anton Chuvakin was formerly a Director of PCI Compliance Solutions at Qualys. Previously, Anton worked at LogLogic as a Chief Logging Evangelist, tasked with educating the world about the importance of logging for security, compliance and operations. Before LogLogic, Anton was employed by a security vendor in a strategic product management role. Anton earned his Ph.D. degree from Stony Brook University.<br />

×