Trine security model (S5; reviewed for 1.0, M10)
What Trine promises about running a program, what backs each promise, and what it doesn't promise. The intended reader runs code they didn't write: a dependency, a script from the internet, or a program an AI agent produced.
Who we defend against
| party | trusted? |
|---|---|
the person running trine | yes: they choose the grants |
| the Trine program being run, its modules and its packages | no |
| the input the program reads | no |
the machine: OS, C compiler, the trine binary itself, the project folder | yes |
Someone who can write to your project folder or your trine binary can already run code as you; that is out of scope (see "The run cache" below).
Guarantees
1. Memory safety: no undefined behaviour from Trine code
A Trine program can't read or write memory it doesn't own, in any engine.
- No pointers, no null, no manual freeing. Values are reference-counted; ownership is inferred (Perceus), and every engine checks it differently (the VM catches double drops and use-after-move at run time; the fuzzer compares engines).
- Every list and string index is bounds-checked. The verifier removes a check only when it proves the index in range (
trine boundslists them); debug native builds and the VM re-check every removed check and report a "verifier bug" if one would have failed. - Integers never wrap.
+ - *on Int trap on overflow, unless the verifier proved they can't overflow (same re-checking as above). Division by zero traps. - Match is exhaustive, map lookups of missing keys trap, recursion deeper than 20,000 calls stops with an error instead of overflowing the stack.
What backs it: the compiler and VM are Rust with **zero unsafe blocks** (S1). The native runtime (runtime/trine_rt.h) and generated code are C, which Rust can't vouch for; that part is covered by testing rather than by construction:
- the differential fuzzer runs random programs on the interpreter, the VM and native code and requires identical output (S3; over a million cases in total across releases);
- native fuzz cases also run under AddressSanitizer and UndefinedBehaviorSanitizer (S6), in debug and release builds, as part of
cargo test; - each optimization that removes checks was mutation-tested: a deliberately broken version had to be caught by the fuzzer.
So the guarantee for native code is "no known violation, under heavy differential and sanitized fuzzing", not a proof.
2. No access to the outside world without a grant
A program can touch files, the environment, standard input and its arguments only through capabilities (#21, CAPABILITIES.md):
main(sys)receives the only system capability; no other code can create one.- Files are reached only through
Dircapabilities, which exist only for directories granted on the command line (--allow-read=,--allow-write=) and can only be narrowed. - Names that escape a directory are refused: absolute paths,
.., symbolic links pointing outside (anywhere on the path). Writes never follow a link at the last step. - Environment variables need
--allow-env(all) or--allow-env=A,B(only those). - A function's type shows what it can reach;
trine capslists every function's reach. Capabilities can't implement traits, be compared, hashed or stored as map keys, so generic code can't smuggle one through a type variable.
The rest of the outside world arrived (M2-M8) as more capabilities and grants, each refused without its flag:
| reach | needs | notes |
|---|---|---|
| clock, seeds | Sys (now, seed) | no grant; deterministic replay records them |
| other programs | --allow-run | run, run_input: any program on the PATH, so this grant is everything the user can do |
| network | --allow-net[=HOSTS] | http, tcp_connect (through Sys or a Net) only to the hosts named; redirects aren't followed (the status and location say where) |
| Python | --allow-python | python(dir, file), py_start(sys): Python has the user's full authority; the grant trusts whatever Python code runs |
| C | --allow-c (pure math excepted) | extern fn: C has full authority and can break memory safety; calls passing pointers must sit in unsafe: blocks (FFI.md), so they are few and visible, and buffers are guard-checked |
trine caps marks every function that reaches Python or C as UNCHECKED: what it can do isn't bounded by its type.
3. Deterministic programs
Without input, a Trine program's output is a function of its source: no clock or randomness without Sys, no hash-order iteration (maps and sets keep insertion order), and no data races: tasks (spawn, async fn) share only immutable values and hold no capability but a Net or a connection they own (the isolation checker, PARALLEL.md), and their output and errors come in program order. This is what lets trine run replay a program natively mid-run, and it makes agent-written programs reproducible.
4. Resource limits (S4)
trine run p.tr --max-steps=1000000 --max-time=5 --max-mem=256M --max-output=1M
| flag | stops the program when | notes |
|---|---|---|
--max-steps=N | more than N steps: a step is one call of a function you wrote or a lambda, or one loop iteration (each loop test, the last one included) | identical in all three engines (tested), so a program stops at the same point everywhere: deterministic, unlike time. Loops inside scheduled kernels (.sched) aren't counted. Native builds count only when built for it (a separate cached build; a plain trine build executable refuses the flag instead of ignoring it), because counting costs speed (fib: 7 → 55 ms) |
--max-time=S | S seconds of wall-clock time | all engines; output already written stays |
--max-mem=SIZE | the heap would grow past SIZE (64M, 1G) | native code (address-space cap) |
--max-output=SIZE | printing a line would exceed SIZE bytes | the line that doesn't fit isn't printed |
Each limit ends the run with a runtime error naming the flag (step limit reached (--max-steps=1000)), exit status 1, code limit in --json.
5. Hostile source code doesn't hurt the compiler
Deeply nested or enormous programs fail fast with an error (S2: nesting ≤ 256, operator chains ≤ 10,000), the type checker has no known super-linear blowups on them, and the lexer, parser, checker and VM are fuzzed for panics and hangs (S3). Generated C escapes every string byte in octal, so no Trine string can inject C code. Constants are computed at compile time with a budget (10 million steps, about a second): a constant written to loop forever is a compile error, not a hung compiler or editor (found in the 1.0 review). Checking a program never runs it otherwise: no build scripts, no macros, no code from packages at compile time.
Not guaranteed (today)
| gap | consequence | plan |
|---|---|---|
| Limits are opt-in | without flags, an infinite loop runs forever and an allocation loop runs until the OS stops it | pass the limits below when running code you don't trust |
**--max-mem is native-only and approximate** | it caps the process's address space (Linux); the interpreter can't cap its own memory, so --interp/--vm refuse the flag and plain trine run compiles first | an allocator-level cap if needed |
| Standard output is ungated | any program can print | intended: output is how programs answer; --max-output caps its size |
| Resolve-then-open race | a different process that can modify a granted directory tree can swap a directory for a link between Trine's path check and the open, redirecting one operation | use openat2(RESOLVE_BENEATH) on Linux; Trine programs themselves can't race (tasks can't hold a Dir) |
| Type-checker blowup on purpose | Hindley-Milner types can grow exponentially in contrived programs (let b = (a, a) repeated); checking such a file is slow or runs out of memory | as in every ML-family language; run trine check on untrusted code with OS limits |
| Fetching a project's git dependencies | checking a project fetches the git URLs its trine.toml names (no code runs; only files are downloaded, then hashed against trine.lock). URLs, revs and lock entries are checked first (below), but git itself still talks to whatever server the URL names | use --locked for projects you don't trust, so nothing new is fetched |
| The shared build cache | native builds are reused across projects from $TRINE_HOME/build, keyed by the C source and compiler; anyone who can write there can replace an executable | same trust as your home directory; TRINE_NO_BUILD_CACHE=1 |
| No cryptographic randomness | random/random_below are SplitMix64: anyone who sees a few outputs can predict the rest. seed(sys) reads the OS (/dev/urandom, rand_s on Windows) but falls back to the clock and process id when that fails, silently; the interpreter on Windows always falls back (it reads /dev/urandom) | never use them for tokens, passwords or keys: use run(sys, ["openssl", "rand", "-hex", "32"]) or Python's secrets (PYTHON.md). After 1.0: a secure_random(sys) that fails instead of falling back |
| Side channels | timing and memory use reveal information about secrets | out of scope |
| The C compiler | native builds trust the system cc | out of scope (same as any compiled language) |
| Proofs for native code | memory safety of generated C rests on fuzzing and sanitizers, not on a proof | the verifier's facts are re-checked in debug builds; a verified backend is far off |
Cryptography and quantum computers
Trine uses no public-key cryptography, which is the part large quantum computers would break (RSA and elliptic curves, through Shor's algorithm). What it does use:
| use | algorithm | quantum view |
|---|---|---|
definition hashes, trine.lock content hashes, build cache keys, trine migrate proofs | SHA-256 (src/hash.rs, no dependencies) | safe: Grover's algorithm halves preimage strength to about 128 bits, still out of reach; collisions stay harder than that in practice |
| pinning git dependencies | git commit ids (SHA-1, or SHA-256 in SHA-256 repositories) | SHA-1 is weak today for classical reasons, not quantum ones; Trine also pins a SHA-256 hash of the files, so a forged commit with different files is refused |
https:// in http | the system's curl | whatever curl's TLS library negotiates; OpenSSL 3.5 and later already use a hybrid post-quantum key exchange (X25519MLKEM768) by default. Trine holds no keys |
plain tcp_connect | none | unencrypted, as documented |
The one place this will matter is signed releases: prebuilt binaries (P8) ship with SHA-256 checksums, which stay safe, and signed build attestations (Sigstore, through GitHub), which use elliptic curves like every signature made today. The plan is to add post-quantum signatures (ML-DSA, FIPS 204) when the signing tools support them, rather than writing our own.
The run cache
trine run keeps native builds in .trine/bin/ keyed by a hash of the program and the compiler, and runs a cached executable without recompiling. Anyone who can write to .trine/ can replace that executable; that's the same trust as the project folder itself (an attacker who can write there can also edit the source). The cache never crosses projects, and TRINE_NO_CACHE=1 disables it. Since M3, native builds are also kept in a cache shared by every project of the same user ($TRINE_HOME/build, PACKAGES.md), with the same trust.
The 1.0 review (M10)
For 1.0 every boundary was walked again with the attacks below; each is a case in the test sandbox_escapes_are_refused (interpreter and native code) unless named otherwise.
| boundary | tried | result |
|---|---|---|
| a granted directory | ../secret, an absolute path, a symlink to a file outside, a symlinked directory leading outside | refused (outside the directory); nothing read |
| read-only grants | writing into a directory granted with --allow-read | refused; the file unchanged |
| ungranted directories, environment, programs, Python | dir(sys, "."), env, run, py_start without their flags | refused with the flag to pass |
| network hosts | http and tcp_connect (through net(sys)) to a host not granted | refused before connecting |
| forging authority | calling a capability builtin with something that isn't a capability | a type error (expected Sys) |
| tasks | a task capturing sys | a type error (the isolation checker) |
| C | a pointer-passing extern outside unsafe:; any C call without --allow-c | a type error; a runtime refusal |
| the compiler | a constant that never finishes | was a hang; now a compile error within a second |
| packages | a git dependency whose files don't match trine.lock | refused (two_package_project_builds_reproducibly_from_a_clean_checkout) |
| buffers given to C | a C function writing past the end | a runtime error, every engine (zlib_through_c_strings_buffers_and_unsafe_blocks) |
The second review: outside input (after 1.0)
AI-driven attack tools find known kinds of weakness quickly, so everything that reaches Trine from outside a program was read again with them in mind: project files, the network, other programs, the C runtime. The attacks are the test hostile_inputs_are_refused_the_same_everywhere unless named otherwise.
| boundary | found | now | |
|---|---|---|---|
trine.lock (comes with a project) | a commit like /../../x went into a temporary folder name: git wrote a checkout outside the package cache (Linux, confirmed), and on Windows the delete before it could remove a folder such as AppData | an entry must be a 40 or 64 digit hex commit id and a sha256: hash of 64 hex digits, or the lock is "damaged" | |
trine.toml git URLs and revs | a URL starting with - was read by git clone as an option (no command ran, but only because the next argument wasn't a repository); ext:: URLs relied on git's own default to stay off | URLs: https, http, ssh, git, file, user@host:path or a local path, never -... or x::...; -- before every URL; protocol.ext and protocol.fd forced off; revs can't start with - | |
run, http and Python on Windows (native) | CreateProcess searches the current directory before the PATH: a curl.exe or python3.exe planted in a project folder would run for a program granted only --allow-net | programs are found on the PATH only, as Rust (the interpreter) does; not yet run on Windows | |
| batch files on Windows (native) | a granted .bat runs under cmd.exe, which re-reads &, `\ | , %VAR%` in its arguments | only plain arguments (letters, digits, _-.,/:=+@\) reach a batch file; not yet run on Windows |
https:// through curl | curl read ~/.curlrc (which can turn on following redirects to ungranted hosts) and expanded {a,b} / [1-100] in URLs (one call, many requests) | curl -q -g --proto =https | |
| HTTP header names | any name without : or spaces; @file names are curl's syntax for reading a file of headers | names are RFC 9110 tokens; values can't hold CR, LF or NUL | |
| HTTP responses | six kinds of malformed response gave different answers on the interpreter and native code (lone \r or \n in the head, signs in numbers, repeated Content-Length, Unicode spaces, chunk extensions) | one rule on every engine: lines end with CRLF only, numbers are plain digits, a repeated length is no length | |
| HTTP requests (native) | the request line was built in a 700-byte buffer: longer URLs were cut short (safe, but a broken request) | any length | |
| the C runtime's HTTP parser | 4,800 fuzzed hostile responses (a server sending mutations of valid responses) against native code under AddressSanitizer and UBSan | no memory errors or undefined behaviour |
Not yet done: a reading of this model and its code by someone other than its authors. Until that happens, the claims above are tested, not independently audited.
Running untrusted code: a checklist
- Run
trine caps program.trand read what each function can reach. - Grant the least you can: a read-only directory (
--allow-read=data), named environment variables only (--allow-env=HOME). - Set limits:
--max-steps(deterministic) or--max-time, plus--max-memand--max-output. - Prefer the interpreter for one-off untrusted runs (
trine run --interp): it involves no C compiler and no cached executables.
Reporting
Security bugs: a Trine program that reads or writes memory it shouldn't, escapes a granted directory, reaches the environment without a grant, crashes the compiler, or makes two engines disagree. Each gets a regression test in tests/ and, where it applies, a fuzzer case.