Skip to content

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

Installer and update.sh: fixed file and folder modes whatever the umask (#5444)

Milind Soni777616294729
diff --git a/deploy/e2e/run.sh b/deploy/e2e/run.sh
index 7a10b485f6..7e55684100 100755
--- a/deploy/e2e/run.sh
+++ b/deploy/e2e/run.sh
@@ -18,9 +18,11 @@ DOMAIN=installer-e2e.internal
DIR=/opt/openmausbot
SETTINGS=(OMB_INSTALL_SKIP_DNS=1 OMB_INSTALL_HTTPS_WAIT_S=20 "OMB_INSTALL_SOURCE=file://$ROOT")
# Without a version asked for: what a person's later run is, which must keep the version update.sh pinned.
-PLAIN_INSTALL=(sudo env "${SETTINGS[@]}" sh "$ROOT/deploy/install.sh")
+# Under umask 000, as a root shell may have it: what the installer makes must not take its modes from there.
+UMASK_000=(sh -c "umask 000; exec sh \"\$0\" \"\$@\"")
+PLAIN_INSTALL=(sudo env "${SETTINGS[@]}" "${UMASK_000[@]}" "$ROOT/deploy/install.sh")
if [ -n "${OMB_INSTALL_IMAGE:-}" ]; then SETTINGS+=("OMB_INSTALL_IMAGE=$OMB_INSTALL_IMAGE"); fi
-INSTALL=(sudo env "${SETTINGS[@]}" sh "$ROOT/deploy/install.sh")
+INSTALL=(sudo env "${SETTINGS[@]}" "${UMASK_000[@]}" "$ROOT/deploy/install.sh")
box_check() { (cd "$DIR" && sudo docker compose exec -T omb node dist-server/bot-box/box-check.js --public=none "$@"); }
as_root() { sudo "$@"; }
@@ -131,6 +133,14 @@ else
echo "this image has no sandbox; the install said so"
fi
+step "what the installer made has fixed modes, though it ran under umask 000"
+getfacl -p /opt 2>/dev/null || true
+for want in "755 $DIR" "644 $DIR/compose.yaml" "644 $DIR/Caddyfile" "600 $DIR/.env" "700 $DIR/data" "755 $DIR/caddy/data" "755 $DIR/caddy/config"; do
+ [ "$(sudo stat -c '%a %n' "${want#* }")" = "$want" ] || fail "$(sudo stat -c '%a %n' "${want#* }"), not $want"
+done
+if [ -f "$DIR/update.sh" ]; then [ "$(sudo stat -c %a "$DIR/update.sh")" = 755 ] || fail "update.sh is $(sudo stat -c %a "$DIR/update.sh"), not 755"; fi
+echo "the install's folder 755, its files 644 and 755, .env 600, data 700"
+
step "update.sh is placed as the repository has it"
if [ -f "$ROOT/deploy/update.sh" ]; then
sudo cmp "$ROOT/deploy/update.sh" "$DIR/update.sh" || fail "update.sh was not placed"
diff --git a/deploy/e2e/update.sh b/deploy/e2e/update.sh
index fac763b5ae..2b1485d93b 100755
--- a/deploy/e2e/update.sh
+++ b/deploy/e2e/update.sh
@@ -20,7 +20,8 @@ TO=${TO_VERSION:-0.1.151}
mkdir -p "$OUT"
if [ "$LAYOUT" = installer ]; then DIR=/opt/openmausbot DATA=/opt/openmausbot/data
else DIR=/srv/openmausbot-e2e/deploy DATA=deploy_data; fi
-UPDATE=(bash "$ROOT/deploy/update.sh" --dir "$DIR")
+# Under umask 000, as a root shell may have it: what update.sh writes must not take its modes from there.
+UPDATE=(bash -c "umask 000; exec bash \"\$0\" \"\$@\"" "$ROOT/deploy/update.sh" --dir "$DIR")
step() { printf '\n== %s\n' "$*"; }
fail() {
@@ -48,7 +49,7 @@ for old in /opt/openmausbot /srv/openmausbot-e2e/deploy; do
if [ -d "$old" ]; then (cd "$old" && docker compose down -v) >/dev/null 2>&1; fi
done
for volume in $(copies); do docker volume rm -f "$volume" >/dev/null 2>&1; done
-rm -rf "$DIR" && mkdir -p "$DIR"
+rm -rf "$DIR" && install -d -m 755 "$(dirname "$DIR")" "$DIR"
docker pull -q "$REPO:v$FROM" >/dev/null && docker pull -q "$REPO:v$TO" >/dev/null || fail "could not pull the two releases"
if [ "$LAYOUT" = installer ]; then
mkdir -p "$DIR/caddy/data" "$DIR/caddy/config"
@@ -116,6 +117,7 @@ step "2. update to the stable version"
[ "$(version)" = "$TO" ] || fail "the server says $(version)"
grep -qx "OMB_IMAGE=$STABLE" "$DIR/.env" || fail ".env is not pinned by digest"
[ "$(stat -c %a "$DIR/.env")" = 600 ] || fail ".env changed its mode"
+[ "$(stat -c %a "$DIR/.update")" = 700 ] || fail ".update is $(stat -c %a "$DIR/.update"), not 700, though update.sh ran under umask 000"
in_data 'cat /d/.openmausbot/e2e-sentinel.txt' | grep -q "written under $FROM" || fail "the data did not carry over"
[ "$(cd "$DIR" && docker compose ps --status running --services | sort | tr '\n' ' ')" = "caddy omb " ] || fail "caddy or omb is not running"
copy >/dev/null || fail "expected one copy of the data"
diff --git a/deploy/install.sh b/deploy/install.sh
index 19e881a298..936be0f27b 100755
--- a/deploy/install.sh
+++ b/deploy/install.sh
@@ -811,7 +811,7 @@ place_update() {
# The sandbox's host preparation and Compose settings are this repository's own (deploy/box, connector/profiles),
# fetched and used as they are, all four from the same place.
fetch_sandbox() {
- mkdir -p "$INSTALL_DIR/box/profiles"
+ install -d -m 755 "$INSTALL_DIR/box" "$INSTALL_DIR/box/profiles"
fetch deploy/box/prepare-host.sh "$INSTALL_DIR/box/prepare-host.sh" 755 &&
fetch deploy/box/compose.box.yaml "$INSTALL_DIR/compose.box.yaml" &&
fetch "connector/profiles/$SECCOMP" "$INSTALL_DIR/box/profiles/$SECCOMP" &&
@@ -938,10 +938,21 @@ wait_https() {
return 1
}
+# An install's folder, or its data, that others may write to (made by hand, or by an installer before this one) is closed
+# to them: root runs what is in it.
+close_to_others() { # folder
+ if [ -n "$(find "$1" -prune \( -perm -020 -o -perm -002 \) 2>/dev/null)" ]; then
+ chmod go-w "$1"
+ note "$1 could be changed by others on this server; now only its owner can change it."
+ fi
+}
+
install_server() {
say ""
say "Installing MausBot in $INSTALL_DIR..."
- mkdir -p "$INSTALL_DIR" "$INSTALL_DIR/caddy/data" "$INSTALL_DIR/caddy/config"
+ if [ -d "$INSTALL_DIR" ]; then close_to_others "$INSTALL_DIR"; fi
+ if [ -d "$INSTALL_DIR/data" ]; then close_to_others "$INSTALL_DIR/data"; fi
+ install -d -m 755 "$INSTALL_DIR" "$INSTALL_DIR/caddy" "$INSTALL_DIR/caddy/data" "$INSTALL_DIR/caddy/config"
write_compose
write_caddyfile
write_settings
@@ -1073,6 +1084,10 @@ run_uninstall() {
}
main() {
+ # SECURITY: what this makes in the install's folder is run by root later (compose.yaml, update.sh), so its modes are
+ # set here and by each write, never left to the caller's umask: a root shell with umask 000 would make them writable
+ # by everyone on the server.
+ umask 022
settings
parse "$@"
case "$MODE" in
diff --git a/deploy/update.sh b/deploy/update.sh
index be6fb0c786..9ee7f5c02a 100755
--- a/deploy/update.sh
+++ b/deploy/update.sh
@@ -28,6 +28,9 @@
# 5 nothing was changed: the version asked for is older than the one running, or either version cannot be told
# 130 interrupted (Ctrl-C) before the new version started: nothing was changed, and the server was started again
set -uo pipefail
+# SECURITY: the files this writes beside the Compose file (compose.yaml, Caddyfile) are what root starts; their modes never
+# come from the caller's umask. .env and the journal keep their own, tighter ones.
+umask 022
# An SSH session that drops sends SIGHUP, and a `| tee` that dies with it would send SIGPIPE: neither may stop an
# update half-way. Output to a terminal that has gone is lost; .update/update.log keeps it.
trap '' HUP PIPE
diff --git a/scripts/server-installer.test.ts b/scripts/server-installer.test.ts
index 57f6423444..40024c88d9 100644
--- a/scripts/server-installer.test.ts
+++ b/scripts/server-installer.test.ts
@@ -19,7 +19,7 @@ const BOX_PASSED = '{"box":"bwrap","total":31,"passed":31,"failed":[],"results":
const BOX_FAILED = '{"box":"bwrap","total":31,"passed":30,"failed":["config-hidden"],"results":[{"id":"config-hidden","ok":false}]}';
const FILES = "https://raw.githubusercontent.com/milind-soni/OpenMausBot";
const SOURCE = `${FILES}/v0.1.151`;
-const REAL_TOOLS = ["sed", "awk", "grep", "cat", "head", "tail", "tr", "sort", "cut", "mkdir", "mktemp", "chmod", "chown", "mv", "rm", "dirname", "sh"];
+const REAL_TOOLS = ["sed", "awk", "grep", "cat", "head", "tail", "tr", "sort", "cut", "mkdir", "mktemp", "chmod", "chown", "mv", "rm", "dirname", "sh", "install", "find"];
/** A server that passes every check: Ubuntu 24.04, x86-64, 4 cores, 8 GB, 80 GB free, Docker 27, free ports, a domain that points here. */
const HEALTHY = {
@@ -38,6 +38,8 @@ interface Server {
apparmor?: string | null;
/** Leave these stand-ins out, as on a server without the tool. */
without?: string[];
+ /** The umask the installer is started with, as a root shell may have it. */
+ umask?: string;
}
/** Stand-ins for every tool install.sh asks about the machine, written once: what each answers comes from the run's FAKE_ settings. */
@@ -146,7 +148,8 @@ function run(args: string[], server: Server = {}, kept?: { dir: string }) {
writeFileSync(join(sysroot, "proc", "meminfo"), `MemTotal: ${server.memoryKb ?? 8_100_000} kB\nMemFree: 1000000 kB\n`);
writeFileSync(join(sysroot, "proc", "sys", "user", "max_user_namespaces"), `${server.userns?.max ?? "63000"}\n`);
if (server.userns?.clone !== undefined) writeFileSync(join(sysroot, "proc", "sys", "kernel", "unprivileged_userns_clone"), `${server.userns.clone}\n`);
- const result = spawnSync("/bin/sh", [SCRIPT, ...args], {
+ const command = server.umask ? ["-c", `umask ${server.umask}; exec /bin/sh "$0" "$@"`, SCRIPT, ...args] : [SCRIPT, ...args];
+ const result = spawnSync("/bin/sh", command, {
encoding: "utf8",
env: { PATH: bin, FAKE_LOG: log, OMB_INSTALL_SYSROOT: sysroot, OMB_INSTALL_DIR: install, MAUSBOT_ADMIN_URL: "https://admin.example.test", ...HEALTHY, ...server.env },
timeout: 60_000,
@@ -421,6 +424,24 @@ describe.skipIf(!posix)("deploy/install.sh", () => {
expect(noCertificate.all).not.toContain("MausBot is running at");
});
+ it("gives what it makes fixed modes whatever the umask it is started with, and closes a folder others could change", () => {
+ const mode = (path: string) => statSync(path).mode & 0o777;
+ const first = run(["--domain", "maus.example.com"], { umask: "000" });
+ expect(first.status, first.all).toBe(0);
+ const expected: Record<string, number> = {
+ "": 0o755, "compose.yaml": 0o644, Caddyfile: 0o644, ".env": 0o600, data: 0o700, "update.sh": 0o755,
+ caddy: 0o755, "caddy/data": 0o755, "caddy/config": 0o755,
+ };
+ for (const [path, want] of Object.entries(expected)) expect(mode(join(first.install, path)).toString(8), path || "the install's folder").toBe(want.toString(8));
+ chmodSync(first.install, 0o777);
+ chmodSync(join(first.install, "data"), 0o777);
+ const again = run([], { umask: "000" }, { dir: first.dir });
+ expect(again.status, again.all).toBe(0);
+ expect(mode(first.install)).toBe(0o755);
+ expect(mode(join(first.install, "data"))).toBe(0o755);
+ expect(again.stdout).toContain(` note ${first.install} could be changed by others on this server; now only its owner can change it.`);
+ });
+
it("--uninstall stops the server and keeps the data, saying where it is", () => {
const nothing = run(["--uninstall"]);
expect(nothing.stderr).toContain("MausBot isn't installed in");
diff --git a/scripts/testing/deploy-update.test.ts b/scripts/testing/deploy-update.test.ts
index a3d30314ac..bf4643e67f 100644
--- a/scripts/testing/deploy-update.test.ts
+++ b/scripts/testing/deploy-update.test.ts
@@ -66,8 +66,10 @@ function install(target: Record<string, unknown> = {}, extra: Record<string, unk
}));
writeFileSync(join(root, "stable.json"), stableRecord(next.version, next.refs[0]!));
const state = () => JSON.parse(readFileSync(join(fake, "state.json"), "utf8"));
- const run = (args: string[] = [], options: { input?: string; env?: Record<string, string> } = {}) => {
- const result = spawnSync("bash", [join(deploy, "update.sh"), ...args], {
+ const run = (args: string[] = [], options: { input?: string; env?: Record<string, string>; umask?: string } = {}) => {
+ const script = [join(deploy, "update.sh"), ...args];
+ const command = options.umask ? ["-c", `umask ${options.umask}; exec bash "$0" "$@"`, ...script] : script;
+ const result = spawnSync("bash", command, {
encoding: "utf8", input: options.input ?? "", timeout: 60_000,
env: {
// sbin: sha256sum is /sbin/sha256sum on macOS.
@@ -401,9 +403,12 @@ services:
writeFileSync(join(box.deploy, "compose.yaml"), oldCompose);
writeFileSync(join(box.deploy, "Caddyfile"), oldCaddyfile);
expect(box.run(["--check"]).stdout).toContain("It would also move Caddy to its own container");
- const done = box.run();
+ // Under umask 000, as a root shell may have it: the files root starts from are still the owner's alone to change.
+ const done = box.run([], { umask: "000" });
expect(done.status, done.stdout).toBe(0);
expect(done.said).toContain("Caddy moves to its own container, so HTTPS stays up whenever the server restarts on its own.");
+ for (const file of ["compose.yaml", "Caddyfile"]) expect((statSync(join(box.deploy, file)).mode & 0o777).toString(8), file).toBe("644");
+ expect((statSync(join(box.deploy, ".update")).mode & 0o777).toString(8)).toBe("700");
const compose = readFileSync(join(box.deploy, "compose.yaml"), "utf8");
expect(compose).not.toContain("network_mode");
expect(compose).toContain("- edge:/run/openmausbot");