ubpf_verifier.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. ja -1
  48. exit
  49. ").unwrap();
  50. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  51. vm.execute_program().unwrap();
  52. }
  53. #[test]
  54. #[should_panic(expected = "[Verifier] Error: invalid destination register (insn #0)")]
  55. fn test_verifier_err_invalid_reg_dst() {
  56. let prog = assemble("
  57. mov r11, 1
  58. exit
  59. ").unwrap();
  60. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  61. vm.execute_program().unwrap();
  62. }
  63. #[test]
  64. #[should_panic(expected = "[Verifier] Error: invalid source register (insn #0)")]
  65. fn test_verifier_err_invalid_reg_src() {
  66. let prog = assemble("
  67. mov r0, r11
  68. exit
  69. ").unwrap();
  70. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  71. vm.execute_program().unwrap();
  72. }
  73. #[test]
  74. #[should_panic(expected = "[Verifier] Error: jump to middle of LD_DW at #2 (insn #0)")]
  75. fn test_verifier_err_jmp_lddw() {
  76. let prog = assemble("
  77. ja +1
  78. lddw r0, 0x1122334455667788
  79. exit
  80. ").unwrap();
  81. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  82. vm.execute_program().unwrap();
  83. }
  84. #[test]
  85. #[should_panic(expected = "[Verifier] Error: jump out of code to #3 (insn #0)")]
  86. fn test_verifier_err_jmp_out() {
  87. let prog = assemble("
  88. ja +2
  89. exit
  90. ").unwrap();
  91. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  92. vm.execute_program().unwrap();
  93. }
  94. #[test]
  95. #[should_panic(expected = "[Verifier] Error: program does not end with “EXIT” instruction")]
  96. fn test_verifier_err_no_exit() {
  97. let prog = assemble("
  98. mov32 r0, 0").unwrap();
  99. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  100. vm.execute_program().unwrap();
  101. }
  102. #[test]
  103. fn test_verifier_err_no_exit_backward_jump() {
  104. let prog = assemble("
  105. ja +1
  106. exit
  107. ja -2").unwrap();
  108. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  109. vm.execute_program().unwrap();
  110. }
  111. #[test]
  112. #[should_panic(expected = "[Verifier] Error: eBPF program length limited to 1000000, here 1000001")]
  113. fn test_verifier_err_too_many_instructions() {
  114. // uBPF uses 65637 instructions, because it sets its limit at 65636.
  115. // We use the classic 4096 limit from kernel, so no need to produce as many instructions.
  116. let mut prog = (0..(1_000_000 * ebpf::INSN_SIZE)).map( |x| match x % 8 {
  117. 0 => 0xb7,
  118. 1 => 0x01,
  119. _ => 0
  120. }).collect::<Vec<u8>>();
  121. prog.append(&mut vec![ 0x95, 0, 0, 0, 0, 0, 0, 0 ]);
  122. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  123. vm.execute_program().unwrap();
  124. }
  125. #[test]
  126. #[should_panic(expected = "[Verifier] Error: unknown eBPF opcode 0x6 (insn #0)")]
  127. fn test_verifier_err_unknown_opcode() {
  128. #[rustfmt::skip]
  129. let prog = &[
  130. 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  132. ];
  133. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  134. vm.execute_program().unwrap();
  135. }
  136. #[test]
  137. #[should_panic(expected = "[Verifier] Error: cannot write into register r10 (insn #0)")]
  138. fn test_verifier_err_write_r10() {
  139. let prog = assemble("
  140. mov r10, 1
  141. exit
  142. ").unwrap();
  143. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  144. vm.execute_program().unwrap();
  145. }
  146. #[test]
  147. #[should_panic(expected = "[Verifier] Error: call out of code to #2 (insn #0)")]
  148. fn test_verifier_err_funcall_over_the_end() {
  149. #[rustfmt::skip]
  150. let prog = &[
  151. 0x85, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  152. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. ];
  154. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  155. vm.execute_program().unwrap();
  156. }