Zoo

staging

← back to the zoo

9. Try the doors

An app cannot open another app's files, and it cannot reach the server's own internal network.

Every request is a fresh wasm instance in rc-host: its own data folder is the only place it can read or write, its database the only one it can open, and its outbound calls pass a fence that refuses the box's loopback, private networks and the metadata service.

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (staging). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e09_doors.odin sha256 bd03aa3ce61d · raw

1package main
2
3// Exhibit 9. The zoo is one wasm instance per request inside rc-host: its files are its own data
4// folder and nothing else (every path is resolved beneath it, no symlink followed), its database is
5// its own (SQLite cannot ATTACH another file), and its outbound calls go through a fence that refuses
6// the server's loopback, private networks, the cloud metadata service and the box's own non-web ports,
7// after DNS and on every redirect. This tries the doors right now, as the zoo itself; the controls
8// (its own file, its own address) must open, or the refusals prove nothing. Root tries the same doors
9// as every wasm app, every hour, from inside the host's own sandbox.
10
11import "core:fmt"
12import "core:strings"
13import "ratcloud:rc"
14
15Door :: struct {
16	door:   string `json:"door"`,
17	result: string `json:"result"`,
18	why:    string `json:"why"`,
19}
20
21doors :: proc() -> []Door {
22	out := make([dynamic]Door)
23	e := env().env
24	file :: proc(out: ^[dynamic]Door, door, path: string) {
25		_, c := rc.read_file(path)
26		append(out, Door{fmt.tprintf("read %s (%s)", door, path), "refused" if c < 0 else "opened", fmt.tprintf("error %d", c) if c < 0 else "read"})
27	}
28	file(&out, "account's database", fmt.tprintf("../../account/%s/account.db", e))
29	file(&out, "account's database", fmt.tprintf("/srv/data/account/%s/account.db", e))
30	file(&out, "account's signing key", fmt.tprintf("/srv/data/account/%s/assert-key.pem", e))
31	file(&out, "the platform's secrets", "/etc/ratcloud/secrets")
32	file(&out, "account's repository", "/opt/account.git/config")
33	p, l := rc.sptr(fmt.tprintf("../../account/%s/account.db", e))
34	h := rc.db_open(p, l)
35	append(&out, Door{"open account's database by name", "refused" if h < 0 else "opened", fmt.tprintf("error %d", h) if h < 0 else "opened"})
36	_, ae := rc.exec(db(), fmt.tprintf("ATTACH DATABASE '/srv/data/account/%s/account.db' AS x", e))
37	append(&out, Door{"ATTACH account's database", "refused" if ae != "" else "opened", ae})
38	_, s1 := rc.secret("account")
39	append(&out, Door{"read the zoo's own sign-in secret", "opened" if s1 else "refused", "it stays in the host"})
40	for u in ([]string{"http://127.0.0.1/", "http://[::1]/", "http://10.0.0.1/", "http://169.254.169.254/latest/meta-data/", fmt.tprintf("http://%s:22/", env().host)}) {
41		res := rc.fetch({url = u, timeout_ms = 5000})
42		refused := strings.has_prefix(res.err, "refused")
43		append(&out, Door{fmt.tprintf("connect to %s", u), "refused" if refused else "opened", res.err if res.err != "" else fmt.tprintf("status %d", res.status)})
44	}
45	return out[:]
46}
47
48// The control: the same tries on doors that should open. If these fail, so would everything.
49controls :: proc() -> map[string]bool {
50	c := make(map[string]bool)
51	w := rc.write_file("doors-control.txt", transmute([]u8)string("mine"))
52	back, re := rc.read_file("doors-control.txt")
53	c["own data"] = w == 0 && re == 0 && string(back) == "mine"
54	c["own address"] = rc.fetch({url = self_url("/lights.json"), timeout_ms = 10_000}).status == 200
55	return c
56}
57
58e09 :: proc() -> Exhibit {
59	return {
60		n = 9, wing = "Isolation",
61		title = "Try the doors",
62		promise = "An app cannot open another app's files, and it cannot reach the server's own internal network.",
63		block = "Every request is a fresh wasm instance in rc-host: its own data folder is the only place it can read or write, its database the only one it can open, and its outbound calls pass a fence that refuses the box's loopback, private networks and the metadata service.",
64		src = {"e09_doors.odin", #load("e09_doors.odin", string)},
65		script = {"web/e09.js", #load("web/e09.js", string)},
66		show = proc(me: ^rc.User) -> string {
67			root := strings.builder_make()
68			for c in jarr(fact("platform.json"), "check", "checks") {
69				line := jstr(c, "line")
70				if !strings.has_prefix(line, "(exhibit 9)") && !strings.has_prefix(line, "An app cannot read") do continue
71				rc.render_to(&root, `<li><span class="tag {{1}}">{{2}}</span>{{3}}</li>`, {{"1", "ok" if jstr(c, "status") == "pass" else "bad"}, {"2", jstr(c, "status")}, {"3", jstr(c, "detail")}})
72			}
73			out := `<p><button class="primary" id="doors-go">Try the doors now</button></p><ul class="list" id="doors"></ul><p class="muted out" id="doors-ctl"></p>`
74			if strings.builder_len(root) > 0 do out = cat(out, `<p class="muted">Root tries them too, as every app, every hour:</p><ul class="list">`, strings.to_string(root), "</ul>")
75			return out
76		},
77		api = proc(a: ^Api) -> bool {
78			if a.do_ != "try" || !a.post do return false
79			reply(a, struct {
80					doors:    []Door `json:"doors"`,
81					controls: map[string]bool `json:"controls"`,
82				}{doors(), controls()})
83			return true
84		},
85		check = proc() -> Check {
86			d := doors()
87			bad := make([dynamic]string)
88			for x in d do if x.result != "refused" do append(&bad, fmt.tprintf("%s: %s", x.door, x.result))
89			if len(bad) > 0 do return fail(strings.join(bad[:], "; "), d)
90			for k, v in controls() do if !v do return fail(cat("the control failed: the zoo could not open its own ", k), d)
91			return pass(fmt.tprintf("%d doors tried, all refused; its own data and address open", len(d)), d)
92		},
93	}
94}

web/e09.js sha256 02262cf1b96e · raw

1document.getElementById("doors-go")?.addEventListener("click", async (ev) => {
2  ev.target.disabled = true; ev.target.textContent = "trying…";
3  const j = await zoo.call("/api/9/try", {});
4  ev.target.disabled = false; ev.target.textContent = "Try again";
5  if (j.error) { document.getElementById("doors").textContent = j.error; return; }
6  document.getElementById("doors").innerHTML = j.doors.map(d =>
7    '<li><span class="tag ' + d.result + '">' + d.result + '</span><span>' + zoo.esc(d.door) + ' <small class="muted">' + zoo.esc(d.why) + '</small></span></li>').join("");
8  document.getElementById("doors-ctl").textContent = "Control, the zoo's own doors: " +
9    Object.entries(j.controls).map(([k, v]) => k + " " + (v ? "opened" : "FAILED")).join(", ") + ".";
10});