vm_load_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package bpf_test
  5. import (
  6. "net"
  7. "testing"
  8. "golang.org/x/net/bpf"
  9. "golang.org/x/net/ipv4"
  10. )
  11. func TestVMLoadAbsoluteOffsetOutOfBounds(t *testing.T) {
  12. vm, done, err := testVM(t, []bpf.Instruction{
  13. bpf.LoadAbsolute{
  14. Off: 100,
  15. Size: 2,
  16. },
  17. bpf.RetA{},
  18. })
  19. if err != nil {
  20. t.Fatalf("failed to load BPF program: %v", err)
  21. }
  22. defer done()
  23. out, err := vm.Run([]byte{
  24. 0xff, 0xff, 0xff, 0xff,
  25. 0xff, 0xff, 0xff, 0xff,
  26. 0, 1, 2, 3,
  27. })
  28. if err != nil {
  29. t.Fatalf("unexpected error while running program: %v", err)
  30. }
  31. if want, got := 0, out; want != got {
  32. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  33. want, got)
  34. }
  35. }
  36. func TestVMLoadAbsoluteOffsetPlusSizeOutOfBounds(t *testing.T) {
  37. vm, done, err := testVM(t, []bpf.Instruction{
  38. bpf.LoadAbsolute{
  39. Off: 8,
  40. Size: 2,
  41. },
  42. bpf.RetA{},
  43. })
  44. if err != nil {
  45. t.Fatalf("failed to load BPF program: %v", err)
  46. }
  47. defer done()
  48. out, err := vm.Run([]byte{
  49. 0xff, 0xff, 0xff, 0xff,
  50. 0xff, 0xff, 0xff, 0xff,
  51. 0,
  52. })
  53. if err != nil {
  54. t.Fatalf("unexpected error while running program: %v", err)
  55. }
  56. if want, got := 0, out; want != got {
  57. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  58. want, got)
  59. }
  60. }
  61. func TestVMLoadAbsoluteBadInstructionSize(t *testing.T) {
  62. _, _, err := testVM(t, []bpf.Instruction{
  63. bpf.LoadAbsolute{
  64. Size: 5,
  65. },
  66. bpf.RetA{},
  67. })
  68. if errStr(err) != "assembling instruction 1: invalid load byte length 0" {
  69. t.Fatalf("unexpected error: %v", err)
  70. }
  71. }
  72. func TestVMLoadConstantOK(t *testing.T) {
  73. vm, done, err := testVM(t, []bpf.Instruction{
  74. bpf.LoadConstant{
  75. Dst: bpf.RegX,
  76. Val: 9,
  77. },
  78. bpf.TXA{},
  79. bpf.RetA{},
  80. })
  81. if err != nil {
  82. t.Fatalf("failed to load BPF program: %v", err)
  83. }
  84. defer done()
  85. out, err := vm.Run([]byte{
  86. 0xff, 0xff, 0xff, 0xff,
  87. 0xff, 0xff, 0xff, 0xff,
  88. 0,
  89. })
  90. if err != nil {
  91. t.Fatalf("unexpected error while running program: %v", err)
  92. }
  93. if want, got := 1, out; want != got {
  94. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  95. want, got)
  96. }
  97. }
  98. func TestVMLoadIndirectOutOfBounds(t *testing.T) {
  99. vm, done, err := testVM(t, []bpf.Instruction{
  100. bpf.LoadIndirect{
  101. Off: 100,
  102. Size: 1,
  103. },
  104. bpf.RetA{},
  105. })
  106. if err != nil {
  107. t.Fatalf("failed to load BPF program: %v", err)
  108. }
  109. defer done()
  110. out, err := vm.Run([]byte{
  111. 0xff, 0xff, 0xff, 0xff,
  112. 0xff, 0xff, 0xff, 0xff,
  113. 0,
  114. })
  115. if err != nil {
  116. t.Fatalf("unexpected error while running program: %v", err)
  117. }
  118. if want, got := 0, out; want != got {
  119. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  120. want, got)
  121. }
  122. }
  123. func TestVMLoadMemShiftOutOfBounds(t *testing.T) {
  124. vm, done, err := testVM(t, []bpf.Instruction{
  125. bpf.LoadMemShift{
  126. Off: 100,
  127. },
  128. bpf.RetA{},
  129. })
  130. if err != nil {
  131. t.Fatalf("failed to load BPF program: %v", err)
  132. }
  133. defer done()
  134. out, err := vm.Run([]byte{
  135. 0xff, 0xff, 0xff, 0xff,
  136. 0xff, 0xff, 0xff, 0xff,
  137. 0,
  138. })
  139. if err != nil {
  140. t.Fatalf("unexpected error while running program: %v", err)
  141. }
  142. if want, got := 0, out; want != got {
  143. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  144. want, got)
  145. }
  146. }
  147. const (
  148. dhcp4Port = 53
  149. )
  150. func TestVMLoadMemShiftLoadIndirectNoResult(t *testing.T) {
  151. vm, in, done := testDHCPv4(t)
  152. defer done()
  153. // Append mostly empty UDP header with incorrect DHCPv4 port
  154. in = append(in, []byte{
  155. 0, 0,
  156. 0, dhcp4Port + 1,
  157. 0, 0,
  158. 0, 0,
  159. }...)
  160. out, err := vm.Run(in)
  161. if err != nil {
  162. t.Fatalf("unexpected error while running program: %v", err)
  163. }
  164. if want, got := 0, out; want != got {
  165. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  166. want, got)
  167. }
  168. }
  169. func TestVMLoadMemShiftLoadIndirectOK(t *testing.T) {
  170. vm, in, done := testDHCPv4(t)
  171. defer done()
  172. // Append mostly empty UDP header with correct DHCPv4 port
  173. in = append(in, []byte{
  174. 0, 0,
  175. 0, dhcp4Port,
  176. 0, 0,
  177. 0, 0,
  178. }...)
  179. out, err := vm.Run(in)
  180. if err != nil {
  181. t.Fatalf("unexpected error while running program: %v", err)
  182. }
  183. if want, got := len(in)-8, out; want != got {
  184. t.Fatalf("unexpected number of output bytes:\n- want: %d\n- got: %d",
  185. want, got)
  186. }
  187. }
  188. func testDHCPv4(t *testing.T) (virtualMachine, []byte, func()) {
  189. // DHCPv4 test data courtesy of David Anderson:
  190. // https://github.com/google/netboot/blob/master/dhcp4/conn_linux.go#L59-L70
  191. vm, done, err := testVM(t, []bpf.Instruction{
  192. // Load IPv4 packet length
  193. bpf.LoadMemShift{Off: 8},
  194. // Get UDP dport
  195. bpf.LoadIndirect{Off: 8 + 2, Size: 2},
  196. // Correct dport?
  197. bpf.JumpIf{Cond: bpf.JumpEqual, Val: dhcp4Port, SkipFalse: 1},
  198. // Accept
  199. bpf.RetConstant{Val: 1500},
  200. // Ignore
  201. bpf.RetConstant{Val: 0},
  202. })
  203. if err != nil {
  204. t.Fatalf("failed to load BPF program: %v", err)
  205. }
  206. // Minimal requirements to make a valid IPv4 header
  207. h := &ipv4.Header{
  208. Len: ipv4.HeaderLen,
  209. Src: net.IPv4(192, 168, 1, 1),
  210. Dst: net.IPv4(192, 168, 1, 2),
  211. }
  212. hb, err := h.Marshal()
  213. if err != nil {
  214. t.Fatalf("failed to marshal IPv4 header: %v", err)
  215. }
  216. hb = append([]byte{
  217. 0xff, 0xff, 0xff, 0xff,
  218. 0xff, 0xff, 0xff, 0xff,
  219. }, hb...)
  220. return vm, hb, done
  221. }