project-pegaprox icon indicating copy to clipboard operation
project-pegaprox copied to clipboard

[Bug] SSH websocket server script is written to and executed from /tmp

Open grupoaxium opened this issue 2 months ago • 2 comments

Describe the bug

At startup, PegaProx writes the SSH websocket server script to /tmp/.ssh_ws_server.py and executes it from there. Running code from a world-writable directory with a predictable file name is fragile: depending on the host and on the service hardening, another local process could pre-create, replace or symlink that file before it is executed.

Steps to Reproduce

  1. Install PegaProx from the Debian package and start the service.
  2. Run ps -o user=,pid=,cmd= -C python3.
  3. Observe the SSH websocket subprocess.

Expected behavior

The SSH websocket server runs from a location owned by the package or by the service user, for example /usr/lib/pegaprox/, not from /tmp.

Environment

  • PegaProx Version: 1.2.0 (Debian package 1.2.0-1)
  • Installation Method: Other: APT repository packages.gyptazy.com, Debian trixie
  • OS: Debian 13 (trixie)
  • Browser: N/A
  • Behind Reverse Proxy? Traefik v3.6 (not relevant to this issue)
  • UI? N/A

Logs

$ ps -o user=,pid=,cmd= -C python3
pegaprox   15075 /usr/bin/python3 /usr/lib/pegaprox/pegaprox_multi_cluster.py
pegaprox   15083 /usr/bin/python3 /tmp/.ssh_ws_server.py

Screenshots

N/A

Checklist

  • [ ] I have searched existing issues to make sure this is not a duplicate
  • [ ] I am using the latest version of PegaProx
  • [ ] I've put enough in here that someone can start on it without asking me three questions first
  • [ ] I understand PegaProx is maintained by volunteers in their free time. Nobody is on call, and writing URGENT doesn't get anyone out of bed 🪄

Suggested fix: ship the script as part of the package, or write it to a private directory owned by the service user with mode 0700. Adding PrivateTmp=yes to the systemd unit would also mitigate it.

grupoaxium avatar Sep 28 '26 11:09 grupoaxium

You are right, and it is fixed on Testing.

The script now goes to the config directory - the one the service already owns and already keeps its database and keys in - and only falls back to a temp directory if that is not writable. It is created with O_CREAT|O_EXCL|O_NOFOLLOW at mode 0600, so a pre-created file or a symlink planted at the path makes the open fail rather than get followed, and a stale file from an earlier run is replaced rather than reused.

One more turn after the first fix: O_NOFOLLOW only refuses a planted symlink. A plain file another user created at the path first would still have been opened and written into, which on the temp-directory fallback is the case that matters. It is now O_EXCL as well, with our own leftover unlinked first, so somebody else's file makes the open fail instead.

On your suggestions: shipping it as a packaged file is the cleaner end state and we would like to get there, but the script is generated from a template at startup (it carries a per-run secret), so that is a larger change than this ticket. PrivateTmp=yes in the unit is a good idea independently of this fix and we will take it - it costs nothing and it narrows more than just this one file.

Nothing about this was exploitable on a normal single-purpose host, to be clear, but "predictable name in a world-writable directory, then executed" is not a shape worth defending, and you were right to file it.

mkellermann97 avatar Sep 28 '26 18:09 mkellermann97

The SSH websocket script no longer runs from /tmp. It is now written to the service's own config directory and created with O_EXCL and O_NOFOLLOW at mode 0600, so a planted file or symlink makes the start fail instead of being executed, and the temp directory is only a last resort when the config directory is not writable. This ships in 1.3.0.

PegaProx runs on donations, https://opencollective.com/pegaprox if you want to chip in.

Marcus

mkellermann97 avatar Oct 07 '26 04:10 mkellermann97