Skip to content
All writing

sudo cp is just root with extra steps

A second developer needed to deploy. The shortest path was one line in sudoers, and that one line would have handed him the Square production token.

The shape

One VPS, three Node services, one Postgres database. All three ran as root, which is what "it works" looks like when you are the only person who has ever touched the box. Being the only person is what was holding it together, and that stopped being true this week.

So: dedicated unprivileged users for the services, the code they run made read only to them, a role gate finally applied to money after months of existing and never being called, and deploying turned into a fixed path wrapper instead of a sudo grant.

old: three Node services running as root, deploy by sudo cp -a, a refund policy no route ever calls
new: dedicated service users, root owned read only code, deploys through a fixed path wrapper, refunds are owner only

sudo cp -a is not a subset of root

deploy.sh calls sudo cp -a, chown, mkdir and psql. Granting an account enough sudo to run that script is granting root, and not approximately. sudo cp -a alone reads any file on the box, including the booking backend's env file that holds the Square production token, and overwrites any file, including /etc/sudoers.

The wrapper does the privileged half from fixed paths, so the caller cannot name the source or the destination. The staging directory is derived from SUDO_USER rather than passed in, so one maintainer cannot install another's tree. Verification runs server side inside the wrapper rather than in the client script, so it cannot be skipped by editing the client. It refuses a partial tree, refuses non source files, refuses symlinks, refuses a staged .env, refuses a rollback stamp that is not a timestamp, and runs node --check on everything before it swaps anything in.

Sudoers grants that wrapper plus systemctl restart and status for two services, and deliberately not cp, chown, mkdir, tee, psql, su, or any shell.

The honest part is written into the wrapper's own header, because a security control that oversells itself is worse than none. Whoever can deploy controls the code, and the code runs as the app's service user, so a deployer can execute arbitrary code as that user and read that user's environment. What they cannot do is read the booking backend's environment, and they cannot become root. That is the boundary this buys. It is worth having and it is not a sandbox.

All of it was tested as him rather than asserted: a real deploy through the wrapper ran the full smoke test and logged the deployer, rollback worked, every guard held, and he could deploy, query the database and read logs while being unable to read either env file, sudo cat, get a root shell, or write to the installed code. Database access is peer auth on a role matching the OS user, so there is no password to send over Slack or store anywhere.

Two service users, not one

A public facing Node process running as root turns any remote code execution bug in the app into full root on the box holding a payment processor's production token. All three services ran that way. None of them needed to.

Two separate service users rather than one shared one, because the separation is the entire value: the web app cannot read the booking backend's environment and therefore cannot reach the Square token, and the backend cannot read the app's. One shared user would have been tidier and worth much less.

Secrets stay mode 600 and root owned, which sounds like it should break everything and does not, because systemd reads EnvironmentFile as root before it drops privileges. The service users never need filesystem access to their own secrets. The code stays root owned and read only to the services, so a compromised process cannot rewrite its own code to persist. NoNewPrivileges, PrivateTmp, ProtectHome, ProtectSystem=full and friends went on as drop in overrides rather than edits to the unit files, so removing the override is a clean rollback.

One thing broke, and it was the honest kind. The notification tick read its env file off the filesystem itself and threw when it could not, so it died the moment it stopped being root. Both call sites already preferred process.env, which means the file had only ever been a fallback that no longer needs to succeed. It now treats a permission error as an empty result and lets systemd supply the values as root.

A policy nothing calls is a comment

lib/hardening.js has had a role policy and a requireRole predicate for a while, with unit tests. One line of that policy read:

paymentsRefund: [ROLES.OWNER], // moves money out; not yet built, gate it when it is

Refunds are built now. And there were zero calls to requireRole anywhere in server.js. The policy was written, unit tested and correct, and no route had ever invoked it, so the owner-only rule for refunds and revenue reporting was not actually being enforced by anything. The app has only ever had two accounts, mine and the owner's, and no staff account has ever existed on it, which is the only reason this is a finding from a hardening pass rather than an incident. But a gate that exists in a module is not a gate that is applied, and I found it while writing down what a second developer would be able to reach, not because anything went wrong.

Five call sites now, all using the policy that was already written: the payments list, payment detail, the CSV export, reports, and the refund POST. Manual payment entry is deliberately not gated, because taking money in is a front desk job and only moving it out is not. Denial is an explanatory 403 rather than a 404, so somebody who reaches the refund form learns that only an owner can issue one instead of concluding the app is broken. The nav hides owner only entries so nobody is shown a link that will 403 them, and the nav filter is presentation while the server gate is the boundary.

Verified with a real temporary front desk account, then deleted. The first run of that test was a false pass: I used the wrong cookie name and read the 302 login redirect as success. The check now demands a 200.

What I learned

A permission you can escalate from is the permission you granted. sudo cp -a reads the file with the payment token and writes the file that defines who can do what. Handing that over is handing over root, so the useful question is never "what does this command do" but "what is the most this command can be turned into."

A policy that nothing calls is a comment. The refund rule was written, unit tested, and correct, and it protected nothing for months because no route ever invoked it. Its own comment said to gate refunds when they were built, and refunds got built without anyone reading the comment. Mechanisms are cheap. The call sites are the security.

Dropping privileges finds the code that was quietly relying on them. Running as root hides every assumption you made about being able to read a file. The notification tick had been reading a secret off disk purely because it could, and the fix was not new permissions, it was accepting that the read is allowed to fail.

If you are the only person who can safely touch your production box, that is a bus factor, and it is the kind of thing Autosterea fixes before it matters.

First published on ravikantdewangan.com.

Tell us what eats your week.

A free 20-minute call. We'll show you what we would take off your plate.

Book a call