Skip to content

Harden agent log file creation in the shared log directory - #515

Merged
teta2k merged 2 commits into
mainfrom
fix/AIKIDO-2HMZ4IHO
Oct 8, 2026
Merged

teta2k merged 2 commits into
mainfrom
fix/AIKIDO-2HMZ4IHO

Conversation

@teta2k

@teta2k teta2k commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The agent writes its logs to /var/log/aikido-<version>/, which is world-writable so PHP processes running as the web server user can log there too. Log file names are predictable, so another local user could create a file or symlink under the name before the agent opens it.

  • Log file creation: open agent log files with O_CREATE|O_EXCL, so an existing file or symlink (including a dangling one) is never followed or reused. If the name is taken, the agent logs to stdout as it already does when the open fails. New log files are 0644 instead of 0666.
  • Unique per-server log names: per-server log files now include the PHP server PID (aikido-agent-<time>-<agent pid>-<token tail>-<server pid>.log). One token can register several servers, and two of them registering in the same second used to share one file name; they opened the same file at offset 0 and overwrote each other's lines. With O_EXCL the second one would have fallen back to stdout, so the names now stay unique instead.
  • Sticky bit: the apk and rpm install scripts now create /var/log/aikido-* and /run/aikido-* as 1777 instead of 0777, so users can't delete or replace each other's files there. On most distros this also enables the kernel's protected_symlinks / protected_regular checks for these directories.
  • Log line escaping: CR/LF in log messages are escaped, so logged request data (domains, routes, ...) can't add extra log lines. Trailing newlines are trimmed first, so existing ...\n messages don't print a literal \n.

Notes

  • With the sticky bit, a non-root agent can no longer remove a stale socket in /run/aikido-* left behind by a root agent.

Testing

  • New lib/agent/log/log_test.go: planted symlink, dangling symlink and pre-existing file are all refused and left untouched; new file is not group/world-writable; newlines are escaped; trailing newlines are trimmed.
  • Agent builds; go vet on log and server_utils is clean; go test ./... in lib/agent passes on Linux.

teta2k added 2 commits October 8, 2026 16:32
The agent log directory is world-writable and log file names are
predictable, so another local user could plant a file or symlink under
the name before the agent opens it. Open log files with O_EXCL so an
existing entry is never followed or reused, and stop creating them
world-writable.

Also set the sticky bit on /var/log/aikido-* and /run/aikido-* so users
cannot remove or replace each other's files there, and escape CR/LF in
log messages so logged request data cannot forge extra log lines.
One token can register several servers (one per PHP server PID), so two
servers with the same token registering in the same second used to get
the same log file name. With O_EXCL the second one would fail to open
its log file, so add the server PID to keep the names unique.
@teta2k
teta2k marked this pull request as ready for review October 8, 2026 14:39
@teta2k
teta2k merged commit 07341cf into main Oct 8, 2026
78 checks passed
@teta2k
teta2k deleted the fix/AIKIDO-2HMZ4IHO branch October 8, 2026 15:05
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