Repository navigation
Description
Affected layers
- Backend (
server/): affected.ensureConfigDirinserver/src/config-dir.tsopens the configured path verbatim withO_NOFOLLOWand chmods the result. On Linux that flag protects the last path component only, and a trailing/or/.makes the kernel resolve a symlink at that component before the flag applies.server/src/index.tspassesCONFIG_DIRfrom the environment unchanged toensureConfigDirat boot and during password setup, and toreconcileServiceConfigFiles, which passes it on.saveSavedStatepassespath.dirnameof the state file, which is normalized, so that call site is not exposed. - Frontend (
src/): not affected. It never sees the config directory path. shared/: not affected. It holds no file handling.
Problem
ensureConfigDir deliberately leaves a symlinked config directory alone, and a test covers that for a bare path. With CONFIG_DIR=/path/to/config/ or /path/to/config/., the same helper opens the link target and sets it to 0700. The Loupe test adapted to main reproduces: a 0755 directory reached through a link is 0700 after the call with either suffix, and untouched without one.
Impact
A local actor who can replace the config directory entry in a shared writable parent points the link at any directory, and the backend, running as root in the Docker image, removes group and other access from it on its next boot, password setup or reconciliation. That denies the directory to other users or services. It is unauthorized chmod and loss of availability, not code execution. It requires a deployment whose CONFIG_DIR carries the suffix and a writable parent; the Dockerfile sets CONFIG_DIR=/app/data/config without one, so the documented deployment is not exposed.
Expected outcome
- A symlinked config directory is left untouched, with the same warning, for every spelling of
CONFIG_DIR, with or without a trailing/or/.. - No spelling of
CONFIG_DIRlets the backend change permissions outside the directory it manages.
Possible approach (non-binding)
Normalize CONFIG_DIR once where server/src/index.ts reads it, for example with path.resolve, so every consumer sees one canonical path; ensureConfigDir can also strip trailing separators itself, or lstat the entry before the no-follow open and refuse a link outright. server/src/config-dir.test.ts has the symlinked-directory test to extend with the two suffixes.
Loupe findings
- project-loupe/audit-sv2-ui#93: a
/or/.suffix on the configured path makes the no-follow open follow a directory symlink (reported as config-files: a symlinked config directory is left untouched for every spelling of CONFIG_DIR #309)
Related issues and PRs
- fix issues related to permissions #279 added the no-follow chmod in
ensureConfigDirthat the suffix bypasses. - config-files: reconciliation never deletes or writes outside the directory it verified #307 reports the same helper's directory path being substituted after it returns.
- Tracker: [Tracker] Config directory and file handling #370.
Loupe export for #309 (finding #93)
Finding
- repo:
stratum-mining/sv2-ui(https://github.com/stratum-mining/sv2-ui.git) - reviewed revision:
92901c423d5648965960f0476c4bb2975bb4fb27 - title: Prevent directory suffixes from bypassing the no-follow chmod guard
- severity:
low - location:
server/src/config-dir.ts:23-27 - cwe: CWE-59
- scanner:
llm-code-review - fingerprint:
4ecb7a6853af5b3e979d504b07ec4bb0277debc47557618fb8a7f02a34c065d4
Description
The O_NOFOLLOW open receives configDir verbatim. On Linux, that flag protects only the final pathname component: appending '/' or '/.' to a symlink to a directory makes the link traversable during directory resolution. Consequently ensureConfigDir(link + '/') opens the link target and fchmods it to 0700, even though the same helper deliberately leaves a bare directory symlink untouched. index.ts passes the CONFIG_DIR environment value unchanged at startup and into reconciliation, so a deployment configured with a trailing slash reaches this path.
A local actor able to replace the configured directory entry in a shared writable parent can plant or swap a link to an unrelated directory. If the backend runs as root, as in the Dockerfile, this changes that target's permissions with service privileges and removes group/other traversal, potentially denying access to other users or services. The actor need not own the target. This requires a writable parent and the path suffix; the default protected Docker path is unaffected. The impact claimed here is unauthorized chmod and availability loss, not arbitrary code execution.
The Linux node:test regression creates an external 0755 directory and tests both suffixes; the unsafe chmod changes it to 0700. Rejecting or skipping the link passes the test. The read-only review worktree prevented executing these filesystem fixtures; the assessment relies on Linux final-component O_NOFOLLOW semantics. validate_poc accepted the diff. Prior #19004 and #25100 were fetched and concern metadata on atomic file replacement, not redirecting directory chmod.
Proof of Concept
diff --git a/server/src/config-dir-suffix.test.ts b/server/src/config-dir-suffix.test.ts
new file mode 100644
--- /dev/null
+++ b/server/src/config-dir-suffix.test.ts
@@ -0,0 +1,39 @@
+import assert from 'node:assert/strict';
+import { chmod, mkdir, mkdtemp, rm, stat, symlink } from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { test } from 'node:test';
+import { ensureConfigDir } from './config-dir.js';
+
+for (const suffix of ['/', '/.']) {
+ test(`does not chmod an external symlink target when CONFIG_DIR ends in ${suffix}`, {
+ skip: process.platform !== 'linux',
+ }, async () => {
+ const parent = await mkdtemp(path.join(os.tmpdir(), 'sv2-ui-dir-suffix-'));
+ const managedParent = path.join(parent, 'managed');
+ const outside = path.join(parent, 'outside-volume');
+ const configDir = path.join(managedParent, 'config');
+ try {
+ await mkdir(managedParent);
+ await mkdir(outside);
+ await chmod(outside, 0o755);
+ // A local actor controlling the directory entry redirects it to a
+ // directory whose permissions the service must not change.
+ await symlink(outside, configDir);
+
+ try {
+ await ensureConfigDir(configDir + suffix);
+ } catch {
+ // Rejecting the unsafe path is also a valid outcome.
+ }
+
+ assert.equal(
+ (await stat(outside)).mode & 0o777,
+ 0o755,
+ 'a directory suffix must not let chmod remove access outside the managed volume',
+ );
+ } finally {
+ await rm(parent, { recursive: true, force: true });
+ }
+ });
+}
Suggested Fix
diff --git a/server/src/config-dir.ts b/server/src/config-dir.ts
--- a/server/src/config-dir.ts
+++ b/server/src/config-dir.ts
@@ -20,9 +20,10 @@ export async function ensureConfigDir(configDir: string): Promise<void> {
let handle: fs.FileHandle | null = null;
try {
+ // Strip directory suffixes so O_NOFOLLOW protects the directory entry.
handle = await fs.open(
- configDir,
+ configDir.replace(/(?:\/\.?)+$/, '') || '/',
fs.constants.O_RDONLY | fs.constants.O_DIRECTORY | fs.constants.O_NOFOLLOW,
);
await handle.chmod(0o700);
} catch (error) {
This finding was discovered by Project Loupe.
Activity
[-]Prevent directory suffixes from bypassing the no-follow chmod guard[/-][+]config-files: a symlinked config directory is left untouched for every spelling of CONFIG_DIR[/+]