How trifectatechfoundation/sudo-rs works
Project type and purpose
• sudo-rs is a systems-level CLI tool, a drop-in replacement for Unix’s sudo, su, and visudo commands.
• Written in Rust, it focuses on safety and memory correctness, targeting Linux 5.9+ and leveraging PAM for authentication.
High-level architecture
┌───────────────────┐ ┌───────────────────────┐ ┌──────────────────────────┐
│ User / Shell │ ──> │ sudo-rs / su-rs / │ ──> │ Core Library (src/) │
│ (CLI invocation) │ │ visudo-rs binaries │ │ – cli modules │
└───────────────────┘ └───────────────────────┘ │ – common, defaults │
│ – parser (sudoers) │
│ – policy engine │
│ – authentication (PAM) │
│ – execution (PTY, sys) │
│ – apparmor integration │
│ – logging (syslog) │
└──────────────────────────┘
│
┌────────────────────────────────────────────────┴──────────────────────────────────┐
│ │
┌──────────────────┐ ┌──────────────────┐
│ Linux Kernel │ │ External files │
│ & Syscalls │<─── exec / system abstractions <───┐ │ & services: │
│ (fork, execve, │ │ │ /etc/sudoers, │
│ chown, pty, │ ├── PAM library │ /etc/pam.d │
│ signal, audit) │ │ │ config files │
└──────────────────┘ │ └──────────────────┘
│
┌────────┴────────┐
│ PAM Library │
│ (libpam.so) │
└─────────────────┘
Main components & interactions
a. Entry points (bin/):
• bin/sudo.rs, bin/su.rs, bin/visudo.rs – thin wrappers that parse CLI flags and hand off to core.
b. Core library (src/lib.rs + submodules):
• cli modules (src/sudo/cli, src/su/cli, src/visudo/cli) – argument parsing & help text.
• defaults (src/defaults) – compile-time and run-time default settings.
• config parser (src/sudoers/*) – lexing, parsing, AST, policy models.
• policy evaluation (src/sudoers/policy.rs) – decides if action is permitted.
• authentication (src/pam/*) – conversing with PAM, password input, secure memory.
• execution (src/exec/*) – spawning the target process, with or without PTY, backchannels, monitoring.
• system abstraction (src/system/*) – file operations, chown, locking, signals, tempdir, timestamps, auditing.
• sandboxing (src/apparmor/*) – AppArmor confinement support on Linux.
• logging (src/log/*) – simple logger to stderr or syslog sink.
c. External interactions:
• Linux kernel syscalls (fork, execve, setuid, ioctl for pty, signals) via system/*.
• PAM (via FFI to libpam, configured by service names “sudo”/“sudo-i”).
• Reading /etc/sudoers[-rs] and /etc/pam.d/* files.
• Writing audit logs and syslog entries.
Architectural patterns and design principles
• Modular separation: CLI, parsing, policy, auth, execution, system layer, logging.
• Shared library core: lib.rs provides reusable building blocks for all three binaries.
• Strategy pattern in exec/use_pty vs exec/no_pty.
• Clear boundaries between parsing (pure Rust code) and system side-effects (isolated in system/*).
• Safety-first: no unsafe except narrow FFI (PAM), AppArmor syscalls.