8000 config-files: reconciliation never deletes or writes outside the directory it verified · Issue #307 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

config-files: reconciliation never deletes or writes outside the directory it verified #307

Open

Description

@lucasbalieiro

Affected layers

  • Backend (server/): affected. reconcileServiceConfigFiles in server/src/service-config.ts secures the directory through ensureConfigDir, closes that descriptor, then reaches every managed file through the configDir string. Obsolete-file cleanup and foreign-entry removal call fs.rm with recursive: true on those paths. The no-follow opens on the files protect the final path component only, not the directory above it. POST /api/reset in server/src/index.ts removes the same managed names the same way.
  • Frontend (src/): not affected. It never touches the config directory.
  • shared/: not affected. It holds no file handling.

Problem

In a deployment where the parent of the config directory is writable by another local account and not sticky, that account can rename the service-owned 0700 config directory and put a symlink in its place at any moment after ensureConfigDir has returned. The rest of the reconciliation run resolves the managed filenames through the link. When the desired set lacks jdc.toml, cleanup deletes whatever jdc.toml names inside the link target, recursively if it is a directory, and the managed TOML names are written into the target. The Loupe test adapted to main reproduces: a sentinel file under outside/jdc.toml/ is gone after reconciling an empty set.

Impact

A local account with write access to the parent directory, and none inside the target, gets the backend to delete a directory tree named jdc.toml or translator.toml anywhere the backend can reach, and to overwrite files of those two names there, with the backend's privileges, which are root's in the Docker image. Targets are limited to those two basenames. The documented Docker deployment is not exposed, because the parent of /app/data/config is root-owned inside the sv2-ui container. A custom deployment whose config directory sits under a shared writable parent is. Reconciliation runs on every start, restart and automatic recovery, so the window reopens often.

Expected outcome

  • A reconciliation run only deletes, replaces or writes entries inside the directory it verified at its start. If that directory's path is renamed or replaced by a link during the run, the run fails without touching the replacement's target.
  • Removing an obsolete or foreign entry at a managed name never deletes anything outside that verified directory.
  • The reset path gives the same guarantee for the managed files it removes.

Possible approach (non-binding)

Node has no openat, so one option is to keep the descriptor from ensureConfigDir open for the run and, before each removal or write, check that lstat of the configured path still names the same device and inode as that descriptor, failing otherwise. Another is to drop recursive: true on managed names: unlink regular files and links, remove a planted directory only when it is empty, and otherwise refuse to start with a clear message. The Loupe test shows how to schedule the swap deterministically by hooking the close of the directory descriptor; server/src/service-config.test.ts already builds real temporary directories with planted FIFOs and symlinks.

Loupe findings

Related issues and PRs

Loupe export for #307 (finding #95)

Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: 92901c423d5648965960f0476c4bb2975bb4fb27
  • title: Prevent config-directory substitution from redirecting recursive cleanup
  • severity: medium
  • location: server/src/service-config.ts:308-319
  • cwe: CWE-367
  • scanner: llm-code-review
  • fingerprint: c868871c4cab96a75afea29ccad672d2088fc7dd4fb3edc4141e0f21c15fc153

Description

After ensureConfigDir closes its directory descriptor, reconciliation resolves every managed filename through the original configDir string. The O_NOFOLLOW checks on individual files do not protect intermediate path components. In a custom deployment with a non-sticky config-directory parent writable by another local account, that account can rename even a service-owned 0700 config directory and replace it with a symlink after successful hardening. The privileged backend then follows that substituted parent. In particular, obsolete-file cleanup recursively deletes the destination's jdc.toml directory and its contents; replacement can also overwrite the destination's managed TOML filenames. The attacker need not have access inside the destination. Targets are constrained to the two managed basenames, and deployments with protected ancestors are not exposed.

The node:test regression deterministically schedules the rename/symlink when ensureConfigDir closes its successfully secured directory. Reconciliation with an empty desired set deletes a sentinel outside the original configuration directory. Reject unsafe mutable ancestry and keep subsequent filesystem operations anchored to a trusted directory; checking only the final entry is insufficient.

Prior #27018 concerns ignored EPERM/EACCES during hardening; this case succeeds at hardening. #22246 concerns initial directory modes, and #19012 concerns a leaf symlink, so neither duplicates this ancestor substitution.

validate_poc accepted the diff. The read-only worktree and absent dependencies prevented a real-filesystem test run; the unchanged lifecycle functions and this regression reproduced the deletion using an in-memory filesystem model.

Proof of Concept

diff --git a/server/src/service-config-parent-race.test.ts b/server/src/service-config-parent-race.test.ts
new file mode 100644
--- /dev/null
+++ b/server/src/service-config-parent-race.test.ts
@@ -0,0 +1,62 @@
+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 cannot delete through a replaced config directory', {
+  skip: process.platform === 'win32',
+}, async (t) => {
+  const root = await fs.mkdtemp(path.join(os.tmpdir(), 'sv2-config-parent-race-'));
+  const parent = path.join(root, 'shared-parent');
+  const configDir = path.join(parent, 'config');
+  const detachedDir = path.join(parent, 'detached-config');
+  const outsideDir = path.join(root, 'outside');
+  const sentinel = path.join(outsideDir, 'jdc.toml', 'keep.txt');
+  const originalOpen = fs.open;
+  let swapped = false;
+
+  try {
+    await fs.mkdir(parent);
+    await fs.chmod(parent, 0o777);
+    await fs.mkdir(configDir, { mode: 0o700 });
+    await fs.mkdir(path.dirname(sentinel), { recursive: true, mode: 0o700 });
+    await fs.writeFile(sentinel, 'data outside the managed config directory');
+
+    // A writer of the non-sticky parent can rename even a 0700 child.
+    // Schedule that attacker action just after the directory was secured.
+    t.mock.method(fs, 'open', async (...args: Parameters<typeof fs.open>) => {
+      const handle = await originalOpen(...args);
+      if (String(args[0]) === configDir && !swapped) {
+        const originalClose = handle.close.bind(handle);
+        t.mock.method(handle, 'close', async () => {
+          await originalClose();
+          if (!swapped) {
+            swapped = true;
+            await fs.rename(configDir, detachedDir);
+            await fs.symlink(outsideDir, configDir);
+          }
+        });
+      }
+      return handle;
+    });
+
+    // Refusing the replaced directory is also safe. Even an error must
+    // leave files outside the original managed directory untouched.
+    try {
+      await reconcileServiceConfigFiles([], configDir);
+    } catch {
+      // A fixed implementation may reject the directory substitution.
+    }
+
+    assert.equal(
+      await fs.readFile(sentinel, 'utf8'),
+      'data outside the managed config directory',
+      'obsolete-config cleanup must not recurse into a substituted parent',
+    );
+  } finally {
+    t.mock.restoreAll();
+    await fs.rm(root, { recursive: true, force: true });
+  }
+});

This finding was discovered by Project Loupe.

Activity

  1. added
    area:config-files
    and removed on Oct 14, 2026
  2. changed the title [-]Prevent config-directory substitution from redirecting recursive cleanup[/-] [+]config-files: reconciliation never deletes or writes outside the directory it verified[/+] on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:config-files

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0