ubpf_verifier.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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: division by 0 (insn #1)")]
  21. fn test_verifier_err_div_by_zero_imm() {
  22. let prog = assemble("
  23. mov32 r0, 1
  24. div32 r0, 0
  25. exit").unwrap();
  26. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  27. vm.execute_program().unwrap();
  28. }
  29. #[test]
  30. #[should_panic(expected = "[Verifier] Error: unsupported argument for LE/BE (insn #0)")]
  31. fn test_verifier_err_endian_size() {
  32. let prog = &[
  33. 0xdc, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  34. 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  36. ];
  37. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  38. vm.execute_program().unwrap();
  39. }
  40. #[test]
  41. #[should_panic(expected = "[Verifier] Error: incomplete LD_DW instruction (insn #0)")]
  42. fn test_verifier_err_incomplete_lddw() { // Note: ubpf has test-err-incomplete-lddw2, which is the same
  43. let prog = &[
  44. 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  45. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  46. ];
  47. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  48. vm.execute_program().unwrap();
  49. }
  50. #[test]
  51. #[should_panic(expected = "[Verifier] Error: infinite loop")]
  52. fn test_verifier_err_infinite_loop() {
  53. let prog = assemble("
  54. ja -1
  55. exit").unwrap();
  56. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  57. vm.execute_program().unwrap();
  58. }
  59. #[test]
  60. #[should_panic(expected = "[Verifier] Error: invalid destination register (insn #0)")]
  61. fn test_verifier_err_invalid_reg_dst() {
  62. let prog = assemble("
  63. mov r11, 1
  64. exit").unwrap();
  65. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  66. vm.execute_program().unwrap();
  67. }
  68. #[test]
  69. #[should_panic(expected = "[Verifier] Error: invalid source register (insn #0)")]
  70. fn test_verifier_err_invalid_reg_src() {
  71. let prog = assemble("
  72. mov r0, r11
  73. exit").unwrap();
  74. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  75. vm.execute_program().unwrap();
  76. }
  77. #[test]
  78. #[should_panic(expected = "[Verifier] Error: jump to middle of LD_DW at #2 (insn #0)")]
  79. fn test_verifier_err_jmp_lddw() {
  80. let prog = assemble("
  81. ja +1
  82. lddw r0, 0x1122334455667788
  83. exit").unwrap();
  84. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  85. vm.execute_program().unwrap();
  86. }
  87. #[test]
  88. #[should_panic(expected = "[Verifier] Error: jump out of code to #3 (insn #0)")]
  89. fn test_verifier_err_jmp_out() {
  90. let prog = assemble("
  91. ja +2
  92. exit").unwrap();
  93. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  94. vm.execute_program().unwrap();
  95. }
  96. #[test]
  97. #[should_panic(expected = "[Verifier] Error: program does not end with “EXIT” instruction")]
  98. fn test_verifier_err_no_exit() {
  99. let prog = assemble("
  100. mov32 r0, 0").unwrap();
  101. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  102. vm.execute_program().unwrap();
  103. }
  104. #[test]
  105. fn test_verifier_err_no_exit_backward_jump() {
  106. let prog = assemble("
  107. ja +1
  108. exit
  109. ja -2").unwrap();
  110. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  111. vm.execute_program().unwrap();
  112. }
  113. #[test]
  114. #[should_panic(expected = "[Verifier] Error: eBPF program length limited to 4096, here 4097")]
  115. fn test_verifier_err_too_many_instructions() {
  116. // uBPF uses 65637 instructions, because it sets its limit at 65636.
  117. // We use the classic 4096 limit from kernel, so no need to produce as many instructions.
  118. let mut prog = (0..(4096 * ebpf::INSN_SIZE)).map( |x| match x % 8 {
  119. 0 => 0xb7,
  120. 1 => 0x01,
  121. _ => 0
  122. }).collect::<Vec<u8>>();
  123. prog.append(&mut vec![ 0x95, 0, 0, 0, 0, 0, 0, 0 ]);
  124. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  125. vm.execute_program().unwrap();
  126. }
  127. #[test]
  128. #[should_panic(expected = "[Verifier] Error: unknown eBPF opcode 0x6 (insn #0)")]
  129. fn test_verifier_err_unknown_opcode() {
  130. let prog = &[
  131. 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  133. ];
  134. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  135. vm.execute_program().unwrap();
  136. }
  137. #[test]
  138. #[should_panic(expected = "[Verifier] Error: cannot write into register r10 (insn #0)")]
  139. fn test_verifier_err_write_r10() {
  140. let prog = assemble("
  141. mov r10, 1
  142. exit").unwrap();
  143. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  144. vm.execute_program().unwrap();
  145. }