| Crates.io | privdrop |
| lib.rs | privdrop |
| version | 0.5.6 |
| created_at | 2016-11-07 20:40:35.188467+00 |
| updated_at | 2025-05-21 15:33:07.945035+00 |
| description | A simple crate to drop privileges |
| homepage | https://github.com/jedisct1/rust-privdrop |
| repository | https://github.com/jedisct1/rust-privdrop |
| max_upload_size | |
| id | 7158 |
| size | 40,343 |
A comprehensive, secure crate for dropping privileges in Unix-based systems.
The privdrop crate provides a robust, security-focused mechanism for applications that need to drop root privileges safely. This is a critical security practice for services that start with root permissions but need to operate with minimal privileges during execution.
The crate enables processes to:
Add the dependency to your Cargo.toml:
[dependencies]
privdrop = "0.5.5"
This example shows the simplest way to drop privileges:
use privdrop::PrivDrop;
fn main() {
// Application starts with root privileges
PrivDrop::default()
.chroot("/var/empty") // Restrict filesystem access
.user("nobody") // Switch to unprivileged user
.apply() // Apply all changes atomically
.unwrap_or_else(|e| panic!("Failed to drop privileges: {}", e));
// Continue running with dropped privileges...
}
This example demonstrates more complex configurations:
use privdrop::PrivDrop;
fn main() {
PrivDrop::default()
// Basic configuration
.chroot("/var/empty") // Change root directory
.user("service-user") // Switch to non-root user
// Group management
.group("service-group") // Set primary group
.group_list(&["www-data", "logs"]) // Add supplementary groups
.include_default_supplementary_groups() // Include user's default groups
// Fallback options
.fallback_to_ids_if_names_are_numeric() // Allow numeric UIDs/GIDs
// Apply all changes
.apply()
.unwrap_or_else(|e| panic!("Failed to drop privileges: {}", e));
// Continue running with limited privileges...
}
The privilege dropping process is carefully designed to prevent security issues:
This crate is supported on:
For detailed API documentation and more examples, see the API documentation.
Licensed under ISC license, see LICENSE for details.