Where the code may connect and write
Egress control decides where a sandboxed program may connect: nowhere at all, or only to an allow-list of destinations. Filesystem control decides what it may read and write: a read-only root, a scratch folder that vanishes, and secrets hidden.
That matters because a stolen key only hurts once it leaves the machine, and a program that cannot write cannot plant anything for later. These two fences close those routes.
By the end of this lesson
Note
I can limit where sandboxed code may connect and which files it may write.
Key words
| Word | What it means | Example |
|---|---|---|
| Egress | traffic leaving a machine or a sandbox | |
| Allow-list of destinations | the only addresses and ports a program may reach | 10.77.0.1 8081 |
| veth pair | two virtual network cards joined like a cable | |
| nftables | the Linux firewall that matches and refuses packets | |
| Bind mount | showing one folder at another place | |
| bubblewrap | a tool that builds a sandbox from namespaces and mounts | bwrap |
The cells students fill in are left blank in this preview.
No network is the simplest egress rule
Most untrusted code needs no network at all: a test run, a data transformation, a student's exercise. A new network namespace gives exactly that, and lesson 2 showed it has no route anywhere.
When code does need to fetch something, such as packages, an allow-list names the exact destinations and refuses the rest. The platform these labs run on works this way: a VM reaches a short list of names.
Go to the lab folder
cd ~/labShown here, not run. Press Start lab to type it into your own lab’s terminal.
Call out with no network
sudo unshare --net python3 agent/agent.py callShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the allow-list
cat egress.allowShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try three destinations through it
sudo sbx egress egress.allowShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: match destinations exactly
# An egress allow-list: exact host and port pairs, everything else refused.
allow = [["pypi.org", 443], ["files.pythonhosted.org", 443]]
def may_connect(host, port):
for pair in allow:
if pair[0] == host and pair[1] == port:
return True
return False
for pair in [["pypi.org", 443], ["pypi.org.evil.test", 443], ["pypi.org", 22]]:
print(pair[0], pair[1], "->", may_connect(pair[0], pair[1]))
# Why must "pypi.org.evil.test" be refused even though it starts with pypi.org?
A common mistake
Warning
Many people think an allow-list can be a pattern, such as anything ending in a trusted name. A pattern is easy to fool: an attacker registers a name that matches. List exact names and ports, and refuse an address typed as numbers unless it is listed.
A read-only root with a scratch folder
bubblewrap, run as bwrap, builds a sandbox from namespaces and mounts in one command. --ro-bind / / shows the whole disk read-only. --tmpfs /tmp puts an empty scratch folder in memory on top of /tmp, gone when the sandbox ends. --bind makes one folder writable.
A --tmpfs over a folder hides what is in it, which is how the key disappears. Later options sit on top of earlier ones.
Try to write to a read-only root
sudo bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /tmp touch /etc/sandbox-testShown here, not run. Press Start lab to type it into your own lab’s terminal.
Hide the secrets folder from the agent
sudo bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /home/student/lab/secrets python3 agent/agent.py secretShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write to the scratch folder
sudo bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /tmp sh -c 'echo scratch > /tmp/note && cat /tmp/note'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what each option did
| Option | What the program sees | The Run printed |
|---|---|---|
| --ro-bind / / | The whole disk, read-only | Read-only file system for /etc |
| --tmpfs /home/student/lab/secrets | An empty folder over the key | No such file or directory |
| --tmpfs /tmp | An empty folder it may write | scratch |
| --bind A A | Folder A, writable | Used in the lab's own task |
The cells students fill in are left blank in this preview.