Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Network egress and read-only filesystems

After this lesson: I can limit where sandboxed code may connect and which files it may write.

60 min lesson7 commandsSandboxing lab: run untrusted code inside namespaces, cgroups, seccomp and gVisor on one private Ubuntu VM

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

WordWhat it meansExample
Egresstraffic leaving a machine or a sandbox
Allow-list of destinationsthe only addresses and ports a program may reach10.77.0.1 8081
veth pairtwo virtual network cards joined like a cable
nftablesthe Linux firewall that matches and refuses packets
Bind mountshowing one folder at another place
bubblewrapa tool that builds a sandbox from namespaces and mountsbwrap

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

Command
cd ~/lab

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Call out with no network

Command
sudo unshare --net python3 agent/agent.py call

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the allow-list

Command
cat egress.allow

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try three destinations through it

Command
sudo sbx egress egress.allow

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: match destinations exactly

Code: python
# 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

Command
sudo bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /tmp touch /etc/sandbox-test

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Hide the secrets folder from the agent

Command
sudo bwrap --ro-bind / / --dev /dev --proc /proc --tmpfs /home/student/lab/secrets python3 agent/agent.py secret

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write to the scratch folder

Command
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

OptionWhat the program seesThe Run printed
--ro-bind / /The whole disk, read-onlyRead-only file system for /etc
--tmpfs /home/student/lab/secretsAn empty folder over the keyNo such file or directory
--tmpfs /tmpAn empty folder it may writescratch
--bind A AFolder A, writableUsed in the lab's own task

The cells students fill in are left blank in this preview.

About this lab and its source: Sandboxing lab: run untrusted code inside namespaces, cgroups, seccomp and gVisor on one private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.