ubpf_verifier.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
  2. // Copyright 2015 Big Switch Networks, Inc
  3. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  4. //
  5. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  6. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  7. // copied, modified, or distributed except according to those terms.
  8. // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
  9. // in this file has a name in the form `test_verifier_<name>`, and corresponds to the
  10. // (human-readable) code in `ubpf/tree/master/tests/<name>`, available at
  11. // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
  12. // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
  13. // version to understand what these program do.
  14. //
  15. // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
  16. // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
  17. // The very few modifications that have been realized should be indicated.
  18. // These are unit tests for the eBPF “verifier”.
  19. extern crate rbpf;
  20. use rbpf::ebpf;
  21. #[test]
  22. #[should_panic(expected = "[Verifier] Error: division by 0 (insn #1)")]
  23. fn test_verifier_err_div_by_zero_imm() {
  24. let prog = &[
  25. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  26. 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  28. ];
  29. let vm = rbpf::EbpfVmNoData::new(prog);
  30. vm.prog_exec();
  31. }
  32. #[test]
  33. #[should_panic(expected = "[Verifier] Error: unsupported argument for LE/BE (insn #0)")]
  34. fn test_verifier_err_endian_size() {
  35. let prog = &[
  36. 0xdc, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  37. 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  39. ];
  40. let vm = rbpf::EbpfVmNoData::new(prog);
  41. vm.prog_exec();
  42. }
  43. #[test]
  44. #[should_panic(expected = "[Verifier] Error: incomplete LD_DW instruction (insn #0)")]
  45. fn test_verifier_err_incomplete_lddw() { // Note: ubpf has test-err-incomplete-lddw2, which is the same
  46. let prog = &[
  47. 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  48. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  49. ];
  50. let vm = rbpf::EbpfVmNoData::new(prog);
  51. vm.prog_exec();
  52. }
  53. #[test]
  54. #[should_panic(expected = "[Verifier] Error: infinite loop")]
  55. fn test_verifier_err_infinite_loop() {
  56. let prog = &[
  57. 0x05, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  58. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  59. ];
  60. let vm = rbpf::EbpfVmNoData::new(prog);
  61. vm.prog_exec();
  62. }
  63. #[test]
  64. #[should_panic(expected = "[Verifier] Error: invalid destination register (insn #0)")]
  65. fn test_verifier_err_invalid_reg_dst() {
  66. let prog = &[
  67. 0xb7, 0x0b, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  68. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  69. ];
  70. let vm = rbpf::EbpfVmNoData::new(prog);
  71. vm.prog_exec();
  72. }
  73. #[test]
  74. #[should_panic(expected = "[Verifier] Error: invalid source register (insn #0)")]
  75. fn test_verifier_err_invalid_reg_src() {
  76. let prog = &[
  77. 0xbf, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  79. ];
  80. let vm = rbpf::EbpfVmNoData::new(prog);
  81. vm.prog_exec();
  82. }
  83. #[test]
  84. #[should_panic(expected = "[Verifier] Error: jump to middle of LD_DW at #2 (insn #0)")]
  85. fn test_verifier_err_jmp_lddw() {
  86. let prog = &[
  87. 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  89. 0x00, 0x00, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11,
  90. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  91. ];
  92. let vm = rbpf::EbpfVmNoData::new(prog);
  93. vm.prog_exec();
  94. }
  95. #[test]
  96. #[should_panic(expected = "[Verifier] Error: jump out of code to #3 (insn #0)")]
  97. fn test_verifier_err_jmp_out() {
  98. let prog = &[
  99. 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  101. ];
  102. let vm = rbpf::EbpfVmNoData::new(prog);
  103. vm.prog_exec();
  104. }
  105. #[test]
  106. #[should_panic(expected = "[Verifier] Error: program does not end with “EXIT” instruction")]
  107. fn test_verifier_err_no_exit() {
  108. let prog = &[
  109. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  110. ];
  111. let vm = rbpf::EbpfVmNoData::new(prog);
  112. vm.prog_exec();
  113. }
  114. #[test]
  115. #[should_panic(expected = "[Verifier] Error: eBPF program length limited to 4096, here 4097")]
  116. fn test_verifier_err_too_many_instructions() {
  117. // uBPF uses 65637 instructions, because it sets its limit at 65636.
  118. // We use the classic 4096 limit from kernel, so no need to produce as many instructions.
  119. let mut prog = (0..(4096 * ebpf::INSN_SIZE)).map( |x| match x % 8 {
  120. 0 => 0xb7,
  121. 1 => 0x01,
  122. _ => 0
  123. }).collect::<Vec<u8>>();
  124. prog.append(&mut vec![ 0x95, 0, 0, 0, 0, 0, 0, 0 ]);
  125. let vm = rbpf::EbpfVmNoData::new(&prog);
  126. vm.prog_exec();
  127. }
  128. #[test]
  129. #[should_panic(expected = "[Verifier] Error: unknown eBPF opcode 0x6 (insn #0)")]
  130. fn test_verifier_err_unknown_opcode() {
  131. let prog = &[
  132. 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  134. ];
  135. let vm = rbpf::EbpfVmNoData::new(prog);
  136. vm.prog_exec();
  137. }
  138. #[test]
  139. #[should_panic(expected = "[Verifier] Error: cannot write into register r10 (insn #0)")]
  140. fn test_verifier_err_write_r10() {
  141. let prog = &[
  142. 0xb7, 0x0a, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  143. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  144. ];
  145. let vm = rbpf::EbpfVmNoData::new(prog);
  146. vm.prog_exec();
  147. }