Abstract
Rust's memory safety is primarily enforced by its "Borrow Checker," which manages ownership and borrowing. However, a significant security gap exists in unsafe code where developers must provide manual Lifetime Annotations (LAs). The Rust compiler trusts these annotations as ground truth, creating an "escape hatch" where incorrect manual contracts can lead to critical vulnerabilities like use-after-free errors. This paper introduces YUGA, a static analysis tool specifically designed to detect these "lying" annotations. By combining lifetime extraction with flow-sensitive alias analysis, YUGA identifies memory safety violations that standard compiler checks and existing tools fail to catch.
The Challenge of Manual Lifetime Annotations
Rust ensures memory safety by tracking how long variables live (their "lifetime"). While the compiler usually infers these lifetimes automatically, complex functions—particularly those involving unsafe blocks or raw pointers—require developers to manually annotate lifetimes.
The fundamental problem is that the compiler does not verify the correctness of these manual annotations against the actual data flow within the function body. If a developer incorrectly claims a returned pointer outlives the data it points to, the code will compile without warnings, but the resulting binary may contain latent memory safety bugs. Existing tools like Rudra or MirChecker focus on other bug classes, leaving these contract violations largely unaddressed.
The YUGA Analysis Pipeline
YUGA is a multi-phase static analysis tool that operates on Rust’s High-Level Intermediate Representation (HIR) and Mid-Level Intermediate Representation (MIR). It follows a four-step process to validate lifetime contracts:
- Lifetime Extraction (Step I): YUGA analyzes function signatures to determine the "outlives" relationships for every nested value, including those hidden inside structures or behind raw pointers.
- Pattern Matching (Step II): It identifies potential "buggy" patterns where the lifetime of a source value (an argument) is shorter than the lifetime required by the target (another argument or the return value).
- Alias Analysis (Step III): To ensure the bug is real, YUGA performs a flow- and field-sensitive alias analysis on the function body. This confirms whether data actually flows from the "short-lived" source to the "long-lived" target. The authors found that without this step, the tool's precision drops from 87.5% to 34.1%.
- Shallow Filtering (Step IV): Finally, it applies heuristics to filter out common false positives, such as intentional memory leaks or standard library patterns that are semantically safe despite appearing suspicious.
Evaluation and Real-World Impact
The researchers evaluated YUGA using three datasets: the RustSec advisory database (23 real-world vulnerabilities), a synthetic dataset of 27 transformed bugs, and a large-scale scan of the top 2,000 most-downloaded Rust crates.
YUGA demonstrated high efficacy, achieving 87.5% precision and 60.9% recall on known vulnerabilities. When run against popular crates in the wild, YUGA discovered 3 new exploitable bugs in libraries with tens of millions of downloads, including cslice and bv. The tool is efficient enough for practical use, scanning 372 major crates in just under two hours.
Conclusion and Future Work
YUGA effectively addresses a critical blind spot in Rust's safety model by validating the semantic contracts established by manual lifetime annotations. While currently limited by intra-procedural analysis and complex type conversions, it has already proven its worth by identifying high-impact bugs in mature codebases. Future developments will focus on expanding the alias analysis to be inter-procedural and improving the handling of indirect lifetime semantics, further hardening the Rust ecosystem against memory safety violations.
Summary updated