Recently my free time has mostly been dedicated to learning more about parallel computing. But I did take a little detour into the world of old-timey chroot(8) jails on OS X. They work, even on 10.8.4, even running a shell. The big trick on OS X is that you need to include the dynamic link editor /usr/lib/dyld in the jail.

Of course you also need to include the programs you want to run in the jail and their dependencies. The otool -L operation on OS X and the ldd command on linux are key to finding these dependencies. There are a lot, and you’ll need to recursively resolve them. Additionally on OS X, when you are recursively finding the dependencies, you’ll need to account for Mach-O’s @executable_path, @loader_path, and @rpath variables. This can get a little tricky. On Linux, the libraries are easier but things like PAM can be tricky. This is why you want software like jailkit, which can help automate the process of creating and running jails.

While jails aren’t so good for security applications, they can be very helpful in trying to reign-in a proprietary installer.

For my purposes, jailkit wasn’t getting the library dependencies on OS X. So I made a little python module and a script to get that part of the job done.

$ ./ -h
usage: [-h] [-l] jail_name jail_programs [jail_programs ...]
Create a "jail" directory appropriate for chroot, with copies of the specified
programs and their dependencies
positional arguments:
  jail_name      the name of the jail to create
  jail_programs  full path programs to import into the jail
optional arguments:
  -h, --help     show this help message and exit
  -l, --link     use hard links instead of copies. !!!WARNING!!!: Changes made
                 to hard-linked files in a jail will also affect the
                 "original" (and probably important) files outside the jail.
                 Additionally, hard links cannot span filesystems. DO NOT USE

An example of creating and entering a jail with everything in /bin and a couple commands from /usr/bin looks like this:

sh-3.2$ sudo ./ alcatraz /bin/* /usr/bin/find /usr/bin/whoami
sh-3.2$ sudo chroot -u nobody -g nobody alcatraz
bash-3.2$ pwd
bash-3.2$ whoami
bash-3.2$ find /
[... and everything else from /bin ...]
bash-3.2$ exit

Then you can use jailkit to put all the config files and non-library dependencies into the jail. Something like this:

jk_init -v -j alcatraz basicshell editors extendedshell netutils ssh sftp scp

Hopefully I’ll find some time to port these changes to jailkit itself.

You can find the code for mkjail on github.