Repository navigation
Description
Affected layers
- Backend (
server/): affected.reconcileServiceConfigFilesinserver/src/service-config.tskeeps an existing managed file whose contents already match the generated TOML and only tightens its mode throughtightenFileMode. A descriptor opened on that file before the chmod keeps its write access, because chmod does not revoke descriptors that are already open. - Frontend (
src/): not affected. It never touches the config directory. shared/: not affected. It holds no file handling.
Problem
Before the owner-only policy, the config directory and the generated translator.toml and jdc.toml could be group- or world-writable. On such an install, reconciliation now secures the directory to 0700 and a matching file to 0600, reports success, and starts the services from that same inode. If another local account opened the file for writing while it was still permissive and kept the descriptor, it can write through it after reconciliation, and the service started next loads the altered file. The Loupe test adapted to the current helper reproduces on main: the managed path holds the tampered pool address after the permission migration.
Impact
A local account that held write access to a legacy, permissive managed file keeps that access across the permission migration without owning the directory or the file. Rewriting the pool address, authority key and user identity together redirects mining work to an endpoint of its choosing. It needs pre-existing access to an insecure legacy file: files created under the current policy are owner-only from the start and are not exposed.
Expected outcome
- A writer that opened a managed file before reconciliation can no longer change the file a service starts with once reconciliation has reported the managed files ready.
- A matching managed file that group or other could write is either replaced by a fresh owner-only file or refused with a clear error; the service never starts from the writable inode.
- A matching managed file that was already owner-only is left in place, as today, so a drift-free restart still rewrites nothing.
Possible approach (non-binding)
In the matching-contents branch, treat a mode with group or other write bits as a reason to rewrite through writeFileAtomically instead of tightening in place: the rename gives the path a new inode that old descriptors cannot reach. server/src/service-config.test.ts already builds permissive legacy fixtures in its mode-migration tests; a regression test can open a writer on a 0666 matching file before reconciling, write through it afterwards, and assert the managed path still holds the generated contents.
Loupe findings
- project-loupe/audit-sv2-ui#96: a matching legacy file is tightened in place, so a writer opened before the chmod keeps its access (reported as config-files: write access obtained before reconciliation cannot reach the config a service starts with #306)
Related issues and PRs
- fix issues related to permissions #279 added the in-place mode migration of matching managed files that an old descriptor survives.
- Tracker: [Tracker] Config directory and file handling #370.
Loupe export for #306 (finding #96)
Finding
- repo:
stratum-mining/sv2-ui(https://github.com/stratum-mining/sv2-ui.git) - reviewed revision:
92901c423d5648965960f0476c4bb2975bb4fb27 - title: Replace writable matching configs to revoke existing attacker descriptors
- severity:
low - location:
server/src/service-config.ts:331-340 - cwe: CWE-732
- scanner:
llm-code-review - fingerprint:
6817bad30b1a3624660e11f5d77df4399b03f57b733255d78c1c8ce36265ee1f
Description
When an existing regular file matches the generated contents, reconciliation keeps its inode and only applies fchmod(0600). This does not revoke writable file descriptors opened before permission repair. A local account that could open a legacy group/other-writable translator.toml can retain that descriptor while leaving the file's contents matching the canonical configuration. Reconciliation secures the directory to 0700 and the file to 0600, reports success, and then the attacker writes through the retained descriptor. The pathname still names the same inode, so a subsequently started service receives the altered configuration. Pool address, authority key, and identity fields can be replaced together to redirect mining work. This requires preexisting access to an insecure legacy file; fresh private files are not exposed.
The node:test regression opens a matching 0666 file before reconciliation, verifies that directory/file permission repair succeeded, and changes the upstream through the old descriptor afterward. Its final assertion requires the accepted configuration to remain trusted. Atomically replacing previously writable matching files with a fresh private inode, or refusing to start, breaks this retained capability.
Prior #19012 concerns matching symbolic links and is addressed by leaf no-follow checks; this is a regular file. #19004 concerns mode inheritance in the atomic writer, which this branch bypasses.
validate_poc accepted the diff. Filesystem execution was unavailable in the read-only worktree with missing dependencies; the actual lifecycle code and regression failed as expected under an in-memory filesystem model.
Proof of Concept
diff --git a/server/src/service-config-open-writer.test.ts b/server/src/service-config-open-writer.test.ts
new file mode 100644
--- /dev/null
+++ b/server/src/service-config-open-writer.test.ts
@@ -0,0 +1,55 @@
+import assert from 'node:assert/strict';
+import fs from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { test } from 'node:test';
+import { reconcileServiceConfigFiles } from './service-config.js';
+
+test('reconciliation revokes preexisting write access to a matching config', {
+ skip: process.platform === 'win32',
+}, async () => {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'sv2-config-open-writer-'));
+ const configDir = path.join(root, 'config');
+ const managedPath = path.join(configDir, 'translator.toml');
+ const trusted = '[[upstreams]]\naddress = "pool.example.com"\n';
+ const tampered = trusted.replace('pool.example.com', 'evil.example.com');
+ let attackerWriter: fs.FileHandle | null = null;
+
+ try {
+ // Model a legacy file which another local account could open before
+ // the service migrated the directory and file to owner-only modes.
+ await fs.chmod(root, 0o755);
+ await fs.mkdir(configDir);
+ await fs.chmod(configDir, 0o755);
+ await fs.writeFile(managedPath, trusted);
+ await fs.chmod(managedPath, 0o666);
+ attackerWriter = await fs.open(managedPath, 'r+');
+
+ let rejected = false;
+ try {
+ await reconcileServiceConfigFiles([{
+ filename: 'translator.toml',
+ contents: trusted,
+ }], configDir);
+ } catch {
+ rejected = true;
+ }
+ if (rejected) return; // Refusing to start with this inode is safe.
+
+ assert.equal((await fs.stat(configDir)).mode & 0o077, 0);
+ assert.equal((await fs.stat(managedPath)).mode & 0o077, 0);
+
+ // chmod does not revoke this already-open capability. The attacker
+ // changes the upstream after reconciliation but before container start.
+ await attackerWriter.writeFile(tampered);
+ await attackerWriter.sync();
+ assert.equal(
+ await fs.readFile(managedPath, 'utf8'),
+ trusted,
+ 'an old writable descriptor must no longer address the accepted config',
+ );
+ } finally {
+ await attackerWriter?.close();
+ await fs.rm(root, { recursive: true, force: true });
+ }
+});
Suggested Fix
diff --git a/server/src/service-config.ts b/server/src/service-config.ts
--- a/server/src/service-config.ts
+++ b/server/src/service-config.ts
@@ -329,7 +329,9 @@ export async function reconcileServiceConfigFiles(
}
const existing = await readExistingFile(filePath);
- if (existing && existing.contents === desiredContents) {
+ // Replace group/other-writable files even when contents match: chmod
+ // cannot revoke write access through an already-open descriptor.
+ if (existing && existing.contents === desiredContents && (existing.mode & 0o022) === 0) {
// Contents match, but files written before the owner-only policy may
// still carry a permissive mode: tighten it in place. Only tighten when
// group/other still have access, so a deliberately more restrictive
This finding was discovered by Project Loupe.
Activity
[-]Replace writable matching configs to revoke existing attacker descriptors[/-][+]config-files: write access obtained before reconciliation cannot reach the config a service starts with[/+]