Sandboxing lab: run untrusted code inside namespaces, cgroups, seccomp and gVisor on one private Ubuntu VM
This lab teaches how to run code you did not write, such as a script an AI agent produced or a program a student or customer uploaded, on one private Ubuntu VM. A harmless stand-in, agent.py, reads a key, calls out and starts processes, and the student fences it in with the Linux kernel’s own tools, one at a time. One script the student writes, with every fence at once, is marked by a check that uses values the student never sees.
Nothing is installed on the student’s computer. Every tool is the real one, installed from Ubuntu’s archive: unshare and setpriv, cgroups v2, libseccomp, bubblewrap, nftables and gVisor’s runsc. It is defensive throughout: the only program contained is the lab’s own, on the student’s own copy.
What students learn
What untrusted code can do, and how each kernel fence stops part of it. PID, network, mount, UTS and user namespaces hide processes, the network, files and root’s power. A cgroup sets memory, CPU and process limits, and the student sees the kernel kill a program at 64 MB and refuse the twentieth process. A seccomp allow-list refuses every system call it does not name, capabilities are dropped with setpriv, an nftables allow-list decides where code may connect, and bubblewrap builds a read-only view with one folder hidden. gVisor then answers the program’s system calls in a kernel of its own, and Firecracker microVMs are explained.
The six parts
First the agent with nothing in its way. Second namespaces made by hand with unshare. Third cgroups, through sbx limit, a short helper of ours. Fourth seccomp, through sbx seccomp, and capabilities. Fifth the network, through sbx egress and an allow-list in a file, and the files, with bubblewrap, and gVisor with runsc. Sixth run-agent.sh, the one script the student writes, which must stop the secret, the network, writes outside one folder, memory over 64 MB, more than 20 processes and any system call not on their allow-list. README.md in the home folder says what to do and gives none of the answers.
Run it in your browser
VM labs are set as a lab task in a module, and the sample is on the lab’s page. The machine is small: one CPU, 1 GB of memory and 10 GB of disk. Everything comes from Ubuntu’s archive, so a visitor’s VM, which reaches that archive alone, can install it.
- Add the lab. In a module you teach, add a task and choose the Ubuntu VM lab card. The page shows a short description of the machine instead of a compose file.
- Start from the sample. The page offers ready-made labs to start from. Choose Sandboxing. Using a sample replaces the text, and Undo brings yours back.
- Publish and start your own copy. Publish, then Start. The lab’s own setup script installs bubblewrap, libseccomp, nftables and gVisor from Ubuntu’s archive and writes the sbx helper, the stand-in agent, the files to work on and the README, the first time the VM starts, in a few minutes.
- Open the terminal. Open its terminal and read README.md in the home folder. Nothing opens in the browser: the lab is the terminal.
A basic lesson on this lab
Sandboxing Basics is a small lesson worked in this lab, which is an Ubuntu VM. It has a hook, an I can outcome, key words, a starter, a check after each idea, a common mistake and an exit check, and each step has a Run button that types its command into the machine’s terminal. A student watches the agent read a key and call out, then puts one fence round it at a time: no network, a memory limit, a seccomp allow-list and a hidden folder.
The lesson looks around and leaves the lab’s own task, the script with every fence, to the student, so it hands out no answers. The undergraduate module Sandboxing untrusted and AI-written code is worked in the same lab.
How it is marked
The lab has a check. It runs on a copy of the saved machine, as root, and puts a program of its own in place of agent.py. It runs the student’s run-agent.sh once for each test: the program must print a random word under a seccomp filter, never print a fresh secret written into the secrets folder, never reach a listener on a random port, write a file with a random name in the one folder allowed and nowhere else, be stopped before holding 200 MB or starting 20 processes, and have one dangerous system call, picked at random from five, refused. The first line of what the check printed says why.
- Hand it out. Assign the lab like any task, with a due date if you want one. Each student gets a VM of their own, kept between sessions.
- Run the check. On a student’s row in the class list, press Run the check. It starts a copy of their saved machine that is not saved back, runs the lab’s check script as root and shows whether it passed, what it printed and when. The student is not told the result.
- Mark it. Read the first line of what the check printed: it says what is not right, such as an agent that read the secret, reached the network or held 200 MB, or a system call that was not refused. Send the work back with a reason if it needs more.
The definition on GitHub
The file is the whole lab definition, the same text the platform runs: size, setup script, the sbx helper, the agent, the files to work on, the README and the check. Copy it and change the limits or the agent to make a longer lab.
Try this lab, and what it costs
A free personal account can work through the basic lessons in this lab, with enough free credit each month to follow most lessons and no card. For a class, a school or college has its first 365 days free for up to 50 students, then pays per student a year, with every lab included and staff free.
Questions people ask
- Does the lab need a licence?
- No. bubblewrap, libseccomp, nftables, util-linux and gVisor are open source, under the LGPL, GPL and Apache-2.0 licences, and are installed from Ubuntu’s archive when the student’s VM starts, so we distribute none of them.
- Why is Firecracker not run?
- Firecracker needs /dev/kvm inside the machine it runs on, and the platform’s VMs offer no nested virtualisation. The lessons explain microVMs and run gVisor, which needs no KVM.
- Can students harm anything?
- Only their own copy. The program they contain is the lab’s own stand-in, its key is a demo value, and the network it reaches is inside the VM. If a student breaks a given file, sbx reset puts it back as it was given.
- Was the lab tested in a real student VM?
- The setup, every lesson’s commands and the check, with each way of faking it, were run in a privileged Ubuntu 26.04 container standing in for the VM, on an arm64 machine. A booted student VM, with its AppArmor rules and the amd64 packages, is worth one try before a class.