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":
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 sanbox running bash
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
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.
*Originally published at APLawrence.com
View All Articles by A.P. Lawrence
Our Daily Email of Breaking eBusiness News
About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com
WebProNews RSS Feed
More Expert Articles Articles