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 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
|
// Package cbpfc implements a cBPF (classic BPF) to eBPF
// (extended BPF, not be confused with cBPF extensions) compiler.
//
// cbpfc can compile cBPF filters to:
// - C, which can be compiled to eBPF with Clang
// - eBPF
//
// Both the C and eBPF output are intended to be accepted by the kernel verifier:
// - All packet loads are guarded with runtime packet length checks
// - RegA and RegX are zero initialized as required
// - Division by zero is guarded by runtime checks
//
// The generated C / eBPF is intended to be embedded into a larger C / eBPF program.
package cbpfc
import (
"fmt"
"sort"
"github.com/pkg/errors"
"golang.org/x/net/bpf"
)
// maxPacketOffset is the maximum packet offset the verifier allows.
// https://elixir.bootlin.com/linux/v5.14.8/source/kernel/bpf/verifier.c#L3223
const maxPacketOffset = 0xFFFF
// Map conditionals to their inverse
var condToInverse = map[bpf.JumpTest]bpf.JumpTest{
bpf.JumpEqual: bpf.JumpNotEqual,
bpf.JumpNotEqual: bpf.JumpEqual,
bpf.JumpGreaterThan: bpf.JumpLessOrEqual,
bpf.JumpLessThan: bpf.JumpGreaterOrEqual,
bpf.JumpGreaterOrEqual: bpf.JumpLessThan,
bpf.JumpLessOrEqual: bpf.JumpGreaterThan,
bpf.JumpBitsSet: bpf.JumpBitsNotSet,
bpf.JumpBitsNotSet: bpf.JumpBitsSet,
}
// pos stores the absolute position of a cBPF instruction
type pos uint
// skips store cBPF jumps, which are relative
type skip uint
// instruction wraps a bpf instruction with it's
// original position
type instruction struct {
bpf.Instruction
id pos
}
func (i instruction) String() string {
return fmt.Sprintf("%d: %v", i.id, i.Instruction)
}
// block contains a linear flow on instructions:
// - Nothing jumps into the middle of a block
// - Nothing jumps out of the middle of a block
//
// A block may start or end with any instruction, as any instruction
// can be the target of a jump.
//
// A block also knows what blocks it jumps to. This forms a DAG of blocks.
type block struct {
// Should not be directly modified, instead copy instructions to new slice
insns []instruction
// Map of absolute instruction positions the last instruction
// of this block can jump to, to the corresponding block
jumps map[pos]*block
// id of the instruction that started this block
// Unique, but not guaranteed to match insns[0].id after blocks are modified
id pos
}
// newBlock creates a block with copy of insns
func newBlock(insns []instruction) *block {
return &block{
insns: insns,
jumps: make(map[pos]*block),
id: insns[0].id,
}
}
func (b *block) Label() string {
return fmt.Sprintf("block_%d", b.id)
}
func (b *block) skipToPos(s skip) pos {
return b.last().id + 1 + pos(s)
}
// Get the target block of a skip
func (b *block) skipToBlock(s skip) *block {
return b.jumps[b.skipToPos(s)]
}
func (b *block) last() instruction {
return b.insns[len(b.insns)-1]
}
// packetGuard is a "fake" cBPF instruction
// that checks packet bounds before data is read from the packet.
type packetGuard interface {
bpf.Instruction
// Extend returns a guard that is the union of the current guard and o.
extend(o packetGuard) packetGuard
// Restrict returns a guard that is the intersection of the current guard and o.
restrict(o packetGuard) packetGuard
// Adjust any instructions that are covered by this guard as required.
adjustInsns(insns []instruction)
}
// packetGuardAbsolute checks packet bounds for absolute packet loads (constant offset).
// We only need to track the last / greatest byte read to ensure it isn't past the packet end.
type packetGuardAbsolute struct {
// The furthest (exclusive) byte read.
end int32
}
func newPacketGuardAbsolute(off uint32, size int) packetGuardAbsolute {
if off > maxPacketOffset {
panic("can't create absolute packet guard for offset")
}
// Absolute offsets are limited to maxPacketOffset so this can't overflow.
return packetGuardAbsolute{int32(off) + int32(size)}
}
func (a packetGuardAbsolute) extend(o packetGuard) packetGuard {
n := a
if b := o.(packetGuardAbsolute); b.end > a.end {
n.end = b.end
}
return n
}
func (a packetGuardAbsolute) restrict(o packetGuard) packetGuard {
n := a
if b := o.(packetGuardAbsolute); b.end < a.end {
n.end = b.end
}
return n
}
// We don't need to adjust instructions for absolute guards.
func (a packetGuardAbsolute) adjustInsns(insns []instruction) {}
// Assemble implements the Instruction Assemble method.
func (p packetGuardAbsolute) Assemble() (bpf.RawInstruction, error) {
return bpf.RawInstruction{}, errors.Errorf("unsupported")
}
// packetGuardIndirect checks packet bounds for indirect packet loads (RegX + constant offset).
// RegX and offset are both allowed to be negative, but RegX + Offset must be >= 0 (the verifier does not allow
// adding negative offsets to packet pointers).
//
// This requires tracking both the first and last byte read (relative to RegX) to check:
// - RegX + start >= 0
// - RegX + end < maxPacketOffset
// - packet_start + RegX + end < packet_end
//
// Bounds / range information is propagated in the verifier by copying a packet pointer,
// adding a constant (which yields a "derived" packet pointer with the same ID), and checking it against the packet_end.
// Subsequent LoadIndirects that are covered by this guard need to use a packet pointer with same ID as the guard to
// take advantage of the bounds.
// Ideally we would use packet_start + RegX and let each LoadIndirect instruction add its own offset,
// but the verifier doesn't allow the use of packet pointers with a negative offset (even if the offset
// would make the read positive: https://elixir.bootlin.com/linux/v5.14.12/source/kernel/bpf/verifier.c#L3287)
//
// So instead we check:
// - RegX + start >= 0
// - RegX + start < maxPacketOffset - length
// - packet_start + RegX + start + length < packet_end
//
// This lets us reuse packet_start + RegX + start as the packet pointer for LoadIndirect,
// but means we need to rewrite the offsets of LoadIndirect instructions covered by this guard to subtract length.
type packetGuardIndirect struct {
// First byte read (inclusive).
start int32
// Last byte read (exclusive).
// int64 to avoid overflows with INT32_MAX + size
end int64
}
func newPacketGuardIndirect(off uint32, size int) packetGuardIndirect {
// cBPF offsets are uint32, but are signed in reality
// LoadIndirect offsets are encoded as uint32 by x/net/bpf, but are signed in reality.
// Unlike LoadAbsolute, restrictions only apply to RegX + Offset and not Offset alone,
// so we have to allow INT32_MAX / INT32_MIN offsets.
return packetGuardIndirect{
start: int32(off),
end: int64(int32(off)) + int64(size),
}
}
func (a packetGuardIndirect) extend(o packetGuard) packetGuard {
b := o.(packetGuardIndirect)
// A 0 guard means no guard, we shouldn't extend it to cover {0,0}
if a == (packetGuardIndirect{}) {
return b
}
if b == (packetGuardIndirect{}) {
return a
}
n := a
if b.start < a.start {
n.start = b.start
}
if b.end > a.end {
n.end = b.end
}
return n
}
func (a packetGuardIndirect) restrict(o packetGuard) packetGuard {
b := o.(packetGuardIndirect)
// A 0 guard means no guard, that restricts everything to no guard.
if a == (packetGuardIndirect{}) || b == (packetGuardIndirect{}) {
return packetGuardIndirect{}
}
n := a
if b.start > a.start {
n.start = b.start
}
if b.end < a.end {
n.end = b.end
}
return n
}
// int32(RegX) + p.start must be < to maxStartOffset().
// This checks that it is positive, and int32(RegX) + p.end doesn't exceed maxPacketOffset.
// Returns 0 (check will always be false) if there is no way for the start and end of the guard to be < maxPacketOffset.
func (p packetGuardIndirect) maxStartOffset() int32 {
length := p.end - int64(p.start)
// If length exceeds maxPacketOffset, there's no way for RegX + start >= 0 and RegX + end < maxPacketOffset.
// Return 0 so the check fails, and we return noMatch.
if length > maxPacketOffset {
return 0
}
// +1 as it needs to be strictly less than.
// This lets us return 0 above to get noMatch.
return int32(maxPacketOffset) - int32(length) + 1
}
// packet_start + (int32(x) + p.start) + p.length() must be <= packet_end.
// This lets us reuse the (int32(x) + p.start) from the maxStartOffset() check, to keep the bounds info.
func (p packetGuardIndirect) length() int32 {
// This can overflow, but it doesn't matter as we'll already have checked maxStartOffset()
// and caught the overflow there.
return int32(p.end - int64(p.start))
}
// Once we've determined the guard that applies for a given set of insns,
// asjust the offsets so they're relative to the smallest / start of the guard.
func (p packetGuardIndirect) adjustInsns(insns []instruction) {
for i := range insns {
switch insn := insns[i].Instruction.(type) {
case bpf.LoadIndirect:
insns[i].Instruction = bpf.LoadIndirect{
Off: uint32(int32(insn.Off) - p.start),
Size: insn.Size,
}
}
}
}
// Assemble implements the Instruction Assemble method.
func (p packetGuardIndirect) Assemble() (bpf.RawInstruction, error) {
return bpf.RawInstruction{}, errors.Errorf("unsupported")
}
// checksXNotZero is a "fake" instruction
// that returns no match if X is 0
type checkXNotZero struct {
}
// Assemble implements the Instruction Assemble method.
func (c checkXNotZero) Assemble() (bpf.RawInstruction, error) {
return bpf.RawInstruction{}, errors.Errorf("unsupported")
}
// compile compiles a cBPF program to an ordered slice of blocks, with:
// - Registers zero initialized as required
// - Required packet access guards added
// - JumpIf and JumpIfX instructions normalized (see normalizeJumps)
func compile(insns []bpf.Instruction) ([]*block, error) {
err := validateInstructions(insns)
if err != nil {
return nil, err
}
instructions := toInstructions(insns)
normalizeJumps(instructions)
// Split into blocks
blocks, err := splitBlocks(instructions)
if err != nil {
return nil, errors.Wrapf(err, "unable to compute blocks")
}
// Initialize registers
err = initializeMemory(blocks)
if err != nil {
return nil, err
}
// Check we don't divide by zero
err = addDivideByZeroGuards(blocks)
if err != nil {
return nil, err
}
rewriteLargePacketOffsets(&blocks)
// Guard packet loads
addAbsolutePacketGuards(blocks)
addIndirectPacketGuards(blocks)
return blocks, nil
}
// validateInstructions checks the instructions are valid, and we support them
func validateInstructions(insns []bpf.Instruction) error {
// Can't do anything meaningful with no instructions
if len(insns) == 0 {
return errors.New("can't compile 0 instructions")
}
for pc, insn := range insns {
// Assemble does some input validation
_, err := insn.Assemble()
if err != nil {
return errors.Errorf("can't assemble instruction %d: %v", pc, insn)
}
switch i := insn.(type) {
case bpf.RawInstruction:
return errors.Errorf("unsupported instruction %d: %v", pc, insn)
// Negative constant offsets are used for extensions (and if they're supported, x/net/bpf will parse them)
// and other packet addressing modes we don't support: https://elixir.bootlin.com/linux/v5.14.10/source/kernel/bpf/core.c#L65
case bpf.LoadAbsolute:
if int32(i.Off) < 0 {
return errors.Errorf("LoadAbsolute negative offset %v", int32(i.Off))
}
case bpf.LoadMemShift:
if int32(i.Off) < 0 {
return errors.Errorf("LoadMemShift negative offset %v", int32(i.Off))
}
case bpf.LoadExtension:
switch i.Num {
case bpf.ExtLen:
break
default:
return errors.Errorf("unsupported BPF extension %d: %v", pc, insn)
}
}
}
return nil
}
func toInstructions(insns []bpf.Instruction) []instruction {
instructions := make([]instruction, len(insns))
for pc, insn := range insns {
instructions[pc] = instruction{
Instruction: insn,
id: pos(pc),
}
}
return instructions
}
// normalizeJumps normalizes conditional jumps to always use skipTrue:
// Jumps that only use skipTrue (skipFalse == 0) are unchanged.
// Jumps that use both skipTrue and skipFalse are unchanged.
// Jumps that only use skipFalse (skipTrue == 0) are inverted to only use skipTrue.
func normalizeJumps(insns []instruction) {
for pc := range insns {
switch i := insns[pc].Instruction.(type) {
case bpf.JumpIf:
if !shouldInvert(i.SkipTrue, i.SkipFalse) {
continue
}
insns[pc].Instruction = bpf.JumpIf{Cond: condToInverse[i.Cond], Val: i.Val, SkipTrue: i.SkipFalse, SkipFalse: i.SkipTrue}
case bpf.JumpIfX:
if !shouldInvert(i.SkipTrue, i.SkipFalse) {
continue
}
insns[pc].Instruction = bpf.JumpIfX{Cond: condToInverse[i.Cond], SkipTrue: i.SkipFalse, SkipFalse: i.SkipTrue}
}
}
}
// Check if a conditional jump should be inverted
func shouldInvert(skipTrue, skipFalse uint8) bool {
return skipTrue == 0 && skipFalse != 0
}
// Traverse instructions until end of first block. Target is absolute start of block.
// Return block-relative jump targets
func visitBlock(insns []instruction, target pos) (*block, []skip) {
for pc, insn := range insns {
// Relative jumps from this instruction
var skips []skip
switch i := insn.Instruction.(type) {
case bpf.Jump:
skips = []skip{skip(i.Skip)}
case bpf.JumpIf:
skips = []skip{skip(i.SkipTrue), skip(i.SkipFalse)}
case bpf.JumpIfX:
skips = []skip{skip(i.SkipTrue), skip(i.SkipFalse)}
case bpf.RetA, bpf.RetConstant:
// No extra targets to visit
default:
// Regular instruction, next please!
continue
}
// every insn including this one
return newBlock(insns[:pc+1]), skips
}
// Try to fall through to next block
return newBlock(insns), []skip{0}
}
// splitBlocks splits the cBPF into an ordered list of blocks.
//
// The blocks are preserved in the order they are found as this guarantees that
// a block only targets later blocks (cBPF jumps are positive, relative offsets).
// This also mimics the layout of the original cBPF, which is good for debugging.
func splitBlocks(instructions []instruction) ([]*block, error) {
// Blocks we've visited already
blocks := []*block{}
// map of targets to blocks that target them
// target 0 is for the base case
targets := map[pos][]*block{
0: nil,
}
// As long as we have un visited targets
for len(targets) > 0 {
sortedTargets := sortTargets(targets)
// Get the first one (not really breadth first, but close enough!)
target := sortedTargets[0]
end := len(instructions)
// If there's a next target, ensure we stop before it
if len(sortedTargets) > 1 {
end = int(sortedTargets[1])
}
next, nextSkips := visitBlock(instructions[target:end], target)
// Add skips to our list of things to visit
for _, s := range nextSkips {
// Convert relative skip to absolute pos
t := next.skipToPos(s)
if t >= pos(len(instructions)) {
return nil, errors.Errorf("instruction %v flows past last instruction", next.last())
}
targets[t] = append(targets[t], next)
}
jmpBlocks := targets[target]
// Mark all the blocks that jump to the block we've just visited as doing so
for _, jmpBlock := range jmpBlocks {
jmpBlock.jumps[target] = next
}
blocks = append(blocks, next)
// Target is now a block!
delete(targets, target)
}
return blocks, nil
}
// sortTargets sorts the target positions (keys), lowest first
func sortTargets(targets map[pos][]*block) []pos {
keys := make([]pos, len(targets))
i := 0
for k := range targets {
keys[i] = k
i++
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
return keys
}
// addDivideByZeroGuards adds runtime guards / checks to ensure
// the program returns no match when it would otherwise divide by zero.
func addDivideByZeroGuards(blocks []*block) error {
isDivision := func(op bpf.ALUOp) bool {
return op == bpf.ALUOpDiv || op == bpf.ALUOpMod
}
// Is RegX known to be none 0 at the start of each block
// We can't divide by RegA, only need to check RegX.
xNotZero := make(map[*block]bool)
for _, block := range blocks {
notZero := xNotZero[block]
// newInsns to replace those in the block
newInsns := []instruction{}
for _, insn := range block.insns {
switch i := insn.Instruction.(type) {
case bpf.ALUOpConstant:
if isDivision(i.Op) && i.Val == 0 {
return errors.Errorf("instruction %v divides by 0", insn)
}
case bpf.ALUOpX:
if isDivision(i.Op) && !notZero {
newInsns = append(newInsns, instruction{Instruction: checkXNotZero{}})
notZero = true
}
}
newInsns = append(newInsns, insn)
// check if X clobbered - check is invalidated
if memWrites(insn.Instruction).regs[bpf.RegX] {
notZero = false
}
}
block.insns = newInsns
// update the status of every block this one jumps to
for _, target := range block.jumps {
targetNotZero, ok := xNotZero[target]
if !ok {
xNotZero[target] = notZero
continue
}
// x needs to be not zero from every possible path
xNotZero[target] = targetNotZero && notZero
}
}
return nil
}
// rewriteLargePacketOffsets replaces packet loads that have constant offsets
// greater than the verifier allows with return 0 (no match) to mimick
// what the kernel does for cBPF.
// While cBPF allows bigger offsets, in practice they cannot match a packet.
// This doesn't work for LoadIndirect as the actual offset is LoadIndirect.Off + RegX,
// we instead rely on runtime checks (see packetGuardIndirect).
func rewriteLargePacketOffsets(blocks *[]*block) {
// All blocks are reachable when we start.
// But some blocks can become unreachable once we've rewritten load instructions to returns.
// The verifier rejects unreachable instructions, track how many other blocks go to a given block
// so we can remove newly unreachable blocks.
blockRefs := make(map[*block]int)
var newBlocks []*block
for i, block := range *blocks {
// No other blocks jump into this block anymore, skip it.
if i != 0 && blockRefs[block] == 0 {
continue
}
newBlocks = append(newBlocks, block)
for _, insn := range block.insns {
var (
offset uint32
size int
)
// LoadIndirect is handled by runtime checks as only RegX + offset is subject to maxPacketOffset.
switch i := insn.Instruction.(type) {
case bpf.LoadAbsolute:
offset = i.Off
size = i.Size
case bpf.LoadMemShift:
offset = i.Off
size = 1
default:
continue
}
// A packetGuard will have to add size to the packet pointer, so it counts towards the limit.
// We've already validate offset isn't signed, so this can't overflow.
if offset+uint32(size) > maxPacketOffset {
// Mimick an out of bounds load in cBPF, returning 0 / no match.
// The block now unconditionally returns, the other instructions in it don't matter.
block.insns = []instruction{
{Instruction: bpf.RetConstant{Val: 0}},
}
// This block doesn't jump to any others anymore.
block.jumps = nil
break
}
}
// cBPF can't jump backwards, so we can build this up as we go.
for _, target := range block.jumps {
blockRefs[target]++
}
}
*blocks = newBlocks
}
// addAbsolutePacketGuard adds required packet guards for absolute packet accesses to blocks.
func addAbsolutePacketGuards(blocks []*block) {
addPacketGuards(blocks, packetGuardOpts{
requiredGuard: func(insns []instruction) requiredGuard {
var biggestGuard packetGuard = packetGuardAbsolute{}
for _, insn := range insns {
switch i := insn.Instruction.(type) {
case bpf.LoadAbsolute:
biggestGuard = biggestGuard.extend(newPacketGuardAbsolute(i.Off, i.Size))
case bpf.LoadMemShift:
biggestGuard = biggestGuard.extend(newPacketGuardAbsolute(i.Off, 1))
}
}
// Guard covers all instructions.
return requiredGuard{
guard: biggestGuard,
alwaysValid: true,
}
},
zeroGuard: func() packetGuard {
return packetGuardAbsolute{}
},
})
}
// addIndirectPacketGuard adds required packet guards for indirect packet accesses to blocks.
func addIndirectPacketGuards(blocks []*block) {
addPacketGuards(blocks, packetGuardOpts{
requiredGuard: func(insns []instruction) requiredGuard {
var (
insnCount int
biggestGuard packetGuard = packetGuardIndirect{}
)
for _, insn := range insns {
insnCount++
switch i := insn.Instruction.(type) {
case bpf.LoadIndirect:
biggestGuard = biggestGuard.extend(newPacketGuardIndirect(i.Off, i.Size))
}
// Check if we clobbered x - this invalidates the guard
if memWrites(insn.Instruction).regs[bpf.RegX] {
return requiredGuard{
guard: biggestGuard,
validForInsns: insnCount,
}
}
}
return requiredGuard{
guard: biggestGuard,
alwaysValid: true,
}
},
zeroGuard: func() packetGuard {
return packetGuardIndirect{}
},
})
}
type packetGuardOpts struct {
// requiredGuard returns the packetGuard needed by insns, and what insns it is valid for.
requiredGuard func(insns []instruction) requiredGuard
// zeroGuard returns an empty guard of the right type.
zeroGuard func() packetGuard
}
type requiredGuard struct {
guard packetGuard
// The guard covers all the requested instructions,
// and is still valid afterwards.
alwaysValid bool
// The guard covers n instructions,
// and isn't valid for the subsequent n+1: instructions (eg RegX was clobbered for indirect guards).
validForInsns int
}
// addPacketGuards adds packet guards as required.
//
// Traversing the DAG of blocks (by visiting the blocks a block jumps to),
// we know all packet guards that exist at the start of a given block.
// We can check if the block requires a longer / bigger guard than
// the shortest / least existing guard.
func addPacketGuards(blocks []*block, opts packetGuardOpts) {
// Guards in effect at the start of each block
// Can't jump backwards so we only need to traverse blocks once
guards := make(map[*block][]packetGuard)
for _, block := range blocks {
blockGuard := addBlockGuards(block, leastGuard(opts.zeroGuard(), guards[block]), opts)
for _, target := range block.jumps {
guards[target] = append(guards[target], blockGuard)
}
}
}
// addBlockGuards add the guards required for the instructions in block.
func addBlockGuards(block *block, currentGuard packetGuard, opts packetGuardOpts) packetGuard {
insns := block.insns
block.insns = nil
for len(insns) != 0 {
required := opts.requiredGuard(insns)
// Need a bigger guard for these insns. Don't use the bigger guard on it's own,
// extend the current one so we keep as much information as we have.
if newGuard := currentGuard.extend(required.guard); newGuard != currentGuard {
currentGuard = newGuard
// Last guard we need for this block -> what our children / target blocks will start with
if required.alwaysValid {
// If packets must go through a bigger guard (guaranteed guard) to match, we can use the guaranteed guard here,
// without changing the return value of the program:
// - packets smaller than the guaranteed guard cannot match anyways, we can safely reject them earlier
// - packets bigger than the guaranteed guard won't be affected by it
currentGuard = currentGuard.extend(guaranteedGuard(block.jumps, opts))
}
block.insns = append(block.insns, instruction{Instruction: currentGuard})
}
coveredInsns := insns
if !required.alwaysValid {
coveredInsns = insns[:required.validForInsns]
}
currentGuard.adjustInsns(coveredInsns)
block.insns = append(block.insns, coveredInsns...)
if required.alwaysValid {
// Guard covers remainder of block, and is still valid at the end.
return currentGuard
} else {
// Guard isn't valid anymore.
currentGuard = opts.zeroGuard()
insns = insns[required.validForInsns:]
}
}
return currentGuard
}
// guaranteedGuard performs a recursive depth first search of blocks in target to determine
// the greatest packet guard that must be made for a packet to match
//
// If the DAG of blocks needs these packet guards:
//
// [4]
// / \
// false [6]
// / \
// true [8]
// / \
// false true
//
// A packet can only match ("true") by going through guards 4 and 6. It does not have to go through guard 8.
// guaranteedGuard would return 6.
func guaranteedGuard(targets map[pos]*block, opts packetGuardOpts) packetGuard {
// Inner implementation - Uses memoization
return guaranteedGuardCached(targets, opts, make(map[*block]packetGuard))
}
// 'cache' is used in order to not calculate guard more than once for the same block.
func guaranteedGuardCached(targets map[pos]*block, opts packetGuardOpts, cache map[*block]packetGuard) packetGuard {
targetGuards := []packetGuard{}
for _, target := range targets {
// Block can't match the packet, ignore it
if blockNeverMatches(target) {
continue
}
if guard, ok := cache[target]; ok {
targetGuards = append(targetGuards, guard)
continue
}
required := opts.requiredGuard(target.insns)
// Guard invalidated by block, stop exploring
if !required.alwaysValid {
targetGuards = append(targetGuards, required.guard)
continue
}
guard := required.guard.extend(guaranteedGuardCached(target.jumps, opts, cache))
cache[target] = guard
targetGuards = append(targetGuards, guard)
}
return leastGuard(opts.zeroGuard(), targetGuards)
}
// leastGuard returns the smallest guard from guards.
// zero if there are no guards.
func leastGuard(zero packetGuard, guards []packetGuard) packetGuard {
least := zero
for i, guard := range guards {
if i == 0 {
least = guard
} else {
least = least.restrict(guard)
}
}
return least
}
// blockNeverMatches returns true IFF the insns in block will never match the input packet
func blockNeverMatches(block *block) bool {
for _, insn := range block.insns {
switch i := insn.Instruction.(type) {
case bpf.RetConstant:
if i.Val == 0 {
return true
}
}
}
return false
}
// memStatus represents a context defined status of registers & scratch
type memStatus struct {
// indexed by bpf.Register
regs [2]bool
scratch [16]bool
}
// merge merges this status with the other by applying policy to regs and scratch
func (r memStatus) merge(other memStatus, policy func(this, other bool) bool) memStatus {
newStatus := memStatus{}
for i := range newStatus.regs {
newStatus.regs[i] = policy(r.regs[i], other.regs[i])
}
for i := range newStatus.scratch {
newStatus.scratch[i] = policy(r.scratch[i], other.scratch[i])
}
return newStatus
}
// and merges this status with the other by logical AND
func (r memStatus) and(other memStatus) memStatus {
return r.merge(other, func(this, other bool) bool {
return this && other
})
}
// and merges this status with the other by logical OR
func (r memStatus) or(other memStatus) memStatus {
return r.merge(other, func(this, other bool) bool {
return this || other
})
}
// initializeMemory zero initializes all the registers that the BPF program reads from before writing to. Returns an error if any scratch memory is used uninitialized.
func initializeMemory(blocks []*block) error {
// memory initialized at the start of each block
statuses := make(map[*block]memStatus)
// uninitialized memory used so far
uninitialized := memStatus{}
for _, block := range blocks {
status := statuses[block]
for _, insn := range block.insns {
insnUninitialized := memUninitializedReads(insn.Instruction, status)
// Check no uninitialized scratch registers are read
for scratch, uninit := range insnUninitialized.scratch {
if uninit {
return errors.Errorf("instruction %v reads potentially uninitialized scratch register M[%d]", insn, scratch)
}
}
uninitialized = uninitialized.or(insnUninitialized)
status = status.or(memWrites(insn.Instruction))
}
// update the status of every block this one jumps to
for _, target := range block.jumps {
targetStatus, ok := statuses[target]
if !ok {
statuses[target] = status
continue
}
// memory needs to be initialized from every possible path
statuses[target] = targetStatus.and(status)
}
}
// new instructions we need to prepend to initialize uninitialized registers
initInsns := []instruction{}
for reg, uninit := range uninitialized.regs {
if !uninit {
continue
}
initInsns = append(initInsns, instruction{
Instruction: bpf.LoadConstant{
Dst: bpf.Register(reg),
Val: 0,
},
})
}
blocks[0].insns = append(initInsns, blocks[0].insns...)
return nil
}
// memUninitializedReads returns the memory read by insn that has not yet been initialized according to initialized.
func memUninitializedReads(insn bpf.Instruction, initialized memStatus) memStatus {
return memReads(insn).merge(initialized, func(read, init bool) bool {
return read && !init
})
}
// memReads returns the memory read by insn
func memReads(insn bpf.Instruction) memStatus {
read := memStatus{}
switch i := insn.(type) {
case bpf.ALUOpConstant:
read.regs[bpf.RegA] = true
case bpf.ALUOpX:
read.regs[bpf.RegA] = true
read.regs[bpf.RegX] = true
case bpf.JumpIf:
read.regs[bpf.RegA] = true
case bpf.JumpIfX:
read.regs[bpf.RegA] = true
read.regs[bpf.RegX] = true
case bpf.LoadIndirect:
read.regs[bpf.RegX] = true
case bpf.LoadScratch:
read.scratch[i.N] = true
case bpf.NegateA:
read.regs[bpf.RegA] = true
case bpf.RetA:
read.regs[bpf.RegA] = true
case bpf.StoreScratch:
read.regs[i.Src] = true
case bpf.TAX:
read.regs[bpf.RegA] = true
case bpf.TXA:
read.regs[bpf.RegX] = true
}
return read
}
// memWrites returns the memory written by insn
func memWrites(insn bpf.Instruction) memStatus {
write := memStatus{}
switch i := insn.(type) {
case bpf.ALUOpConstant:
write.regs[bpf.RegA] = true
case bpf.ALUOpX:
write.regs[bpf.RegA] = true
case bpf.LoadAbsolute:
write.regs[bpf.RegA] = true
case bpf.LoadConstant:
write.regs[i.Dst] = true
case bpf.LoadExtension:
write.regs[bpf.RegA] = true
case bpf.LoadIndirect:
write.regs[bpf.RegA] = true
case bpf.LoadMemShift:
write.regs[bpf.RegX] = true
case bpf.LoadScratch:
write.regs[i.Dst] = true
case bpf.NegateA:
write.regs[bpf.RegA] = true
case bpf.StoreScratch:
write.scratch[i.N] = true
case bpf.TAX:
write.regs[bpf.RegX] = true
case bpf.TXA:
write.regs[bpf.RegA] = true
}
return write
}
|