PortGate

Resources · Security guides · Security guide

Keep secrets out of ps, core dumps and swap

Command-line arguments are visible to every user, crashing processes write memory to disk, and swap can hold secrets after a process exits. Close each of those paths.

Check command

ps -eo user,args | grep -iE 'password|secret|token|api[_-]?key' | grep -v grep

Copied.

Secrets leave a process in three places people forget: the command line, crash dumps and swap. Each is easy to close.

Command-line arguments are public

Every user on the host can read every process's arguments:

ps -eo user,args | grep -iE 'password|secret|token|api[_-]?key' | grep -v grep

mysql -pS3cret, curl -H "Authorization: Bearer ..." in a cron job, or --api-key=... in a systemd ExecStart line all show up there, and in shell history. Pass secrets another way:

  • Environment from a file that only the service user can read: EnvironmentFile=/etc/myapp/env with mode 600.
  • systemd credentials on recent systemd: LoadCredential=db-password:/etc/myapp/db-password, read from $CREDENTIALS_DIRECTORY inside the service.
  • Client config files: ~/.my.cnf for MySQL, ~/.pgpass for Postgres, mode 600.
  • stdin: many tools accept --password-stdin (Docker does).

Core dumps write memory to disk

When a process crashes, the kernel can write its memory to a file. That memory includes decrypted secrets, session data and keys. Unless you debug crashes on production, turn dumps off.

# /etc/security/limits.d/90-nocore.conf
*  hard  core  0
# /etc/sysctl.d/90-nocore.conf
fs.suid_dumpable = 0

On systemd hosts, also set Storage=none in /etc/systemd/coredump.conf and reload with sudo sysctl --system. Check with ulimit -c in a new login shell: it should print 0.

Swap can outlive the process

Memory paged to swap stays on disk after the process exits until it is overwritten. On a VPS with a swap file and sensitive data in memory:

  • Size swap small, or use zram (compressed swap in RAM) instead of a disk file.
  • If you need disk swap, encrypt it, or put it on an encrypted volume.
  • Programs that handle keys can lock their memory (mlock) so it is never swapped. Database and secret-manager daemons often have a setting for this.

swapon --show lists what the host uses now.

Logs are the fourth place

Debug logging of request bodies or headers writes tokens to disk in plain text. Keep log levels at info or above in production, and scrub Authorization and cookie headers in any request logging you add.