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 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
|
// 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"
"strconv"
"strings"
)
// 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 []Instruction
}
// Length returns the number of instructions in the program.
func (p Program) Length() int {
return len(p.instructions)
}
// Compile performs validation and optimization on a sequence of BPF
// instructions before wrapping them in a Program.
func Compile(insns []Instruction, optimize bool) (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}
}
}
}
if optimize {
insns = Optimize(insns)
}
return Program{insns}, nil
}
// machine represents the state of a BPF virtual machine.
type machine struct {
A uint32
X uint32
M [ScratchMemRegisters]uint32
}
func conditionalJumpOffset(insn Instruction, 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[endian Endianness](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 := load32[endian](in, i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = val
case Ld | Abs | H:
val, ok := load16[endian](in, i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Abs | B:
val, ok := load8(in, i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Ind | W:
val, ok := load32[endian](in, m.X+i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = val
case Ld | Ind | H:
val, ok := load16[endian](in, m.X+i.K)
if !ok {
return 0, Error{InvalidLoad, pc}
}
m.A = uint32(val)
case Ld | Ind | B:
val, ok := load8(in, 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 = uint32(len(in))
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 = uint32(len(in))
case Ldx | Msh | B:
val, ok := load8(in, 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}
}
// ExecutionMetrics represents the result of executing a BPF program.
type ExecutionMetrics struct {
// ReturnValue is the result of the program execution.
ReturnValue uint32
// Coverage maps instruction indexes to whether or not they were executed.
// This slice has the same size as the number of instructions as the BPF
// program that was run, so it can be used as a way to get the program size.
// Since an instruction can never run twice in BPF, this can also be used
// to determine how many instructions were executed.
Coverage []bool
// InputAccessed maps input byte offsets to whether or not they were
// read by the program during execution.
InputAccessed []bool
}
// String returns a human-readable view of an `Execution`.
func (e *ExecutionMetrics) String() string {
type intRange struct {
from, to int
}
// addRangeString formats an `intRange` and writes it to `sb`.
addRangeString := func(sb *strings.Builder, rng intRange) {
if rng.from == rng.to {
sb.WriteString(strconv.Itoa(rng.from))
} else {
sb.WriteString(strconv.Itoa(rng.from))
sb.WriteRune('-')
sb.WriteString(strconv.Itoa(rng.to))
}
}
// `getRanges` takes a slice of booleans and returns ranges of all-true
// indexes.
getRanges := func(s []bool) []intRange {
var ranges []intRange
firstTrueIndex := -1
for i, covered := range s {
if covered {
if firstTrueIndex == -1 {
firstTrueIndex = i
}
continue
}
if firstTrueIndex != -1 {
ranges = append(ranges, intRange{firstTrueIndex, i - 1})
firstTrueIndex = -1
}
}
if firstTrueIndex != -1 {
ranges = append(ranges, intRange{firstTrueIndex, len(s) - 1})
}
return ranges
}
// ranges returns a human-friendly representation of the
// ranges of items in `s` that are contiguously `true`.
ranges := func(s []bool) string {
if len(s) == 0 {
return "empty"
}
allFalse := true
allTrue := true
for _, v := range s {
if v {
allFalse = false
} else {
allTrue = false
}
}
if allFalse {
return "none"
}
if allTrue {
return "all"
}
ranges := getRanges(s)
var sb strings.Builder
for i, rng := range ranges {
if i != 0 {
sb.WriteRune(',')
}
addRangeString(&sb, rng)
}
return sb.String()
}
executedInstructions := 0
for _, covered := range e.Coverage {
if covered {
executedInstructions++
}
}
return fmt.Sprintf("returned %d, covered %d/%d instructions (%s), read input bytes %s (%d total input bytes)", e.ReturnValue, executedInstructions, len(e.Coverage), ranges(e.Coverage), ranges(e.InputAccessed), len(e.InputAccessed))
}
// markInputRead marks the `bytesRead` bytes starting at `offset` as having
// been read from the input. This function assumes that the offset and number
// of bytes have already been verified as valid.
func (e *ExecutionMetrics) markInputRead(offset uint32, bytesRead int) {
if int(offset)+bytesRead > len(e.InputAccessed) {
panic(fmt.Sprintf("invalid offset or number of bytes read: offset=%d bytesRead=%d len=%d", offset, bytesRead, len(e.InputAccessed)))
}
for i := 0; i < bytesRead; i++ {
e.InputAccessed[int(offset)+i] = true
}
}
// InstrumentedExec executes a BPF program over the given input while
// instrumenting it: recording memory accesses and lines executed.
// This is slower than Exec, but should return equivalent results.
func InstrumentedExec[endian Endianness](p Program, in Input) (ExecutionMetrics, error) {
ret := ExecutionMetrics{
Coverage: make([]bool, len(p.instructions)),
InputAccessed: make([]bool, len(in)),
}
var m machine
var pc int
for ; pc < len(p.instructions); pc++ {
ret.Coverage[pc] = true
i := p.instructions[pc]
switch i.OpCode {
case Ld | Imm | W:
m.A = i.K
case Ld | Abs | W:
val, ok := load32[endian](in, i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(i.K, 4)
m.A = val
case Ld | Abs | H:
val, ok := load16[endian](in, i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(i.K, 2)
m.A = uint32(val)
case Ld | Abs | B:
val, ok := load8(in, i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(i.K, 1)
m.A = uint32(val)
case Ld | Ind | W:
val, ok := load32[endian](in, m.X+i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(m.X+i.K, 4)
m.A = val
case Ld | Ind | H:
val, ok := load16[endian](in, m.X+i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(m.X+i.K, 2)
m.A = uint32(val)
case Ld | Ind | B:
val, ok := load8(in, m.X+i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(m.X+i.K, 1)
m.A = uint32(val)
case Ld | Mem | W:
m.A = m.M[int(i.K)]
case Ld | Len | W:
m.A = uint32(len(in))
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 = uint32(len(in))
case Ldx | Msh | B:
val, ok := load8(in, i.K)
if !ok {
return ret, Error{InvalidLoad, pc}
}
ret.markInputRead(i.K, 1)
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 ret, 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 ret, 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:
ret.ReturnValue = i.K
return ret, nil
case Ret | A:
ret.ReturnValue = m.A
return ret, nil
case Misc | Tax:
m.A = m.X
case Misc | Txa:
m.X = m.A
default:
return ret, Error{InvalidOpcode, pc}
}
}
return ret, Error{InvalidEndOfProgram, pc}
}
|