Skip to content

fix: null-terminate readlink buffer in GetSelfPath#275

Merged
blondfrogs merged 1 commit intoRunOnFlux:masterfrom
MorningLightMountain713:fix/readlink-null-terminate
Apr 21, 2026
Merged

fix: null-terminate readlink buffer in GetSelfPath#275
blondfrogs merged 1 commit intoRunOnFlux:masterfrom
MorningLightMountain713:fix/readlink-null-terminate

Conversation

@MorningLightMountain713
Copy link
Copy Markdown
Contributor

Summary

  • Null-terminate the buffer returned by readlink() before passing it to dirname() in GetSelfPath()

Problem

Observed fluxd failing to start intermittently with:

2026-03-13 11:27:52 Path: /usr/local/bin/fluxd
2026-03-13 11:27:52 Error: Failed to find benchmark application
2026-03-13 11:27:52 Shutdown: In progress...

After systemd restarted the service, it worked fine:

2026-03-13 11:28:37 Path: /usr/local/bin
2026-03-13 11:28:37 Found fluxbenchd in /usr/local/bin

On the failed start, GetSelfPath() returned /usr/local/bin/fluxd (full binary path) instead of /usr/local/bin (directory only). This caused FindBenchmarkPath("fluxbenchd", "/usr/local/bin/fluxd") to look for /usr/local/bin/fluxd/fluxbenchd which doesn't exist.

Root cause

readlink() does not append a null byte to the buffer. GetSelfPath() passes the un-terminated buffer directly to dirname(), which reads past the valid bytes into whatever garbage is on the stack. If that garbage happens to contain a / character, dirname() finds it and returns the wrong directory. Whether the stack contains a / depends on prior call history, ASLR, environment size, etc. — hence the intermittent nature.

Test plan

Built a test binary that reproduces the issue by simulating dirty stack memory:

broken raw: /usr/local/bin/fluxdaaaaa/aaaa
broken dirname: /usr/local/bin/fluxdaaaaa    <-- wrong

fixed  raw: /usr/local/bin/fluxd
fixed  dirname: /usr/local/bin               <-- correct

🤖 Generated with Claude Code

readlink does not append a null byte. Without null-termination,
dirname reads into uninitialized stack memory causing intermittent
failures where GetSelfPath returns the full binary path instead of
the directory, preventing fluxbenchd from being found at startup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@blondfrogs blondfrogs merged commit 239f639 into RunOnFlux:master Apr 21, 2026
1 of 4 checks passed
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