Agentic Discovery of Soundness Bugs in Safe Rust Bindings
Organizations: University of California, Berkeley
Abstract
Rust crates confine unsafe code, such as calls into C or C++ libraries or system calls, behind safe interfaces that must uphold Rust's invariants. If an interface encodes one rule of the code beneath it too weakly, a program written in purely safe Rust can cause undefined behavior. Static analyzers and fuzzers are bound to fixed patterns or to the paths they execute. We propose LLM agents as auditors of these interfaces, and study how well they do it. We first define a taxonomy of the defects of such interfaces, including undefined behavior, divergences from the wrapped library, and incomplete unsafe contracts. We then measure, in repeated audits of a real-world crate by a closed and an open model, Claude Opus 5.5 and DeepSeek V4.1 Flash, which defect classes agents find, how well they triage them, and what makes them find more. Agents find the first two classes unprompted, but find few incomplete contracts even when asked. Their findings are almost always real, even without an independent reviewer, yet they lack triage discipline: they seldom confirm with a sanitizer, and they stop early. With the same guidance, the open model finds as many defects as the closed one at a quarter of the cost. We integrate these insights in Crustify, an agentic system for automating C-to-Rust tasks, and run it at scale. In 16 widely used crates, including the bindings to the C libraries OpenSSL, SQLite, libgit2, and libcurl and to the C++ engine V8, Crustify found 225 undefined-behavior bugs, 23 behavioral divergences, and 36 incomplete unsafe contracts, between 3 and 61 per crate at a median cost under $50 per audit. In the Linux kernel's Rust abstractions, it found 11 more undefined-behavior bugs and 4 incomplete contracts. Maintainers have fixed 26 of the 83 findings that we reported.
Figures & tables
| Identifier | The crate meets the objective if … | |
|---|---|---|
| ub | no use of the API that meets its documented contracts can cause undefined behavior | |
| contracts | every unsafe function documents conditions under which calling it is sound, and they suffice; every unsafe block says why its conditions hold | |
| equiv | a caller observes what a caller of the wrapped library observes: the same results, errors, output, and side effects | |
| unsafety | no unsafe code exists where safe code can do the same work | |
| correct | it does what its specification intends, also where the library departs from that specification | |
| ergo | the API is hard to misuse, and the library’s conventions do not leak through it |
| Hazard | The rule that is violated |
|---|---|
| Lifetimes | A reference, slice, or handle outlives the memory it refers to, which the code beneath frees or reuses on a later call; or the code beneath keeps a pointer to a Rust value that moves or is dropped. |
| Callbacks, re-entrancy | The code beneath calls into Rust after the closure or its data is gone, from within an operation that the callback re-enters, or on another thread; or a panic unwinds across the boundary. |
| Threads | Send or Sync promises what the wrapped object does not have, or state that must be locked is reached without the lock. |
| Aliasing | A mutable access derives from a shared borrow, or the code beneath writes behind a shared reference (a Stacked or Tree Borrows violation). |
| Untrusted values | A length, index, pointer, or return value crosses the interface unchecked; data is not a valid value of its type, or is read uninitialized. |
| Ownership | An object is freed twice, by the wrong side, or with the wrong deallocator; a once-only operation runs twice; an invariant relies on a destructor that safe code may skip. |
| Kind | What is stored with the finding |
|---|---|
| Reproducer | Where the defect can be triggered: a program that triggers it through the affected API, the command that runs it, and its output, e.g., the report of a sanitizer or of Miri, or an assertion against the behavior of the C library |
| Witness | Where the defect cannot be triggered but can be measured: a change or a measurement that shows the shortfall, with its output |
| Review | Where the defect is a matter of judgment: the record of an agent that did not find the lead, tried to refute it, and failed |
| Distinct real defects | Non-public, found by | Cost | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Model | Guidance | Filed | Public | Not public | 1 | 2 | 3 audits | Minutes | Audit | Defect |
| Closed | none | 30 / 36 / 33 | 20 / 25 / 16 | 15 / 19 / 28 | 32 | 20 | 10 | 16 / 43 / 11 | 9.46 | 0.46 |
| Closed | full | 29 / 34 / 37 | 15 / 18 / 16 | 12 / 18 / 16 | 26 | 14 | 6 | 13 / 18 / 18 | 10.20 | 0.67 |
| Open | none | 34 / 18 / 35 | 24 / 20 / 26 | 18 / 0 / 14 | 23 | 9 | 0 | 21 / 9 / 23 | 0.93 | 0.09 |
| Open | +objectives | 22 / 14 / 17 | 17 / 14 / 13 | 7 / 1 / 5 | 7 | 5 | 1 | 25 / 13 / 34 | 0.47 | 0.11 |
| Open | +hazards | 32 / 38 / 41 | 23 / 24 / 25 | 17 / 17 / 15 | 26 | 17 | 6 | 26 / 26 / 26 | 1.44 | 0.09 |
| Objective | Filed | Real | Nit | Main evidence | Min. | USD |
|---|---|---|---|---|---|---|
| unsafety | 28 | 19 | 9 | patch, tests pass | 56 | 13.48 |
| idiom | 43 | 21 | 22 | lint report, patch | 45 | 11.23 |
| ergo | 56 | 42 | 14 | review, reproducer | 23 | 10.35 |
| Fails a rule | Meets the rules | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Model | Guidance | Filed | False | Unclear | No stored output | Several causes | Repeats a cause | Overstated | Sanitizer | Findings | Share |
| Closed | none | 99 | 0 | 0 | 31 | 21 | 4 | 2 | 3 | 50 | 51% |
| Closed | full | 100 | 1 | 1 | 0 | 9 | 7 | 4 | 48 | 81 | 81% |
| Open | none | 87 | 0 | 0 | 51 | 20 | 3 | 3 | 0 | 27 | 31% |
| Open | +objectives | 53 | 0 | 0 | 28 | 9 | 2 | 2 | 11 | 21 | 40% |
| Open | +hazards | 111 | 0 | 2 | 89 | 17 | 4 | 5 | 0 | 19 | 17% |
| Findings | Fixed | |||||||
|---|---|---|---|---|---|---|---|---|
| Crate | DL | UB | Contr. | Equiv. | Rep. | ours | others | Cost |
| tree-sitter 0.27.0 | 59 | 9 | 0 | 0 | 0 | 0 | 6 | 103 |
| zstd 0.13.3 | 499 | 23 | 0 | 0 | 2 | 2 | 16 | 87 |
| curl 0.4.50 | 50 | 18 | 0 | 0 | 8 | 0 | 3 | 146 |
| openssl 0.10.81 | 471 | 9 | 0 | 0 | 9 | 5 | 0 | 109 |
| aws-lc-rs 1.18.0 | 331 | 2 | 20 | 4 | 2 | 0 | 1 | 310 |
| Crates | ||||||||
|---|---|---|---|---|---|---|---|---|
| Hazard | Study | ASan | TSan | Miri | BSan | rest | all | Kernel |
| Lifetimes | 13 | 40 | 1 | – | – | 19 | 60 | 2 |
| Callbacks, re-entrancy | 1 | 12 | – | – | – | 2 | 14 | – |
| Threads | 8 | – | 20 | – | – | 3 | 23 | – |
| Aliasing | 4 | 2 | – | 4 | 13 | 2 | 21 | 1 |
| Untrusted values | 5 | 42 | 1 | 4 | – | 19 | 66 | 3 |
Appendix figures & tables2 assets
Supplementary material from the paper’s appendix.
Appendix
| Model | Guidance | ub | equiv | contr. | Panic | Leak | Other |
|---|---|---|---|---|---|---|---|
| Closed | none | 15 / 21 / 14 | 2 / 7 / 8 | 0 / 0 / 0 | 8 / 12 / 16 | 4 / 3 / 3 | 6 / 1 / 3 |
| Closed | full | 14 / 17 / 15 | 13 / 18 / 17 | 0 / 1 / 0 | 0 / 0 / 0 | 0 / 0 / 0 | 0 / 0 / 0 |
| Open | none | 18 / 13 / 23 | 4 / 3 / 8 | 0 / 0 / 0 | 11 / 1 / 6 | 5 / 3 / 3 | 4 / 0 / 0 |
| Open | +objectives | 19 / 13 / 16 | 5 / 2 / 2 | 0 / 0 / 0 | 0 / 0 / 0 | 0 / 0 / 0 | 0 / 0 / 0 |
| Open | +hazards | 19 / 20 / 21 | 8 / 4 / 4 | 0 / 0 / 0 | 10 / 13 / 11 | 3 / 4 / 4 | 0 / 0 / 0 |
| Open | +triage | 12 / 14 / 18 | 4 / 3 / 5 | 0 / 0 / 0 | 0 / 9 / 4 | 0 / 3 / 3 | 0 / 0 / 0 |
| Crate | Finding | Class | Instrument | Report | State |
|---|---|---|---|---|---|
| aws-lc-rs | des-weak-key-parity-bypass | Equivalence | assertion | #1300 | open |
| aws-lc-rs | rsa-public-key-components-skip-key-check | Equivalence | assertion | #1299 | open |
| curl | easy-info-getters-uaf-across-perform | UB (safe) | ASan/UBSan | #658 | open |
| curl | easy2-get-ref-uaf-across-perform | UB (safe) | ASan/UBSan | #697 | open |
| curl | easy2-handler-reentrancy-uaf | UB (safe) | ASan/UBSan | #698 | open |
| curl | getinfo-str-uaf-after-perform | UB (safe) | ASan/UBSan | #658 | open |
AI Agent Auditing
AI agent auditing examines agents’ actions, tool use, evidence, and outcomes to assess safety, compliance, reliability, and accountability.
See all 324 papers