beetlebug

Apps with plugins in a sandbox

Build an app that runs the plugins of other people with no trust in their code. Each plugin runs in its own child process with a compute limit, a memory limit, and only the capabilities that the app gives it.

This page shows a prototype: a text tool with six demo plugins. Two plugins do useful work, and four plugins try to do harm.

The Plugin Host window: six plugins, each with its status, and a table of events with the compute units of each run
The Plugin Host after a run of each plugin.

What the plugins do

Plugin What it tries What happens
title-case, word-stats Change and count the words of the text. The result shows in the window.
spin Loops with no end. The compute limit stops it at 20,000,000 compute units.
hog Allocates with no end. The memory limit of the thread, 256 MiB, stops it.
snoop Reads notes.txt, then /etc/passwd, a file outside its folder, and a symbolic link to that file. It reads notes.txt. Each other open gives an error.
phone-home Opens a socket. The app gives it no socket capability, so the command does not exist.

How it works

Each run of a plugin is a new thread at the isolation level process. On Linux the child process runs in new namespaces, with Landlock and seccomp. The table at the top of the app gives the capabilities and the compute limit of each plugin. A thread never gets thread, so a plugin cannot send a script back to the window.

The window loads the plugin, then calls run with -async. The event loop of the window runs while the plugin works (the code):

set t [thread::create -joinable -isolation process -caps [dict get $spec caps] \
    -compute [dict get $spec compute] -meter run]
thread::send $t [readfile [file join $::here plugins $name.tcl]]
...
thread::send -async $t [list run [.pw.left.text get 1.0 end-1c]] ::result

The result goes into ::result. A compute limit or a memory limit ends the script of the thread with an error, and thread::errorproc gives the error to the window. The meter of ztcl::meter counts the compute units of each run.

Run it from the folder of the example. The folder is the root folder, so a plugin with file reads only that folder:

cd examples/usecases/plugin-host
ztk --allow thread,meter,event --root . app.tcl

Real today

The isolation level, the limits, the capabilities, and the meter are features of ztk and ztcl. The app is a prototype: the plugins are short demo scripts, and all plugins share one folder.

The ztcl runner also has the level vm, a Linux guest on KVM for each thread. Run untrusted code on the ztcl site shows it.