xtk_sandbox.h
A jail, enforced by the Linux kernel, for a process that reads untrusted files.
A program that takes apart files from anyone (a parser, an image decoder, a compiler) can have a bug that a file made on purpose turns into running code. This module locks such a process into a jail, so that code a file managed to run there finds nothing worth stealing and no way to send anything out or change anything. The jail is made of two features of the Linux kernel, Landlock and seccomp. They need no administrator rights, no helper program and no library.
What the jail allows. Only the paths in its rules can be read (and, for XTK_SANDBOX_RUN, run). Nothing can be written except a file given as XTK_SANDBOX_DISCARD (meant for /dev/null), and nothing can be created, renamed or removed.
What it refuses, whatever the rules say:
- every new socket (TCP, UDP and local ones, so also the X server and D-Bus); a socket the process already holds keeps working;
- ptrace and reading or writing another process's memory;
- unshare() and setns(), a new namespace through the flags of clone(), mounting, chroot, the kernel keyring, io_uring, bpf, perf events, userfaultfd, loading kernel modules, swap and rebooting; clone3(), whose flags cannot be checked, fails as if the kernel lacked it, so the C library uses clone() (threads and fork() work as before);
- emptying a file that may only be read (
truncate(), or an open for reading withO_TRUNC), on every kernel, though Landlock itself refuses it only from Linux 6.2;openat2()is refused altogether; - gaining rights: no_new_privs is set, so a setuid program gains nothing;
- system calls of another processor mode (32-bit calls on x86-64, and x32 calls): the process is killed;
- from Linux 6.12 (Landlock version 6), signals to processes outside the jail and connections to abstract local sockets.
xtk_sandbox_enter_for_parsing() also takes away starting programs and processes (fork, clone, so also threads, and exec) and sending any signal, on every kernel. That is the jail for code that only reads, computes and answers. xtk_sandbox_refuse_group_signals() is for a jailed process that must still start a program, such as a compiler.
All or nothing. If any part of the jail cannot be set up, entering fails and the caller must not go on to read the untrusted data. There is no weaker fallback. What a kernel can enforce is asked of the kernel when entering, so a newer kernel gives a tighter jail with the same program.
How to use it. Make the rules before fork(), in the program, then enter the jail in the child. The enter calls make only system calls, as a child of a program with threads must. The jail holds for the rest of the process and for everything it starts, and it can never be made wider. It holds only for what is opened after it: a descriptor that is already open stays usable, so close (or mark close-on-exec) every descriptor the jail must not keep.
static XtkSandboxRules rules; // about 2 MB: not on the stack
rules.count = 0;
xtk_sandbox_allow(&rules, "/usr", XTK_SANDBOX_READ);
xtk_sandbox_allow(&rules, "/dev/null", XTK_SANDBOX_DISCARD);
xtk_sandbox_allow_project(&rules, project_folder);
pid_t child = fork();
if (child == 0) {
if (xtk_sandbox_enter_for_parsing(&rules)) _exit(127); // no jail: do nothing
// ... read and parse, answer over a descriptor already open ...
_exit(0);
}
*
The Safety page of the guide shows how a program uses this together with xtk_worker.h.
Known limitations. The jail exists only on Linux 5.13 or newer with Landlock enabled, on x86-64 and ARM64. Elsewhere xtk_sandbox_available() returns 0 and every enter call fails. Before Linux 6.12, a jailed process that may still send signals (xtk_sandbox_enter() without the parsing variant) can send one to a single other process of the same user by its number.
#include "xtk_sandbox.h"
Types: XtkSandboxAccess, XtkSandboxRules
Constants: XTK_SANDBOX_MAX_RULES
Functions: xtk_sandbox_available, xtk_sandbox_allow, xtk_sandbox_allow_project, xtk_sandbox_enter, xtk_sandbox_enter_for_parsing, xtk_sandbox_refuse_group_signals
Types
XtkSandboxAccess
What a rule lets the jailed process do with a path.
| Name | Value | Description |
|---|---|---|
XTK_SANDBOX_READ |
Read this file, or any file and folder under this folder. | |
XTK_SANDBOX_RUN |
The same as XTK_SANDBOX_READ, and also run the files as programs. |
|
XTK_SANDBOX_DISCARD |
Read and write this one file, and empty it: meant for /dev/null. |
XtkSandboxRules
The paths a jail lets in.
Start with count set to 0 and add paths with xtk_sandbox_allow() or xtk_sandbox_allow_project(). The structure is large (512 paths of PATH_MAX bytes, about 2 MB), so make it static or allocate it, rather than put it on the stack.
| Field | Type | Description |
|---|---|---|
count |
int |
How many entries of rules are in use. |
path[PATH_MAX] |
char |
The path, as given. |
access |
XtkSandboxAccess |
What the jailed process may do with it. |
rules[XTK_SANDBOX_MAX_RULES] |
struct XtkSandboxRules |
One path and what may be done with it. The rules, count of them. |
Constants
XTK_SANDBOX_MAX_RULES
#define XTK_SANDBOX_MAX_RULES 512
The most rules one XtkSandboxRules can hold; xtk_sandbox_allow() drops any beyond it.
Functions
xtk_sandbox_available
int xtk_sandbox_available(void);
Tells whether this kernel and processor can make the jail at all.
It asks the kernel for Landlock and seccomp. A program can use it to say early that a feature which needs the jail is off.
Returns: 1 when xtk_sandbox_enter() can work, 0 when it cannot (another system, another processor, an older kernel, or Landlock disabled).
xtk_sandbox_allow
void xtk_sandbox_allow(XtkSandboxRules *rules, const char *path, XtkSandboxAccess access);
Adds a path that the jailed process may reach.
Nothing is checked or resolved now. A path that does not exist (or cannot be reached) when the jail is entered is skipped then. A path of PATH_MAX bytes or more, and a rule beyond XTK_SANDBOX_MAX_RULES, are dropped without a word.
| Parameter | Description |
|---|---|
rules |
The rules to add to. |
path |
The file or folder. |
access |
What may be done with it. |
xtk_sandbox_allow_project
int xtk_sandbox_allow_project(XtkSandboxRules *rules, const char *path);
Adds a folder that an untrusted file or project named, unless it would let in the user's home.
The path is resolved first (links followed, .. removed). The root folder /, the home folder (from HOME), and any folder that contains the home folder are refused, so a project cannot name its way to all of the user's files. A folder inside the home folder is allowed, for reading only. When HOME is not set, only / is refused.
| Parameter | Description |
|---|---|
rules |
The rules to add to. |
path |
The path. |
Returns: 1 when the resolved path was added; 0 when it was refused, does not exist, the rules are full (XTK_SANDBOX_MAX_RULES), the resolved path is too long, or rules or path is NULL.
xtk_sandbox_enter
int xtk_sandbox_enter(const XtkSandboxRules *rules);
Enters the jail, for good: this process and everything it starts.
Call it in the child after fork(), just before exec() or before reading the untrusted data. It only makes system calls. The process keeps the right to start programs it may run (XTK_SANDBOX_RUN) and to send signals; use xtk_sandbox_enter_for_parsing() when it needs neither.
| Parameter | Description |
|---|---|
rules |
The paths the process may reach. |
Returns: 0 when the process is in the jail; -1 when it is not (some part could not be set up, or rules is NULL), and then the untrusted program must not be run.
xtk_sandbox_enter_for_parsing
int xtk_sandbox_enter_for_parsing(const XtkSandboxRules *rules);
Enters the jail of xtk_sandbox_enter() and also gives up starting anything and signalling anyone.
On top of xtk_sandbox_enter(), it refuses fork, vfork, clone and clone3 (so also new threads), execve and execveat, and every way to send a signal (kill, tkill, tgkill, the sigqueue calls and the pidfd calls), on every kernel. It is the jail for a process that only reads, computes and answers over a descriptor it already holds, such as the worker of xtk_worker.h.
| Parameter | Description |
|---|---|
rules |
The paths the process may reach; with count 0 it may read no file at all. |
Returns: 0 when the process is in the jail; -1 when it is not, and then nothing untrusted may be read.
xtk_sandbox_refuse_group_signals
int xtk_sandbox_refuse_group_signals(void);
Refuses, from now on, a signal to every process or to a process group.
After this call, kill() with a process number of 0 or less (a process group, or every process with -1) fails. A signal to one process by its number still works. Call it after xtk_sandbox_enter() (which sets no_new_privs, which the kernel needs for it), in a process that must keep starting a program, such as a compiler: a supervisor in the same jail can keep kill(0) to end the whole group, and its child calls this before it runs the compiler. Before Linux 6.12 (Landlock version 6), a signal to one process outside the jail cannot be stopped.
Returns: 0 when it holds; -1 when it does not, and then the program must not be run.
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats