Answer#
No program is free of bugs, so the useful question is: what can an attacker do once they control this process? With least privilege the answer is "only what the service was allowed to do". The measures are mostly outside the code, with a few habits inside it.
In the operating system (described here, not tested for this thread):
| Measure | Why |
|---|---|
| A dedicated service account, not an administrator or root | A flaw does not give system control |
| Folder permissions: read for programs and settings, write only for the data and log folders | Stops a flaw from changing the program itself |
| Listen on a port above 1023 and put a reverse proxy in front, if you need 80 or 443 | Avoids running with the power to bind low ports |
| A secrets file readable by the service account only | Other accounts cannot read it |
Service-manager restrictions (on Linux with systemd, options such as NoNewPrivileges=, ProtectSystem=, PrivateTmp= and ReadWritePaths= are documented in systemd.exec; on Windows, service accounts with limited privileges) | The platform enforces limits even if the program is compromised |
| Network rules that allow only the connections the service needs | Limits movement and data leaving |
| Containers or sandboxes with a read-only root file system, where practical | Same idea, packaged |
In the code: open files with the least access they need. The program below opens a configuration file for reading only, shows that writing through that handle fails, and uses create_new for a log file so an existing file is never silently reused.
use std::fs::{self, OpenOptions};
use std::io::Write;
fn main() -> std::io::Result<()> {
let dir = tempfile::tempdir()?;
let path = dir.path().join("app.conf");
fs::write(&path, "port = 8080\n")?;
// The service only needs to read its configuration: ask for read access and nothing else.
let mut read_only = OpenOptions::new().read(true).open(&path)?;
match read_only.write_all(b"port = 1\n") {
Ok(()) => println!("write succeeded (unexpected)"),
Err(e) => println!("write through a read-only handle failed: {:?}", e.kind()),
}
// Data files are written by this process alone; create_new refuses to reuse an existing file.
let log = dir.path().join("audit.log");
let first = OpenOptions::new().append(true).create_new(true).open(&log);
println!("first create: {}", first.is_ok());
Ok(())
}write through a read-only handle failed: PermissionDenied
first create: true(The error kind shown is what Windows 11 reported; other platforms may name the error differently.)
How to do it#
- Write down what the service needs: folders to read, folders to write, ports, other services.
- Create the account and set permissions to exactly that.
- Test as that account, not as an administrator: missing permissions show up as errors in the log, and each one is a decision.
- Give the database account only the statements the service uses (no
DROP, no access to other databases). - Review the list when features are added.
Common mistakes#
- Running as an administrator "because it works". It works because nothing is checked.
- One shared database login for all applications.
- A writable program folder, so a flaw can replace the executable.
Related questions#
Should a Rust program drop privileges after start-up?#
It can, but starting without them is simpler and safer: let the service manager bind the port or hand over the socket, and run the program unprivileged from the first instruction.
Is a container enough?#
A container limits what the process sees, but a process that runs as root inside it, or with extra capabilities, can still do more than it needs. Combine it with an unprivileged user.
Sources#
- OWASP Secure Product Design Cheat Sheet, "Least Privilege": https://cheatsheetseries.owasp.org/cheatsheets/Secure_Product_Design_Cheat_Sheet.html
- systemd.exec(5): https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html
- Standard library,
OpenOptions: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html