rust-security について
コーディングエージェント向けのRustセキュリティ実践スキルです。依存関係の監査、ポリシー検査、安全なFFI、ファジング、Miriによる確認を扱います。
主な機能
- 01cargo-auditによる脆弱性スキャン
- 02cargo-denyによる依存ポリシー適用
- 03RUSTSECセキュリティ勧告の参照
- 04メモリ安全なFFIの作成
- 05ファジングとMiriによる検査
活用シーン
- 01Rustプロジェクトの依存関係の脆弱性確認
- 02CIでのライセンス・ソース規則の適用
- 03RustとCの境界における安全性の監査
- 04Rustライブラリのファジングと未定義動作の確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill rust-securityインストール後、Claude Code で/rust-securityを入力して読み込まれたか確認します。
詳細
Rust セキュリティ
目的
cargo-audit による依存関係の監査、cargo-deny によるポリシーの適用、RUSTSEC アドバイザリデータベース、FFI 向けのメモリ安全なパターン、セキュリティレビューにおけるファジングと Miri の組み合わせなど、Rust のセキュリティ対策をエージェントに案内します。
トリガー
- 「Rust の依存関係に CVE がないか確認するにはどうすればよいですか?」
- 「cargo-audit を使うにはどうすればよいですか?」
- 「CI で依存関係のポリシーを適用するにはどうすればよいですか?」
- 「RUSTSEC アドバイザリデータベースとは何ですか?」
- 「Rust でメモリ安全な FFI を書くにはどうすればよいですか?」
- 「Rust ライブラリのセキュリティ上のバグを見つけるためにファズテストを行うにはどうすればよいですか?」
ワークフロー
1. cargo-audit — 脆弱性スキャン
# Install
cargo install cargo-audit --locked
# Scan current project
cargo audit
# Full output including ignored
cargo audit --deny warnings
# Audit the lockfile (CI-friendly)
cargo audit --file Cargo.lock
# JSON output for CI integration
cargo audit --json | jq '.vulnerabilities.list[].advisory.id'
出力形式:
error[RUSTSEC-2023-0052]: Vulnerability in `vm-superio`
Severity: low
Title: MMIO Register Misuse
Solution: upgrade to `>= 0.7.0`
2. cargo-deny — ポリシーの適用
cargo-deny は監査の範囲を超え、ライセンスポリシーを適用し、特定のクレートを禁止し、ソースの出所を確認し、重複する依存関係のバージョンを検証します。
cargo install cargo-deny --locked
# Initialize deny.toml
cargo deny init
# Run all checks
cargo deny check
# Run specific check
cargo deny check advisories
cargo deny check licenses
cargo deny check bans
cargo deny check sources
deny.toml の設定:
[advisories]
vulnerability = "deny" # Deny known vulnerabilities
unmaintained = "warn" # Warn on unmaintained crates
yanked = "deny" # Deny yanked versions
# Ignore specific advisories
ignore = [
"RUSTSEC-2021-0145", # known false positive for our usage
]
[licenses]
unlicensed = "deny"
allow = [
"MIT", "Apache-2.0", "Apache-2.0 WITH LLVM-exception",
"BSD-2-Clause", "BSD-3-Clause", "ISC", "Unicode-DFS-2016",
]
# Deny GPL for proprietary projects
deny = ["GPL-2.0", "GPL-3.0"]
[bans]
multiple-versions = "warn" # Warn if same crate appears twice
wildcards = "deny" # Deny wildcard dependencies
[[bans.deny]]
name = "openssl" # Force rustls instead
wrappers = ["reqwest"] # Allow if only required by these
[sources]
unknown-registry = "deny"
unknown-git = "deny"
allow-git = [
"https://github.com/my-org/private-crate",
]
GitHub Actions の CI 統合:
- name: Security audit
run: |
cargo install cargo-deny --locked
cargo deny check
3. RUSTSEC アドバイザリデータベース
https://rustsec.org/ の RUSTSEC データベースでは、脆弱性、メンテナンスされていないクレート、安全性に問題のあるコードを追跡しています。
# Browse advisories from CLI
cargo audit --db ~/.cargo/advisory-db fetch
ls ~/.cargo/advisory-db/crates/
# Check a specific advisory
curl https://rustsec.org/advisories/RUSTSEC-2023-0001.json | jq .
# Common categories
# type: vulnerability — exploitable security bug
# type: unmaintained — no longer maintained (supply chain risk)
# type: unsound — documented unsoundness in safe API
# type: yanked — crate version yanked from crates.io
4. メモリ安全な FFI パターン
Rust/C 境界でよくある安全でないコードの原因:
// UNSAFE pattern — raw pointer from C, no lifetime
extern "C" fn process_data(data: *const u8, len: usize) {
// Don't do this — no bounds check, no lifetime guarantee
let slice = unsafe { std::slice::from_raw_parts(data, len) };
}
// SAFE pattern — validate before using
extern "C" fn process_data(data: *const u8, len: usize) -> i32 {
// Validate pointer and length
if data.is_null() || len == 0 || len > 1024 * 1024 {
return -1;
}
// Safety: non-null, len validated, called from C with valid buffer
let slice = unsafe { std::slice::from_raw_parts(data, len) };
do_work(slice);
0
}
// Use safe wrapper crates for common patterns
use nix::unistd::read; // safe POSIX wrappers
use windows::Win32::System::Memory::VirtualAlloc; // safe Windows bindings
5. セキュリティ上のバグを見つけるためのファジング
# cargo-fuzz — libFuzzer-based
cargo install cargo-fuzz
# Initialize
cargo fuzz init
cargo fuzz add my_target
# fuzz/fuzz_targets/my_target.rs
# #![no_main]
# use libfuzzer_sys::fuzz_target;
# fuzz_target!(|data: &[u8]| {
# if let Ok(s) = std::str::from_utf8(data) {
# let _ = my_lib::parse(s);
# }
# });
# Run fuzzing (long-running)
cargo fuzz run my_target
# With sanitizers for security coverage
cargo fuzz run my_target -- -sanitizer=address
# Reproduce a crash
cargo fuzz run my_target artifacts/my_target/crash-xxxx
# Honggfuzz — good for security targets
cargo install honggfuzz
cargo hfuzz run my_target
6. 健全性を確認するための Miri
# Install Miri
rustup +nightly component add miri
# Run tests under Miri
cargo +nightly miri test
# Check for UB in unsafe code
MIRIFLAGS="-Zmiri-disable-isolation -Zmiri-backtrace=full" \
cargo +nightly miri test
# Miri detects:
# - Use-after-free
# - Dangling references
# - Invalid pointer arithmetic
# - Data races (with -Zmiri-tree-borrows)
# - Uninitialized memory reads
7. サプライチェーンの強化
# Pin Cargo.lock in applications (not libraries)
# Always commit Cargo.lock for binaries
# Verify checksums (cargo already does this)
cargo fetch --locked # fails if Cargo.lock doesn't match
# Audit all dependencies including transitive
cargo tree # view full dependency tree
cargo tree -d # show duplicate versions
# Use cargo-vet for peer review of new deps
cargo install cargo-vet
cargo vet # check all deps have been vetted
# Minimal dependency principle
cargo machete # finds unused dependencies
関連スキル
- Miri とサニタイザーの詳細については、
skills/rust/rust-sanitizers-miriを使用してください - ファジング戦略とコーパス管理については、
skills/runtimes/fuzzingを使用してください - 安全でないコードの監査パターンについては、
skills/rust/rust-unsafeを使用してください - Cargo.lock とワークスペースの管理については、
skills/rust/cargo-workflowsを使用してください
コメント
Markdown 対応