21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

access to the filesystem, the potential for damage (whether it is reading sensitive data<br />

or destroying data) is localized to the restricted directory hierarchy imposed by altering<br />

the process’s root directory.<br />

Unfortunately, one often overlooked caveat applies to using chroot( ). The first time<br />

that chroot( ) is called, it does not necessarily alter the process’s current directory,<br />

which means that until the current directory is forcibly changed, it may still be possible<br />

to access areas of the filesystem outside the new root directory structure. It is<br />

therefore imperative that the process calling chroot( ) immediately change its current<br />

directory to a directory within the new root directory structure. This is easily<br />

accomplished as follows:<br />

#include <br />

chroot("/new/root/directory");<br />

chdir("/");<br />

One final point regarding the use of chroot( ) is that the system call requires the calling<br />

process to have superuser privileges.<br />

2.13 Restricting Filesystem and Network Access<br />

on FreeBSD<br />

<strong>Problem</strong><br />

Your program runs primarily (if not exclusively) on FreeBSD, and you want to<br />

impose restrictions on your program’s filesystem and network capabilities that are<br />

above and beyond what chroot( ) can do. (See Recipe 2.12.)<br />

Solution<br />

FreeBSD implements a system call known as jail( ), which will “imprison” a process<br />

and its descendants. It does all that chroot( ) does and more.<br />

Discussion<br />

Ordinarily, a jail is constructed on FreeBSD by the system administrator using the<br />

jail program, which is essentially a wrapper around the jail( ) system call. (Discounting<br />

comments and blank lines, the code is a mere 35 lines.) However, it is possible<br />

to use the jail( ) system call in your own programs.<br />

The FreeBSD jail does everything that chroot( ) does, and then some. It restricts<br />

much of the superuser’s normal abilities, and it restricts the IP address that programs<br />

running inside the jail may use.<br />

Restricting Filesystem and Network Access on FreeBSD | 69<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!