Safety
A program that shows files from anywhere (a picture a document names, a project's settings, data a program sends to a terminal) takes apart data that anyone can have made. Xtk has two parts for doing that safely: a jail, enforced by the Linux kernel, and one jailed worker process for each program. This page explains both, and how a program starts the worker at the top of main().
Why a jail
Code that reads a file format is large, and like all such code it can have bugs. A file made on purpose to trigger one can make the program run code it never meant to run, with all the rights of the user: read private files, change them, or send them away. Xtk's image decoders, its Markdown and XML readers and a program's own parsers are such code.
So a program can have the dangerous part done in a separate process, locked in a jail: it can read nothing (or only what its rules allow), write nothing, reach no network, start nothing and signal nothing. If a hostile file took that process over, it would find an empty room. The program checks every answer that comes back before it uses it, because a process that a file took over could send back anything.
The red border is the jail. The two processes talk over one socket that the program made before the worker started.
The jailed worker
xtk_worker.h gives each program one worker process. The first request starts it, and it is kept for the rest of the program's life, so that many requests a second cost no new process each. It does two kinds of work:
- Pictures.
xtk_worker_decode_image()turns the bytes of a PNG, JPEG, GIF, BMP or XPM file into RGBA pixels, andxtk_worker_decode_image_file()does the same for a file, which the program reads first (only a regular file, without blocking). When both a largest width and height are given, a larger picture is scaled down to fit with its shape kept; a picture with a side over 16384 pixels, or with more than 64 megapixels, is refused. - Jobs of the program's own. A job is a function of the program,
XtkWorkerJob: bytes in, bytes out.xtk_worker_add_job()gives it a number, andxtk_worker_run()runs it in the worker with the given bytes and returns its answer. A job can parse the bytes of a JSON file, decode the pictures a program sends to a terminal, or take apart anything else; like the worker, it can open no file. Up to 16 jobs.
For a rich text view, xtk_worker_richtext_decoder() is a ready-made image decoder: xtk_richtext_set_image_decoder(view, xtk_worker_richtext_decoder, NULL) has every picture a document names decoded in the jail.
That is all the worker does, and only when the program asks for it. Nothing in Xtk sends work to the worker by itself: the functions of xtk_image.h, the Markdown, XML, JSON and CSV readers, and a rich text view that was given no decoder all run in the program's own process. To have one of them work in the jail, the program calls it from a job of its own, or, for the pictures of a rich text view, sets the decoder above.
Starting the worker at the top of main()
The worker is the program itself, started again: the program runs its own file (/proc/self/exe) with argv[0] set to XTK_WORKER_NAME ("xtk-worker"). The new process has none of the program's memory, no connection to the X server and no open files, only its socket and /dev/null as its standard input and output. So the first lines of main() must recognize it and hand it over, before the program opens the display or does anything else:
static int count_lines(const unsigned char *input, size_t length,
unsigned char **output, size_t *output_length) {
size_t lines = 0;
for (size_t i = 0; i < length; i++) lines += input[i] == '\n';
*output = malloc(sizeof(lines));
if (!*output) return -1;
memcpy(*output, &lines, sizeof(lines));
*output_length = sizeof(lines);
return 0;
}
static int count_lines_job = -1;
int main(int argc, char **argv) {
count_lines_job = xtk_worker_add_job(count_lines); /* 1. the jobs, in both processes */
if (xtk_worker_is_worker(argc, argv)) return xtk_worker_main(); /* 2. the worker stops here */
/* 3. the program: setlocale(), XOpenDisplay(), the widgets ... */
}
Add every job before xtk_worker_is_worker(), always in the same order. Both processes run these lines, so both give each job the same number. Adding the same function again gives the number it already has.
Then, where the program would have taken data apart itself, it asks the worker:
unsigned char *rgba;
int width, height;
if (xtk_worker_decode_image_file(path, 640, 480, &rgba, &width, &height)) {
/* width * height * 4 bytes: red, green, blue, alpha, row by row */
free(rgba);
}
unsigned char *answer;
size_t answer_length;
if (xtk_worker_run(count_lines_job, text, text_length, &answer, &answer_length, sizeof(size_t)) == 0
&& answer_length == sizeof(size_t)) {
size_t lines;
memcpy(&lines, answer, sizeof(lines));
free(answer);
}
| Call | What it does |
|---|---|
xtk_worker_add_job() |
Adds a job and returns its number (from XTK_WORKER_FIRST_JOB, 16), or -1 when there are 16 already. |
xtk_worker_is_worker() |
1 when this process is the worker. |
xtk_worker_main() |
The worker's own main: sets its limits, enters the jail with no file it may read, and answers requests until the program closes the connection. Returns the exit status. |
xtk_worker_run() |
Runs a job: the bytes in (at most XTK_WORKER_DATA_MAX, 256 MB), the answer out (allocated, followed by a zero byte, for the program to free), and the most bytes the answer may have. 0 on success, -1 on failure. |
xtk_worker_decode_image(), xtk_worker_decode_image_file() |
Decode a picture; 1 on success, 0 on failure. |
xtk_worker_richtext_decoder() |
The same as an image decoder for xtk_richtext. |
What the program can count on
- It never waits long. A request waits at most
XTK_WORKER_DEADLINE_MS(5 seconds) from sending to the end of the answer, and every wait on the socket has that limit, so a worker that stops answering cannot freeze the program. Requests block the calling thread, one at a time: make them from one thread. - Every answer is checked. An answer longer than the caller's limit is refused before any memory is set aside for it. A picture's size must be one the request allows, and the pixels must all be there.
- A bad worker is replaced. A worker that crashes, misses the deadline or answers something that does not add up is killed, and the next request starts a new one. A job that only says it cannot do its input (returns -1) leaves the worker running.
- The worker dies with the program. The kernel ends it when the program ends.
- No jail, no worker. On a system where the jail cannot be made, every request fails. There is no weaker way, on purpose: the program shows no picture rather than decode one outside the jail.
The jail itself
xtk_sandbox.h is the jail that the worker uses, and a program can use it for processes of its own (a parser of its own, or a compiler it runs). It is made of two features of the Linux kernel, Landlock and seccomp, which need no administrator rights and nothing to install. It needs Linux 5.13 or newer with Landlock enabled, on an x86-64 or ARM64 processor; xtk_sandbox_available() says whether this system can make it.
| Area | In the jail |
|---|---|
| Files | Only the paths in the rules can be read (XTK_SANDBOX_READ: a file, or everything under a folder), or read and run (XTK_SANDBOX_RUN). Nothing can be written but a file given as XTK_SANDBOX_DISCARD, which is meant for /dev/null. Nothing can be created, renamed or removed, and a file that may only be read cannot be emptied, on any kernel. |
| Network | No new socket of any kind: no internet, no local network, and no local sockets, so also no X server and no D-Bus. A socket the process already holds keeps working. |
| Other processes | No debugging of other processes and no reading of their memory. From Linux 6.12, no signals to processes outside the jail. |
| Kernel features | No unshare() or setns(), no mounting, no kernel keyring, no io_uring, no bpf, no loading of kernel modules, no rebooting, and no new namespace through the flags of clone(). clone3(), whose flags cannot be checked, fails as on a kernel without it, so the C library uses clone(); threads and new processes work as before. System calls of another processor mode end the process. |
| Privileges | Can never gain rights: a setuid program it starts gains nothing. |
| For parsers | xtk_sandbox_enter_for_parsing() also takes away starting programs, new processes and threads, and sending any signal, on every kernel. The worker uses this jail. |
The jail is all or nothing: if any part cannot be set up, entering fails, and the caller must then not go on. It can only be made narrower, never wider, and it holds for the rest of the process and everything it starts. It holds for what is opened after it, so close every descriptor the jailed process 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);
if (!xtk_sandbox_allow_project(&rules, project_folder)) { /* refused: it holds the home folder */ }
pid_t child = fork();
if (child == 0) {
if (xtk_sandbox_enter_for_parsing(&rules)) _exit(127); /* no jail: read nothing */
/* ... read, parse, answer over a descriptor already open ... */
_exit(0);
}
| Call | What it does |
|---|---|
xtk_sandbox_allow() |
Adds a path and what may be done with it (up to 512 paths). A path that does not exist when the jail is entered is skipped. |
xtk_sandbox_allow_project() |
Adds a folder that untrusted data named, for reading, unless it is /, the home folder or a folder that holds it, so a project cannot name its way to all of the user's files. It returns 0 when the folder was not let in, also when the 512 paths are used up. |
xtk_sandbox_enter() |
Enters the jail. The process can still run the programs its rules let it run, and send signals. |
xtk_sandbox_enter_for_parsing() |
Enters the jail for code that only reads and answers. |
xtk_sandbox_refuse_group_signals() |
For a jailed process that must still start a program: from then on it cannot signal every process or a whole process group. |
Make the rules in the program, before fork(), and enter the jail in the child: the enter calls make only system calls, as the child of a program with threads must.
Known limitations
- The jail exists only on Linux 5.13 and newer with Landlock enabled, on x86-64 and ARM64. Elsewhere every enter call fails, and the worker never starts.
- Before Linux 6.12, a process jailed with
xtk_sandbox_enter()(not the parsing variant) can still send a signal to one other process of the same user by its number. - The worker belongs to the thread that started it: when that thread ends, the kernel ends the worker, and the next request starts another.
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
