____ ____ _____ __ __ _ __ _
| __ )| _ \| ___| \ \ / /__ _ __(_)/ _(_) ___ _ __
| _ \| |_) | |_ \ \ / / _ \ '__| | |_| |/ _ \ '__|
| |_) | __/| _| \ V / __/ | | | _| | __/ |
|____/|_| |_| \_/ \___|_| |_|_| |_|\___|_|
#๐ English
Ever wondered what it takes to verify that a piece of eBPF code won't crash your kernel? Well, you're looking at it!
This is a from-scratch Rust implementation of the BPF verifier - the gatekeeper that decides whether your eBPF programs are safe enough to run in kernel space. No C code, no FFI nightmares, just pure Rust goodness with #![no_std] compatibility.
Because I was curious. And because Rust makes systems programming fun again.
The Linux kernel's BPF verifier is a ~30,000 line C beast. I thought: "What if I could have all that power, but with Rust's safety guarantees?" So here it is.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ญ The Magic Architecture โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Your Platform โโโโโโโโโโถโ bpf-verifier-core โ โ
โ โ (Linux, Your โ traits โ (the brain ๐ง ) โ โ
โ โ own OS, etc) โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Want to run BPF on your own OS? Just implement the โ
โ PlatformSpec trait. That's it. No kidding. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Crate | What it does | Vibe |
|---|---|---|
bpf-verifier-core |
The platform-agnostic brain | ๐ง Pure logic |
bpf-verifier-linux |
Linux-specific stuff | ๐ง Penguin approved |
bpf-verifier |
Convenience re-exports | ๐ Easy mode |
# Clone it
git clone https://github.com/anthropics/verifier-rs
cd verifier-rs
# Build it
cargo build --release
# Test it (I have tests, lots of them)
cargo test --workspace
# Feeling fancy? Check for lint
cargo clippy --workspaceUsing with Linux:
use bpf_verifier_core::verifier::{GenericVerifierEnv, GenericMainVerifier};
use bpf_verifier_linux::LinuxSpec;
// Create the platform - Linux in this case
let platform = LinuxSpec::new();
// Your BPF program (the instructions you want to verify)
let insns = vec![/* your BPF instructions here */];
// Set up the verifier environment
let mut env = GenericVerifierEnv::new(
platform,
insns,
6, // program type (XDP in this case)
false, // allow_ptr_leaks (usually false unless you're privileged)
)?;
// Let's verify! ๐
let mut verifier = GenericMainVerifier::new(&mut env);
verifier.verify()?;
println!("โ
Your program is safe!");Building your own platform:
use bpf_verifier_core::platform::*;
// Your custom platform - maybe for your own OS?
#[derive(Clone)]
struct MyAwesomeOS {
helper: MyHelperProvider,
// ... other providers
}
impl PlatformSpec for MyAwesomeOS {
type Helper = MyHelperProvider;
type ProgType = MyProgTypeProvider;
type Kfunc = MyKfuncProvider;
type Map = MyMapProvider;
type Context = MyContextProvider;
fn name(&self) -> &'static str { "my-awesome-os" }
// implement the rest...
}
// Now use it!
let platform = MyAwesomeOS::new();
let mut env = GenericVerifierEnv::new(platform, insns, prog_type, false)?;The secret sauce that makes this all work:
| Trait | What it's for | Example |
|---|---|---|
PlatformSpec |
The main combo trait | Ties everything together |
HelperProvider |
BPF helper functions | bpf_map_lookup_elem, etc. |
ProgTypeProvider |
Program types | XDP, kprobe, tracepoint... |
KfuncProvider |
Kernel functions | The new hotness |
MapProvider |
Map types | HashMap, Array, RingBuf... |
ContextProvider |
Context structures | What's in R1 when you start |
- Register tracking: All 11 registers, with types and bounds. I know exactly what's in each one.
- Memory safety: Stack, maps, packets, context - I check 'em all.
- Control flow: Every path explored. No shortcuts.
- Reference tracking: Acquired a lock? I'll make sure you release it.
- State pruning: Smart equivalence checking so I don't explore the same state twice.
verifier-rs/
โโโ crates/
โ โโโ bpf-verifier-core/ # ๐ง The brain
โ โโโ bpf-verifier-linux/ # ๐ง Linux specifics
โ โโโ bpf-verifier/ # ๐ Easy imports
โโโ docs/
โ โโโ CHANGELOG.md # ๐ What's new
โ โโโ PERFORMANCE.md # โก Speed stuff
โ โโโ UNSAFE_AUDIT.md # ๐ Safety report
โโโ README.md # ๐ You are here
| Doc | What's inside |
|---|---|
| CHANGELOG | The journey so far |
| PERFORMANCE | Numbers that go brrr |
| UNSAFE_AUDIT | My unsafe code confessions |
- Rust 1.82.0+ (I use some nice features)
#![no_std]compatible (no OS needed!)alloccrate (I do need some heap though)
GPL-2.0-only - Because I believe in freedom.
Found a bug? Have an idea? PRs and issues are welcome!
This project was born from curiosity and a love for Rust. Every contribution, no matter how small, makes it better.
Built with ๐ and lots of โ by MCB-SMART-BOY
A sophomore student who just really likes BPF and Rust.
ๆๆฒกๆๆณ่ฟ๏ผๆๆ ทๆ่ฝ้ช่ฏไธๆฎต eBPF ไปฃ็ ไธไผๆๅ ๆ ธๆๅดฉ๏ผไฝ ็ฐๅจ็ๅฐ็ๅฐฑๆฏ็ญๆก๏ผ
่ฟๆฏไธไธชไป้ถๅผๅง็จ Rust ๅ็ BPF ้ช่ฏๅจโโๅฎ่ด่ดฃๅณๅฎไฝ ็ eBPF ็จๅบๆฏไธๆฏ่ถณๅคๅฎๅ
จใ่ฝไธ่ฝๅจๅ
ๆ ธ้่ทใๆฒกๆ C ไปฃ็ ๏ผๆฒกๆ FFI ้ฃไบ็ ดไบๅฟ๏ผๅฐฑๆฏ็บฏ็บฏ็ Rust๏ผ่ไธ่ฟๆฏๆ #![no_std]ใ
ๅ ไธบๅฅฝๅฅๅใ่ไธ Rust ่ฎฉ็ณป็ป็ผ็จ้ๆฐๅๅพๆๆๆไบใ
Linux ๅ ๆ ธ็ BPF ้ช่ฏๅจๆฏไธชๅคง็บฆ 30,000 ่ก็ C ไปฃ็ ๆชๅ ฝใๆๅฝๆถๆณ๏ผ"่ฆๆฏ่ฝๆ่ฟไบ่ฝๅ้ฝๆฟ่ฟๆฅ๏ผ่ฟ่ฝไบซๅ Rust ็ๅฎๅ จไฟ่ฏๅข๏ผ" ไบๆฏๅฐฑๆไบ่ฟ็ฉๆๅฟใ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ญ ๆถๆ็้ญๆณ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ ไฝ ็ๅนณๅฐ โโโโโโโโโโถโ bpf-verifier-core โ โ
โ โ (Linux, ไฝ ่ชๅทฑ โ traits โ (ๅคง่ ๐ง ) โ โ
โ โ ็OS, ้ไพฟๅฅ) โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ ๆณๅจ่ชๅทฑ็ๆไฝ็ณป็ปไธ่ท BPF๏ผๅฎ็ฐไธไธ PlatformSpec โ
โ trait ๅฐฑ่กใๅฐฑ่ฟไน็ฎๅ๏ผๆฒก้ชไฝ ใ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Crate | ๅนฒๅฅ็ | ๆ่ง |
|---|---|---|
bpf-verifier-core |
ๅนณๅฐๆ ๅ ณ็ๅคง่ | ๐ง ็บฏ้ป่พ |
bpf-verifier-linux |
Linux ไธๅฑ็ไธ่ฅฟ | ๐ง ไผ้น ่ฎค่ฏ |
bpf-verifier |
ๆนไพฟๅฏผๅ ฅ็้ๅฏผๅบ | ๐ ็ฎๅๆจกๅผ |
# ๅ
้ไธๆฅ
git clone https://github.com/anthropics/verifier-rs
cd verifier-rs
# ็ผ่ฏ
cargo build --release
# ่ทๆต่ฏ๏ผๆๅไบไธๅ ๆต่ฏ๏ผ
cargo test --workspace
# ๆณๆดไธไธ็น๏ผ่ทไธช lint
cargo clippy --workspace็จ Linux ๅนณๅฐ๏ผ
use bpf_verifier_core::verifier::{GenericVerifierEnv, GenericMainVerifier};
use bpf_verifier_linux::LinuxSpec;
// ๅๅปบๅนณๅฐ - ่ฟ้็จ Linux
let platform = LinuxSpec::new();
// ไฝ ็ BPF ็จๅบ๏ผ่ฆ้ช่ฏ็ๆไปค๏ผ
let insns = vec![/* ไฝ ็ BPF ๆไปค */];
// ่ฎพ็ฝฎ้ช่ฏๅจ็ฏๅข
let mut env = GenericVerifierEnv::new(
platform,
insns,
6, // ็จๅบ็ฑปๅ๏ผ่ฟ้ๆฏ XDP๏ผ
false, // allow_ptr_leaks๏ผ้ค้ไฝ ๆฏ็นๆ็จๆท๏ผไธ็ถไธ่ฌๆฏ false๏ผ
)?;
// ๅผๅง้ช่ฏ๏ผ๐
let mut verifier = GenericMainVerifier::new(&mut env);
verifier.verify()?;
println!("โ
ไฝ ็็จๅบๆฏๅฎๅ
จ็๏ผ");ๆไธช่ชๅทฑ็ๅนณๅฐ๏ผ
use bpf_verifier_core::platform::*;
// ไฝ ็่ชๅฎไนๅนณๅฐ - ไน่ฎธๆฏ็ปไฝ ่ชๅทฑ็ๆไฝ็ณป็ป๏ผ
#[derive(Clone)]
struct MyAwesomeOS {
helper: MyHelperProvider,
// ... ๅ
ถไป provider
}
impl PlatformSpec for MyAwesomeOS {
type Helper = MyHelperProvider;
type ProgType = MyProgTypeProvider;
type Kfunc = MyKfuncProvider;
type Map = MyMapProvider;
type Context = MyContextProvider;
fn name(&self) -> &'static str { "my-awesome-os" }
// ๅฎ็ฐๅฉไธ็...
}
// ็จ่ตทๆฅ๏ผ
let platform = MyAwesomeOS::new();
let mut env = GenericVerifierEnv::new(platform, insns, prog_type, false)?;่ฎฉ่ฟไธๅ่ฟ่ฝฌ็็งๅฏๆญฆๅจ๏ผ
| Trait | ๅนฒๅฅ็จ็ | ไธพไธชไพๅญ |
|---|---|---|
PlatformSpec |
ไธป trait๏ผๆๆๆไธ่ฅฟไธฒ่ตทๆฅ | ็ปๅๅจ |
HelperProvider |
BPF helper ๅฝๆฐ | bpf_map_lookup_elem ไน็ฑป็ |
ProgTypeProvider |
็จๅบ็ฑปๅ | XDP, kprobe, tracepoint... |
KfuncProvider |
ๅ ๆ ธๅฝๆฐ | ๆฐ็ฉๆๅฟ |
MapProvider |
Map ็ฑปๅ | HashMap, Array, RingBuf... |
ContextProvider |
ไธไธๆ็ปๆ | ๅฏๅจๆถ R1 ้่ฃ ็ๅฅ |
- ๅฏๅญๅจ่ฟฝ่ธช๏ผๅ จ้จ 11 ไธชๅฏๅญๅจ๏ผๅธฆ็ฑปๅๅ่พน็ใๆ็ฒพ็กฎ็ฅ้ๆฏไธช้้ขๆฏๅฅใ
- ๅ ๅญๅฎๅ จ๏ผๆ ใmapใๆฐๆฎๅ ใไธไธๆโโๅ จ้ฝๆฃๆฅใ
- ๆงๅถๆต๏ผๆฏๆก่ทฏๅพ้ฝ่ตฐไธ้ใไธๅทๆใ
- ๅผ็จ่ฟฝ่ธช๏ผๆฟไบ้๏ผๆไผ็กฎไฟไฝ ้ๆพใ
- ็ถๆๅชๆ๏ผๆบ่ฝ็็ญไปทๆงๆฃๆฅ๏ผๅๆ ท็็ถๆไธไผ่ตฐไธค้ใ
verifier-rs/
โโโ crates/
โ โโโ bpf-verifier-core/ # ๐ง ๅคง่
โ โโโ bpf-verifier-linux/ # ๐ง Linux ็ธๅ
ณ
โ โโโ bpf-verifier/ # ๐ ๆนไพฟๅฏผๅ
ฅ
โโโ docs/
โ โโโ CHANGELOG.md # ๐ ๆดๆฐๆฅๅฟ
โ โโโ PERFORMANCE.md # โก ๆง่ฝๆฐๆฎ
โ โโโ UNSAFE_AUDIT.md # ๐ ๅฎๅ
จๆฅๅ
โโโ README.md # ๐ ไฝ ๅจ่ฟๅฟ
| ๆๆกฃ | ้้ขๆๅฅ |
|---|---|
| CHANGELOG | ไธ่ทฏ่ตฐๆฅ็ๅ็จ |
| PERFORMANCE | ่ทๅๆฐๆฎ |
| UNSAFE_AUDIT | unsafe ไปฃ็ ็ไบคไปฃ |
- Rust 1.82.0+๏ผ็จไบไธไบๆฐ็นๆง๏ผ
#![no_std]ๅ ผๅฎน๏ผไธ้่ฆๆไฝ็ณป็ป๏ผ๏ผalloccrate๏ผไฝ็กฎๅฎ้่ฆ็นๅ ๅ ๅญ๏ผ
GPL-2.0-only - ๅ ไธบๆไฟก่ช็ฑใ
ๅ็ฐ bug ไบ๏ผๆๆณๆณ๏ผๆฌข่ฟๆ PR ๅ issue๏ผ
่ฟไธช้กน็ฎๆบไบๅฅฝๅฅๅฟๅๅฏน Rust ็็ญ็ฑใๆฏไธไปฝ่ดก็ฎ๏ผไธ็ฎกๅคๅฐ๏ผ้ฝ่ฝ่ฎฉๅฎๅๅพๆดๅฅฝใ
็จ ๐ ๅไธๅ โ ๆ้ ๏ผไฝ่ MCB-SMART-BOY
ไธไธชๅฐฑๆฏๅพๅๆฌข BPF ๅ Rust ็ๅคงไบๅญฆ็ใ