FAQ - Help and questions

How do I run a Rust service with least privilege?

#security#deployment#least-privilegepublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Run the service under its own unprivileged account, give it access only to the folders it needs, open files with the minimum access mode, keep secrets readable by that account alone, and use the operating system's service controls to restrict the process. A flaw then reaches only what the service could reach anyway.

Checked with rustc 1.93.0, tempfile 3.27.0, Windows 11 (the code only; the operating-system settings are described, not run)

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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):

MeasureWhy
A dedicated service account, not an administrator or rootA flaw does not give system control
Folder permissions: read for programs and settings, write only for the data and log foldersStops 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 443Avoids running with the power to bind low ports
A secrets file readable by the service account onlyOther 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 needsLimits movement and data leaving
Containers or sandboxes with a read-only root file system, where practicalSame 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.

src/main.rsrust
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(())
}
text
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#

  1. Write down what the service needs: folders to read, folders to write, ports, other services.
  2. Create the account and set permissions to exactly that.
  3. Test as that account, not as an administrator: missing permissions show up as errors in the log, and each one is a decision.
  4. Give the database account only the statements the service uses (no DROP, no access to other databases).
  5. 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.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet