This library uses unsafe code to interface with Linux kernel APIs. All unsafe blocks have been reviewed for memory safety.
getpid()calls (lines 67, 164): Safe - Simple FFI call to get process ID for fanout group ID generationmmap()call (line 209): Reviewed - Creates memory-mapped region for ring buffer. Pointer validity checked before use.mem::transmutefor sockaddr (line 247): Reviewed - Required for socket API compatibility. Transmutes between sockaddr_ll and sockaddr, which is the standard pattern for BSD socket APIs.bind()call (line 249): Safe - Standard socket binding operation with properly initialized sockaddr structure.poll()call (line 263): Safe - Standard I/O polling operation.std::slice::from_raw_parts_mut()(line 273): Critical - Creates mutable slice from mmap'd memory. Safety relies on:- Valid pointer from successful mmap
- Correct size calculation
- No aliasing (enforced by Ring's &mut self methods)
unsafe impl Send for Ring(line 297): Reviewed - Safe because Ring owns its resources and file descriptors are thread-safe in Linux.
socket()call (line 88): Safe - Standard socket creation. Error handling in place.ioctl()call (line 103): Safe - Standard ioctl operation with properly boxed IfReq structure.setsockopt()call (line 123): Safe - Standard socket option setting with correct size calculation.getsockopt()call (line 144): Safe - Standard socket option retrieval with proper pointer handling.if_nametoindex()call (line 152): Safe - Standard interface name lookup with CString validation.
mem::transmutefor sockaddr (line 37): Same as rx.rs - standard pattern for socket APIs.sendto()call (line 39): Safe - Standard packet transmission. Usesframe.len()to get correct packet size andframe.as_ptr()for proper pointer handling.
This library requires the following Linux capabilities:
CAP_NET_RAW- Required for raw packet captureCAP_NET_ADMIN- May be required for setting promiscuous mode
- No bounds checking on packet data: Users must validate packet lengths before parsing.
- File descriptor leaks: Ring buffers should be properly dropped to avoid FD leaks.
- Memory mapping: Failed mmap operations are properly handled, but long-running applications should monitor memory usage.
- Input Validation: Always validate interface names and configuration parameters before creating Ring instances.
- Error Handling: Check all Result types, especially when creating Ring instances.
- Resource Cleanup: Ensure Ring instances are properly dropped or use RAII patterns.
- Testing: Test with various network conditions and interface states.
- Monitoring: Monitor for packet drops using
get_rx_statistics().
Security issues should be reported to the repository maintainers at: https://github.com/P4X-ng/rs-af_packet/security/advisories