ubpf_verifier.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
  3. // Copyright 2015 Big Switch Networks, Inc
  4. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  5. // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
  6. // in this file has a name in the form `test_verifier_<name>`, and corresponds to the
  7. // (human-readable) code in `ubpf/tree/master/tests/<name>`, available at
  8. // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
  9. // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
  10. // version to understand what these program do.
  11. //
  12. // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
  13. // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
  14. // The very few modifications that have been realized should be indicated.
  15. // These are unit tests for the eBPF “verifier”.
  16. extern crate rbpf;
  17. use rbpf::assembler::assemble;
  18. use rbpf::ebpf;
  19. #[test]
  20. #[should_panic(expected = "[Verifier] Error: unsupported argument for LE/BE (insn #0)")]
  21. fn test_verifier_err_endian_size() {
  22. #[rustfmt::skip]
  23. let prog = &[
  24. 0xdc, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  25. 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  26. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  27. ];
  28. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  29. vm.execute_program().unwrap();
  30. }
  31. // Note: uBPF has test-err-incomplete-lddw2, which is the same
  32. #[test]
  33. #[should_panic(expected = "[Verifier] Error: incomplete LD_DW instruction (insn #0)")]
  34. fn test_verifier_err_incomplete_lddw() {
  35. #[rustfmt::skip]
  36. let prog = &[
  37. 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  38. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  39. ];
  40. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  41. vm.execute_program().unwrap();
  42. }
  43. #[test]
  44. #[should_panic(expected = "[Verifier] Error: infinite loop")]
  45. fn test_verifier_err_infinite_loop() {
  46. let prog = assemble(
  47. "
  48. ja -1
  49. exit
  50. ",
  51. )
  52. .unwrap();
  53. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  54. vm.execute_program().unwrap();
  55. }
  56. #[test]
  57. #[should_panic(expected = "[Verifier] Error: invalid destination register (insn #0)")]
  58. fn test_verifier_err_invalid_reg_dst() {
  59. let prog = assemble(
  60. "
  61. mov r11, 1
  62. exit
  63. ",
  64. )
  65. .unwrap();
  66. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  67. vm.execute_program().unwrap();
  68. }
  69. #[test]
  70. #[should_panic(expected = "[Verifier] Error: invalid source register (insn #0)")]
  71. fn test_verifier_err_invalid_reg_src() {
  72. let prog = assemble(
  73. "
  74. mov r0, r11
  75. exit
  76. ",
  77. )
  78. .unwrap();
  79. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  80. vm.execute_program().unwrap();
  81. }
  82. #[test]
  83. #[should_panic(expected = "[Verifier] Error: jump to middle of LD_DW at #2 (insn #0)")]
  84. fn test_verifier_err_jmp_lddw() {
  85. let prog = assemble(
  86. "
  87. ja +1
  88. lddw r0, 0x1122334455667788
  89. exit
  90. ",
  91. )
  92. .unwrap();
  93. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  94. vm.execute_program().unwrap();
  95. }
  96. #[test]
  97. #[should_panic(expected = "[Verifier] Error: jump out of code to #3 (insn #0)")]
  98. fn test_verifier_err_jmp_out() {
  99. let prog = assemble(
  100. "
  101. ja +2
  102. exit
  103. ",
  104. )
  105. .unwrap();
  106. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  107. vm.execute_program().unwrap();
  108. }
  109. #[test]
  110. #[should_panic(expected = "[Verifier] Error: program does not end with “EXIT” instruction")]
  111. fn test_verifier_err_no_exit() {
  112. let prog = assemble(
  113. "
  114. mov32 r0, 0",
  115. )
  116. .unwrap();
  117. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  118. vm.execute_program().unwrap();
  119. }
  120. #[test]
  121. fn test_verifier_err_no_exit_backward_jump() {
  122. let prog = assemble(
  123. "
  124. ja +1
  125. exit
  126. ja -2",
  127. )
  128. .unwrap();
  129. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  130. vm.execute_program().unwrap();
  131. }
  132. #[test]
  133. #[should_panic(expected = "[Verifier] Error: eBPF program length limited to 1000000, here 1000001")]
  134. fn test_verifier_err_too_many_instructions() {
  135. // uBPF uses 65637 instructions, because it sets its limit at 65636.
  136. // We use the classic 4096 limit from kernel, so no need to produce as many instructions.
  137. let mut prog = (0..(1_000_000 * ebpf::INSN_SIZE))
  138. .map(|x| match x % 8 {
  139. 0 => 0xb7,
  140. 1 => 0x01,
  141. _ => 0,
  142. })
  143. .collect::<Vec<u8>>();
  144. prog.append(&mut vec![0x95, 0, 0, 0, 0, 0, 0, 0]);
  145. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  146. vm.execute_program().unwrap();
  147. }
  148. #[test]
  149. #[should_panic(expected = "[Verifier] Error: unknown eBPF opcode 0x6 (insn #0)")]
  150. fn test_verifier_err_unknown_opcode() {
  151. #[rustfmt::skip]
  152. let prog = &[
  153. 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  154. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  155. ];
  156. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  157. vm.execute_program().unwrap();
  158. }
  159. #[test]
  160. #[should_panic(expected = "[Verifier] Error: cannot write into register r10 (insn #0)")]
  161. fn test_verifier_err_write_r10() {
  162. let prog = assemble(
  163. "
  164. mov r10, 1
  165. exit
  166. ",
  167. )
  168. .unwrap();
  169. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  170. vm.execute_program().unwrap();
  171. }
  172. #[test]
  173. #[should_panic(expected = "[Verifier] Error: call out of code to #2 (insn #0)")]
  174. fn test_verifier_err_funcall_over_the_end() {
  175. #[rustfmt::skip]
  176. let prog = &[
  177. 0x85, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  178. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. ];
  180. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  181. vm.execute_program().unwrap();
  182. }