1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bpf
import (
"fmt"
"gvisor.dev/gvisor/pkg/abi/linux"
)
// Possible values for ProgramError.Code.
const (
// DivisionByZero indicates that a program contains, or executed, a
// division or modulo by zero.
DivisionByZero = iota
// InvalidEndOfProgram indicates that the last instruction of a program is
// not a return.
InvalidEndOfProgram
// InvalidInstructionCount indicates that a program has zero instructions
// or more than MaxInstructions instructions.
InvalidInstructionCount
// InvalidJumpTarget indicates that a program contains a jump whose target
// is outside of the program's bounds.
InvalidJumpTarget
// InvalidLoad indicates that a program executed an invalid load of input
// data.
InvalidLoad
// InvalidOpcode indicates that a program contains an instruction with an
// invalid opcode.
InvalidOpcode
// InvalidRegister indicates that a program contains a load from, or store
// to, a non-existent M register (index >= ScratchMemRegisters).
InvalidRegister
)
// Error is an error encountered while compiling or executing a BPF program.
type Error struct {
// Code indicates the kind of error that occurred.
Code int
// PC is the program counter (index into the list of instructions) at which
// the error occurred.
PC int
}
func (e Error) codeString() string {
switch e.Code {
case DivisionByZero:
return "division by zero"
case InvalidEndOfProgram:
return "last instruction must be a return"
case InvalidInstructionCount:
return "invalid number of instructions"
case InvalidJumpTarget:
return "jump target out of bounds"
case InvalidLoad:
return "load out of bounds or violates input alignment requirements"
case InvalidOpcode:
return "invalid instruction opcode"
case InvalidRegister:
return "invalid M register"
default:
return "unknown error"
}
}
// Error implements error.Error.
func (e Error) Error() string {
return fmt.Sprintf("at l%d: %s", e.PC, e.codeString())
}
// Program is a BPF program that has been validated for consistency.
//
// +stateify savable
type Program struct {
instructions []linux.BPFInstruction
}
// Length returns the number of instructions in the program.
func (p Program) Length() int {
return len(p.instructions)
}
// Compile performs validation on a sequence of BPF instructions before
// wrapping them in a Program.
func Compile(insns []linux.BPFInstruction) (Program, error) {
if len(insns) == 0 || len(insns) > MaxInstructions {
return Program{}, Error{InvalidInstructionCount, len(insns)}
}
// The last instruction must be a return.
if last := insns[len(insns)-1]; last.OpCode != (Ret|K) && last.OpCode != (Ret|A) {
return Program{}, Error{InvalidEndOfProgram, len(insns) - 1}
}
// Validate each instruction. Note that we skip a validation Linux does:
// Linux additionally verifies that every load from an M register is
// preceded, in every path, by a store to the same M register, in order to
// avoid having to clear M between programs
// (net/core/filter.c:check_load_and_stores). We always start with a zeroed
// M array.
for pc, i := range insns {
if i.OpCode&unusedBitsMask != 0 {
return Program{}, Error{InvalidOpcode, pc}
}
switch i.OpCode & instructionClassMask {
case Ld:
mode := i.OpCode & loadModeMask
switch i.OpCode & loadSizeMask {
case W:
if mode != Imm && mode != Abs && mode != Ind && mode != Mem && mode != Len {
return Program{}, Error{InvalidOpcode, pc}
}
if mode == Mem && i.K >= ScratchMemRegisters {
return Program{}, Error{InvalidRegister, pc}
}
case H, B:
if mode != Abs && mode != Ind {
return Program{}, Error{InvalidOpcode, pc}
}
default:
return Program{}, Error{InvalidOpcode, pc}
}
case Ldx:
mode := i.OpCode & loadModeMask
switch i.OpCode & loadSizeMask {
case W:
if mode != Imm && mode != Mem && mode != Len {
return Program{}, Error{InvalidOpcode, pc}
}
if mode == Mem && i.K >= ScratchMemRegisters {
return Program{}, Error{InvalidRegister, pc}
}
case B:
if mode != Msh {
return Program{}, Error{InvalidOpcode, pc}
}
default:
return Program{}, Error{InvalidOpcode, pc}
}
case St, Stx:
if i.OpCode&storeUnusedBitsMask != 0 {
return Program{}, Error{InvalidOpcode, pc}
}
if i.K >= ScratchMemRegisters {
return Program{}, Error{InvalidRegister, pc}
}
case Alu:
switch i.OpCode & aluMask {
case Add, Sub, Mul, Or, And, Lsh, Rsh, Xor:
break
case Div, Mod:
if src := i.OpCode & srcAluJmpMask; src == K && i.K == 0 {
return Program{}, Error{DivisionByZero, pc}
}
case Neg:
// Negation doesn't take a source operand.
if i.OpCode&srcAluJmpMask != 0 {
return Program{}, Error{InvalidOpcode, pc}
}
default:
return Program{}, Error{InvalidOpcode, pc}
}
case Jmp:
switch i.OpCode & jmpMask {
case Ja:
// Unconditional jump doesn't take a source operand.
if i.OpCode&srcAluJmpMask != 0 {
return Program{}, Error{InvalidOpcode, pc}
}
// Do the comparison in 64 bits to avoid the possibility of
// overflow from a very large i.K.
if uint64(pc)+uint64(i.K)+1 >= uint64(len(insns)) {
return Program{}, Error{InvalidJumpTarget, pc}
}
case Jeq, Jgt, Jge, Jset:
// jt and jf are uint16s, so there's no threat of overflow.
if pc+int(i.JumpIfTrue)+1 >= len(insns) {
return Program{}, Error{InvalidJumpTarget, pc}
}
if pc+int(i.JumpIfFalse)+1 >= len(insns) {
return Program{}, Error{InvalidJumpTarget, pc}
}
default:
return Program{}, Error{InvalidOpcode, pc}
}
case Ret:
if i.OpCode&retUnusedBitsMask != 0 {
return Program{}, Error{InvalidOpcode, pc}
}
if src := i.OpCode & srcRetMask; src != K && src != A {
return Program{}, Error{InvalidOpcode, pc}
}
case Misc:
if misc := i.OpCode & miscMask; misc != Tax && misc != Txa {
return Program{}, Error{InvalidOpcode, pc}
}
}
}
return Program{insns}, nil
}
// Input represents a source of input data for a BPF program. (BPF
// documentation sometimes refers to the input data as the "packet" due to its
// origins as a packet processing DSL.)
//
// For all of Input's Load methods:
//
// - The second (bool) return value is true if the load succeeded and false
// otherwise.
//
// - Inputs should not assume that the loaded range falls within the input
// data's length. Inputs should return false if the load falls outside of the
// input data.
//
// - Inputs should not assume that the offset is correctly aligned. Inputs may
// choose to service or reject loads to unaligned addresses.
type Input interface {
// Load32 reads 32 bits from the input starting at the given byte offset.
Load32(off uint32) (uint32, bool)
// Load16 reads 16 bits from the input starting at the given byte offset.
Load16(off uint32) (uint16, bool)
// Load8 reads 8 bits from the input starting at the given byte offset.
Load8(off uint32) (uint8, bool)
// Length returns the length of the input in bytes.
Length() uint32
}
// machine represents the state of a BPF virtual machine.
type machine struct {
A uint32
X uint32
M [ScratchMemRegisters]uint32
}
func conditionalJumpOffset(insn linux.BPFInstruction, cond bool) int {
if cond {
return int(insn.JumpIfTrue)
}
return int(insn.JumpIfFalse)
}
// Exec executes a BPF program over the given input and returns its return
// value.
func Exec(p Program, in Input) (uint32, error) {
var m machine
var pc int
for ; pc < len(p.instructions); pc++ {
i := p.instructions[pc]
switch i.OpCode {
case Ld | Imm | W:
m.A = i.K
case Ld | Abs | W:
val, ok := in.Load32(i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = val
case Ld | Abs | H:
val, ok := in.Load16(i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Abs | B:
val, ok := in.Load8(i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Ind | W:
val, ok := in.Load32(m.X + i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = val
case Ld | Ind | H:
val, ok := in.Load16(m.X + i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Ind | B:
val, ok := in.Load8(m.X + i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Mem | W:
m.A = m.M[int(i.K)]
case Ld | Len | W:
m.A = in.Length()
case Ldx | Imm | W:
m.X = i.K
case Ldx | Mem | W:
m.X = m.M[int(i.K)]
case Ldx | Len | W:
m.X = in.Length()
case Ldx | Msh | B:
val, ok := in.Load8(i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.X = 4 * uint32(val&0xf)
case St:
m.M[int(i.K)] = m.A
case Stx:
m.M[int(i.K)] = m.X
case Alu | Add | K:
m.A += i.K
case Alu | Add | X:
m.A += m.X
case Alu | Sub | K:
m.A -= i.K
case Alu | Sub | X:
m.A -= m.X
case Alu | Mul | K:
m.A *= i.K
case Alu | Mul | X:
m.A *= m.X
case Alu | Div | K:
// K != 0 already checked by Compile.
m.A /= i.K
case Alu | Div | X:
if m.X == 0 {
return 0, Error{DivisionByZero, pc}
}
m.A /= m.X
case Alu | Or | K:
m.A |= i.K
case Alu | Or | X:
m.A |= m.X
case Alu | And | K:
m.A &= i.K
case Alu | And | X:
m.A &= m.X
case Alu | Lsh | K:
m.A <<= i.K
case Alu | Lsh | X:
m.A <<= m.X
case Alu | Rsh | K:
m.A >>= i.K
case Alu | Rsh | X:
m.A >>= m.X
case Alu | Neg:
m.A = uint32(-int32(m.A))
case Alu | Mod | K:
// K != 0 already checked by Compile.
m.A %= i.K
case Alu | Mod | X:
if m.X == 0 {
return 0, Error{DivisionByZero, pc}
}
m.A %= m.X
case Alu | Xor | K:
m.A ^= i.K
case Alu | Xor | X:
m.A ^= m.X
case Jmp | Ja:
pc += int(i.K)
case Jmp | Jeq | K:
pc += conditionalJumpOffset(i, m.A == i.K)
case Jmp | Jeq | X:
pc += conditionalJumpOffset(i, m.A == m.X)
case Jmp | Jgt | K:
pc += conditionalJumpOffset(i, m.A > i.K)
case Jmp | Jgt | X:
pc += conditionalJumpOffset(i, m.A > m.X)
case Jmp | Jge | K:
pc += conditionalJumpOffset(i, m.A >= i.K)
case Jmp | Jge | X:
pc += conditionalJumpOffset(i, m.A >= m.X)
case Jmp | Jset | K:
pc += conditionalJumpOffset(i, (m.A&i.K) != 0)
case Jmp | Jset | X:
pc += conditionalJumpOffset(i, (m.A&m.X) != 0)
case Ret | K:
return i.K, nil
case Ret | A:
return m.A, nil
case Misc | Tax:
m.A = m.X
case Misc | Txa:
m.X = m.A
default:
return 0, Error{InvalidOpcode, pc}
}
}
return 0, Error{InvalidEndOfProgram, pc}
}
|