4
0

load_elf.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 byteorder;
  7. extern crate elf;
  8. use std::path::PathBuf;
  9. extern crate rbpf;
  10. use rbpf::helpers;
  11. // The following example uses an ELF file that has been compiled from this C source code:
  12. //
  13. // ```
  14. // #include <linux/ip.h>
  15. // #include <linux/in.h>
  16. // #include <linux/tcp.h>
  17. // #include <linux/bpf.h>
  18. //
  19. // #define ETH_ALEN 6
  20. // #define ETH_P_IP 0x0008 /* htons(0x0800) */
  21. // #define TCP_HDR_LEN 20
  22. //
  23. // #define BLOCKED_TCP_PORT 0x9999
  24. //
  25. // struct eth_hdr {
  26. // unsigned char h_dest[ETH_ALEN];
  27. // unsigned char h_source[ETH_ALEN];
  28. // unsigned short h_proto;
  29. // };
  30. //
  31. // #define SEC(NAME) __attribute__((section(NAME), used))
  32. // SEC(".classifier")
  33. // int handle_ingress(struct __sk_buff *skb)
  34. // {
  35. // void *data = (void *)(long)skb->data;
  36. // void *data_end = (void *)(long)skb->data_end;
  37. // struct eth_hdr *eth = data;
  38. // struct iphdr *iph = data + sizeof(*eth);
  39. // struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*iph);
  40. //
  41. // /* single length check */
  42. // if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*tcp) > data_end)
  43. // return 0;
  44. // if (eth->h_proto != ETH_P_IP)
  45. // return 0;
  46. // if (iph->protocol != IPPROTO_TCP)
  47. // return 0;
  48. // if (tcp->source == BLOCKED_TCP_PORT || tcp->dest == BLOCKED_TCP_PORT)
  49. // return -1;
  50. // return 0;
  51. // }
  52. // ```
  53. //
  54. // It was compiled with the following command:
  55. //
  56. // ```bash
  57. // clang -O2 -emit-llvm -c block_a_port.c -o - | \ llc -march=bpf -filetype=obj -o block_a_port.o
  58. // ```
  59. //
  60. // Once compiled, can be injected into Linux kernel, with tc for instance. Sadly, we need to bring
  61. // some modifications to the generated bytecode in order to run it: the three instructions with
  62. // opcode 0x61 load data from a packet area as 4-byte words, where we need to load it as 8-bytes
  63. // double words (0x79). The kernel does the same kind of translation before running the program,
  64. // but rbpf does not implement this.
  65. //
  66. // In addition, the offset at which the pointer to the packet data is stored must be changed: since
  67. // we use 8 bytes instead of 4 for the start and end addresses of the data packet, we cannot use
  68. // the offsets produced by clang (0x4c and 0x50), the addresses would overlap. Instead we can use,
  69. // for example, 0x40 and 0x50.
  70. //
  71. // These change were applied with the following script:
  72. //
  73. // ```bash
  74. // xxd block_a_port.o | sed '
  75. // s/6112 5000 0000 0000/7912 5000 0000 0000/ ;
  76. // s/6111 4c00 0000 0000/7911 4000 0000 0000/ ;
  77. // s/6111 2200 0000 0000/7911 2200 0000 0000/' | xxd -r > block_a_port.tmp
  78. // mv block_a_port.tmp block_a_port.o
  79. // ```
  80. //
  81. // The eBPF program was placed into the `.classifier` ELF section (see C code above), which means
  82. // that you can retrieve the raw bytecode with `readelf -x .classifier block_a_port.o` or with
  83. // `objdump -s -j .classifier block_a_port.o`.
  84. //
  85. // Once the bytecode has been edited, we can load the bytecode directly from the ELF object file.
  86. fn main() {
  87. let filename = "examples/block_a_port.o";
  88. let path = PathBuf::from(filename);
  89. let file = match elf::File::open_path(&path) {
  90. Ok(f) => f,
  91. Err(e) => panic!("Error: {:?}", e),
  92. };
  93. let text_scn = match file.get_section(".classifier") {
  94. Some(s) => s,
  95. None => panic!("Failed to look up .classifier section"),
  96. };
  97. let ref prog = &text_scn.data;
  98. let mut packet1 = vec![
  99. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  100. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  101. 0x08, 0x00, // ethertype
  102. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  103. 0xa6, 0xab, 0x40, 0x00,
  104. 0x40, 0x06, 0x96, 0x0f,
  105. 0x7f, 0x00, 0x00, 0x01,
  106. 0x7f, 0x00, 0x00, 0x01,
  107. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  108. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  109. 0xd1, 0xe5, 0xc4, 0x9d,
  110. 0xd4, 0x30, 0xb5, 0xd2,
  111. 0x80, 0x18, 0x01, 0x56,
  112. 0xfe, 0x2f, 0x00, 0x00,
  113. 0x01, 0x01, 0x08, 0x0a, // start data
  114. 0x00, 0x23, 0x75, 0x89,
  115. 0x00, 0x23, 0x63, 0x2d,
  116. 0x71, 0x64, 0x66, 0x73,
  117. 0x64, 0x66, 0x0au8
  118. ];
  119. let mut packet2 = vec![
  120. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  121. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  122. 0x08, 0x00, // ethertype
  123. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  124. 0xa6, 0xab, 0x40, 0x00,
  125. 0x40, 0x06, 0x96, 0x0f,
  126. 0x7f, 0x00, 0x00, 0x01,
  127. 0x7f, 0x00, 0x00, 0x01,
  128. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  129. 0x98, 0x76, 0xc6, 0xcc, // start tcp_hdr
  130. 0xd1, 0xe5, 0xc4, 0x9d,
  131. 0xd4, 0x30, 0xb5, 0xd2,
  132. 0x80, 0x18, 0x01, 0x56,
  133. 0xfe, 0x2f, 0x00, 0x00,
  134. 0x01, 0x01, 0x08, 0x0a, // start data
  135. 0x00, 0x23, 0x75, 0x89,
  136. 0x00, 0x23, 0x63, 0x2d,
  137. 0x71, 0x64, 0x66, 0x73,
  138. 0x64, 0x66, 0x0au8
  139. ];
  140. let mut vm = rbpf::EbpfVmFixedMbuff::new(&prog, 0x40, 0x50);
  141. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);
  142. let res = vm.prog_exec(&mut packet1);
  143. println!("Packet #1, program returned: {:?} ({:#x})", res, res);
  144. assert_eq!(res, 0xffffffff);
  145. vm.jit_compile();
  146. unsafe {
  147. let res = vm.prog_exec_jit(&mut packet2);
  148. println!("Packet #2, program returned: {:?} ({:#x})", res, res);
  149. assert_eq!(res, 0);
  150. }
  151. }