FAQ - Help and questions

How do I check my Rust dependencies for known vulnerabilities with cargo audit?

#security#cargo-audit#supply-chain#rustsecpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Install cargo-audit and run cargo audit in the project folder. It reads Cargo.lock and compares every crate version with the RustSec advisory database, listing vulnerabilities and warnings with a fixed version where one exists. Run it in CI on every change and on a schedule, because new advisories appear for old code.

Checked with cargo-audit 0.22.2, rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

The RustSec project keeps a public database of security advisories for crates published on crates.io. cargo audit downloads that database and checks the versions pinned in your Cargo.lock against it. It tells you what is affected, how serious it is, and which version fixes it.

text
cargo install cargo-audit --locked
cargo audit

A project with no known problems prints a summary and exits with status 0:

text
    Fetching advisory database from `https://github.com/RustSec/advisory-db.git`
      Loaded 1617 security advisories (from C:\Users\...\.cargo\advisory-db)
    Updating crates.io index
    Scanning Cargo.lock for vulnerabilities (16 crate dependencies)

(The number of advisories in the database changes constantly; the number here is what it held when this thread was checked.)

To show what a finding looks like, the next run is on a small throw-away project that pins time = "=0.1.45", a version with a real advisory (RUSTSEC-2020-0071). The output is shortened:

text
    Scanning Cargo.lock for vulnerabilities (7 crate dependencies)
Crate:     time
Version:   0.1.45
Title:     Potential segfault in the time crate
Date:      2020-11-18
ID:        RUSTSEC-2020-0071
URL:       https://rustsec.org/advisories/RUSTSEC-2020-0071
Severity:  6.2 (medium)
Solution:  Upgrade to >=0.2.23

error: 1 vulnerability found!

cargo audit exits with a non-zero status when it finds a vulnerability, so a CI job fails by itself. The advisory text on rustsec.org says that the problem affects non-Windows targets; read the advisory, not just the title, to judge how it applies to you.

How to do it#

  1. Commit Cargo.lock for applications (see the thread on Cargo.lock), because the audit reads it.
  2. Run cargo audit in CI on every push and on a daily or weekly schedule: an unchanged project can become vulnerable overnight.
  3. Fix by updating: cargo update -p <crate> for a compatible release, or raise the version in Cargo.toml for a breaking one.
  4. If a crate is unmaintained or yanked, cargo audit reports it as a warning. Plan a replacement.
  5. If you cannot fix an advisory yet, record the reason; see the thread on handling advisories you cannot fix.

Common mistakes#

  • Auditing only before a release.
  • Treating "no vulnerabilities found" as "safe". The database lists what has been reported.
  • Ignoring warnings. Unmaintained crates often become vulnerabilities later.

Does it scan my own code?#

No. It checks dependencies by name and version. Use code review, tests, Clippy and fuzzing for your own code.

Where does the database come from?#

It is the RustSec advisory-db repository on GitHub, maintained by the Rust Secure Code working group and contributors.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet