Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle program rejection from verifier #31

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions README.md
Expand Up @@ -132,25 +132,26 @@ All these structs implement the same public functions:

```rust
// called with EbpfVmMbuff:: prefix
pub fn new(prog: &'a [u8]) -> EbpfVmMbuff<'a>
pub fn new(prog: &'a [u8]) -> Result<EbpfVmMbuff<'a>, Error>

// called with EbpfVmFixedMbuff:: prefix
pub fn new(prog: &'a [u8],
data_offset: usize,
data_end_offset: usize) -> EbpfVmFixedMbuff<'a>
data_end_offset: usize) -> Result<EbpfVmFixedMbuff<'a>, Error>

// called with EbpfVmRaw:: prefix
pub fn new(prog: &'a [u8]) -> EbpfVmRaw<'a>
pub fn new(prog: &'a [u8]) -> Result<EbpfVmRaw<'a>, Error>

// called with EbpfVmNoData:: prefix
pub fn new(prog: &'a [u8]) -> EbpfVmNoData<'a>
pub fn new(prog: &'a [u8]) -> Result<EbpfVmNoData<'a>, Error>
```

This is used to create a new instance of a VM. The return type is dependent of
the struct from which the function is called. For instance,
`rbpf::EbpfVmRaw::new(my_program)` would return an instance of `struct
rbpf::EbpfVmRaw`. When a program is loaded, it is checked with a very simple
verifier (nothing close to the one for Linux kernel).
rbpf::EbpfVmRaw` (wrapped in a `Result`). When a program is loaded, it is
checked with a very simple verifier (nothing close to the one for Linux
kernel).

For `struct EbpfVmFixedMbuff`, two additional arguments must be passed to the
constructor: `data_offset` and `data_end_offset`. They are the offset (byte
Expand All @@ -161,12 +162,12 @@ do not need those offsets.

```rust
// for struct EbpfVmMbuff, struct EbpfVmRaw and struct EbpfVmRawData
pub fn set_prog(&mut self, prog: &'a [u8])
pub fn set_prog(&mut self, prog: &'a [u8]) -> Result<(), Error>

// for struct EbpfVmFixedMbuff
pub fn set_prog(&mut self, prog: &'a [u8],
data_offset: usize,
data_end_offset: usize)
data_end_offset: usize) -> Result<(), Error>
```

You can use for example `my_vm.set_prog(my_program);` to change the loaded
Expand Down Expand Up @@ -253,7 +254,7 @@ fn main() {
// Instantiate a struct EbpfVmNoData. This is an eBPF VM for programs that
// takes no packet data in argument.
// The eBPF program is passed to the constructor.
let vm = rbpf::EbpfVmNoData::new(prog);
let vm = rbpf::EbpfVmNoData::new(prog).unwrap();

// Execute (interpret) the program. No argument required for this VM.
assert_eq!(vm.prog_exec(), 0x3);
Expand All @@ -280,7 +281,7 @@ fn main() {

// This is an eBPF VM for programs reading from a given memory area (it
// directly reads from packet data)
let mut vm = rbpf::EbpfVmRaw::new(prog);
let mut vm = rbpf::EbpfVmRaw::new(prog).unwrap();

// This time we JIT-compile the program.
vm.jit_compile();
Expand Down Expand Up @@ -321,7 +322,7 @@ fn main() {
}

// This eBPF VM is for program that use a metadata buffer.
let mut vm = rbpf::EbpfVmMbuff::new(prog);
let mut vm = rbpf::EbpfVmMbuff::new(prog).unwrap();

// Here again we JIT-compile the program.
vm.jit_compile();
Expand Down Expand Up @@ -408,7 +409,7 @@ fn main() {
// We must provide the offsets at which the pointers to packet data start
// and end must be stored: these are the offsets at which the program will
// load the packet data from the metadata buffer.
let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50);
let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50).unwrap();

// We register a helper function, that can be called by the program, into
// the VM.
Expand Down
2 changes: 1 addition & 1 deletion examples/load_elf.rs
Expand Up @@ -111,7 +111,7 @@ fn main() {
0x64, 0x66, 0x0au8
];

let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50);
let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50).unwrap();
vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);

let res = vm.prog_exec(packet1);
Expand Down
4 changes: 2 additions & 2 deletions examples/uptime.rs
Expand Up @@ -39,7 +39,7 @@ fn main() {
];

// Create a VM: this one takes no data. Load prog1 in it.
let mut vm = rbpf::EbpfVmNoData::new(prog1);
let mut vm = rbpf::EbpfVmNoData::new(prog1).unwrap();
// Execute prog1.
assert_eq!(vm.prog_exec(), 0x3);

Expand All @@ -51,7 +51,7 @@ fn main() {
// In the following example we use a helper to get the elapsed time since boot time: we
// reimplement uptime in eBPF, in Rust. Because why not.

vm.set_prog(prog2);
vm.set_prog(prog2).unwrap();
vm.register_helper(helpers::BPF_KTIME_GETNS_IDX, helpers::bpf_time_getns);

let time;
Expand Down