12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874 |
- // SPDX-License-Identifier: (Apache-2.0 OR MIT)
- // Derived from uBPF <https://github.com/iovisor/ubpf>
- // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
- // Copyright 2023 Isovalent, Inc. <quentin@isovalent.com>
- //! Virtual machine and JIT compiler for eBPF programs.
- #![doc(
- html_logo_url = "https://raw.githubusercontent.com/qmonnet/rbpf/main/misc/rbpf.png",
- html_favicon_url = "https://raw.githubusercontent.com/qmonnet/rbpf/main/misc/rbpf.ico"
- )]
- // Test examples from README.md as part as doc tests.
- #![doc = include_str!("../README.md")]
- // Configures the crate to be `no_std` when `std` feature is disabled.
- #![cfg_attr(not(feature = "std"), no_std)]
- extern crate byteorder;
- extern crate combine;
- extern crate log;
- #[cfg(feature = "std")]
- extern crate time;
- #[cfg(not(feature = "std"))]
- extern crate alloc;
- #[cfg(feature = "cranelift")]
- extern crate cranelift_codegen;
- #[cfg(feature = "cranelift")]
- extern crate cranelift_frontend;
- #[cfg(feature = "cranelift")]
- extern crate cranelift_jit;
- #[cfg(feature = "cranelift")]
- extern crate cranelift_module;
- #[cfg(feature = "cranelift")]
- extern crate cranelift_native;
- use crate::lib::*;
- use byteorder::{ByteOrder, LittleEndian};
- mod asm_parser;
- pub mod assembler;
- #[cfg(feature = "cranelift")]
- mod cranelift;
- pub mod disassembler;
- pub mod ebpf;
- pub mod helpers;
- pub mod insn_builder;
- mod interpreter;
- #[cfg(all(not(windows), feature = "std"))]
- mod jit;
- #[cfg(not(feature = "std"))]
- mod no_std_error;
- mod verifier;
- /// Reexports all the types needed from the `std`, `core`, and `alloc`
- /// crates. This avoids elaborate import wrangling having to happen in every
- /// module. Inspired by the design used in `serde`.
- pub mod lib {
- mod core {
- #[cfg(not(feature = "std"))]
- pub use core::*;
- #[cfg(feature = "std")]
- pub use std::*;
- }
- pub use self::core::convert::TryInto;
- pub use self::core::mem;
- pub use self::core::mem::ManuallyDrop;
- pub use self::core::ptr;
- pub use self::core::f64;
- #[cfg(feature = "std")]
- pub use std::println;
- #[cfg(not(feature = "std"))]
- pub use alloc::vec;
- #[cfg(not(feature = "std"))]
- pub use alloc::vec::Vec;
- #[cfg(feature = "std")]
- pub use std::vec::Vec;
- #[cfg(not(feature = "std"))]
- pub use alloc::string::{String, ToString};
- #[cfg(feature = "std")]
- pub use std::string::{String, ToString};
- // In no_std we cannot use randomness for hashing, thus we need to use
- // BTree-based implementations of Maps and Sets. The cranelift module uses
- // BTrees by default, hence we need to expose it twice here.
- #[cfg(not(feature = "std"))]
- pub use alloc::collections::{BTreeMap as HashMap, BTreeMap, BTreeSet as HashSet, BTreeSet};
- #[cfg(feature = "std")]
- pub use std::collections::{BTreeMap, HashMap, HashSet};
- /// In no_std we use a custom implementation of the error which acts as a
- /// replacement for the io Error.
- #[cfg(not(feature = "std"))]
- pub use crate::no_std_error::{Error, ErrorKind};
- #[cfg(feature = "std")]
- pub use std::io::{Error, ErrorKind};
- #[cfg(not(feature = "std"))]
- pub use alloc::format;
- }
- /// eBPF verification function that returns an error if the program does not meet its requirements.
- ///
- /// Some examples of things the verifier may reject the program for:
- ///
- /// - Program does not terminate.
- /// - Unknown instructions.
- /// - Bad formed instruction.
- /// - Unknown eBPF helper index.
- pub type Verifier = fn(prog: &[u8]) -> Result<(), Error>;
- /// eBPF helper function.
- pub type Helper = fn(u64, u64, u64, u64, u64) -> u64;
- // A metadata buffer with two offset indications. It can be used in one kind of eBPF VM to simulate
- // the use of a metadata buffer each time the program is executed, without the user having to
- // actually handle it. The offsets are used to tell the VM where in the buffer the pointers to
- // packet data start and end should be stored each time the program is run on a new packet.
- struct MetaBuff {
- data_offset: usize,
- data_end_offset: usize,
- buffer: Vec<u8>,
- }
- /// A virtual machine to run eBPF program. This kind of VM is used for programs expecting to work
- /// on a metadata buffer containing pointers to packet data.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff at offset 8 into R1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Just for the example we create our metadata buffer from scratch, and we store the pointers
- /// // to packet data start and end in it.
- /// let mut mbuff = [0u8; 32];
- /// unsafe {
- /// let mut data = mbuff.as_ptr().offset(8) as *mut u64;
- /// let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
- /// *data = mem.as_ptr() as u64;
- /// *data_end = mem.as_ptr() as u64 + mem.len() as u64;
- /// }
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// // Provide both a reference to the packet data, and to the metadata buffer.
- /// let res = vm.execute_program(mem, &mut mbuff).unwrap();
- /// assert_eq!(res, 0x2211);
- /// ```
- pub struct EbpfVmMbuff<'a> {
- prog: Option<&'a [u8]>,
- verifier: Verifier,
- #[cfg(all(not(windows), feature = "std"))]
- jit: Option<jit::JitMemory<'a>>,
- #[cfg(feature = "cranelift")]
- cranelift_prog: Option<cranelift::CraneliftProgram>,
- helpers: HashMap<u32, ebpf::Helper>,
- allowed_memory: HashSet<u64>,
- }
- impl<'a> EbpfVmMbuff<'a> {
- /// Create a new virtual machine instance, and load an eBPF program into that instance.
- /// When attempting to load the program, it passes through a simple verifier.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into R1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- /// ```
- pub fn new(prog: Option<&'a [u8]>) -> Result<EbpfVmMbuff<'a>, Error> {
- if let Some(prog) = prog {
- verifier::check(prog)?;
- }
- Ok(EbpfVmMbuff {
- prog,
- verifier: verifier::check,
- #[cfg(all(not(windows), feature = "std"))]
- jit: None,
- #[cfg(feature = "cranelift")]
- cranelift_prog: None,
- helpers: HashMap::new(),
- allowed_memory: HashSet::new(),
- })
- }
- /// Load a new eBPF program into the virtual machine instance.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let prog2 = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into R1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog1)).unwrap();
- /// vm.set_program(prog2).unwrap();
- /// ```
- pub fn set_program(&mut self, prog: &'a [u8]) -> Result<(), Error> {
- (self.verifier)(prog)?;
- self.prog = Some(prog);
- Ok(())
- }
- /// Set a new verifier function. The function should return an `Error` if the program should be
- /// rejected by the virtual machine. If a program has been loaded to the VM already, the
- /// verifier is immediately run.
- ///
- /// # Examples
- ///
- /// ```
- /// use rbpf::lib::{Error, ErrorKind};
- /// use rbpf::ebpf;
- ///
- /// // Define a simple verifier function.
- /// fn verifier(prog: &[u8]) -> Result<(), Error> {
- /// let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);
- /// if last_insn.opc != ebpf::EXIT {
- /// return Err(Error::new(ErrorKind::Other,
- /// "[Verifier] Error: program does not end with “EXIT” instruction"));
- /// }
- /// Ok(())
- /// }
- ///
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog1)).unwrap();
- /// // Change the verifier.
- /// vm.set_verifier(verifier).unwrap();
- /// ```
- pub fn set_verifier(&mut self, verifier: Verifier) -> Result<(), Error> {
- if let Some(prog) = self.prog {
- verifier(prog)?;
- }
- self.verifier = verifier;
- Ok(())
- }
- /// Register a built-in or user-defined helper function in order to use it later from within
- /// the eBPF program. The helper is registered into a hashmap, so the `key` can be any `u32`.
- ///
- /// If using JIT-compiled eBPF programs, be sure to register all helpers before compiling the
- /// program. You should be able to change registered helpers after compiling, but not to add
- /// new ones (i.e. with new keys).
- ///
- /// # Examples
- ///
- /// ```
- /// use rbpf::helpers;
- ///
- /// // This program was compiled with clang, from a C program containing the following single
- /// // instruction: `return bpf_trace_printk("foo %c %c %c\n", 10, 1, 2, 3);`
- /// let prog = &[
- /// 0x18, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load 0 as u64 into r1 (That would be
- /// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // replaced by tc by the address of
- /// // the format string, in the .map
- /// // section of the ELF file).
- /// 0xb7, 0x02, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, // mov r2, 10
- /// 0xb7, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // mov r3, 1
- /// 0xb7, 0x04, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // mov r4, 2
- /// 0xb7, 0x05, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // mov r5, 3
- /// 0x85, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, // call helper with key 6
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// // Register a helper.
- /// // On running the program this helper will print the content of registers r3, r4 and r5 to
- /// // standard output.
- /// # #[cfg(feature = "std")]
- /// vm.register_helper(6, helpers::bpf_trace_printf).unwrap();
- /// ```
- pub fn register_helper(&mut self, key: u32, function: Helper) -> Result<(), Error> {
- self.helpers.insert(key, function);
- Ok(())
- }
- /// Register a set of addresses that the eBPF program is allowed to load and store.
- ///
- /// When using certain helpers, typically map lookups, the Linux kernel will return pointers
- /// to structs that the eBPF program needs to interact with. By default rbpf only allows the
- /// program to interact with its stack, the memory buffer and the program itself, making it
- /// impossible to supply functional implementations of these helpers.
- /// This option allows you to pass in a list of addresses that rbpf will allow the program
- /// to load and store to. Given Rust's memory model you will always know these addresses up
- /// front when implementing the helpers.
- ///
- /// Each invocation of this method will append to the set of allowed addresses.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::iter::FromIterator;
- /// use std::ptr::addr_of;
- ///
- /// struct MapValue {
- /// data: u8
- /// }
- /// static VALUE: MapValue = MapValue { data: 1 };
- ///
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- /// let start = addr_of!(VALUE) as u64;
- /// let addrs = Vec::from_iter(start..start+size_of::<MapValue>() as u64);
- /// vm.register_allowed_memory(&addrs);
- /// ```
- pub fn register_allowed_memory(&mut self, addrs: &[u64]) {
- for i in addrs {
- self.allowed_memory.insert(*i);
- }
- }
- /// Execute the program loaded, with the given packet data and metadata buffer.
- ///
- /// If the program is made to be compatible with Linux kernel, it is expected to load the
- /// address of the beginning and of the end of the memory area used for packet data from the
- /// metadata buffer, at some appointed offsets. It is up to the user to ensure that these
- /// pointers are correctly stored in the buffer.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into R1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Just for the example we create our metadata buffer from scratch, and we store the
- /// // pointers to packet data start and end in it.
- /// let mut mbuff = [0u8; 32];
- /// unsafe {
- /// let mut data = mbuff.as_ptr().offset(8) as *mut u64;
- /// let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
- /// *data = mem.as_ptr() as u64;
- /// *data_end = mem.as_ptr() as u64 + mem.len() as u64;
- /// }
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// // Provide both a reference to the packet data, and to the metadata buffer.
- /// let res = vm.execute_program(mem, &mut mbuff).unwrap();
- /// assert_eq!(res, 0x2211);
- /// ```
- pub fn execute_program(&self, mem: &[u8], mbuff: &[u8]) -> Result<u64, Error> {
- interpreter::execute_program(self.prog, mem, mbuff, &self.helpers, &self.allowed_memory)
- }
- /// JIT-compile the loaded program. No argument required for this.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into R1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// vm.jit_compile();
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub fn jit_compile(&mut self) -> Result<(), Error> {
- let prog = match self.prog {
- Some(prog) => prog,
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: No program set, call prog_set() to load one",
- ))?,
- };
- self.jit = Some(jit::JitMemory::new(prog, &self.helpers, true, false)?);
- Ok(())
- }
- /// Execute the previously JIT-compiled program, with the given packet data and metadata
- /// buffer, in a manner very similar to `execute_program()`.
- ///
- /// If the program is made to be compatible with Linux kernel, it is expected to load the
- /// address of the beginning and of the end of the memory area used for packet data from the
- /// metadata buffer, at some appointed offsets. It is up to the user to ensure that these
- /// pointers are correctly stored in the buffer.
- ///
- /// # Safety
- ///
- /// **WARNING:** JIT-compiled assembly code is not safe, in particular there is no runtime
- /// check for memory access; so if the eBPF program attempts erroneous accesses, this may end
- /// very bad (program may segfault). It may be wise to check that the program works with the
- /// interpreter before running the JIT-compiled version of it.
- ///
- /// For this reason the function should be called from within an `unsafe` bloc.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into r1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Just for the example we create our metadata buffer from scratch, and we store the
- /// // pointers to packet data start and end in it.
- /// let mut mbuff = [0u8; 32];
- /// unsafe {
- /// let mut data = mbuff.as_ptr().offset(8) as *mut u64;
- /// let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
- /// *data = mem.as_ptr() as u64;
- /// *data_end = mem.as_ptr() as u64 + mem.len() as u64;
- /// }
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// vm.jit_compile();
- ///
- /// // Provide both a reference to the packet data, and to the metadata buffer.
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// unsafe {
- /// let res = vm.execute_program_jit(mem, &mut mbuff).unwrap();
- /// assert_eq!(res, 0x2211);
- /// }
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub unsafe fn execute_program_jit(
- &self,
- mem: &mut [u8],
- mbuff: &'a mut [u8],
- ) -> Result<u64, Error> {
- // If packet data is empty, do not send the address of an empty slice; send a null pointer
- // as first argument instead, as this is uBPF's behavior (empty packet should not happen
- // in the kernel; anyway the verifier would prevent the use of uninitialized registers).
- // See `mul_loop` test.
- let mem_ptr = match mem.len() {
- 0 => std::ptr::null_mut(),
- _ => mem.as_ptr() as *mut u8,
- };
- // The last two arguments are not used in this function. They would be used if there was a
- // need to indicate to the JIT at which offset in the mbuff mem_ptr and mem_ptr + mem.len()
- // should be stored; this is what happens with struct EbpfVmFixedMbuff.
- match &self.jit {
- Some(jit) => Ok(jit.get_prog()(
- mbuff.as_ptr() as *mut u8,
- mbuff.len(),
- mem_ptr,
- mem.len(),
- 0,
- 0,
- )),
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: program has not been JIT-compiled",
- )),
- }
- }
- /// Compile the loaded program using the Cranelift JIT.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into R1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// vm.cranelift_compile();
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn cranelift_compile(&mut self) -> Result<(), Error> {
- use crate::cranelift::CraneliftCompiler;
- let prog = match self.prog {
- Some(prog) => prog,
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: No program set, call prog_set() to load one",
- ))?,
- };
- let compiler = CraneliftCompiler::new(self.helpers.clone());
- let program = compiler.compile_function(prog)?;
- self.cranelift_prog = Some(program);
- Ok(())
- }
- /// Execute the previously compiled program, with the given packet data and metadata
- /// buffer, in a manner very similar to `execute_program()`.
- ///
- /// If the program is made to be compatible with Linux kernel, it is expected to load the
- /// address of the beginning and of the end of the memory area used for packet data from the
- /// metadata buffer, at some appointed offsets. It is up to the user to ensure that these
- /// pointers are correctly stored in the buffer.
- ///
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // Load mem from mbuff into r1.
- /// 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Just for the example we create our metadata buffer from scratch, and we store the
- /// // pointers to packet data start and end in it.
- /// let mut mbuff = [0u8; 32];
- /// unsafe {
- /// let mut data = mbuff.as_ptr().offset(8) as *mut u64;
- /// let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
- /// *data = mem.as_ptr() as u64;
- /// *data_end = mem.as_ptr() as u64 + mem.len() as u64;
- /// }
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
- ///
- /// vm.cranelift_compile();
- ///
- /// // Provide both a reference to the packet data, and to the metadata buffer.
- /// let res = vm.execute_program_cranelift(mem, &mut mbuff).unwrap();
- /// assert_eq!(res, 0x2211);
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn execute_program_cranelift(
- &self,
- mem: &mut [u8],
- mbuff: &'a mut [u8],
- ) -> Result<u64, Error> {
- // If packet data is empty, do not send the address of an empty slice; send a null pointer
- // as first argument instead, as this is uBPF's behavior (empty packet should not happen
- // in the kernel; anyway the verifier would prevent the use of uninitialized registers).
- // See `mul_loop` test.
- let mem_ptr = match mem.len() {
- 0 => ptr::null_mut(),
- _ => mem.as_ptr() as *mut u8,
- };
- // The last two arguments are not used in this function. They would be used if there was a
- // need to indicate to the JIT at which offset in the mbuff mem_ptr and mem_ptr + mem.len()
- // should be stored; this is what happens with struct EbpfVmFixedMbuff.
- match &self.cranelift_prog {
- Some(prog) => {
- Ok(prog.execute(mem_ptr, mem.len(), mbuff.as_ptr() as *mut u8, mbuff.len()))
- }
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: program has not been compiled with cranelift",
- )),
- }
- }
- }
- /// A virtual machine to run eBPF program. This kind of VM is used for programs expecting to work
- /// on a metadata buffer containing pointers to packet data, but it internally handles the buffer
- /// so as to save the effort to manually handle the metadata buffer for the user.
- ///
- /// This struct implements a static internal buffer that is passed to the program. The user has to
- /// indicate the offset values at which the eBPF program expects to find the start and the end of
- /// packet data in the buffer. On calling the `execute_program()` or `execute_program_jit()` functions, the
- /// struct automatically updates the addresses in this static buffer, at the appointed offsets, for
- /// the start and the end of the packet data the program is called upon.
- ///
- /// # Examples
- ///
- /// This was compiled with clang from the following program, in C:
- ///
- /// ```c
- /// #include <linux/bpf.h>
- /// #include "path/to/linux/samples/bpf/bpf_helpers.h"
- ///
- /// SEC(".classifier")
- /// int classifier(struct __sk_buff *skb)
- /// {
- /// void *data = (void *)(long)skb->data;
- /// void *data_end = (void *)(long)skb->data_end;
- ///
- /// // Check program is long enough.
- /// if (data + 5 > data_end)
- /// return 0;
- ///
- /// return *((char *)data + 5);
- /// }
- /// ```
- ///
- /// Some small modifications have been brought to have it work, see comments.
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// // Here opcode 0x61 had to be replace by 0x79 so as to load a 8-bytes long address.
- /// // Also, offset 0x4c had to be replace with e.g. 0x40 so as to prevent the two pointers
- /// // from overlapping in the buffer.
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load pointer to mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// // Here opcode 0x61 had to be replace by 0x79 so as to load a 8-bytes long address.
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load ptr to mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x67, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, // r0 >>= 56
- /// 0xc7, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, // r0 <<= 56 (arsh) extend byte sign to u64
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem1 = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- /// let mem2 = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x27
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// // Provide only a reference to the packet data. We do not manage the metadata buffer.
- /// let res = vm.execute_program(mem1).unwrap();
- /// assert_eq!(res, 0xffffffffffffffdd);
- ///
- /// let res = vm.execute_program(mem2).unwrap();
- /// assert_eq!(res, 0x27);
- /// ```
- pub struct EbpfVmFixedMbuff<'a> {
- parent: EbpfVmMbuff<'a>,
- mbuff: MetaBuff,
- }
- impl<'a> EbpfVmFixedMbuff<'a> {
- /// Create a new virtual machine instance, and load an eBPF program into that instance.
- /// When attempting to load the program, it passes through a simple verifier.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- /// ```
- pub fn new(
- prog: Option<&'a [u8]>,
- data_offset: usize,
- data_end_offset: usize,
- ) -> Result<EbpfVmFixedMbuff<'a>, Error> {
- let parent = EbpfVmMbuff::new(prog)?;
- let get_buff_len = |x: usize, y: usize| if x >= y { x + 8 } else { y + 8 };
- let buffer = vec![0u8; get_buff_len(data_offset, data_end_offset)];
- let mbuff = MetaBuff {
- data_offset,
- data_end_offset,
- buffer,
- };
- Ok(EbpfVmFixedMbuff { parent, mbuff })
- }
- /// Load a new eBPF program into the virtual machine instance.
- ///
- /// At the same time, load new offsets for storing pointers to start and end of packet data in
- /// the internal metadata buffer.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let prog2 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x27,
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog1), 0, 0).unwrap();
- /// vm.set_program(prog2, 0x40, 0x50);
- ///
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 0x27);
- /// ```
- pub fn set_program(
- &mut self,
- prog: &'a [u8],
- data_offset: usize,
- data_end_offset: usize,
- ) -> Result<(), Error> {
- let get_buff_len = |x: usize, y: usize| if x >= y { x + 8 } else { y + 8 };
- let buffer = vec![0u8; get_buff_len(data_offset, data_end_offset)];
- self.mbuff.buffer = buffer;
- self.mbuff.data_offset = data_offset;
- self.mbuff.data_end_offset = data_end_offset;
- self.parent.set_program(prog)?;
- Ok(())
- }
- /// Set a new verifier function. The function should return an `Error` if the program should be
- /// rejected by the virtual machine. If a program has been loaded to the VM already, the
- /// verifier is immediately run.
- ///
- /// # Examples
- ///
- /// ```
- /// use rbpf::lib::{Error, ErrorKind};
- /// use rbpf::ebpf;
- ///
- /// // Define a simple verifier function.
- /// fn verifier(prog: &[u8]) -> Result<(), Error> {
- /// let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);
- /// if last_insn.opc != ebpf::EXIT {
- /// return Err(Error::new(ErrorKind::Other,
- /// "[Verifier] Error: program does not end with “EXIT” instruction"));
- /// }
- /// Ok(())
- /// }
- ///
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog1)).unwrap();
- /// // Change the verifier.
- /// vm.set_verifier(verifier).unwrap();
- /// ```
- pub fn set_verifier(&mut self, verifier: Verifier) -> Result<(), Error> {
- self.parent.set_verifier(verifier)
- }
- /// Register a built-in or user-defined helper function in order to use it later from within
- /// the eBPF program. The helper is registered into a hashmap, so the `key` can be any `u32`.
- ///
- /// If using JIT-compiled eBPF programs, be sure to register all helpers before compiling the
- /// program. You should be able to change registered helpers after compiling, but not to add
- /// new ones (i.e. with new keys).
- ///
- /// # Examples
- ///
- /// ```
- /// #[cfg(feature = "std")] {
- /// use rbpf::helpers;
- ///
- /// // This program was compiled with clang, from a C program containing the following single
- /// // instruction: `return bpf_trace_printk("foo %c %c %c\n", 10, 1, 2, 3);`
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 6 instructions
- /// 0x71, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r1
- /// 0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r2, 0
- /// 0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r3, 0
- /// 0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r4, 0
- /// 0xb7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r5, 0
- /// 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // call helper with key 1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x09,
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// // Register a helper. This helper will store the result of the square root of r1 into r0.
- /// vm.register_helper(1, helpers::sqrti);
- ///
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 3);
- /// }
- /// ```
- pub fn register_helper(
- &mut self,
- key: u32,
- function: fn(u64, u64, u64, u64, u64) -> u64,
- ) -> Result<(), Error> {
- self.parent.register_helper(key, function)
- }
- /// Register an object that the eBPF program is allowed to load and store.
- ///
- /// When using certain helpers, typically map lookups, the Linux kernel will return pointers
- /// to structs that the eBPF program needs to interact with. By default rbpf only allows the
- /// program to interact with its stack, the memory buffer and the program itself, making it
- /// impossible to supply functional implementations of these helpers.
- /// This option allows you to pass in a list of addresses that rbpf will allow the program
- /// to load and store to. Given Rust's memory model you will always know these addresses up
- /// front when implementing the helpers.
- ///
- /// Each invocation of this method will append to the set of allowed addresses.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::iter::FromIterator;
- /// use std::ptr::addr_of;
- ///
- /// struct MapValue {
- /// data: u8
- /// }
- /// static VALUE: MapValue = MapValue { data: 1 };
- ///
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- /// let start = addr_of!(VALUE) as u64;
- /// let addrs = Vec::from_iter(start..start+size_of::<MapValue>() as u64);
- /// vm.register_allowed_memory(&addrs);
- /// ```
- pub fn register_allowed_memory(&mut self, allowed: &[u64]) {
- self.parent.register_allowed_memory(allowed)
- }
- /// Execute the program loaded, with the given packet data.
- ///
- /// If the program is made to be compatible with Linux kernel, it is expected to load the
- /// address of the beginning and of the end of the memory area used for packet data from some
- /// metadata buffer, which in the case of this VM is handled internally. The offsets at which
- /// the addresses should be placed should have be set at the creation of the VM.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// // Provide only a reference to the packet data. We do not manage the metadata buffer.
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 0xdd);
- /// ```
- pub fn execute_program(&mut self, mem: &'a mut [u8]) -> Result<u64, Error> {
- let l = self.mbuff.buffer.len();
- // Can this ever happen? Probably not, should be ensured at mbuff creation.
- if self.mbuff.data_offset + 8 > l || self.mbuff.data_end_offset + 8 > l {
- Err(Error::new(ErrorKind::Other, format!("Error: buffer too small ({:?}), cannot use data_offset {:?} and data_end_offset {:?}",
- l, self.mbuff.data_offset, self.mbuff.data_end_offset)))?;
- }
- LittleEndian::write_u64(
- &mut self.mbuff.buffer[(self.mbuff.data_offset)..],
- mem.as_ptr() as u64,
- );
- LittleEndian::write_u64(
- &mut self.mbuff.buffer[(self.mbuff.data_end_offset)..],
- mem.as_ptr() as u64 + mem.len() as u64,
- );
- self.parent.execute_program(mem, &self.mbuff.buffer)
- }
- /// JIT-compile the loaded program. No argument required for this.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// vm.jit_compile();
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub fn jit_compile(&mut self) -> Result<(), Error> {
- let prog = match self.parent.prog {
- Some(prog) => prog,
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: No program set, call prog_set() to load one",
- ))?,
- };
- self.parent.jit = Some(jit::JitMemory::new(prog, &self.parent.helpers, true, true)?);
- Ok(())
- }
- /// Execute the previously JIT-compiled program, with the given packet data, in a manner very
- /// similar to `execute_program()`.
- ///
- /// If the program is made to be compatible with Linux kernel, it is expected to load the
- /// address of the beginning and of the end of the memory area used for packet data from some
- /// metadata buffer, which in the case of this VM is handled internally. The offsets at which
- /// the addresses should be placed should have be set at the creation of the VM.
- ///
- /// # Safety
- ///
- /// **WARNING:** JIT-compiled assembly code is not safe, in particular there is no runtime
- /// check for memory access; so if the eBPF program attempts erroneous accesses, this may end
- /// very bad (program may segfault). It may be wise to check that the program works with the
- /// interpreter before running the JIT-compiled version of it.
- ///
- /// For this reason the function should be called from within an `unsafe` bloc.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// vm.jit_compile();
- ///
- /// // Provide only a reference to the packet data. We do not manage the metadata buffer.
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// unsafe {
- /// let res = vm.execute_program_jit(mem).unwrap();
- /// assert_eq!(res, 0xdd);
- /// }
- /// ```
- // This struct redefines the `execute_program_jit()` function, in order to pass the offsets
- // associated with the fixed mbuff.
- #[cfg(all(not(windows), feature = "std"))]
- pub unsafe fn execute_program_jit(&mut self, mem: &'a mut [u8]) -> Result<u64, Error> {
- // If packet data is empty, do not send the address of an empty slice; send a null pointer
- // as first argument instead, as this is uBPF's behavior (empty packet should not happen
- // in the kernel; anyway the verifier would prevent the use of uninitialized registers).
- // See `mul_loop` test.
- let mem_ptr = match mem.len() {
- 0 => ptr::null_mut(),
- _ => mem.as_ptr() as *mut u8,
- };
- match &self.parent.jit {
- Some(jit) => Ok(jit.get_prog()(
- self.mbuff.buffer.as_ptr() as *mut u8,
- self.mbuff.buffer.len(),
- mem_ptr,
- mem.len(),
- self.mbuff.data_offset,
- self.mbuff.data_end_offset,
- )),
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: program has not been JIT-compiled",
- )),
- }
- }
- /// Compile the loaded program using the Cranelift JIT.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// vm.cranelift_compile();
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn cranelift_compile(&mut self) -> Result<(), Error> {
- use crate::cranelift::CraneliftCompiler;
- let prog = match self.parent.prog {
- Some(prog) => prog,
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: No program set, call prog_set() to load one",
- ))?,
- };
- let compiler = CraneliftCompiler::new(self.parent.helpers.clone());
- let program = compiler.compile_function(prog)?;
- self.parent.cranelift_prog = Some(program);
- Ok(())
- }
- /// Execute the previously compiled program, with the given packet data and metadata
- /// buffer, in a manner very similar to `execute_program()`.
- ///
- /// If the program is made to be compatible with Linux kernel, it is expected to load the
- /// address of the beginning and of the end of the memory area used for packet data from some
- /// metadata buffer, which in the case of this VM is handled internally. The offsets at which
- /// the addresses should be placed should have be set at the creation of the VM.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x79, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem from r1[0x40] to r2
- /// 0x07, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // add r2, 5
- /// 0x79, 0x11, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // load mem_end from r1[0x50] to r1
- /// 0x2d, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // if r2 > r1 skip 3 instructions
- /// 0x71, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load r2 (= *(mem + 5)) into r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Instantiate a VM. Note that we provide the start and end offsets for mem pointers.
- /// let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
- ///
- /// vm.cranelift_compile();
- ///
- /// // Provide only a reference to the packet data. We do not manage the metadata buffer.
- /// let res = vm.execute_program_cranelift(mem).unwrap();
- /// assert_eq!(res, 0xdd);
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn execute_program_cranelift(&mut self, mem: &'a mut [u8]) -> Result<u64, Error> {
- // If packet data is empty, do not send the address of an empty slice; send a null pointer
- // as first argument instead, as this is uBPF's behavior (empty packet should not happen
- // in the kernel; anyway the verifier would prevent the use of uninitialized registers).
- // See `mul_loop` test.
- let mem_ptr = match mem.len() {
- 0 => ptr::null_mut(),
- _ => mem.as_ptr() as *mut u8,
- };
- let l = self.mbuff.buffer.len();
- // Can this ever happen? Probably not, should be ensured at mbuff creation.
- if self.mbuff.data_offset + 8 > l || self.mbuff.data_end_offset + 8 > l {
- Err(Error::new(ErrorKind::Other, format!("Error: buffer too small ({:?}), cannot use data_offset {:?} and data_end_offset {:?}",
- l, self.mbuff.data_offset, self.mbuff.data_end_offset)))?;
- }
- LittleEndian::write_u64(
- &mut self.mbuff.buffer[(self.mbuff.data_offset)..],
- mem.as_ptr() as u64,
- );
- LittleEndian::write_u64(
- &mut self.mbuff.buffer[(self.mbuff.data_end_offset)..],
- mem.as_ptr() as u64 + mem.len() as u64,
- );
- match &self.parent.cranelift_prog {
- Some(prog) => Ok(prog.execute(
- mem_ptr,
- mem.len(),
- self.mbuff.buffer.as_ptr() as *mut u8,
- self.mbuff.buffer.len(),
- )),
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: program has not been compiled with cranelift",
- )),
- }
- }
- }
- /// A virtual machine to run eBPF program. This kind of VM is used for programs expecting to work
- /// directly on the memory area representing packet data.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- /// ];
- ///
- /// // Instantiate a VM.
- /// let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// // Provide only a reference to the packet data.
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 0x22cc);
- /// ```
- pub struct EbpfVmRaw<'a> {
- parent: EbpfVmMbuff<'a>,
- }
- impl<'a> EbpfVmRaw<'a> {
- /// Create a new virtual machine instance, and load an eBPF program into that instance.
- /// When attempting to load the program, it passes through a simple verifier.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- /// ```
- pub fn new(prog: Option<&'a [u8]>) -> Result<EbpfVmRaw<'a>, Error> {
- let parent = EbpfVmMbuff::new(prog)?;
- Ok(EbpfVmRaw { parent })
- }
- /// Load a new eBPF program into the virtual machine instance.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let prog2 = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x27,
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog1)).unwrap();
- /// vm.set_program(prog2);
- ///
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 0x22cc);
- /// ```
- pub fn set_program(&mut self, prog: &'a [u8]) -> Result<(), Error> {
- self.parent.set_program(prog)?;
- Ok(())
- }
- /// Set a new verifier function. The function should return an `Error` if the program should be
- /// rejected by the virtual machine. If a program has been loaded to the VM already, the
- /// verifier is immediately run.
- ///
- /// # Examples
- ///
- /// ```
- /// use rbpf::lib::{Error, ErrorKind};
- /// use rbpf::ebpf;
- ///
- /// // Define a simple verifier function.
- /// fn verifier(prog: &[u8]) -> Result<(), Error> {
- /// let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);
- /// if last_insn.opc != ebpf::EXIT {
- /// return Err(Error::new(ErrorKind::Other,
- /// "[Verifier] Error: program does not end with “EXIT” instruction"));
- /// }
- /// Ok(())
- /// }
- ///
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog1)).unwrap();
- /// // Change the verifier.
- /// vm.set_verifier(verifier).unwrap();
- /// ```
- pub fn set_verifier(&mut self, verifier: Verifier) -> Result<(), Error> {
- self.parent.set_verifier(verifier)
- }
- /// Register a built-in or user-defined helper function in order to use it later from within
- /// the eBPF program. The helper is registered into a hashmap, so the `key` can be any `u32`.
- ///
- /// If using JIT-compiled eBPF programs, be sure to register all helpers before compiling the
- /// program. You should be able to change registered helpers after compiling, but not to add
- /// new ones (i.e. with new keys).
- ///
- /// # Examples
- ///
- /// ```
- /// #[cfg(feature = "std")] {
- /// use rbpf::helpers;
- ///
- /// let prog = &[
- /// 0x79, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxdw r1, r1[0x00]
- /// 0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r2, 0
- /// 0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r3, 0
- /// 0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r4, 0
- /// 0xb7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r5, 0
- /// 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // call helper with key 1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// // Register a helper. This helper will store the result of the square root of r1 into r0.
- /// vm.register_helper(1, helpers::sqrti);
- ///
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 0x10000000);
- /// }
- /// ```
- pub fn register_helper(
- &mut self,
- key: u32,
- function: fn(u64, u64, u64, u64, u64) -> u64,
- ) -> Result<(), Error> {
- self.parent.register_helper(key, function)
- }
- /// Register an object that the eBPF program is allowed to load and store.
- ///
- /// When using certain helpers, typically map lookups, the Linux kernel will return pointers
- /// to structs that the eBPF program needs to interact with. By default rbpf only allows the
- /// program to interact with its stack, the memory buffer and the program itself, making it
- /// impossible to supply functional implementations of these helpers.
- /// This option allows you to pass in a list of addresses that rbpf will allow the program
- /// to load and store to. Given Rust's memory model you will always know these addresses up
- /// front when implementing the helpers.
- ///
- /// Each invocation of this method will append to the set of allowed addresses.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::iter::FromIterator;
- /// use std::ptr::addr_of;
- ///
- /// struct MapValue {
- /// data: u8
- /// }
- /// static VALUE: MapValue = MapValue { data: 1 };
- ///
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- /// let start = addr_of!(VALUE) as u64;
- /// let addrs = Vec::from_iter(start..start+size_of::<MapValue>() as u64);
- /// vm.register_allowed_memory(&addrs);
- /// ```
- pub fn register_allowed_memory(&mut self, allowed: &[u64]) {
- self.parent.register_allowed_memory(allowed)
- }
- /// Execute the program loaded, with the given packet data.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x27
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// let res = vm.execute_program(mem).unwrap();
- /// assert_eq!(res, 0x22cc);
- /// ```
- pub fn execute_program(&self, mem: &'a mut [u8]) -> Result<u64, Error> {
- self.parent.execute_program(mem, &[])
- }
- /// JIT-compile the loaded program. No argument required for this.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// vm.jit_compile();
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub fn jit_compile(&mut self) -> Result<(), Error> {
- let prog = match self.parent.prog {
- Some(prog) => prog,
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: No program set, call prog_set() to load one",
- ))?,
- };
- self.parent.jit = Some(jit::JitMemory::new(
- prog,
- &self.parent.helpers,
- false,
- false,
- )?);
- Ok(())
- }
- /// Execute the previously JIT-compiled program, with the given packet data, in a manner very
- /// similar to `execute_program()`.
- ///
- /// # Safety
- ///
- /// **WARNING:** JIT-compiled assembly code is not safe, in particular there is no runtime
- /// check for memory access; so if the eBPF program attempts erroneous accesses, this may end
- /// very bad (program may segfault). It may be wise to check that the program works with the
- /// interpreter before running the JIT-compiled version of it.
- ///
- /// For this reason the function should be called from within an `unsafe` bloc.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x27
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// vm.jit_compile();
- ///
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// unsafe {
- /// let res = vm.execute_program_jit(mem).unwrap();
- /// assert_eq!(res, 0x22cc);
- /// }
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub unsafe fn execute_program_jit(&self, mem: &'a mut [u8]) -> Result<u64, Error> {
- let mut mbuff = vec![];
- self.parent.execute_program_jit(mem, &mut mbuff)
- }
- /// Compile the loaded program using the Cranelift JIT.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// vm.cranelift_compile();
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn cranelift_compile(&mut self) -> Result<(), Error> {
- use crate::cranelift::CraneliftCompiler;
- let prog = match self.parent.prog {
- Some(prog) => prog,
- None => Err(Error::new(
- ErrorKind::Other,
- "Error: No program set, call prog_set() to load one",
- ))?,
- };
- let compiler = CraneliftCompiler::new(self.parent.helpers.clone());
- let program = compiler.compile_function(prog)?;
- self.parent.cranelift_prog = Some(program);
- Ok(())
- }
- /// Execute the previously compiled program, with the given packet data, in a manner very
- /// similar to `execute_program()`.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0x71, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // ldxb r1[0x04], r1
- /// 0x07, 0x01, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, // add r1, 0x22
- /// 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, r1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mem = &mut [
- /// 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0x27
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
- ///
- /// vm.cranelift_compile();
- ///
- /// let res = vm.execute_program_cranelift(mem).unwrap();
- /// assert_eq!(res, 0x22cc);
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn execute_program_cranelift(&self, mem: &'a mut [u8]) -> Result<u64, Error> {
- let mut mbuff = vec![];
- self.parent.execute_program_cranelift(mem, &mut mbuff)
- }
- }
- /// A virtual machine to run eBPF program. This kind of VM is used for programs that do not work
- /// with any memory area—no metadata buffer, no packet data either.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // mov r1, 1
- /// 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // mov r2, 2
- /// 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // mov r3, 3
- /// 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, // mov r4, 4
- /// 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, // mov r5, 5
- /// 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, // mov r6, 6
- /// 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, // mov r7, 7
- /// 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // mov r8, 8
- /// 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // or r0, r5
- /// 0x47, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, // or r0, 0xa0
- /// 0x57, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00, // and r0, 0xa3
- /// 0xb7, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00, // mov r9, 0x91
- /// 0x5f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // and r0, r9
- /// 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, // lsh r0, 32
- /// 0x67, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, // lsh r0, 22
- /// 0x6f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // lsh r0, r8
- /// 0x77, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, // rsh r0, 32
- /// 0x77, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, // rsh r0, 19
- /// 0x7f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // rsh r0, r7
- /// 0xa7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // xor r0, 0x03
- /// 0xaf, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xor r0, r2
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- /// // Provide only a reference to the packet data.
- /// let res = vm.execute_program().unwrap();
- /// assert_eq!(res, 0x11);
- /// ```
- pub struct EbpfVmNoData<'a> {
- parent: EbpfVmRaw<'a>,
- }
- impl<'a> EbpfVmNoData<'a> {
- /// Create a new virtual machine instance, and load an eBPF program into that instance.
- /// When attempting to load the program, it passes through a simple verifier.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let vm = rbpf::EbpfVmNoData::new(Some(prog));
- /// ```
- pub fn new(prog: Option<&'a [u8]>) -> Result<EbpfVmNoData<'a>, Error> {
- let parent = EbpfVmRaw::new(prog)?;
- Ok(EbpfVmNoData { parent })
- }
- /// Load a new eBPF program into the virtual machine instance.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- /// let prog2 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog1)).unwrap();
- ///
- /// let res = vm.execute_program().unwrap();
- /// assert_eq!(res, 0x2211);
- ///
- /// vm.set_program(prog2);
- ///
- /// let res = vm.execute_program().unwrap();
- /// assert_eq!(res, 0x1122);
- /// ```
- pub fn set_program(&mut self, prog: &'a [u8]) -> Result<(), Error> {
- self.parent.set_program(prog)?;
- Ok(())
- }
- /// Set a new verifier function. The function should return an `Error` if the program should be
- /// rejected by the virtual machine. If a program has been loaded to the VM already, the
- /// verifier is immediately run.
- ///
- /// # Examples
- ///
- /// ```
- /// use rbpf::lib::{Error, ErrorKind};
- /// use rbpf::ebpf;
- ///
- /// // Define a simple verifier function.
- /// fn verifier(prog: &[u8]) -> Result<(), Error> {
- /// let last_insn = ebpf::get_insn(prog, (prog.len() / ebpf::INSN_SIZE) - 1);
- /// if last_insn.opc != ebpf::EXIT {
- /// return Err(Error::new(ErrorKind::Other,
- /// "[Verifier] Error: program does not end with “EXIT” instruction"));
- /// }
- /// Ok(())
- /// }
- ///
- /// let prog1 = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmMbuff::new(Some(prog1)).unwrap();
- /// // Change the verifier.
- /// vm.set_verifier(verifier).unwrap();
- /// ```
- pub fn set_verifier(&mut self, verifier: Verifier) -> Result<(), Error> {
- self.parent.set_verifier(verifier)
- }
- /// Register a built-in or user-defined helper function in order to use it later from within
- /// the eBPF program. The helper is registered into a hashmap, so the `key` can be any `u32`.
- ///
- /// If using JIT-compiled eBPF programs, be sure to register all helpers before compiling the
- /// program. You should be able to change registered helpers after compiling, but not to add
- /// new ones (i.e. with new keys).
- ///
- /// # Examples
- ///
- /// ```
- /// #[cfg(feature = "std")] {
- /// use rbpf::helpers;
- ///
- /// let prog = &[
- /// 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, // mov r1, 0x010000000
- /// 0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r2, 0
- /// 0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r3, 0
- /// 0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r4, 0
- /// 0xb7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r5, 0
- /// 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // call helper with key 1
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- /// // Register a helper. This helper will store the result of the square root of r1 into r0.
- /// vm.register_helper(1, helpers::sqrti).unwrap();
- ///
- /// let res = vm.execute_program().unwrap();
- /// assert_eq!(res, 0x1000);
- /// }
- /// ```
- pub fn register_helper(
- &mut self,
- key: u32,
- function: fn(u64, u64, u64, u64, u64) -> u64,
- ) -> Result<(), Error> {
- self.parent.register_helper(key, function)
- }
- /// Register an object that the eBPF program is allowed to load and store.
- ///
- /// When using certain helpers, typically map lookups, the Linux kernel will return pointers
- /// to structs that the eBPF program needs to interact with. By default rbpf only allows the
- /// program to interact with its stack, the memory buffer and the program itself, making it
- /// impossible to supply functional implementations of these helpers.
- /// This option allows you to pass in a list of addresses that rbpf will allow the program
- /// to load and store to. Given Rust's memory model you will always know these addresses up
- /// front when implementing the helpers.
- ///
- /// Each invocation of this method will append to the set of allowed addresses.
- ///
- /// # Examples
- ///
- /// ```
- /// use std::iter::FromIterator;
- /// use std::ptr::addr_of;
- ///
- /// struct MapValue {
- /// data: u8
- /// }
- /// static VALUE: MapValue = MapValue { data: 1 };
- ///
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // mov r0, 0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// // Instantiate a VM.
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- /// let start = addr_of!(VALUE) as u64;
- /// let addrs = Vec::from_iter(start..start+size_of::<MapValue>() as u64);
- /// vm.register_allowed_memory(&addrs);
- /// ```
- pub fn register_allowed_memory(&mut self, allowed: &[u64]) {
- self.parent.register_allowed_memory(allowed)
- }
- /// JIT-compile the loaded program. No argument required for this.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- ///
- /// vm.jit_compile();
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub fn jit_compile(&mut self) -> Result<(), Error> {
- self.parent.jit_compile()
- }
- /// Execute the program loaded, without providing pointers to any memory area whatsoever.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- /// // For this kind of VM, the `execute_program()` function needs no argument.
- /// let res = vm.execute_program().unwrap();
- /// assert_eq!(res, 0x1122);
- /// ```
- pub fn execute_program(&self) -> Result<u64, Error> {
- self.parent.execute_program(&mut [])
- }
- /// Execute the previously JIT-compiled program, without providing pointers to any memory area
- /// whatsoever, in a manner very similar to `execute_program()`.
- ///
- /// # Safety
- ///
- /// **WARNING:** JIT-compiled assembly code is not safe, in particular there is no runtime
- /// check for memory access; so if the eBPF program attempts erroneous accesses, this may end
- /// very bad (program may segfault). It may be wise to check that the program works with the
- /// interpreter before running the JIT-compiled version of it.
- ///
- /// For this reason the function should be called from within an `unsafe` bloc.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// vm.jit_compile();
- ///
- /// # #[cfg(all(not(windows), feature = "std"))]
- /// unsafe {
- /// let res = vm.execute_program_jit().unwrap();
- /// assert_eq!(res, 0x1122);
- /// }
- /// ```
- #[cfg(all(not(windows), feature = "std"))]
- pub unsafe fn execute_program_jit(&self) -> Result<u64, Error> {
- self.parent.execute_program_jit(&mut [])
- }
- /// Compile the loaded program using the Cranelift JIT.
- ///
- /// If using helper functions, be sure to register them into the VM before calling this
- /// function.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- ///
- /// vm.cranelift_compile();
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn cranelift_compile(&mut self) -> Result<(), Error> {
- self.parent.cranelift_compile()
- }
- /// Execute the previously JIT-compiled program, without providing pointers to any memory area
- /// whatsoever, in a manner very similar to `execute_program()`.
- ///
- /// # Examples
- ///
- /// ```
- /// let prog = &[
- /// 0xb7, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00, // mov r0, 0x2211
- /// 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, // be16 r0
- /// 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // exit
- /// ];
- ///
- /// let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
- ///
- /// vm.cranelift_compile();
- ///
- /// let res = vm.execute_program_cranelift().unwrap();
- /// assert_eq!(res, 0x1122);
- /// ```
- #[cfg(feature = "cranelift")]
- pub fn execute_program_cranelift(&self) -> Result<u64, Error> {
- self.parent.execute_program_cranelift(&mut [])
- }
- }
|