Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

docs(air-gapped): mktemp inside the subshell — outside it, my own TMPDIR advice was the exploit

xerj-org9a31960f3c43
diff --git a/docs/recipes/air-gapped-deployment.md b/docs/recipes/air-gapped-deployment.md
index a9cea952c..091610f4f 100644
--- a/docs/recipes/air-gapped-deployment.md
+++ b/docs/recipes/air-gapped-deployment.md
@@ -108,13 +108,13 @@ VERSION="${TAG#v}"
STAGE="xerj-${VERSION}-x86_64-unknown-linux-musl"
ASSET="${STAGE}.tar.gz"
-work="$(mktemp -d)"
-trap 'rm -rf "$work"' EXIT
-cp "/opt/xerj-staging/release/$ASSET" "$work/$ASSET"
-cp "/opt/xerj-staging/release/$ASSET.sha256" "$work/$ASSET.sha256"
-
(
set -eu
+
+ work="$(mktemp -d)"
+ trap 'rm -rf "$work"' EXIT
+ cp "/opt/xerj-staging/release/$ASSET" "$work/$ASSET"
+ cp "/opt/xerj-staging/release/$ASSET.sha256" "$work/$ASSET.sha256"
cd "$work"
want="$(sha256sum "$ASSET" | cut -d ' ' -f 1)"
@@ -179,14 +179,31 @@ one was broken through the handoff rather than through the digest. There is no
handoff now, so nothing later has to be trusted and no exit status has to be
checked.
-**`mktemp -d` puts the work directory under `TMPDIR`, and that is an
-assumption.** It is safe when `TMPDIR` is sticky and not attacker-writable —
-the default `/tmp` on any normal system. It is NOT safe if `/tmp` has been made
-world-writable without the sticky bit, or if `TMPDIR` points into a directory
-the attacker controls: renaming a directory entry needs write on the parent, so
-the work directory can then be swapped however restrictive its own mode is. If
-your enclave is unusual here, set `TMPDIR` to a root-owned directory before
-running these blocks.
+**`mktemp -d` runs INSIDE the subshell, under `set -eu`.** It was outside in an
+earlier version, where a failure was not fatal and its status was never checked:
+`work` became the empty string, both `cp` lines failed, and `cd ""` returns 0 in
+bash without changing directory — so the block hashed and extracted `$ASSET`
+from whatever directory the operator happened to be in. If that was the staging
+directory, it silently became the double read again, printed the genuine digest
+and installed attacker bytes at exit 0.
+
+**The work directory must be writable by you and by nobody else.** `mktemp -d`
+uses `TMPDIR`, which is safe when that is sticky and not attacker-writable — the
+default `/tmp` on any normal system. It is not safe if `/tmp` has been made
+world-writable without the sticky bit, or if `TMPDIR` points somewhere the
+attacker can write: renaming a directory entry needs write on the parent, so the
+work directory can be swapped however restrictive its own mode is. If your
+enclave is unusual here, make a private directory and point `TMPDIR` at it:
+
+```bash
+install -d -m 0700 "$HOME/xerj-verify"
+export TMPDIR="$HOME/xerj-verify"
+```
+
+An earlier version of this page said "set `TMPDIR` to a root-owned directory",
+which is worse than no advice: a root-owned directory the attacker cannot write
+is one you cannot write either, so `mktemp` fails — and with the failure
+unchecked, that sentence was itself the trigger for the defect above.
An attacker who rewrites the archive AND recomputes its `.sha256` still passes,
and always will: that is the one case a checksum cannot answer, and the prose
@@ -255,14 +272,14 @@ installing anything — the same fail-closed rule as section 1, and the reason
the check is a command here rather than a sentence:
```bash
-mwork="$(mktemp -d)"
-trap 'rm -rf "$mwork"' EXIT
-cp /opt/xerj-staging/model/all-MiniLM-L6-v2/{config.json,tokenizer.json,model.safetensors} \
- "$mwork/"
-cp /opt/xerj-staging/model/model.sha256 "$mwork/model.sha256"
-
(
set -eu
+
+ mwork="$(mktemp -d)"
+ trap 'rm -rf "$mwork"' EXIT
+ cp /opt/xerj-staging/model/all-MiniLM-L6-v2/{config.json,tokenizer.json,model.safetensors} \
+ "$mwork/"
+ cp /opt/xerj-staging/model/model.sha256 "$mwork/model.sha256"
cd "$mwork"
for f in config.json tokenizer.json model.safetensors; do
diff --git a/landing/docs/recipes/air-gapped-deployment.html b/landing/docs/recipes/air-gapped-deployment.html
index dfb50a246..50d422900 100644
--- a/landing/docs/recipes/air-gapped-deployment.html
+++ b/landing/docs/recipes/air-gapped-deployment.html
@@ -112,13 +112,13 @@ VERSION="${TAG#v}"
STAGE="xerj-${VERSION}-x86_64-unknown-linux-musl"
ASSET="${STAGE}.tar.gz"
-work="$(mktemp -d)"
-trap 'rm -rf "$work"' EXIT
-cp "/opt/xerj-staging/release/$ASSET" "$work/$ASSET"
-cp "/opt/xerj-staging/release/$ASSET.sha256" "$work/$ASSET.sha256"
-
(
set -eu
+
+ work="$(mktemp -d)"
+ trap 'rm -rf "$work"' EXIT
+ cp "/opt/xerj-staging/release/$ASSET" "$work/$ASSET"
+ cp "/opt/xerj-staging/release/$ASSET.sha256" "$work/$ASSET.sha256"
cd "$work"
want="$(sha256sum "$ASSET" | cut -d ' ' -f 1)"
@@ -141,7 +141,7 @@ cp "/opt/xerj-staging/release/$ASSET.sha256" "$work/$ASSET.sha256"
sudo install -m 0755 "$STAGE/xerj" /opt/xerj/bin/xerj
echo "installed $ASSET ($want)"
)</pre>
- <p><strong>The archive is copied once, then hashed and extracted from that copy.</strong> Eight earlier versions of this recipe opened the archive twice at the staging path &mdash; once by <code class="inline">sha256sum</code>, once by <code class="inline">tar</code> &mdash; and anyone who can write that directory swaps the file between the two opens: the genuine <code class="inline">.sha256</code> verifies, the block prints the genuine digest and exits 0, and a backdoor is installed. Copying first collapses that to a single read, because the bytes hashed and the bytes extracted are the same file, in a directory the attacker cannot reach. <strong>The digest is computed here and the file is searched for it</strong>, not the other way round: <code class="inline">sha256sum -c</code> reads filenames out of the <code class="inline">.sha256</code> and exits 0 if any one line verified. <strong>The block that verifies is the block that installs</strong>, so nothing later has to be trusted and no exit status has to be checked. <strong><code class="inline">mktemp -d</code> puts the work directory under <code class="inline">TMPDIR</code></strong>, which is safe when that is sticky and not attacker-writable (the default <code class="inline">/tmp</code>); if yours is not, point <code class="inline">TMPDIR</code> at a root-owned directory first.</p>
+ <p><strong>The archive is copied once, then hashed and extracted from that copy.</strong> Earlier versions opened the archive twice at the staging path &mdash; once by <code class="inline">sha256sum</code>, once by <code class="inline">tar</code> &mdash; and anyone who can write that directory swaps the file between the two opens. Copying first collapses that to a single read. <strong><code class="inline">mktemp -d</code> runs inside the subshell, under <code class="inline">set -eu</code></strong>: outside it, a failed <code class="inline">mktemp</code> left <code class="inline">work</code> empty, <code class="inline">cd ""</code> returned 0 without moving, and the block silently hashed and extracted from whatever directory the operator was in &mdash; the double read again, at exit 0. <strong>The work directory must be writable by you and nobody else.</strong> The default sticky <code class="inline">/tmp</code> is fine; if yours is not, <code class="inline">install -d -m 0700 "$HOME/xerj-verify"</code> and point <code class="inline">TMPDIR</code> there. This page previously advised a <em>root-owned</em> directory, which an unprivileged operator cannot write &mdash; that advice was itself the trigger for the defect above.</p>
<h2>2. Configure the base lexical node</h2>
<p>Create the service account first, then install only the staged binary. No model directory is needed for lexical mode:</p>
@@ -177,14 +177,14 @@ done
sha256sum config.json tokenizer.json model.safetensors &gt; ../model.sha256
)</pre>
<p>Transfer <code class="inline">OUT/model</code> with the release. Verify it inside the enclave before installing anything &mdash; the same fail-closed rule as section 1, and the reason the check is a command here rather than a sentence:</p>
-<pre class="code">mwork="$(mktemp -d)"
-trap 'rm -rf "$mwork"' EXIT
-cp /opt/xerj-staging/model/all-MiniLM-L6-v2/{config.json,tokenizer.json,model.safetensors} \
- "$mwork/"
-cp /opt/xerj-staging/model/model.sha256 "$mwork/model.sha256"
-
-(
+<pre class="code">(
set -eu
+
+ mwork="$(mktemp -d)"
+ trap 'rm -rf "$mwork"' EXIT
+ cp /opt/xerj-staging/model/all-MiniLM-L6-v2/{config.json,tokenizer.json,model.safetensors} \
+ "$mwork/"
+ cp /opt/xerj-staging/model/model.sha256 "$mwork/model.sha256"
cd "$mwork"
for f in config.json tokenizer.json model.safetensors; do