APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

sandbox-exec (Mac sandbox wrapper)

Mac OS X Leopard includes a new command line tool called "sandbox-exec". What it does is provide a "sandbox" - a restricted environment. You can use this to provide more safety when testing unknown software (such as that whiz-bang thingy you just downloaded) or to provide additional security for our ordinary applications and daemons..

Let's try something very simple. First we need to create a sandbox profile. There are pre-made profiles in /usr/share/sandbox, but we'll make our own here. I'll call it "my.sb":

(version 1)
(allow default)
(deny network*)

Not much to it, is there? Yes, just three lines: I didn't leave anything out. Let's take it for a spin.

new-host-2:~ apl$ sandbox-exec -f ./my.sb /bin/bash
bash-3.2$ # we're in the sandbox running bash
bash-3.2$ date
Sat Dec 29 19:22:05 EST 2007
bash-3.2$ echo "I'm in a sandbox"
I'm in a sandbox
bash-3.2$ ping aplawrence.com
bash: /sbin/ping: Operation not permitted
bash-3.2$ ssh aplawrence.com
ssh: connect to host aplawrence.com port 22: Operation not permitted
bash-3.2$ exit
new-host-2:~ apl$ 

The "deny network*" in our profile stopped us from using the network. There are other restrictions we can enforce in our sandbox: we can prohibit file system writes, prohibit writes except to /tmp or other specific places, allow only certain libraries to be read , and much more. The best place to get an idea of what's possible is to look at the /usr/share/sandbox files: strangely, they aren't XML as most of Mac OS X is now, but they are easy enough to understand in spite of being undocumented.

If we had a suspicious binary, we could run it within a sandbox that locked it down from doing anything potentially harmful. We could also run processes that we fear might be subverted - a web server or ssh server, for example. Any sub-process started by a sandboxed process will be just as restricted, so this would let us very specifically allow only what our process needs to do its work. If it were hacked, the resulting process would be prevented from doing anything more. In combination with our normal security measures (permissions, acl's and so on), this could provide a very large degree of security.

A comment in one of the /usr/share/sandbox files might indicate that a different name for this was contemplated:

 Use "debug deny" to log only operations that are denied by seatbelt

Seatbelt? OK, a seatbelt restrains you and keeps you from hurting yourself..

There is, of course, a system call for the same purpose - sandbox_init() - and a man page. There's also a "sandbox" man page that is supposedly an overview of this facilty, but neither of them are very helpful. A little experimentation would probably tease out their secrets.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> sandbox-exec (Mac sandbox wrapper)


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Sun Dec 30 16:39:11 2007: 3357   TonyLawrence

Similar ideas for Linux: (link) seems very similar.

Plash ( (link) ) restricts file access only ; I'm sure there are more..

Mon Mar 2 23:53:32 2009: 5581   anonymous

great article, thanks for sharing this!

Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

C++ is a badly designed and ugly language. It would be a shame to use it in Emacs. (Richard Stallman)

This post tagged: