Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(bot-box): make the seccomp filter file 0600 whatever was there before

DeryFerdae8c7cbdea6a
diff --git a/server/bot-box/box.test.ts b/server/bot-box/box.test.ts
index c3b19fdb41..3dd6191612 100644
--- a/server/bot-box/box.test.ts
+++ b/server/bot-box/box.test.ts
@@ -1,4 +1,4 @@
-import { chmodSync, closeSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { chmodSync, closeSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
@@ -7,7 +7,7 @@ import { BotBox, BoxRunner, BOX_EGRESS_PORT, flyApiPrivate, writeSeccompFile } f
import { processStart, seesProcess } from "./box-probe.ts";
import { procHidepid } from "./hidepid.ts";
import { boxUploadsDir, visibleInBox } from "./plan.ts";
-import { DENIED_SYSCALLS, seccompBytes } from "./seccomp.ts";
+import { DENIED_SYSCALLS, seccompBytes, seccompSupported } from "./seccomp.ts";
import { engineInstallRoots, npmEnginesPrefix } from "../engine-roots.ts";
import { spawnCli } from "../procs.ts";
import { waitForExit } from "../procs.ts";
@@ -125,6 +125,19 @@ describe("the box's hardening (T0c)", () => {
rmSync(dir, { recursive: true, force: true });
});
+ it("gives the filter file 0600 even when a looser file is already there", () => {
+ // writeFileSync's mode applies only when it creates the file, so this is the case that used to slip through.
+ if (process.platform === "win32" || !seccompSupported()) return;
+ const dir = mkdtempSync(join(tmpdir(), "omb-seccomp-mode-"));
+ const file = join(dir, "seccomp.bpf");
+ writeFileSync(file, "a stale filter");
+ chmodSync(file, 0o644);
+ expect(writeSeccompFile(dir)).toBe(file);
+ expect(statSync(file).mode & 0o777).toBe(0o600);
+ expect(readFileSync(file)).toEqual(seccompBytes());
+ rmSync(dir, { recursive: true, force: true });
+ });
+
it("reads the host's hidepid and Fly's socket folder mode", () => {
expect(procHidepid(() => "proc /proc proc rw,nosuid,nodev,noexec,relatime,hidepid=invisible 0 0\n")).toBe(true);
expect(procHidepid(() => "proc /proc proc rw,nosuid,nodev,noexec,relatime,hidepid=2 0 0\n")).toBe(true);
diff --git a/server/bot-box/box.ts b/server/bot-box/box.ts
index 10918a55f2..76cd8862f1 100644
--- a/server/bot-box/box.ts
+++ b/server/bot-box/box.ts
@@ -3,9 +3,10 @@
// with owner "bot") in bubblewrap by the plan (plan.ts). Nothing here changes a spawn when the box is off: the command,
// its arguments and its environment go through untouched, so every host that is not a Cloud home behaves as before.
import { randomBytes } from "node:crypto";
-import { closeSync, existsSync, mkdirSync, openSync, rmSync, statSync, writeFileSync } from "node:fs";
+import { closeSync, existsSync, mkdirSync, openSync, rmSync, statSync } from "node:fs";
import { dirname, join, relative, resolve, sep } from "node:path";
+import { writeFileAtomic } from "../atomic.ts";
import { engineInstallRoots } from "../engine-roots.ts";
import { homeProgramDirs, resetPathCache } from "../env-path.ts";
import { SERVER_ROOT, resolveProxy } from "../proxy-paths.ts";
@@ -232,7 +233,10 @@ export interface BoxReport {
export function writeSeccompFile(dir: string): string | null {
if (!seccompSupported()) return null;
const file = join(dir, "seccomp.bpf");
- writeFileSync(file, seccompBytes(), { mode: 0o600 });
+ // Security: writeFileSync's mode applies only when it creates the file, so a seccomp.bpf left with looser
+ // permissions would keep them and a local writer could swap in a filter that allows everything. A fresh
+ // inode with the mode, renamed over it, makes the 0600 above true whatever was there before.
+ writeFileAtomic(file, seccompBytes(), { mode: 0o600 });
return file;
}