secbuf
Secure, high-performance buffer management for Rust with automatic memory zeroing and aggressive cleanup.
secbuf provides memory-safe buffers that automatically zero their contents on drop, preventing sensitive data from lingering in memory. Inspired by Dropbear SSH's buffer management patterns, it's designed for security-critical applications like network servers, cryptographic systems, and protocol implementations.
โจ Features
- ๐ Automatic Secure Zeroing - All buffers use compiler-resistant memory clearing via
zeroize - โก High Performance - Lock-free buffer pools with thread-local caching (~5ns allocation)
- ๐ฏ Zero-Copy Operations - Efficient circular buffers for streaming data
- ๐งน Aggressive Memory Management - Detect and reclaim idle buffers automatically
- ๐ Connection Lifecycle - Built-in connection-scoped buffer management
- ๐ SIMD Acceleration - Optional AVX2 support for bulk operations (x86_64)
- ๐ Memory Diagnostics - Detailed statistics and waste detection
๐ Quick Start
Add to your Cargo.toml:
[]
= "0.1"
Basic Usage
use *;
Buffer Pooling
use *;
use Arc;
// Create a buffer pool
let pool = new;
// Acquire buffers (automatically returned on drop)
let mut buf1 = pool.acquire;
buf1.put_bytes?;
// Buffers are reused, not reallocated
let mut buf2 = pool.acquire;
// Check pool statistics
let stats = pool.stats;
println!;
Circular Buffers for Streaming
use CircularBuffer;
// Create a circular buffer with lazy allocation
let mut ring = new;
// Write data
ring.write?;
ring.write?;
// Read data
let mut output = vec!;
ring.read?;
assert_eq!;
Connection Management
use ;
// Create connection-scoped buffers
let mut conn = with_config;
// Initialize buffers as needed
conn.init_read_buf;
conn.init_write_buf;
conn.add_stream_buf;
// Automatic cleanup on drop with secure zeroing
๐๏ธ Architecture
Buffer Types
| Type | Use Case | Key Features |
|---|---|---|
Buffer |
Linear read/write | Position tracking, SSH-style strings, SIMD support |
CircularBuffer |
Streaming I/O | Lazy allocation, wrap-around, zero-copy |
BufferPool |
Standard pooling | Mutex-based, simple, reliable |
FastBufferPool |
High-throughput | Lock-free, thread-local cache, 10-20x faster |
ConnectionBuffers |
Connection lifecycle | Multiple buffers, packet queue, diagnostics |
Memory Management Levels
- Automatic - Secure zeroing on drop (always enabled)
- Periodic - Idle detection and shrinking (configurable)
- Aggressive - Force cleanup on memory pressure
๐ Examples
SSH-Style Protocol Buffer
use Buffer;
// Write an SSH-style packet
let mut buf = new;
buf.put_string?; // Method name
buf.put_string?; // Username
buf.put_string?; // Service
buf.put_string?; // Auth type
buf.put_byte?; // Password change flag
buf.put_string?; // Password
// Read it back
buf.set_pos?;
let method = buf.get_string?;
let username = buf.get_string?;
// ... process packet
// Password is automatically zeroed when buf drops
High-Performance Server
use ;
use Arc;
use thread;
let pool = new;
// Spawn worker threads
let handles: = .map.collect;
for h in handles
// Check performance stats
let stats = pool.stats;
println!;
println!;
Memory Diagnostics
use ConnectionBuffers;
let mut conn = new;
conn.init_read_buf;
// Check memory usage
let stats = conn.memory_usage;
println!;
println!;
println!;
println!;
if stats.is_problematic
๐ Security Guarantees
Automatic Memory Zeroing
All buffers are zeroed on drop using the zeroize crate:
// <- Memory is securely zeroed here (compiler can't optimize it away)
Explicit Cleanup
For extra control:
let mut buf = new;
buf.put_bytes?;
// Immediate secure zeroing
buf.burn;
// Or zero + free memory
buf.burn_and_free_memory;
Connection Cleanup
let mut conn = new;
conn.init_read_buf;
// ... use buffers
// Secure cleanup
conn.burn; // Zero all data
conn.aggressive_cleanup; // Zero + free all memory
โก Performance
Buffer Pool Comparison
| Operation | BufferPool |
FastBufferPool |
Speedup |
|---|---|---|---|
| Acquire (cache hit) | ~100ns | ~5ns | 20x |
| Acquire (pool hit) | ~100ns | ~20ns | 5x |
| Acquire (miss) | ~100ns | ~100ns | 1x |
SIMD Acceleration
On x86_64 with AVX2:
// Automatically uses AVX2 if available
buf.put_bytes_fast?; // 2-3x faster for 1KB+ data
๐งช Testing
Run the test suite:
Run with all features:
Benchmark:
๐ Documentation
๐ ๏ธ Feature Flags
Currently, all features are enabled by default. Future versions may add:
simd- SIMD acceleration (planned)anyhow- Error conversion support (planned)
๐ค Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure
cargo testpasses - Submit a pull request
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
๐ Acknowledgments
- Inspired by Dropbear SSH's secure buffer management
- Built on the excellent
zeroizecrate by RustCrypto - Lock-free queues via
crossbeam
๐ Related Projects
bytes- General-purpose buffer managementzeroize- Secure memory zeroingbuffer-pool- Simple buffer pooling
Made with โค๏ธ for the Rust community