1 xtk_worker
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_worker.h

One jailed worker process for each program, which takes apart untrusted data for it.

A picture that a document names, a picture that a program sends to a terminal, a project's settings file: any of them can come from anyone, and taking such data apart is where a bug lets a hostile file in. A program built on xtk can have that work done by a worker: a second process of its own, locked in the jail of xtk_sandbox.h, that can read no file at all, write nothing, reach no network, start nothing and signal nothing. It can only answer over the socket that connects it to the program. The program sends it bytes (it reads a file itself first), and checks every answer before it uses it.

What it does.

Nothing else in xtk goes through the worker by itself: xtk_image.h, the Markdown, XML, JSON and CSV readers, and a rich text view without a decoder all work in the program's own process.

One worker, started again when needed. The first request starts the worker, and it is kept for the rest of the program's life, so that many requests a second cost no new process each. A worker that crashes, takes longer than XTK_WORKER_DEADLINE_MS for a request, or answers anything that does not add up is killed, and the next request starts a new one. A worker also dies with the program.

How a program starts it. The worker is the program itself, started again: /proc/self/exe run with argv[0] set to XTK_WORKER_NAME, so it gets a new address space with none of the program's memory, no X connection and no open files (only its socket, and /dev/null as its standard input and output). So the top of main() must hand such a process over to xtk_worker_main() before it does anything else. Every job of the program's own is added first, in both processes and in the same order, so that both give each job the same number:

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);   // in both processes
    if (xtk_worker_is_worker(argc, argv)) return xtk_worker_main();

    // ... the program: open the display, make the window ...
    unsigned char *rgba;
    int width, height;
    if (xtk_worker_decode_image_file("picture.png", 640, 480, &rgba, &width, &height)) {
        // ... use the pixels, then 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)) {
        // ... check the answer, use it, then free(answer) ...
    }
}
 * 

Requests. A request blocks the calling thread until the answer is there or the deadline has passed, never longer than XTK_WORKER_DEADLINE_MS, which includes starting a new worker. Requests are one at a time: make them from one thread. The program's side never waits on the socket without a time limit, so a worker that stops answering cannot freeze the program. A size the worker sends is checked against the caller's limit before any memory is set aside for it.

Limits in the worker. At most 2 GB of memory and 16 open descriptors, no core dump, no file it may write, and a process that cannot be debugged.

Without a jail there is no worker. On a system where xtk_sandbox_available() returns 0, every request fails. There is no weaker way on purpose: a program shows no picture rather than decode one outside the jail.

Children. xtk knows its worker by a pidfd (Linux 5.3 and later), so a program that reaps any child (waitpid(-1, ...), a SIGCHLD handler) or ignores SIGCHLD does no harm: the worker's kill and wait reach the worker and never a process that later got its number. Whether the worker still runs is read from its socket. Without pidfds, the worker is killed only while waitpid() shows it is still the program's own unreaped child.

Known limitations. The worker is tied to the thread that started it: if that thread ends, the kernel kills the worker, and the next request starts another.

#include "xtk_worker.h"

Types: XtkWorkerJob

Constants: XTK_WORKER_NAME, XTK_WORKER_DEADLINE_MS, XTK_WORKER_DATA_MAX, XTK_WORKER_FIRST_JOB, XTK_WORKER_JOBS_MAX

Functions: xtk_worker_is_worker, xtk_worker_add_job, xtk_worker_main, xtk_worker_run, xtk_worker_decode_image, xtk_worker_decode_image_file, xtk_worker_richtext_decoder

Types

XtkWorkerJob

typedef int (*XtkWorkerJob)(const unsigned char *input, size_t input_length, unsigned char **output, size_t *output_length);

A job of the program's own, run in the worker: bytes in, bytes out.

It runs in the jail, so it can read no file and must not try to: everything it needs is in its input. It allocates its output with malloc(); the worker sends it and frees it. Output longer than XTK_WORKER_DATA_MAX counts as a failure.

Constants

XTK_WORKER_NAME

#define XTK_WORKER_NAME "xtk-worker"

The argv[0] of a worker process. xtk_worker_is_worker() looks for it.

XTK_WORKER_DEADLINE_MS

#define XTK_WORKER_DEADLINE_MS 5000

The longest one request may take, from sending it to the end of the answer, in milliseconds.

XTK_WORKER_DATA_MAX

#define XTK_WORKER_DATA_MAX (256u << 20)

The most bytes a request or an answer may have (256 MB).

XTK_WORKER_FIRST_JOB

#define XTK_WORKER_FIRST_JOB 16

The number of the first job of the program's own; xtk's own jobs have smaller numbers.

XTK_WORKER_JOBS_MAX

#define XTK_WORKER_JOBS_MAX 16

The most jobs of the program's own.

Functions

xtk_worker_is_worker

int xtk_worker_is_worker(int argc, char **argv);

Tells whether this process is a worker. Call it at the top of main(), after adding the jobs.

Parameter Description
argc The argc of main().
argv The argv of main().

Returns: 1 when argv[0] is XTK_WORKER_NAME, and main() must then return xtk_worker_main(); 0 for the program itself.

xtk_worker_add_job

int xtk_worker_add_job(XtkWorkerJob job);

Adds a job of the program's own, and gives it its number.

Call it in main() before xtk_worker_is_worker(), so that the program and its worker both add the job, in the same order, and give it the same number. Adding the same function again gives its number again.

Parameter Description
job The function that does the job, in the worker.

Returns: The job's number, for xtk_worker_run(); -1 when job is NULL or XTK_WORKER_JOBS_MAX jobs have been added already.

xtk_worker_main

int xtk_worker_main(void);

The worker's own main(): enters the jail and answers requests until the program goes.

It sets the worker's limits and enters the jail of xtk_sandbox_enter_for_parsing() with no file it may read, before it reads its first request.

Returns: The exit status for main() to return: 1 when the program closed the connection (or sent something that is not a request), 3 when the limits or the jail could not be set up.

xtk_worker_run

int xtk_worker_run(
    int number,
    const unsigned char *input,
    size_t input_length,
    unsigned char **output,
    size_t *output_length,
    size_t output_limit);

Runs a job of the program's own in the worker, and waits for its answer.

Starts the worker first when there is none. Treat the output as hostile: check that it makes sense before using it.

Parameter Description
number The job's number, as xtk_worker_add_job() gave it.
input The bytes for the job (may be NULL when input_length is 0).
input_length How many bytes, at most XTK_WORKER_DATA_MAX.
output Set to the job's output on success, allocated with malloc() and followed by one zero byte that is not counted; the caller frees it. Set to NULL on failure.
output_length Set to the length of the output (0 on failure).
output_limit The most bytes the answer may have; a longer answer is refused before it is read, and the worker is stopped.

Returns: 0 on success; -1 on failure: no jail on this system, no worker could be started, a bad argument, the job failed, the deadline passed, or the answer did not add up.

xtk_worker_decode_image

int xtk_worker_decode_image(
    const unsigned char *data,
    size_t length,
    int max_width,
    int max_height,
    unsigned char **rgba,
    int *width,
    int *height);

Decodes a picture file's bytes (PNG, JPEG, GIF, BMP or XPM) to RGBA pixels, in the jail.

A picture with a side longer than 16384 pixels, or with more than 64 megapixels, is refused. When both max_width and max_height are above 0, a larger picture is scaled down to fit that box with its shape kept, and a smaller one is left as it is. When either is 0, the picture comes at its own size. The size the worker sends back is checked against these limits, and the pixels against the size.

Parameter Description
data The bytes of the file.
length How many bytes.
max_width The widest the result may be, or 0.
max_height The tallest the result may be, or 0.
rgba Set to the pixels on success: width * height * 4 bytes, four per pixel (red, green, blue, alpha), row by row, allocated with malloc(); the caller frees them. Set to NULL on failure.
width Set to the width of the result, on success.
height Set to the height of the result, on success.

Returns: 1 on success, 0 on failure (no jail, not a picture it can read, too large, the deadline passed, or an answer that did not add up).

xtk_worker_decode_image_file

int xtk_worker_decode_image_file(
    const char *path,
    int max_width,
    int max_height,
    unsigned char **rgba,
    int *width,
    int *height);

Decodes a picture file in the jail: the program reads the file, the worker takes it apart.

Only a regular file is read (not a pipe or a device), and it is read without blocking. Otherwise it is xtk_worker_decode_image().

Parameter Description
path The file.
max_width The widest the result may be, or 0 (see xtk_worker_decode_image()).
max_height The tallest the result may be, or 0.
rgba Set to the pixels on success, allocated with malloc(); NULL on failure.
width Set to the width of the result, on success.
height Set to the height of the result, on success.

Returns: 1 on success, 0 on failure (the file cannot be read, is empty, is larger than XTK_WORKER_DATA_MAX, or cannot be decoded).

xtk_worker_richtext_decoder

int xtk_worker_richtext_decoder(
    const char *path,
    int max_width,
    int max_height,
    unsigned char **rgba,
    int *width,
    int *height,
    void *user_data);

xtk_worker_decode_image_file() in the form of an XtkRichTextImageDecoder.

Give it to a rich text view with xtk_richtext_set_image_decoder(view, xtk_worker_richtext_decoder, NULL), and the view's pictures are decoded in the jail.

Parameter Description
path The picture file the document names.
max_width The widest the result may be, or 0.
max_height The tallest the result may be, or 0.
rgba Set to the pixels on success, allocated with malloc(); NULL on failure.
width Set to the width of the result, on success.
height Set to the height of the result, on success.
user_data Not used.

Returns: 1 on success, 0 on failure.