Lesson 2 of 12 · 15m
Environment Variables Are Not Private
Anything in a process's environment is readable by its owner through /proc, inherited by every child, visible to orchestrators, and frozen into core dumps.
Run cat /proc/self/environ | tr '\0' '\n' in a shell. The kernel hands every process a null-separated environment block at execve, stores it in the process's address space, and exposes it at /proc/<pid>/environ. The access rule: the process owner and root can read it. On a shared host, that means every other process running as your user can dump your environment — and a compromised dependency running inside your process can do it without touching /proc at all, because getenv() is a plain function call.
The inheritance problem
fork() + execve() passes the parent's environment unless you deliberately scrub it. os.system(), subprocess.run(), child_process.spawn() — all inherit by default. Your app shells out to git, curl, a PDF renderer, a backup script; every one of those children carries AWS_SECRET_ACCESS_KEY whether it needs it or not. Now one of those children has a CVE, or is just poorly written, or calls env in an error path — the Codecov incident was precisely this: a modified script ran env and exfiltrated everything the CI job held. The blast radius of one vulnerable child is every secret in the parent's environment.
ps eww prints your own processes' environments to your terminal — on a multi-user box, root's ps eww prints everyone's. systemctl show prints Environment= values for any unit you can query, and systemd-analyze verify will happily echo EnvironmentFile= paths into your output. docker inspect renders the container's Env array in JSON — anyone with Docker socket access owns every env-injected secret in every container. kubectl get pod -o yaml and kubectl describe pod show env in the spec for anyone with get pods. docker-compose config interpolates and prints resolved values to stdout. None of these are bugs. They are the documented behavior of the mechanism, and the audience for each is wider than "the process that needed the secret."
The dependency angle is the one nobody budgets for. Any library in your process can call getenv("AWS_SECRET_ACCESS_KEY") — your secrets are ambient to every line of code in the address space, including the logging middleware that cheerfully dumps process.env into a debug response, and the APM agent that ships env vars to a third-party collector unless you configure a denylist (Datadog's tracer scrubs DD_* and a fixed set; your custom key names are on you).
The wrong mental model
"Environment variables are in memory, so they don't touch disk and are safer than files." Three failures in that sentence. First, memory is readable — getenv, /proc, and ps all exist. Second, it does touch disk: core dumps embed the environment block, swap files hold it, and VM snapshots capture it. Third, "safer than files" ignores that env injection makes the value ambient — every library and every child gets it by default, which is the opposite of least privilege. A file can be 0400 on a tmpfs mount readable by exactly one process. An env var is broadcast to the whole process tree.
Concrete practice
- Prefer file injection over env injection. Kubernetes secret volumes are tmpfs-backed by default,
LoadCredential=drops secrets under$CREDENTIALS_DIRECTORY, and Docker secrets land in/run/secrets— all per-mount readable, none of them inherited by children. - When env is unavoidable, scrub for children:
env -i PATH=/usr/bin:/bin /usr/bin/child, orenv/clearenvequivalents in your language, so only what the child needs survives. - Keep secrets out of anything you
docker inspectorkubectl describe. In Kubernetes, never put secrets inenv:literal fields orenvFrom:— usesecretKeyRefat minimum, and prefer volume mounts because env vars are frozen at exec while mounted files update. - Rotate any credential that has ever lived in an environment a crash reporter, a core dump handler, or a
subprocesswith full-env inheritance might have touched.
Env vars are a delivery channel, not a vault. Use them to hand a secret to one process, then assume everything below that process can see it.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
RSA Is Simple. Deploying RSA Correctly Almost Never Is.
The capstone to our RSA series. The math checks out. Shipping it as written gets you breached. This is the nineteen-year-old attack that proves it, and what belongs in production instead.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
Implementing RSA, AES-GCM, and a TLS 1.3 Handshake from Scratch in Python
What is Encryption? The Math That Keeps Your Data Private
Encryption transforms readable data into unreadable ciphertext that only someone with the right key can reverse. It's what makes HTTPS, messaging apps, banking, and password storage work. Here's how it actually functions.