4
0

ubpf_verifier.rs 5.1 KB

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