load_elf.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  4. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  5. // copied, modified, or distributed except according to those terms.
  6. extern crate elf;
  7. use std::path::PathBuf;
  8. extern crate rbpf;
  9. use rbpf::helpers;
  10. // The following example uses an ELF file that has been compiled from the C program available in
  11. // `load_elf__block_a_port.c` in the same directory.
  12. //
  13. // It was compiled with the following command:
  14. //
  15. // ```bash
  16. // clang -O2 -emit-llvm -c load_elf__block_a_port.c -o - | \
  17. // llc -march=bpf -filetype=obj -o load_elf__block_a_port.o
  18. // ```
  19. //
  20. // Once compiled, this program can be injected into Linux kernel, with tc for instance. Sadly, we
  21. // need to bring some modifications to the generated bytecode in order to run it: the three
  22. // instructions with opcode 0x61 load data from a packet area as 4-byte words, where we need to
  23. // load it as 8-bytes double words (0x79). The kernel does the same kind of translation before
  24. // running the program, but rbpf does not implement this.
  25. //
  26. // In addition, the offset at which the pointer to the packet data is stored must be changed: since
  27. // we use 8 bytes instead of 4 for the start and end addresses of the data packet, we cannot use
  28. // the offsets produced by clang (0x4c and 0x50), the addresses would overlap. Instead we can use,
  29. // for example, 0x40 and 0x50.
  30. //
  31. // These change were applied with the following script:
  32. //
  33. // ```bash
  34. // xxd load_elf__block_a_port.o | sed '
  35. // s/6112 5000 0000 0000/7912 5000 0000 0000/ ;
  36. // s/6111 4c00 0000 0000/7911 4000 0000 0000/ ;
  37. // s/6111 2200 0000 0000/7911 2200 0000 0000/' | xxd -r > load_elf__block_a_port.tmp
  38. // mv load_elf__block_a_port.tmp load_elf__block_a_port.o
  39. // ```
  40. //
  41. // The eBPF program was placed into the `.classifier` ELF section (see C code above), which means
  42. // that you can retrieve the raw bytecode with `readelf -x .classifier load_elf__block_a_port.o` or
  43. // with `objdump -s -j .classifier load_elf__block_a_port.o`.
  44. //
  45. // Once the bytecode has been edited, we can load the bytecode directly from the ELF object file.
  46. fn main() {
  47. let filename = "examples/load_elf__block_a_port.o";
  48. let path = PathBuf::from(filename);
  49. let file = match elf::File::open_path(&path) {
  50. Ok(f) => f,
  51. Err(e) => panic!("Error: {:?}", e),
  52. };
  53. let text_scn = match file.get_section(".classifier") {
  54. Some(s) => s,
  55. None => panic!("Failed to look up .classifier section"),
  56. };
  57. let prog = &text_scn.data;
  58. let packet1 = &mut [
  59. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  60. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  61. 0x08, 0x00, // ethertype
  62. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  63. 0xa6, 0xab, 0x40, 0x00,
  64. 0x40, 0x06, 0x96, 0x0f,
  65. 0x7f, 0x00, 0x00, 0x01,
  66. 0x7f, 0x00, 0x00, 0x01,
  67. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  68. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  69. 0xd1, 0xe5, 0xc4, 0x9d,
  70. 0xd4, 0x30, 0xb5, 0xd2,
  71. 0x80, 0x18, 0x01, 0x56,
  72. 0xfe, 0x2f, 0x00, 0x00,
  73. 0x01, 0x01, 0x08, 0x0a, // start data
  74. 0x00, 0x23, 0x75, 0x89,
  75. 0x00, 0x23, 0x63, 0x2d,
  76. 0x71, 0x64, 0x66, 0x73,
  77. 0x64, 0x66, 0x0au8
  78. ];
  79. let packet2 = &mut [
  80. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  81. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  82. 0x08, 0x00, // ethertype
  83. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  84. 0xa6, 0xab, 0x40, 0x00,
  85. 0x40, 0x06, 0x96, 0x0f,
  86. 0x7f, 0x00, 0x00, 0x01,
  87. 0x7f, 0x00, 0x00, 0x01,
  88. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  89. 0x98, 0x76, 0xc6, 0xcc, // start tcp_hdr
  90. 0xd1, 0xe5, 0xc4, 0x9d,
  91. 0xd4, 0x30, 0xb5, 0xd2,
  92. 0x80, 0x18, 0x01, 0x56,
  93. 0xfe, 0x2f, 0x00, 0x00,
  94. 0x01, 0x01, 0x08, 0x0a, // start data
  95. 0x00, 0x23, 0x75, 0x89,
  96. 0x00, 0x23, 0x63, 0x2d,
  97. 0x71, 0x64, 0x66, 0x73,
  98. 0x64, 0x66, 0x0au8
  99. ];
  100. let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50);
  101. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);
  102. let res = vm.prog_exec(packet1);
  103. println!("Packet #1, program returned: {:?} ({:#x})", res, res);
  104. assert_eq!(res, 0xffffffff);
  105. vm.jit_compile();
  106. unsafe {
  107. let res = vm.prog_exec_jit(packet2);
  108. println!("Packet #2, program returned: {:?} ({:#x})", res, res);
  109. assert_eq!(res, 0);
  110. }
  111. }