4
0

misc.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // These crates would be needed to load bytecode from a BPF-compiled object file. Since the crates
  7. // are not used anywhere else in the library, it is deactivated: we do not want to load and compile
  8. // them just for the tests. If you want to use them, do not forget to add the following
  9. // dependencies to your Cargo.toml file:
  10. //
  11. // ---
  12. // byteorder = "0.5.3"
  13. // elf = "0.0.10"
  14. // ---
  15. //
  16. // extern crate byteorder;
  17. // extern crate elf;
  18. // use std::path::PathBuf;
  19. extern crate rbpf;
  20. use rbpf::helpers;
  21. // The following two examples have been compiled from C with the following command:
  22. //
  23. // ```bash
  24. // clang -O2 -emit-llvm -c <file.c> -o - | llc -march=bpf -filetype=obj -o <file.o>
  25. // ```
  26. //
  27. // The C source code was the following:
  28. //
  29. // ```c
  30. // #include <linux/ip.h>
  31. // #include <linux/in.h>
  32. // #include <linux/tcp.h>
  33. // #include <linux/bpf.h>
  34. //
  35. // #define ETH_ALEN 6
  36. // #define ETH_P_IP 0x0008 /* htons(0x0800) */
  37. // #define TCP_HDR_LEN 20
  38. //
  39. // #define BLOCKED_TCP_PORT 0x9999
  40. //
  41. // struct eth_hdr {
  42. // unsigned char h_dest[ETH_ALEN];
  43. // unsigned char h_source[ETH_ALEN];
  44. // unsigned short h_proto;
  45. // };
  46. //
  47. // #define SEC(NAME) __attribute__((section(NAME), used))
  48. // SEC(".classifier")
  49. // int handle_ingress(struct __sk_buff *skb)
  50. // {
  51. // void *data = (void *)(long)skb->data;
  52. // void *data_end = (void *)(long)skb->data_end;
  53. // struct eth_hdr *eth = data;
  54. // struct iphdr *iph = data + sizeof(*eth);
  55. // struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*iph);
  56. //
  57. // /* single length check */
  58. // if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*tcp) > data_end)
  59. // return 0;
  60. // if (eth->h_proto != ETH_P_IP)
  61. // return 0;
  62. // if (iph->protocol != IPPROTO_TCP)
  63. // return 0;
  64. // if (tcp->source == BLOCKED_TCP_PORT || tcp->dest == BLOCKED_TCP_PORT)
  65. // return -1;
  66. // return 0;
  67. // }
  68. // char _license[] SEC(".license") = "GPL";
  69. // ```
  70. //
  71. // This program, once compiled, can be injected into Linux kernel, with tc for instance. Sadly, we
  72. // need to bring some modifications to the generated bytecode in order to run it: the three
  73. // instructions with opcode 0x61 load data from a packet area as 4-byte words, where we need to
  74. // load it as 8-bytes double words (0x79). The kernel does the same kind of translation before
  75. // running the program, but rbpf does not implement this.
  76. //
  77. // In addition, the offset at which the pointer to the packet data is stored must be changed: since
  78. // we use 8 bytes instead of 4 for the start and end addresses of the data packet, we cannot use
  79. // the offsets produced by clang (0x4c and 0x50), the addresses would overlap. Instead we can use,
  80. // for example, 0x40 and 0x50. See comments on the bytecode below to see the modifications.
  81. //
  82. // Once the bytecode has been (manually, in our case) edited, we can load the bytecode directly
  83. // from the ELF object file. This is easy to do, but requires the addition of two crates in the
  84. // Cargo.toml file (see comments above), so here we use just the hardcoded bytecode instructions
  85. // instead.
  86. #[test]
  87. fn test_vm_block_port() {
  88. // To load the bytecode from an object file instead of using the hardcoded instructions,
  89. // use the additional crates commented at the beginning of this file (and also add them to your
  90. // Cargo.toml). See comments above.
  91. //
  92. // ---
  93. // let filename = "my_ebpf_object_file.o";
  94. //
  95. // let path = PathBuf::from(filename);
  96. // let file = match elf::File::open_path(&path) {
  97. // Ok(f) => f,
  98. // Err(e) => panic!("Error: {:?}", e),
  99. // };
  100. //
  101. // let text_scn = match file.get_section(".classifier") {
  102. // Some(s) => s,
  103. // None => panic!("Failed to look up .classifier section"),
  104. // };
  105. //
  106. // let ref prog = &text_scn.data;
  107. // ---
  108. let prog = vec![
  109. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  111. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  112. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  114. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  117. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  119. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  122. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  124. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  125. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  129. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  134. ];
  135. let mut packet = vec![
  136. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  137. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  138. 0x08, 0x00, // ethertype
  139. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  140. 0xa6, 0xab, 0x40, 0x00,
  141. 0x40, 0x06, 0x96, 0x0f,
  142. 0x7f, 0x00, 0x00, 0x01,
  143. 0x7f, 0x00, 0x00, 0x01,
  144. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  145. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  146. 0xd1, 0xe5, 0xc4, 0x9d,
  147. 0xd4, 0x30, 0xb5, 0xd2,
  148. 0x80, 0x18, 0x01, 0x56,
  149. 0xfe, 0x2f, 0x00, 0x00,
  150. 0x01, 0x01, 0x08, 0x0a, // start data
  151. 0x00, 0x23, 0x75, 0x89,
  152. 0x00, 0x23, 0x63, 0x2d,
  153. 0x71, 0x64, 0x66, 0x73,
  154. 0x64, 0x66, 0x0au8
  155. ];
  156. let mut vm = rbpf::EbpfVmFixedMbuff::new(&prog, 0x40, 0x50);
  157. vm.register_helper(helpers::BPF_TRACE_PRINTF_IDX, helpers::bpf_trace_printf);
  158. let res = vm.prog_exec(&mut packet);
  159. println!("Program returned: {:?} ({:#x})", res, res);
  160. assert_eq!(res, 0xffffffff);
  161. }
  162. #[test]
  163. fn test_jit_block_port() {
  164. // To load the bytecode from an object file instead of using the hardcoded instructions,
  165. // use the additional crates commented at the beginning of this file (and also add them to your
  166. // Cargo.toml). See comments above.
  167. //
  168. // ---
  169. // let filename = "my_ebpf_object_file.o";
  170. //
  171. // let path = PathBuf::from(filename);
  172. // let file = match elf::File::open_path(&path) {
  173. // Ok(f) => f,
  174. // Err(e) => panic!("Error: {:?}", e),
  175. // };
  176. //
  177. // let text_scn = match file.get_section(".classifier") {
  178. // Some(s) => s,
  179. // None => panic!("Failed to look up .classifier section"),
  180. // };
  181. //
  182. // let ref prog = &text_scn.data;
  183. // ---
  184. let prog = vec![
  185. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  187. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  188. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  190. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  192. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  193. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  195. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  198. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  199. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  200. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  201. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  203. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  205. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  210. ];
  211. let mut packet = vec![
  212. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  213. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  214. 0x08, 0x00, // ethertype
  215. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  216. 0xa6, 0xab, 0x40, 0x00,
  217. 0x40, 0x06, 0x96, 0x0f,
  218. 0x7f, 0x00, 0x00, 0x01,
  219. 0x7f, 0x00, 0x00, 0x01,
  220. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  221. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  222. 0xd1, 0xe5, 0xc4, 0x9d,
  223. 0xd4, 0x30, 0xb5, 0xd2,
  224. 0x80, 0x18, 0x01, 0x56,
  225. 0xfe, 0x2f, 0x00, 0x00,
  226. 0x01, 0x01, 0x08, 0x0a, // start data
  227. 0x00, 0x23, 0x75, 0x89,
  228. 0x00, 0x23, 0x63, 0x2d,
  229. 0x71, 0x64, 0x66, 0x73,
  230. 0x64, 0x66, 0x0au8
  231. ];
  232. let mut vm = rbpf::EbpfVmFixedMbuff::new(&prog, 0x40, 0x50);
  233. vm.register_helper(helpers::BPF_TRACE_PRINTF_IDX, helpers::bpf_trace_printf);
  234. vm.jit_compile();
  235. let res = vm.prog_exec_jit(&mut packet);
  236. println!("Program returned: {:?} ({:#x})", res, res);
  237. assert_eq!(res, 0xffffffff);
  238. }
  239. // Program and memory come from uBPF test ldxh.
  240. #[test]
  241. fn test_vm_mbuff() {
  242. let prog = vec![
  243. // Load mem from mbuff into R1
  244. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  245. // ldhx r1[2], r0
  246. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  247. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  248. ];
  249. let mut mem = vec![
  250. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  251. ];
  252. let mbuff = vec![0u8; 32];
  253. unsafe {
  254. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  255. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  256. *data = mem.as_ptr() as u64;
  257. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  258. }
  259. let vm = rbpf::EbpfVmMbuff::new(&prog);
  260. assert_eq!(vm.prog_exec(&mut mem, mbuff), 0x2211);
  261. }
  262. // Program and memory come from uBPF test ldxh.
  263. #[test]
  264. fn test_jit_mbuff() {
  265. let prog = vec![
  266. // Load mem from mbuff into R1
  267. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  268. // ldhx r1[2], r0
  269. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  270. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  271. ];
  272. let mut mem = vec![
  273. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  274. ];
  275. let mbuff = vec![0u8; 32];
  276. unsafe {
  277. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  278. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  279. *data = mem.as_ptr() as u64;
  280. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  281. }
  282. let mut vm = rbpf::EbpfVmMbuff::new(&prog);
  283. vm.jit_compile();
  284. assert_eq!(vm.prog_exec_jit(&mut mem, mbuff), 0x2211);
  285. }