Skip to content

fix: reject symlink destinations in local filesystem writer - #807

Merged
Nick Keller (NickKeller) merged 5 commits into
mainfrom
fix/localfswriter-nofollow
Nov 17, 2026
Merged

Nick Keller (NickKeller) merged 5 commits into
mainfrom
fix/localfswriter-nofollow

Conversation

@NickKeller

@NickKeller Nick Keller (NickKeller) commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Prevent generated output files from following a symlink at the destination and truncating or writing its target. Repository-controlled symlinks at fixed output names can otherwise redirect generated content outside the checkout; validation of template value bytes does not protect the output path.

  • Use a platform-specific openFileNoFollow helper while retaining the shared write and close error handling.
  • On Unix, open with O_WRONLY | O_CREATE | O_TRUNC | O_NOFOLLOW, rejecting a final-component symlink atomically during open.
  • On Windows, use CreateFile with FILE_FLAG_OPEN_REPARSE_POINT and OPEN_ALWAYS to open without following the final reparse point or truncating it. Inspect the opened handle, reject reparse points, and only then truncate the same handle. This avoids a separate path-check/open race.
  • Cover regular-file creation and truncation, existing symlink targets remaining unchanged, and dangling symlink targets remaining absent. Windows symlink tests skip only when symlink creation fails for lack of privileges.

Why platform-specific files are necessary

The initial version failed the Windows release preflight because syscall.O_NOFOLLOW is undefined on Windows. A runtime GOOS conditional cannot fix this: Go still resolves constants and type-checks references in runtime branches at compile time. Platform-specific source selection keeps the Unix flag out of Windows builds and provides the Windows no-follow equivalent.

Scope

This protects the final path component. Symlinked parent directories require separate hardening; this change does not establish checkout-root confinement.

Validation

  • Passed go test ./pkg/templatewriter/... ./pkg/osutil on Linux.
  • Successfully built the full application for every release target: Windows amd64, Linux amd64/arm64, and Darwin amd64/arm64, using GOOS=<os> GOARCH=<arch> go build -o /tmp/opencode/draft-nofollow-<target> ..
  • Successfully cross-compiled the writer test package for Windows amd64, Linux arm64, and Darwin amd64/arm64 using go test -exec=true ./pkg/templatewriter/writers; these checks compile but do not execute foreign-platform tests.
  • Passed git diff --check.
  • Windows runtime behavior has not been exercised locally; all five full application builds were verified from Linux.

@NickKeller

Copy link
Copy Markdown
Member Author

Fixed the Windows release build failure in cc6f2ca. syscall.O_NOFOLLOW is Unix-only, so a runtime conditional would still fail Windows compilation. The file-open helper now uses platform-specific implementations: Unix retains O_NOFOLLOW; Windows opens the final reparse point without truncating, checks the opened handle, rejects reparse points, and truncates only an accepted regular-file handle.

Verified full application builds for all five release targets (Windows amd64, Linux amd64/arm64, Darwin amd64/arm64). Linux regression tests pass, and the writer tests cross-compile for the remaining targets. These cross-compilation checks do not execute Windows/macOS tests. The PR description now documents the implementation, scope, and exact validation.

@NickKeller

Copy link
Copy Markdown
Member Author

CI follow-up: the Windows binary build and Release Build Preflight now pass for cc6f2ca, and Windows create/update integration jobs pass as well.

The linked Erlang Helm failure occurs after Draft successfully generates the files and the image successfully builds. Both deployed app containers repeatedly terminate with Reason: OOMKilled, exit code 137, under a 1Gi memory limit. They remain unready/CrashLoopBackOff, rollout times out, and the final curl-port-forward step fails because the app never becomes available. The Erlang Kustomize job also reports OOMKilled containers under the same limit.

This points to an Erlang application/runtime memory issue rather than a no-follow file-open failure. The template and integration resource settings were not changed by this PR. The precise source of the Erlang memory use is not established by these logs; it needs a separate runtime reproduction/profile before choosing a memory-limit or VM-configuration change.

@NickKeller

Copy link
Copy Markdown
Member Author

Reproduction attempt completed in the requested development subscription/resource group, using an isolated namespace on an existing AKS cluster, plus local Docker/kind controls. The CI OOM did not reproduce.

  • Built the same sample commit (8922db58edd2981d2ecd9bd2c20a8da6ed680a15) with the generated Dockerfile and integration config: Erlang 27.0-alpine, Alpine 3.17, rebar3 3.28.0, elli 3.3.0. Base-image digests match the CI logs.
  • Tested both generated plain manifests and the generated Helm chart on AKS. Preserved two replicas, 1 CPU/1Gi limits, 0.5 CPU/0.5Gi requests, probes, and container security settings; adapted namespace/image pull settings and used ClusterIP services.
  • All four AKS pods remained ready with zero restarts, approximately 64Mi working set each, startup peak approximately 92Mi, and zero cgroup OOM events. 120 consecutive HTTP checks passed (Hello World!). Manifest pods observed for six minutes and Helm pods for over three minutes.
  • Also tested on local Docker with a 1Gi/1CPU limit and kind v0.30.0 / Kubernetes v1.34.0 (matching CI versions). Both generated deployment formats were healthy; kind startup peak approximately 112Mi, with no OOM events.

These results do not establish the CI-specific cause. Its exact built image was in an ephemeral registry; the rebuilt image is not byte-identical. CI runs Ubuntu 26.04, so host kernel/cgroup/process-accounting differences also remain. Capturing the exact image and memory.events/memory.stat/process RSS on the failing CI runner is the next useful diagnostic step. No application memory limits or VM flags were changed during these tests.

@NickKeller

Copy link
Copy Markdown
Member Author

Updated the kind test stack and added resource diagnostics for the next CI run:

  • SHA-pinned helm/kind-action v1.15.2 and explicitly selected kind v0.33.0.
  • Pinned the v1.34.11 node image by digest and matched kubectl to v1.34.11, keeping the Kubernetes minor version unchanged.
  • Capture runner kernel/CPU/RAM/swap/disk, Docker version/configuration/stats, node capacity/allocatable resources, pod statuses, events, kind Docker limits, process RSS, and cgroup memory limits/peak/events/statistics. Diagnostics run after cluster setup and again on deployment-job failures before kind cleanup.
  • Kept the application 1Gi limit unchanged. Kind normally shares host memory; increasing node memory would not increase the pod limit.

Validation: YAML parsing and bash syntax checks passed, the new pinned kind/node pair successfully created a cluster, and all three diagnostic scripts executed successfully locally.

New finding: the Erlang image now reproduces OOMKilled/exit 137 locally on this updated node image, whereas it was healthy on the earlier v1.34.0 node image. An experimental Erlang scheduler cap did not resolve startup failures. Therefore this update is an instrumented CI experiment, not a confirmed OOM fix. The newer node image reports containerd 2.3.4; runtime/node-image differences now warrant investigation. Captured local evidence is under /tmp/opencode/draft-erlang-repro/updated-kind-oom-diagnostics.log.

@NickKeller

Copy link
Copy Markdown
Member Author

The updated CI run still fails, but diagnostics rule out node memory exhaustion: the runner has 4 vCPUs / 15.61GiB RAM with approximately 14GiB available; the kind Docker container has no explicit memory cap and Kubernetes reports MemoryPressure=False.

Local root-cause experiment: on the updated kind node, the Erlang container inherits an open-file limit (ulimit -n) of 1,073,741,816. Starting the same Erlang image with that limit produced approximately 2GiB RSS, explaining failure under its 1Gi limit. Capping Erlang scheduler counts did not help. Starting with only ulimit -n 65536 reduced memory to approximately 75MiB and returned Hello World.

Confirmed under the original deployment constraints: patched only the local test deployment startup command to /bin/sh -c "ulimit -n 65536; exec /opt/rel/bin/start_script foreground", preserving the 1 CPU / 1Gi limit and original probes/security settings. Both replicas became ready, HTTP returned Hello World, startup memory peak was 98,156,544 bytes, and cgroup counters showed zero OOM events. /proc/1/limits confirmed a 65,536 open-file limit.

This explains why giving the kind node more memory is not the appropriate fix and why this runtime limit disproportionately affects Erlang. The local A/B test establishes the nofile limit as the trigger in the reproduction; the failing CI pod nofile limit itself was not captured in this run. A targeted nofile-limit correction should be tested next rather than increasing application memory. No additional repository changes made in this investigation.

@NickKeller

Copy link
Copy Markdown
Member Author

Historical pipeline comparison strengthens the runner-environment hypothesis and confirms this OOM predates PR #807:

Erlang Helm job Runner image kind / Kubernetes Result
Oct 22 ubuntu-24.04, ubuntu24/20261012.334 v0.30.0 / v1.34.0 Both pods ready, zero restarts, Hello World
Nov 1 ubuntu-24.04, ubuntu24/20261025.353 v0.30.0 / v1.34.0 Both pods ready, zero restarts, Hello World
Nov 14, unrelated dependency PR ubuntu-26.04, ubuntu26/20261101.186 v0.30.0 / v1.34.0 Both pods OOMKilled/137, four restarts each
Nov 16, updated kind ubuntu-26.04, ubuntu26/20261111.196 v0.33.0 / v1.34.11 OOMKilled persists

Across the Oct 22, Nov 1, and Nov 14 logs: same sample commit 8922db58edd2981d2ecd9bd2c20a8da6ed680a15; same erlang:27.0-alpine and alpine:3.17 base-image digests; rebar3 3.28.0 and elli 3.3.0; same 1Gi limit. The Nov 1 passing and Nov 14 failing Helm deployments even have the same pod-template hash (8b8f64f7d). The branch comparison between those runs changes codeql workflow and Go dependency files, not Erlang templates/integration configuration.

The observed pass/fail boundary therefore correlates with ubuntu-latest moving from 24.04 to 26.04, not the no-follow change or kind version alone. The newer runner lists kernel 7.0.0-1019-azure, systemd 259.9, Docker 29.4.2. The exact changed runtime default still needs isolation: historical jobs did not print pod nofile limits, and old runner software metadata could not be retrieved.

Mechanism evidence: Erlang OTP 27 source (erts/emulator/sys/common/erl_poll.c, initialization around lines 2086-2106) reads sysconf(_SC_OPEN_MAX) and explicitly notes the port-table size is based on max_fds and should not be huge. This supports the local A/B result: inherited nofile=1,073,741,816 causes ~2GiB RSS; nofile=65,536 restores normal startup under the unchanged 1Gi cap. It does not substitute for measuring the failing CI pod limit.

@NickKeller

Copy link
Copy Markdown
Member Author

Updated the generated Erlang Dockerfile entrypoint to apply ulimit -n 65536 before exec-ing the existing release startup script. The wrapper forwards arguments with "$@", preserving the default CMD ["foreground"] and release command overrides.

Verified using a freshly generated Dockerfile and rebuilt sample image, launched with the problematic inherited nofile limit of 1,073,741,816 and the original 1Gi memory / 1 CPU limits:

  • HTTP returns Hello World.
  • /proc/1/limits confirms nofile=65,536.
  • Startup memory peak: 99,790,848 bytes (~95MiB), steady usage ~69MiB.
  • Zero OOM events; container stays running.
  • Docker image build and git diff --check pass.

This targets the locally reproduced Erlang startup memory issue directly; CI will validate it in the previously failing environment.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@NickKeller
Nick Keller (NickKeller) merged commit 5c94812 into main Nov 17, 2026
148 checks passed
@NickKeller
Nick Keller (NickKeller) deleted the fix/localfswriter-nofollow branch November 17, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants